log.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* $Id$ */
  8. #include <math.h>
  9. #include <float.h>
  10. #include <errno.h>
  11. #include "localmath.h"
  12. double
  13. log(double x)
  14. {
  15. /* Algorithm and coefficients from:
  16. "Software manual for the elementary functions"
  17. by W.J. Cody and W. Waite, Prentice-Hall, 1980
  18. */
  19. static double a[] = {
  20. -0.64124943423745581147e2,
  21. 0.16383943563021534222e2,
  22. -0.78956112887491257267e0
  23. };
  24. static double b[] = {
  25. -0.76949932108494879777e3,
  26. 0.31203222091924532844e3,
  27. -0.35667977739034646171e2,
  28. 1.0
  29. };
  30. double znum, zden, z, w;
  31. int exponent;
  32. if (__IsNan(x)) {
  33. errno = EDOM;
  34. return x;
  35. }
  36. if (x < 0) {
  37. errno = EDOM;
  38. return -HUGE_VAL;
  39. }
  40. else if (x == 0) {
  41. errno = ERANGE;
  42. return -HUGE_VAL;
  43. }
  44. if (x <= DBL_MAX) {
  45. }
  46. else return x; /* for infinity and Nan */
  47. x = frexp(x, &exponent);
  48. if (x > M_1_SQRT2) {
  49. znum = (x - 0.5) - 0.5;
  50. zden = x * 0.5 + 0.5;
  51. }
  52. else {
  53. znum = x - 0.5;
  54. zden = znum * 0.5 + 0.5;
  55. exponent--;
  56. }
  57. z = znum/zden; w = z * z;
  58. x = z + z * w * (POLYNOM2(w,a)/POLYNOM3(w,b));
  59. z = exponent;
  60. x += z * (-2.121944400546905827679e-4);
  61. return x + z * 0.693359375;
  62. }