Skip to content
This repository was archived by the owner on Jan 11, 2020. It is now read-only.

Implement the <math.h> sqrt() function #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/math/exp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This is free and unencumbered software released into the public domain. */

#ifdef HAVE_CONFIG_H
#include <config.h>
#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;
}
}
2 changes: 1 addition & 1 deletion src/math/fabs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

double
fabs(const double x) {
return (x < 0) ? -x: x;
return (x < 0) ? -x : x;
}
3 changes: 2 additions & 1 deletion src/math/ldexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#include <config.h>
#endif

#include <math.h> //For pow()

/**
* @date 2017-10-04
* @author Samuel Sarle
* @see http://libc11.org/math/ldexp.html
*/

#include <math.h> //For pow()

double
ldexp(const double x, const long long y) {
Expand Down
28 changes: 28 additions & 0 deletions src/math/sqrt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* This is free and unencumbered software released into the public domain. */

#ifdef HAVE_CONFIG_H
#include <config.h>
#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);
}