2023-06-01 14:36:14 +00:00
|
|
|
#ifndef COMMON_MATH_LIBRARY_H
|
|
|
|
#define COMMON_MATH_LIBRARY_H
|
|
|
|
|
|
|
|
#define TAYLOR_SERIES_DEGREE 34
|
2023-06-01 16:29:58 +00:00
|
|
|
#define SQRT_NEWTON_ITERATIONS 26
|
2023-06-01 14:36:14 +00:00
|
|
|
|
|
|
|
const float PI = 3.1415926535897932384626433f;
|
|
|
|
|
2023-06-01 16:29:58 +00:00
|
|
|
/**
|
|
|
|
* Computes the sign of the parameter x.
|
|
|
|
* Returns 1 if x >= 0 and -1 if x < 0
|
|
|
|
* @param x
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
float sgn(float x);
|
|
|
|
|
2023-06-01 14:36:14 +00:00
|
|
|
/**
|
|
|
|
* Compute the factorial of parameter x.
|
|
|
|
* This function only properly works on natural numbers.
|
|
|
|
* The implementation is an iterative one and is only limited by the size
|
|
|
|
* of the type used in the calculation.
|
|
|
|
* @param x the factorial of x or 1 if x < 0
|
|
|
|
* @return the factorial for x
|
|
|
|
*/
|
|
|
|
long long fac(long long x);
|
|
|
|
|
2023-06-01 16:29:58 +00:00
|
|
|
/**
|
|
|
|
* Computes the n-th element of the fibonacci sequence.
|
|
|
|
* This function only properly works on natural numbers.
|
|
|
|
* The implementation is an iterative one and is only limited by the size
|
|
|
|
* of the type used in the calculation.
|
|
|
|
* @param n the n-th element to compute
|
|
|
|
* @return the n-th fibonacci number
|
|
|
|
*/
|
|
|
|
long long fib(long long n);
|
|
|
|
|
2023-06-01 14:36:14 +00:00
|
|
|
/**
|
|
|
|
* Truncates the parameter x.
|
|
|
|
* In other words this function will cut of the fraction part of the number x.
|
|
|
|
* @param x
|
|
|
|
* @return trunc(x)
|
|
|
|
*/
|
|
|
|
inline float trunc(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the fraction of x.
|
|
|
|
* In other words this function will only keep the fractional part and cut of the integral.
|
|
|
|
* @param x
|
|
|
|
* @return fract(x)
|
|
|
|
*/
|
|
|
|
inline float fract(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the floor of x.
|
|
|
|
* In other words this function will round x towards negative infinity.
|
|
|
|
* This is effectively the same as trunc(x)
|
|
|
|
* @param x
|
|
|
|
* @return floor(x)
|
|
|
|
*/
|
|
|
|
inline float floor(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the ceil of x.
|
|
|
|
* In other words this function will round x towards positive infinity
|
|
|
|
* @param x
|
|
|
|
* @return ceil(x)
|
|
|
|
*/
|
|
|
|
inline float ceil(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Round the number x.
|
|
|
|
* In other words this function will round x towards negative infinity if x the fraction of x is smaller than 0.5 and else
|
|
|
|
* towards positive infinity
|
|
|
|
* @param x
|
|
|
|
* @return round(x)
|
|
|
|
*/
|
|
|
|
inline float round(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the modulo division of x and y.
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @return mod(x,y)
|
|
|
|
*/
|
|
|
|
float mod(float x, float y);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the result of e raised to the power of x.
|
|
|
|
* This function only performs an approximation based on the famous taylor series for e^x.
|
|
|
|
* @param x the exponent of e^x
|
|
|
|
* @return approximation of e^x
|
|
|
|
*/
|
|
|
|
float exp(float x);
|
|
|
|
|
2023-06-01 16:29:58 +00:00
|
|
|
/**
|
|
|
|
* Approximate the sqrt of x using the newton's method.
|
|
|
|
* Precision an be optimized via SQRT_NEWTON_ITERATIONS
|
|
|
|
* @param x
|
|
|
|
* @return sqrt(x)
|
|
|
|
*/
|
|
|
|
float sqrt(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Approximate the hypotenuse of x and y (sqrt(x²+y²)) using the babylonian method.
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @return sqrt(x²+y²)
|
|
|
|
*/
|
|
|
|
float hypot(float x, float y);
|
|
|
|
|
2023-06-01 14:36:14 +00:00
|
|
|
/**
|
|
|
|
* Computes the sine of x.
|
|
|
|
* @param x
|
|
|
|
* @return sin(x)
|
|
|
|
*/
|
|
|
|
float sin(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the cosine of x.
|
|
|
|
* @param x
|
|
|
|
* @return cos(x)
|
|
|
|
*/
|
|
|
|
float cos(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the tangent of x.
|
|
|
|
* @param x
|
|
|
|
* @return tan(x)
|
|
|
|
*/
|
|
|
|
float tan(float x);
|
|
|
|
|
2023-06-01 16:29:58 +00:00
|
|
|
/**
|
|
|
|
* Computes an approximation of the cosecant of parameter x.
|
|
|
|
* @param x
|
|
|
|
* @return csc(x)
|
|
|
|
*/
|
|
|
|
float csc(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes an approximation of the secant of parameter x.
|
|
|
|
* @param x
|
|
|
|
* @return sec(x)
|
|
|
|
*/
|
|
|
|
float sec(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes an approximation of the cotangent of parameter x.
|
|
|
|
* @param x
|
|
|
|
* @return cot(x)
|
|
|
|
*/
|
|
|
|
float cot(float x);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Approximates the inverse of the tangent of x
|
|
|
|
* @param x
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
float arctan(float x);
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-01 14:36:14 +00:00
|
|
|
#endif //COMMON_MATH_LIBRARY_H
|