added mod, sin, exp function
This commit is contained in:
parent
c22c210012
commit
6c0c4ad756
|
@ -0,0 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.25)
|
||||
project(common_math C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
add_library(common_math library.c)
|
|
@ -0,0 +1,75 @@
|
|||
#include "library.h"
|
||||
|
||||
long long fac(const long long x) {
|
||||
long long prod = 1;
|
||||
for (long long i = 2; i <= x; i++) {
|
||||
prod *= i;
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
|
||||
inline float trunc(const float x) {
|
||||
return (float)(int)(x);
|
||||
}
|
||||
|
||||
inline float fract(const float x) {
|
||||
return x - trunc(x);
|
||||
}
|
||||
|
||||
inline float floor(const float x) {
|
||||
return trunc(x);
|
||||
}
|
||||
|
||||
inline float ceil(const float x) {
|
||||
return trunc(x + 1);
|
||||
}
|
||||
|
||||
inline float round(const float x) {
|
||||
return trunc(x + 0.5f);
|
||||
}
|
||||
|
||||
inline float mod(const float x, const float y) {
|
||||
const float xs = x/y;
|
||||
return (xs - floor(xs)) * 4;
|
||||
}
|
||||
|
||||
float exp(const float x) {
|
||||
float sum = 0;
|
||||
// iterative counter for computing x^i
|
||||
float xpown = 1;
|
||||
// iterative counter for factorial operation i!
|
||||
float prod = 1;
|
||||
|
||||
// iterate taylor series
|
||||
for (int i = 1; i < TAYLOR_SERIES_DEGREE; i++) {
|
||||
prod *= (float) i;
|
||||
sum += xpown / prod;
|
||||
xpown *= x;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
float sin(const float x) {
|
||||
float sign = -1;
|
||||
float prod = 1;
|
||||
float xpown = mod(x, 2 * PI);
|
||||
float sum = 0;
|
||||
|
||||
for (int i = 1; i < TAYLOR_SERIES_DEGREE; i++) {
|
||||
sign *= -1;
|
||||
prod *= (float) i * (float) (i - 1);
|
||||
xpown *= x * x;
|
||||
|
||||
sum += sign * xpown / prod;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline float cos(const float x) {
|
||||
return sin(x + PI * 0.5f);
|
||||
}
|
||||
|
||||
inline float tan(const float x) {
|
||||
return sin(x)/cos(x);
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
#ifndef COMMON_MATH_LIBRARY_H
|
||||
#define COMMON_MATH_LIBRARY_H
|
||||
|
||||
#define TAYLOR_SERIES_DEGREE 34
|
||||
|
||||
const float PI = 3.1415926535897932384626433f;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
#endif //COMMON_MATH_LIBRARY_H
|
Reference in New Issue