214 lines
4.2 KiB
C
214 lines
4.2 KiB
C
#ifndef COMMON_MATH_LIBRARY_H
|
|
#define COMMON_MATH_LIBRARY_H
|
|
|
|
#define TAYLOR_SERIES_DEGREE 34
|
|
#define SQRT_NEWTON_ITERATIONS 26
|
|
#define INV_SQRT_NEWTON_STEPS 2
|
|
|
|
const float PI = 3.1415926535897932384626433f;
|
|
|
|
/**
|
|
* Computes the sign of the parameter x.
|
|
* Returns 1 if x >= 0 and -1 if x < 0
|
|
* @param x
|
|
* @return
|
|
*/
|
|
float sgn(float x);
|
|
|
|
/**
|
|
* Computes the signum of the parameter x.
|
|
* Returns 1 if x > 0, 0 if x equals 0 and -1 if x < 0
|
|
* @param x
|
|
* @return
|
|
*/
|
|
float signum(float x);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* 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 atan(float x);
|
|
|
|
/**
|
|
* Approximates the inverse of the sine of x
|
|
* @param x
|
|
* @return
|
|
*/
|
|
float asin(float x);
|
|
|
|
/**
|
|
* Approximates the inverse of the cosine of x
|
|
* @param x
|
|
* @return
|
|
*/
|
|
float acos(float x);
|
|
|
|
/**
|
|
* Approximates the inverse of the cotangent of x
|
|
* @param x
|
|
* @return
|
|
*/
|
|
float acot(float x);
|
|
|
|
/**
|
|
* Approximates the inverse of the cosecant of x
|
|
* @param x
|
|
* @return
|
|
*/
|
|
float acsc(float x);
|
|
|
|
/**
|
|
* Approximates the inverse of the secant of x
|
|
* @param x
|
|
* @return
|
|
*/
|
|
float asec(float x);
|
|
|
|
/**
|
|
* Approximate the inverse square root.
|
|
* This implementation is based on the algorithm introduced in the Quake game with
|
|
* various improvements.
|
|
* @param x
|
|
* @return 1/sqrt(x)
|
|
*/
|
|
float inv_sqrt(float x);
|
|
|
|
#endif //COMMON_MATH_LIBRARY_H
|