+++ /dev/null
-/* Definitions of some C99 math library functions, for those platforms\r
- that don't implement these functions already. */\r
-\r
-#include "Python.h"\r
-#include <float.h>\r
-#include "_math.h"\r
-\r
-/* The following copyright notice applies to the original\r
- implementations of acosh, asinh and atanh. */\r
-\r
-/*\r
- * ====================================================\r
- * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\r
- *\r
- * Developed at SunPro, a Sun Microsystems, Inc. business.\r
- * Permission to use, copy, modify, and distribute this\r
- * software is freely granted, provided that this notice\r
- * is preserved.\r
- * ====================================================\r
- */\r
-\r
-static const double ln2 = 6.93147180559945286227E-01;\r
-static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */\r
-static const double two_pow_p28 = 268435456.0; /* 2**28 */\r
-static const double zero = 0.0;\r
-\r
-/* acosh(x)\r
- * Method :\r
- * Based on\r
- * acosh(x) = log [ x + sqrt(x*x-1) ]\r
- * we have\r
- * acosh(x) := log(x)+ln2, if x is large; else\r
- * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else\r
- * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.\r
- *\r
- * Special cases:\r
- * acosh(x) is NaN with signal if x<1.\r
- * acosh(NaN) is NaN without signal.\r
- */\r
-\r
-double\r
-_Py_acosh(double x)\r
-{\r
- if (Py_IS_NAN(x)) {\r
- return x+x;\r
- }\r
- if (x < 1.) { /* x < 1; return a signaling NaN */\r
- errno = EDOM;\r
-#ifdef Py_NAN\r
- return Py_NAN;\r
-#else\r
- return (x-x)/(x-x);\r
-#endif\r
- }\r
- else if (x >= two_pow_p28) { /* x > 2**28 */\r
- if (Py_IS_INFINITY(x)) {\r
- return x+x;\r
- }\r
- else {\r
- return log(x)+ln2; /* acosh(huge)=log(2x) */\r
- }\r
- }\r
- else if (x == 1.) {\r
- return 0.0; /* acosh(1) = 0 */\r
- }\r
- else if (x > 2.) { /* 2 < x < 2**28 */\r
- double t = x*x;\r
- return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));\r
- }\r
- else { /* 1 < x <= 2 */\r
- double t = x - 1.0;\r
- return m_log1p(t + sqrt(2.0*t + t*t));\r
- }\r
-}\r
-\r
-\r
-/* asinh(x)\r
- * Method :\r
- * Based on\r
- * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]\r
- * we have\r
- * asinh(x) := x if 1+x*x=1,\r
- * := sign(x)*(log(x)+ln2)) for large |x|, else\r
- * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else\r
- * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))\r
- */\r
-\r
-double\r
-_Py_asinh(double x)\r
-{\r
- double w;\r
- double absx = fabs(x);\r
-\r
- if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {\r
- return x+x;\r
- }\r
- if (absx < two_pow_m28) { /* |x| < 2**-28 */\r
- return x; /* return x inexact except 0 */\r
- }\r
- if (absx > two_pow_p28) { /* |x| > 2**28 */\r
- w = log(absx)+ln2;\r
- }\r
- else if (absx > 2.0) { /* 2 < |x| < 2**28 */\r
- w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));\r
- }\r
- else { /* 2**-28 <= |x| < 2= */\r
- double t = x*x;\r
- w = m_log1p(absx + t / (1.0 + sqrt(1.0 + t)));\r
- }\r
- return copysign(w, x);\r
-\r
-}\r
-\r
-/* atanh(x)\r
- * Method :\r
- * 1.Reduced x to positive by atanh(-x) = -atanh(x)\r
- * 2.For x>=0.5\r
- * 1 2x x\r
- * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * -------)\r
- * 2 1 - x 1 - x\r
- *\r
- * For x<0.5\r
- * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))\r
- *\r
- * Special cases:\r
- * atanh(x) is NaN if |x| >= 1 with signal;\r
- * atanh(NaN) is that NaN with no signal;\r
- *\r
- */\r
-\r
-double\r
-_Py_atanh(double x)\r
-{\r
- double absx;\r
- double t;\r
-\r
- if (Py_IS_NAN(x)) {\r
- return x+x;\r
- }\r
- absx = fabs(x);\r
- if (absx >= 1.) { /* |x| >= 1 */\r
- errno = EDOM;\r
-#ifdef Py_NAN\r
- return Py_NAN;\r
-#else\r
- return x/zero;\r
-#endif\r
- }\r
- if (absx < two_pow_m28) { /* |x| < 2**-28 */\r
- return x;\r
- }\r
- if (absx < 0.5) { /* |x| < 0.5 */\r
- t = absx+absx;\r
- t = 0.5 * m_log1p(t + t*absx / (1.0 - absx));\r
- }\r
- else { /* 0.5 <= |x| <= 1.0 */\r
- t = 0.5 * m_log1p((absx + absx) / (1.0 - absx));\r
- }\r
- return copysign(t, x);\r
-}\r
-\r
-/* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed\r
- to avoid the significant loss of precision that arises from direct\r
- evaluation of the expression exp(x) - 1, for x near 0. */\r
-\r
-double\r
-_Py_expm1(double x)\r
-{\r
- /* For abs(x) >= log(2), it's safe to evaluate exp(x) - 1 directly; this\r
- also works fine for infinities and nans.\r
-\r
- For smaller x, we can use a method due to Kahan that achieves close to\r
- full accuracy.\r
- */\r
-\r
- if (fabs(x) < 0.7) {\r
- double u;\r
- u = exp(x);\r
- if (u == 1.0)\r
- return x;\r
- else\r
- return (u - 1.0) * x / log(u);\r
- }\r
- else\r
- return exp(x) - 1.0;\r
-}\r
-\r
-/* log1p(x) = log(1+x). The log1p function is designed to avoid the\r
- significant loss of precision that arises from direct evaluation when x is\r
- small. */\r
-\r
-#ifdef HAVE_LOG1P\r
-\r
-double\r
-_Py_log1p(double x)\r
-{\r
- /* Some platforms supply a log1p function but don't respect the sign of\r
- zero: log1p(-0.0) gives 0.0 instead of the correct result of -0.0.\r
-\r
- To save fiddling with configure tests and platform checks, we handle the\r
- special case of zero input directly on all platforms.\r
- */\r
- if (x == 0.0) {\r
- return x;\r
- }\r
- else {\r
- return log1p(x);\r
- }\r
-}\r
-\r
-#else\r
-\r
-double\r
-_Py_log1p(double x)\r
-{\r
- /* For x small, we use the following approach. Let y be the nearest float\r
- to 1+x, then\r
-\r
- 1+x = y * (1 - (y-1-x)/y)\r
-\r
- so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, the\r
- second term is well approximated by (y-1-x)/y. If abs(x) >=\r
- DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest\r
- then y-1-x will be exactly representable, and is computed exactly by\r
- (y-1)-x.\r
-\r
- If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be\r
- round-to-nearest then this method is slightly dangerous: 1+x could be\r
- rounded up to 1+DBL_EPSILON instead of down to 1, and in that case\r
- y-1-x will not be exactly representable any more and the result can be\r
- off by many ulps. But this is easily fixed: for a floating-point\r
- number |x| < DBL_EPSILON/2., the closest floating-point number to\r
- log(1+x) is exactly x.\r
- */\r
-\r
- double y;\r
- if (fabs(x) < DBL_EPSILON/2.) {\r
- return x;\r
- }\r
- else if (-0.5 <= x && x <= 1.) {\r
- /* WARNING: it's possible than an overeager compiler\r
- will incorrectly optimize the following two lines\r
- to the equivalent of "return log(1.+x)". If this\r
- happens, then results from log1p will be inaccurate\r
- for small x. */\r
- y = 1.+x;\r
- return log(y)-((y-1.)-x)/y;\r
- }\r
- else {\r
- /* NaNs and infinities should end up here */\r
- return log(1.+x);\r
- }\r
-}\r
-\r
-#endif /* ifdef HAVE_LOG1P */\r