Wow, maths functions!

This commit is contained in:
Kat R. 2022-10-27 08:57:08 -05:00
parent d9b8e481aa
commit c0639c609a
3 changed files with 52 additions and 0 deletions

21
math/log.c Normal file
View file

@ -0,0 +1,21 @@
#include <math.h>
double _agm(double x, double y) {
double a = x;
double g = y;
double temp;
for(int i = 0; i < 15; i++) {
temp = 0.5 * (a + g);
g = sqrt(a * g);
a = temp;
}
return a;
}
double log(double x) {
double s = x * exp2(24);
double nat_log = (M_PI / (2 * _agm(1, 4/s))) - (24 * M_LN2);
return nat_log;
}

5
math/pow.c Normal file
View file

@ -0,0 +1,5 @@
#include <math.h>
double pow(double x, double y) {
return exp(y * log(x));
}

26
math/sqrt.c Normal file
View file

@ -0,0 +1,26 @@
#include <math.h>
typedef union {
double d;
struct {
unsigned long int mantissa : 52;
unsigned int exponent : 11;
unsigned int sign_bit : 1;
} parts;
} _dbl_cast;
double sqrt(double x) {
_dbl_cast S = { .d = x };
double estimate = (0.5 + 0.5 * S.parts.mantissa) *
exp2(S.parts.exponent - 1023);
double a_n, b_n;
for(int i = 0; i < 15; i++) {
a_n = (x - (estimate * estimate)) / (2 * estimate);
b_n = estimate + a_n;
estimate = b_n - ((a_n * a_n) / (2 * b_n));
}
return estimate;
}