libnumerixpp  0.1.1
A Powerful C++ Library for High-Performance Numerical Computing
mathematics::quadratic Namespace Reference

Functions

double calculateDiscriminant (double a, double b, double c)
 Calculates the discriminant. More...
 
std::vector< double > calculateRootsByDiscriminant (double discriminant, double a, double b)
 Calculates the roots by discriminant. More...
 
std::vector< double > getRootsByVietaTheorem (double a, double b, double c)
 Gets the roots by vieta theorem. More...
 

Detailed Description

mathematics utils for quadratic equations and other

#include <iostream>
#include <vector>
#include "libnumerixpp/core/common.hpp"
#include "libnumerixpp/libnumerixpp.hpp"
int main() {
println("LIBNUMERIXPP");
// SQUARE AND SQR //
double num = 100.0;
double num_sq = mathematics::square_it_up(num);
double num_sqr = mathematics::get_square_root(num);
std::cout << "Square " << num << ": " << num_sq << std::endl;
std::cout << "Square root " << num << ": " << num_sqr << std::endl;
std::cout << std::endl;
// CALCULATE QUADRATIC EQUATION BY DISCRIMINANT //
double a = -2;
double b = 5;
double c = 5;
std::vector<double> roots = mathematics::quadratic::calculateRootsByDiscriminant(d, a, b);
std::cout << "Quadratic Equation: a=" << a << "; b=" << b << "; c=" << c << std::endl;
std::cout << "D=" << d << std::endl;
std::cout << "Roots:" << std::endl;
for (double root : roots) {
std::cout << root << std::endl;
}
std::cout << std::endl;
// PERCENTAGE //
double nump = mathematics::add_percent_to_number(100.0, 10.0);
std::cout << "100+10%: " << nump << std::endl;
std::cout << std::endl;
// POWER / Algorithms for fast exponentiation //
double bestPowVal = 100;
double pow_results[5] = { mathematics::oldApproximatePower(10.0, 2.0),
std::cout << "0 oldApproximatePower : base 10 exponent 2: " << pow_results[0] << std::endl;
std::cout << "1 anotherApproximatePower: base 10 exponent 2: " << pow_results[1] << std::endl;
std::cout << "2 binaryPower : base 10 exponent 2: " << pow_results[2]
<< std::endl;
std::cout << "3 fastPowerDividing : base 10 exponent 2: " << pow_results[3] << std::endl;
std::cout << "4 fastPowerFractional : base 10 exponent 2: " << pow_results[4] << std::endl;
for (int i = 0; i < sizeof(pow_results) / sizeof(pow_results[0]); i++) {
double error = bestPowVal - pow_results[i];
std::cout << "POW Algorithm #" << i << ": error=" << error << std::endl;
}
std::cout << std::endl;
// Other //
std::cout << "-10 number module: " << mathematics::intabs(-10) << std::endl;
return 0;
}
void credits(void)
print credits
Definition: common.cpp:8
void println(std::string string)
Print string with new line.
Definition: libnumerixpp.cpp:11
Core utils for mathematics.
double calculateDiscriminant(double a, double b, double c)
Calculates the discriminant.
Definition: quadratic_equations.cpp:12
std::vector< double > calculateRootsByDiscriminant(double discriminant, double a, double b)
Calculates the roots by discriminant.
Definition: quadratic_equations.cpp:18
int intabs(int x)
Getting the modulus of a number without a comparison operation.
Definition: core.cpp:108
double binaryPower(double base, unsigned long long exponent)
Algorithm: Binary exponentiation.
Definition: core.cpp:23
double add_percent_to_number(double number, double percentage)
Adds a percent to number.
Definition: core.cpp:76
double fastPowerFractional(double base, double exponent)
Algorithm: "Fractional fast power".
Definition: core.cpp:63
double oldApproximatePower(double base, double exponent)
Algorithm for fast exponentiation "'Old' approximation".
Definition: core.cpp:10
double fastPowerDividing(double base, double exponent)
Algorithm: "Dividing fast power".
Definition: core.cpp:36
double get_square_root(double num)
Gets the square root.
Definition: core.cpp:85
double anotherApproximatePower(double base, double exponent)
Algorithm for fast exponentiation "'Another' approximation".
Definition: core.cpp:51
double square_it_up(double num)
Gets the number square (N^2).
Definition: core.cpp:83
Quadratic utils for mathematics.

Function Documentation

◆ calculateDiscriminant()

double mathematics::quadratic::calculateDiscriminant ( double  a,
double  b,
double  c 
)

Calculates the discriminant.

Based on discriminant formula: \(b^{2} - 4ac\)

Parameters
[in]aa
[in]bb
[in]cc
Returns
The discriminant.

◆ calculateRootsByDiscriminant()

std::vector< double > mathematics::quadratic::calculateRootsByDiscriminant ( double  discriminant,
double  a,
double  b 
)

Calculates the roots by discriminant.

Calculate the roots by discriminant \(\frac{-b +- \sqrt{D}}{2a}\). D > 0 = 2 roots, D == 0 = 1 root, D < 0 = 0 roots.

Parameters
[in]discriminantThe discriminant
[in]aa
[in]bb
Returns
The roots by discriminant.

◆ getRootsByVietaTheorem()

std::vector< double > mathematics::quadratic::getRootsByVietaTheorem ( double  a,
double  b,
double  c 
)

Gets the roots by vieta theorem.

Parameters
[in]aa
[in]bb
[in]cc
Returns
The roots by vieta theorem.