cubrt.c 329 B

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