log.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * (c) copyright 1989 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 <errno.h>
  10. extern int errno;
  11. extern double frexp();
  12. double
  13. log(x)
  14. double x;
  15. {
  16. /* Algorithm and coefficients from:
  17. "Software manual for the elementary functions"
  18. by W.J. Cody and W. Waite, Prentice-Hall, 1980
  19. */
  20. static double a[] = {
  21. -0.64124943423745581147e2,
  22. 0.16383943563021534222e2,
  23. -0.78956112887491257267e0
  24. };
  25. static double b[] = {
  26. -0.76949932108494879777e3,
  27. 0.31203222091924532844e3,
  28. -0.35667977739034646171e2,
  29. 1.0
  30. };
  31. double znum, zden, z, w;
  32. int exponent;
  33. if (x <= 0) {
  34. errno = EDOM;
  35. return 0;
  36. }
  37. x = frexp(x, &exponent);
  38. if (x > M_1_SQRT2) {
  39. znum = (x - 0.5) - 0.5;
  40. zden = x * 0.5 + 0.5;
  41. }
  42. else {
  43. znum = x - 0.5;
  44. zden = znum * 0.5 + 0.5;
  45. exponent--;
  46. }
  47. z = znum/zden; w = z * z;
  48. x = z + z * w * (POLYNOM2(w,a)/POLYNOM3(w,b));
  49. z = exponent;
  50. x += z * (-2.121944400546905827679e-4);
  51. return x + z * 0.693359375;
  52. }