From f07528dace9fc00c178c290ea8b43873f57a2735 Mon Sep 17 00:00:00 2001 From: Kat Richey Date: Thu, 27 Oct 2022 23:57:29 -0500 Subject: [PATCH 1/2] Why am I doing exp(24)?! It's a constant!!! --- math/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/log.c b/math/log.c index a0a0c15..51567fc 100644 --- a/math/log.c +++ b/math/log.c @@ -15,7 +15,7 @@ double _agm(double x, double y) { } double log(double x) { - double s = x * exp2(24); + double s = x * 16777216; double nat_log = (M_PI / (2 * _agm(1, 4/s))) - (24 * M_LN2); return nat_log; } \ No newline at end of file From 6e8348fca6a5792a1d850fd5a9679ebf92f591a6 Mon Sep 17 00:00:00 2001 From: Kat Richey Date: Fri, 28 Oct 2022 00:05:56 -0500 Subject: [PATCH 2/2] Removed a circular dependency for pow --- math/sqrt.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/math/sqrt.c b/math/sqrt.c index 9d2b077..83277e5 100644 --- a/math/sqrt.c +++ b/math/sqrt.c @@ -9,10 +9,28 @@ typedef union { } parts; } _dbl_cast; +double _iexp2(int x) { + double result = 1.0; + if(x > 0) { + for(int i = 0; i < x; i++) { + result *= 2; + } + } + else if(x < 0) { + for(int i = 0; i > x; i--) { + result /= 2; + } + } + return result; +} + +/* + First gets an initial estimate then refines it using the Bakhshali method +*/ double sqrt(double x) { _dbl_cast S = { .d = x }; double estimate = (0.5 + 0.5 * S.parts.mantissa) * - exp2(S.parts.exponent - 1023); + _iexp2(S.parts.exponent - 1023); double a_n, b_n;