log.c 1011 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. log returns the natural logarithm of its floating
  3. point argument.
  4. The coefficients are #2705 from Hart & Cheney. (19.38D)
  5. It calls frexp.
  6. */
  7. #include <lib9.h>
  8. #define log2 0.693147180559945309e0
  9. #define ln10o1 .4342944819032518276511
  10. #define sqrto2 0.707106781186547524e0
  11. #define p0 -.240139179559210510e2
  12. #define p1 0.309572928215376501e2
  13. #define p2 -.963769093377840513e1
  14. #define p3 0.421087371217979714e0
  15. #define q0 -.120069589779605255e2
  16. #define q1 0.194809660700889731e2
  17. #define q2 -.891110902798312337e1
  18. double
  19. log(double arg)
  20. {
  21. double x, z, zsq, temp;
  22. int exp;
  23. if(arg <= 0)
  24. return NaN();
  25. x = frexp(arg, &exp);
  26. while(x < 0.5) {
  27. x *= 2;
  28. exp--;
  29. }
  30. if(x < sqrto2) {
  31. x *= 2;
  32. exp--;
  33. }
  34. z = (x-1) / (x+1);
  35. zsq = z*z;
  36. temp = ((p3*zsq + p2)*zsq + p1)*zsq + p0;
  37. temp = temp/(((zsq + q2)*zsq + q1)*zsq + q0);
  38. temp = temp*z + exp*log2;
  39. return temp;
  40. }
  41. double
  42. log10(double arg)
  43. {
  44. if(arg <= 0)
  45. return NaN();
  46. return log(arg) * ln10o1;
  47. }