libnumerixpp  0.1.3
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

auto old_approximate_power (double base, double exponent) -> double
 Algorithm for fast exponentiation "'Old' approximation". More...
 
auto binary_power (double b, unsigned long long e) -> double
 Algorithm: Binary exponentiation. More...
 
auto fast_power_dividing (double base, double exponent) -> double
 Algorithm: "Dividing fast power". More...
 
auto another_approximate_power (double base, double exponent) -> double
 Algorithm for fast exponentiation "'Another' approximation". More...
 
auto fast_power_fractional (double base, double exponent) -> double
 Algorithm: "Fractional fast power". More...
 
auto add_percent_to_number (double number, double percentage) -> double
 Adds a percent to number. More...
 
auto square_it_up (double num) -> double
 Gets the number square (N^2). More...
 
auto get_square_root (double num) -> double
 Gets the square root. More...
 
auto intabs (int x) -> int
 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"
auto main() -> int {
println("LIBNUMERIXPP");
// SQUARE AND SQR //
double const num = 100.0;
double const num_sq = mathematics::square_it_up(num);
double const num_sqr = mathematics::get_square_root(num);
std::cout << "Square " << num << ": " << num_sq << '\n';
std::cout << "Square root " << num << ": " << num_sqr << '\n';
std::cout << '\n';
// CALCULATE QUADRATIC EQUATION BY DISCRIMINANT //
double const a = -2;
double const b = 5;
double const c = 5;
std::vector<double> const roots =
std::cout << "Quadratic Equation: a=" << a << "; b=" << b << "; c=" << c << '\n';
std::cout << "D=" << d << '\n';
std::cout << "Roots:" << '\n';
for (double const root : roots) {
std::cout << root << '\n';
}
std::cout << '\n';
// PERCENTAGE //
double const nump = mathematics::add_percent_to_number(100.0, 10.0);
std::cout << "100+10%: " << nump << '\n';
std::cout << '\n';
// POWER / Algorithms for fast exponentiation //
double const best_pow_val = 100;
double const pow_results[5] = { mathematics::old_approximate_power(10.0, 2.0),
std::cout << "0 oldApproximatePower : base 10 exponent 2: " << pow_results[0] << '\n';
std::cout << "1 anotherApproximatePower: base 10 exponent 2: " << pow_results[1] << '\n';
std::cout << "2 binaryPower : base 10 exponent 2: " << pow_results[2]
<< '\n';
std::cout << "3 fastPowerDividing : base 10 exponent 2: " << pow_results[3] << '\n';
std::cout << "4 fastPowerFractional : base 10 exponent 2: " << pow_results[4] << '\n';
for (int i = 0; i < sizeof(pow_results) / sizeof(pow_results[0]); i++) {
double const error = best_pow_val - pow_results[i];
std::cout << "POW Algorithm #" << i << ": error=" << error << '\n';
}
std::cout << '\n';
// Other //
std::cout << "-10 number module: " << mathematics::intabs(-10) << '\n';
return 0;
}
void credits()
print credits
Definition: common.cpp:8
Core utils for mathematics.
auto calculate_roots_by_discriminant(double discriminant, double a, double b) -> std::vector< double >
Calculates the roots by discriminant.
Definition: quadratic_equations.cpp:18
auto calculate_discriminant(double a, double b, double c) -> double
Calculates the discriminant.
Definition: quadratic_equations.cpp:12
auto old_approximate_power(double base, double exponent) -> double
Algorithm for fast exponentiation "'Old' approximation".
Definition: core.cpp:10
auto get_square_root(double num) -> double
Gets the square root.
Definition: core.cpp:94
auto intabs(int x) -> int
Getting the modulus of a number without a comparison operation.
Definition: core.cpp:119
auto fast_power_fractional(double base, double exponent) -> double
Algorithm: "Fractional fast power".
Definition: core.cpp:67
auto another_approximate_power(double base, double exponent) -> double
Algorithm for fast exponentiation "'Another' approximation".
Definition: core.cpp:55
auto fast_power_dividing(double base, double exponent) -> double
Algorithm: "Dividing fast power".
Definition: core.cpp:38
auto binary_power(double base, unsigned long long exponent) -> double
Algorithm: Binary exponentiation.
Definition: core.cpp:23
auto square_it_up(double num) -> double
Gets the number square (N^2).
Definition: core.cpp:92
auto add_percent_to_number(double number, double percentage) -> double
Adds a percent to number.
Definition: core.cpp:85
Quadratic utils for mathematics.

Function Documentation

◆ add_percent_to_number()

auto mathematics::add_percent_to_number ( double  number,
double  percentage 
) -> double

Adds a percent to number.

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

◆ another_approximate_power()

auto mathematics::another_approximate_power ( double  base,
double  exponent 
) -> double

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

◆ binary_power()

auto mathematics::binary_power ( double  b,
unsigned long long  e 
) -> double

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

◆ fast_power_dividing()

auto mathematics::fast_power_dividing ( double  base,
double  exponent 
) -> double

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

◆ fast_power_fractional()

auto mathematics::fast_power_fractional ( double  base,
double  exponent 
) -> double

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()

auto mathematics::get_square_root ( double  num) -> double

Gets the square root.

Parameters
[in]numThe number
Returns
The square root.

◆ intabs()

auto mathematics::intabs ( int  x) -> int

Getting the modulus of a number without a comparison operation.

Parameters
[in]xnumber
Returns
modulus of number

◆ old_approximate_power()

auto mathematics::old_approximate_power ( double  base,
double  exponent 
) -> double

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()

auto mathematics::square_it_up ( double  num) -> double

Gets the number square (N^2).

Parameters
[in]numThe number
Returns
The number square.