log.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #define __NO_DEFS
  9. #include <math.h>
  10. #include <pc_err.h>
  11. #if __STDC__
  12. #include <pc_math.h>
  13. #include <float.h>
  14. #endif
  15. #undef HUGE
  16. #define HUGE 1e1000
  17. double
  18. _log(x)
  19. double x;
  20. {
  21. /* Algorithm and coefficients from:
  22. "Software manual for the elementary functions"
  23. by W.J. Cody and W. Waite, Prentice-Hall, 1980
  24. */
  25. static double a[] = {
  26. -0.64124943423745581147e2,
  27. 0.16383943563021534222e2,
  28. -0.78956112887491257267e0
  29. };
  30. static double b[] = {
  31. -0.76949932108494879777e3,
  32. 0.31203222091924532844e3,
  33. -0.35667977739034646171e2,
  34. 1.0
  35. };
  36. extern double _fef();
  37. double znum, zden, z, w;
  38. int exponent;
  39. if (x <= 0) {
  40. _trp(ELOG);
  41. return -HUGE;
  42. }
  43. x = _fef(x, &exponent);
  44. if (x > M_1_SQRT2) {
  45. znum = (x - 0.5) - 0.5;
  46. zden = x * 0.5 + 0.5;
  47. }
  48. else {
  49. znum = x - 0.5;
  50. zden = znum * 0.5 + 0.5;
  51. exponent--;
  52. }
  53. z = znum/zden; w = z * z;
  54. x = z + z * w * (POLYNOM2(w,a)/POLYNOM3(w,b));
  55. z = exponent;
  56. x += z * (-2.121944400546905827679e-4);
  57. return x + z * 0.693359375;
  58. }