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

Functions

auto calculate_discriminant (double a, double b, double c) -> double
 Calculates the discriminant. More...
 
auto calculate_roots_by_discriminant (double discriminant, double a, double b) -> std::vector< double >
 Calculates the roots by discriminant. More...
 
auto get_roots_by_vieta_theorem (double a, double b, double c) -> std::vector< double >
 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"
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

◆ calculate_discriminant()

auto mathematics::quadratic::calculate_discriminant ( double  a,
double  b,
double  c 
) -> double

Calculates the discriminant.

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

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

◆ calculate_roots_by_discriminant()

auto mathematics::quadratic::calculate_roots_by_discriminant ( double  discriminant,
double  a,
double  b 
) -> std::vector< double >

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.

◆ get_roots_by_vieta_theorem()

auto mathematics::quadratic::get_roots_by_vieta_theorem ( double  a,
double  b,
double  c 
) -> std::vector< double >

Gets the roots by vieta theorem.

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