Added `hypot`, `sqrt` and `sgn` function.

This commit is contained in:
Sven Vogel 2023-06-01 18:29:58 +02:00
parent 3d56a93a7c
commit 37efbe333c
2 changed files with 123 additions and 0 deletions

View File

@ -1,5 +1,9 @@
#include "library.h"
float sgn(const float x) {
return x < 0 ? -1 : 1;
}
long long fac(const long long x) {
long long prod = 1;
for (long long i = 2; i <= x; i++) {
@ -8,6 +12,16 @@ long long fac(const long long x) {
return prod;
}
long long fib(const long long n) {
long long sum = 0;
long long prev = 0;
for (long long i = 1LL; i <= n; i++) {
sum += prev;
prev = i - 1;
}
return sum;
}
inline float trunc(const float x) {
return (float)(int)(x);
}
@ -49,6 +63,18 @@ float exp(const float x) {
return sum;
}
float sqrt(const float x) {
float a = x;
for (int i = 0; i < SQRT_NEWTON_ITERATIONS; i++) {
a = 0.5f * (x/a + a);
}
return a;
}
float hypot(const float x, const float y) {
return x + 0.5f * y * y / x;
}
float sin(const float x) {
float sign = -1;
float prod = 1;
@ -73,3 +99,35 @@ inline float cos(const float x) {
inline float tan(const float x) {
return sin(x)/cos(x);
}
inline float csc(const float x) {
return 1.0f / sin(x);
}
inline float sec(const float x) {
return 1.0f / cos(x);
}
inline float cot(const float x) {
return tan(PI * 0.5f - x);
}
float arctan(const float x) {
const float a = 0.6204500718160764f;
const float b = 0.6204500718160764f;
const float c = 0.9031580043522688f;
return a * x / (b + sqrt(c + x * x));
}
inline float arccot(const float x) {
return PI * 0.5f - arctan(x);
}
float atan2(const float x, const float y) {
return arctan(y/x);
}
float arcsin(const float x) {
}

View File

@ -2,9 +2,18 @@
#define COMMON_MATH_LIBRARY_H
#define TAYLOR_SERIES_DEGREE 34
#define SQRT_NEWTON_ITERATIONS 26
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);
/**
* Compute the factorial of parameter x.
* This function only properly works on natural numbers.
@ -15,6 +24,16 @@ const float PI = 3.1415926535897932384626433f;
*/
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.
@ -73,6 +92,22 @@ float mod(float x, float y);
*/
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
@ -94,4 +129,34 @@ float cos(float 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 arctan(float x);
#endif //COMMON_MATH_LIBRARY_H