You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
7.3 KiB

11 years ago
  1. /* Definitions of some C99 math library functions, for those platforms
  2. that don't implement these functions already. */
  3. #include "Python.h"
  4. #include <float.h>
  5. #include "_math.h"
  6. /* The following copyright notice applies to the original
  7. implementations of acosh, asinh and atanh. */
  8. /*
  9. * ====================================================
  10. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  11. *
  12. * Developed at SunPro, a Sun Microsystems, Inc. business.
  13. * Permission to use, copy, modify, and distribute this
  14. * software is freely granted, provided that this notice
  15. * is preserved.
  16. * ====================================================
  17. */
  18. #if !defined(HAVE_ACOSH) || !defined(HAVE_ASINH)
  19. static const double ln2 = 6.93147180559945286227E-01;
  20. static const double two_pow_p28 = 268435456.0; /* 2**28 */
  21. #endif
  22. #if !defined(HAVE_ASINH) || !defined(HAVE_ATANH)
  23. static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
  24. #endif
  25. #if !defined(HAVE_ATANH) && !defined(Py_NAN)
  26. static const double zero = 0.0;
  27. #endif
  28. #ifndef HAVE_ACOSH
  29. /* acosh(x)
  30. * Method :
  31. * Based on
  32. * acosh(x) = log [ x + sqrt(x*x-1) ]
  33. * we have
  34. * acosh(x) := log(x)+ln2, if x is large; else
  35. * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
  36. * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
  37. *
  38. * Special cases:
  39. * acosh(x) is NaN with signal if x<1.
  40. * acosh(NaN) is NaN without signal.
  41. */
  42. double
  43. _Py_acosh(double x)
  44. {
  45. if (Py_IS_NAN(x)) {
  46. return x+x;
  47. }
  48. if (x < 1.) { /* x < 1; return a signaling NaN */
  49. errno = EDOM;
  50. #ifdef Py_NAN
  51. return Py_NAN;
  52. #else
  53. return (x-x)/(x-x);
  54. #endif
  55. }
  56. else if (x >= two_pow_p28) { /* x > 2**28 */
  57. if (Py_IS_INFINITY(x)) {
  58. return x+x;
  59. }
  60. else {
  61. return log(x) + ln2; /* acosh(huge)=log(2x) */
  62. }
  63. }
  64. else if (x == 1.) {
  65. return 0.0; /* acosh(1) = 0 */
  66. }
  67. else if (x > 2.) { /* 2 < x < 2**28 */
  68. double t = x * x;
  69. return log(2.0 * x - 1.0 / (x + sqrt(t - 1.0)));
  70. }
  71. else { /* 1 < x <= 2 */
  72. double t = x - 1.0;
  73. return m_log1p(t + sqrt(2.0 * t + t * t));
  74. }
  75. }
  76. #endif /* HAVE_ACOSH */
  77. #ifndef HAVE_ASINH
  78. /* asinh(x)
  79. * Method :
  80. * Based on
  81. * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
  82. * we have
  83. * asinh(x) := x if 1+x*x=1,
  84. * := sign(x)*(log(x)+ln2)) for large |x|, else
  85. * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
  86. * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
  87. */
  88. double
  89. _Py_asinh(double x)
  90. {
  91. double w;
  92. double absx = fabs(x);
  93. if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
  94. return x+x;
  95. }
  96. if (absx < two_pow_m28) { /* |x| < 2**-28 */
  97. return x; /* return x inexact except 0 */
  98. }
  99. if (absx > two_pow_p28) { /* |x| > 2**28 */
  100. w = log(absx) + ln2;
  101. }
  102. else if (absx > 2.0) { /* 2 < |x| < 2**28 */
  103. w = log(2.0 * absx + 1.0 / (sqrt(x * x + 1.0) + absx));
  104. }
  105. else { /* 2**-28 <= |x| < 2= */
  106. double t = x*x;
  107. w = m_log1p(absx + t / (1.0 + sqrt(1.0 + t)));
  108. }
  109. return copysign(w, x);
  110. }
  111. #endif /* HAVE_ASINH */
  112. #ifndef HAVE_ATANH
  113. /* atanh(x)
  114. * Method :
  115. * 1.Reduced x to positive by atanh(-x) = -atanh(x)
  116. * 2.For x>=0.5
  117. * 1 2x x
  118. * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * -------)
  119. * 2 1 - x 1 - x
  120. *
  121. * For x<0.5
  122. * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
  123. *
  124. * Special cases:
  125. * atanh(x) is NaN if |x| >= 1 with signal;
  126. * atanh(NaN) is that NaN with no signal;
  127. *
  128. */
  129. double
  130. _Py_atanh(double x)
  131. {
  132. double absx;
  133. double t;
  134. if (Py_IS_NAN(x)) {
  135. return x+x;
  136. }
  137. absx = fabs(x);
  138. if (absx >= 1.) { /* |x| >= 1 */
  139. errno = EDOM;
  140. #ifdef Py_NAN
  141. return Py_NAN;
  142. #else
  143. return x / zero;
  144. #endif
  145. }
  146. if (absx < two_pow_m28) { /* |x| < 2**-28 */
  147. return x;
  148. }
  149. if (absx < 0.5) { /* |x| < 0.5 */
  150. t = absx+absx;
  151. t = 0.5 * m_log1p(t + t*absx / (1.0 - absx));
  152. }
  153. else { /* 0.5 <= |x| <= 1.0 */
  154. t = 0.5 * m_log1p((absx + absx) / (1.0 - absx));
  155. }
  156. return copysign(t, x);
  157. }
  158. #endif /* HAVE_ATANH */
  159. #ifndef HAVE_EXPM1
  160. /* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed
  161. to avoid the significant loss of precision that arises from direct
  162. evaluation of the expression exp(x) - 1, for x near 0. */
  163. double
  164. _Py_expm1(double x)
  165. {
  166. /* For abs(x) >= log(2), it's safe to evaluate exp(x) - 1 directly; this
  167. also works fine for infinities and nans.
  168. For smaller x, we can use a method due to Kahan that achieves close to
  169. full accuracy.
  170. */
  171. if (fabs(x) < 0.7) {
  172. double u;
  173. u = exp(x);
  174. if (u == 1.0)
  175. return x;
  176. else
  177. return (u - 1.0) * x / log(u);
  178. }
  179. else
  180. return exp(x) - 1.0;
  181. }
  182. #endif /* HAVE_EXPM1 */
  183. /* log1p(x) = log(1+x). The log1p function is designed to avoid the
  184. significant loss of precision that arises from direct evaluation when x is
  185. small. */
  186. double
  187. _Py_log1p(double x)
  188. {
  189. #ifdef HAVE_LOG1P
  190. /* Some platforms supply a log1p function but don't respect the sign of
  191. zero: log1p(-0.0) gives 0.0 instead of the correct result of -0.0.
  192. To save fiddling with configure tests and platform checks, we handle the
  193. special case of zero input directly on all platforms.
  194. */
  195. if (x == 0.0) {
  196. return x;
  197. }
  198. else {
  199. return log1p(x);
  200. }
  201. #else
  202. /* For x small, we use the following approach. Let y be the nearest float
  203. to 1+x, then
  204. 1+x = y * (1 - (y-1-x)/y)
  205. so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, the
  206. second term is well approximated by (y-1-x)/y. If abs(x) >=
  207. DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
  208. then y-1-x will be exactly representable, and is computed exactly by
  209. (y-1)-x.
  210. If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
  211. round-to-nearest then this method is slightly dangerous: 1+x could be
  212. rounded up to 1+DBL_EPSILON instead of down to 1, and in that case
  213. y-1-x will not be exactly representable any more and the result can be
  214. off by many ulps. But this is easily fixed: for a floating-point
  215. number |x| < DBL_EPSILON/2., the closest floating-point number to
  216. log(1+x) is exactly x.
  217. */
  218. double y;
  219. if (fabs(x) < DBL_EPSILON / 2.) {
  220. return x;
  221. }
  222. else if (-0.5 <= x && x <= 1.) {
  223. /* WARNING: it's possible that an overeager compiler
  224. will incorrectly optimize the following two lines
  225. to the equivalent of "return log(1.+x)". If this
  226. happens, then results from log1p will be inaccurate
  227. for small x. */
  228. y = 1.+x;
  229. return log(y) - ((y - 1.) - x) / y;
  230. }
  231. else {
  232. /* NaNs and infinities should end up here */
  233. return log(1.+x);
  234. }
  235. #endif /* ifdef HAVE_LOG1P */
  236. }