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

Basic mathematics utils. More...

Namespaces

 equations
 namespace for equation solving namespace
 
 quadratic
 
 statistics
 Statistics namespace.
 

Functions

double oldApproximatePower (double base, double exponent)
 Algorithm for fast exponentiation "'Old' approximation". More...
 
double binaryPower (double b, unsigned long long e)
 Algorithm: Binary exponentiation. More...
 
double fastPowerDividing (double base, double exponent)
 Algorithm: "Dividing fast power". More...
 
double anotherApproximatePower (double base, double exponent)
 Algorithm for fast exponentiation "'Another' approximation". More...
 
double fastPowerFractional (double base, double exponent)
 Algorithm: "Fractional fast power". More...
 
double add_percent_to_number (double number, double percentage)
 Adds a percent to number. More...
 
double square_it_up (double num)
 Gets the number square (N^2). More...
 
double get_square_root (double num)
 Gets the square root. More...
 
int intabs (int x)
 Getting the modulus of a number without a comparison operation. More...
 

Detailed Description

Basic mathematics utils.

#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

◆ add_percent_to_number()

double mathematics::add_percent_to_number ( double  number,
double  percentage 
)

Adds a percent to number.

Parameters
[in]numberThe number
[in]percentageThe percentage
Returns
number

◆ anotherApproximatePower()

double mathematics::anotherApproximatePower ( double  base,
double  exponent 
)

Algorithm for fast exponentiation "'Another' approximation".

Speed increase: ~9 times. Accuracy: <1.5%. Limitations: accuracy drops rapidly as the absolute value of the degree increases and remains acceptable in the range [-10, 10] (see also 'old' approximation: oldApproximatePower()).

Parameters
[in]baseThe base
[in]exponentThe exponent
Returns
raised value

◆ binaryPower()

double mathematics::binaryPower ( double  b,
unsigned long long  e 
)

Algorithm: Binary exponentiation.

Speed increase: ~7.5 times on average, the advantage remains until numbers are raised to a power of 134217728, Speed increase: ~7.5 times on average, the advantage remains until the numbers are raised to the power of 134217728. Error: none, but it is worth noting that the multiplication operation is not associative for floating point numbers, i.e. 1.21 * 1.21 is not the same as 1.1 * 1.1 * 1.1 * 1.1, however, when compared with standard functions, errors do not arise, as mentioned earlier. Restrictions: the degree must be an integer not less than 0

Parameters
[in]basebase
[in]exponentexponent
Returns
raised value

◆ fastPowerDividing()

double mathematics::fastPowerDividing ( double  base,
double  exponent 
)

Algorithm: "Dividing fast power".

Speed increase: ~3.5 times. Accuracy: ~13%. The code below contains checks for special input data. Without them, the code runs only 10% faster, but the error increases tenfold (especially when using negative powers).

Parameters
[in]baseThe base
[in]exponentThe exponent
Returns
raised value

◆ fastPowerFractional()

double mathematics::fastPowerFractional ( double  base,
double  exponent 
)

Algorithm: "Fractional fast power".

Speed increase: ~4.4 times. Accuracy: ~0.7%

Parameters
[in]baseThe base
[in]exponentThe exponent
Returns
raised value

◆ get_square_root()

double mathematics::get_square_root ( double  num)

Gets the square root.

Parameters
[in]numThe number
Returns
The square root.

◆ intabs()

int mathematics::intabs ( int  x)

Getting the modulus of a number without a comparison operation.

Parameters
[in]xnumber
Returns
modulus of number

◆ oldApproximatePower()

double mathematics::oldApproximatePower ( double  base,
double  exponent 
)

Algorithm for fast exponentiation "'Old' approximation".

If accuracy is not important to you and the degrees are in the range from -1 to 1, you can use this method (see also 'another' approximation: anotherApproximatePower()). This method is based on the algorithm used in the 2005 game Quake III Arena. He raised the number x to the power -0.5, i.e. found the value: \(\frac{1}{\sqrt{x}}\)

Parameters
[in]baseThe base
[in]exponentThe exponent
Returns
raised value

◆ square_it_up()

double mathematics::square_it_up ( double  num)

Gets the number square (N^2).

Parameters
[in]numThe number
Returns
The number square.