From 08bb68ce71b7798d16ed5b71d99784a8162c640c Mon Sep 17 00:00:00 2001 From: Samuel Sarle Date: Thu, 5 Oct 2017 20:35:26 +0000 Subject: [PATCH 1/4] Fixed formatting on fabs() and ldexp(). --- src/math/fabs.c | 2 +- src/math/ldexp.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/math/fabs.c b/src/math/fabs.c index 7c8ba36..e6ee22a 100644 --- a/src/math/fabs.c +++ b/src/math/fabs.c @@ -12,5 +12,5 @@ double fabs(const double x) { - return (x < 0) ? -x: x; + return (x < 0) ? -x : x; } diff --git a/src/math/ldexp.c b/src/math/ldexp.c index e79c300..1d79fdb 100644 --- a/src/math/ldexp.c +++ b/src/math/ldexp.c @@ -4,13 +4,14 @@ #include #endif +#include //For pow() + /** * @date 2017-10-04 * @author Samuel Sarle * @see http://libc11.org/math/ldexp.html */ -#include //For pow() double ldexp(const double x, const long long y) { From 6244a3c919094495f99099f6ad1b92d8f9efc24b Mon Sep 17 00:00:00 2001 From: Samuel Sarle Date: Thu, 5 Oct 2017 20:36:46 +0000 Subject: [PATCH 2/4] Implemented the sqrt() function. --- src/math/sqrt.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/math/sqrt.c diff --git a/src/math/sqrt.c b/src/math/sqrt.c new file mode 100644 index 0000000..0ab880b --- /dev/null +++ b/src/math/sqrt.c @@ -0,0 +1,40 @@ +/* This is free and unencumbered software released into the public domain. */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +/** + * @date 2017-10-05 + * @author Samuel Sarle + * @see http://libc11.org/math/sqrt.html + */ + +double +sqrt(const double x) { + if (x <= 0) { + return 0; + } + + int i = 0; + while( (i * i) <= x ) { + i++; + } + i--; + double z = x - i * i; //Bakhshali approximation + double y = z / (2 * i); + double w = i + y; + return w - (y * y) / (2 * w); +} + +#include + +int +main() { + printf("%.10f\n",sqrt(5)); + printf("%.10f\n",sqrt(10)); + printf("%.10f\n",sqrt(2)); + printf("%.10f\n",sqrt(4)); + printf("%.10f\n",sqrt(-16)); + printf("%.10f\n",sqrt(1000000)); +} From 42bff9f2bb30f0e7966ab70cd893a523eeed6481 Mon Sep 17 00:00:00 2001 From: Samuel Sarle Date: Thu, 5 Oct 2017 20:41:11 +0000 Subject: [PATCH 3/4] Removed accidental junk. --- src/math/sqrt.c | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/math/sqrt.c b/src/math/sqrt.c index 0ab880b..1929e93 100644 --- a/src/math/sqrt.c +++ b/src/math/sqrt.c @@ -26,15 +26,3 @@ sqrt(const double x) { double w = i + y; return w - (y * y) / (2 * w); } - -#include - -int -main() { - printf("%.10f\n",sqrt(5)); - printf("%.10f\n",sqrt(10)); - printf("%.10f\n",sqrt(2)); - printf("%.10f\n",sqrt(4)); - printf("%.10f\n",sqrt(-16)); - printf("%.10f\n",sqrt(1000000)); -} From e42a4dd4a282c208aa2e7989ef5e1138abe09705 Mon Sep 17 00:00:00 2001 From: Samuel Sarle Date: Mon, 9 Oct 2017 20:14:46 +0000 Subject: [PATCH 4/4] Implemented the exp() function. --- src/math/exp.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/math/exp.c diff --git a/src/math/exp.c b/src/math/exp.c new file mode 100644 index 0000000..71830b2 --- /dev/null +++ b/src/math/exp.c @@ -0,0 +1,19 @@ +/* This is free and unencumbered software released into the public domain. */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +/** + * @date 2017-10-09 + * @author Samuel Sarle + * @see http://libc11.org/math/exp.html + */ + +double +exp(double x) { + x = 1 + (x / 1048576); + for (int i = 0; i < 20; i++) { + x *= x; + } +}