xref: /plan9/sys/src/cmd/map/libmap/cubrt.c (revision 59cc4ca53493a3c6d2349fe2b7f7c40f7dce7294)
1 #include <u.h>
2 #include <libc.h>
3 #include "map.h"
4 
5 double
cubrt(double a)6 cubrt(double a)
7 {
8 	double x,y,x1;
9 	if(a==0)
10 		return(0.);
11 	y = 1;
12 	if(a<0) {
13 		y = -y;
14 		a = -a;
15 	}
16 	while(a<1) {
17 		a *= 8;
18 		y /= 2;
19 	}
20 	while(a>1) {
21 		a /= 8;
22 		y *= 2;
23 	}
24 	x = 1;
25 	do {
26 		x1 = x;
27 		x = (2*x1+a/(x1*x1))/3;
28 	} while(fabs(x-x1)>10.e-15);
29 	return(x*y);
30 }
31