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.

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