blob: d314887fa0b7d1a846d0b7886f253315d9dc4a60 [file] [log] [blame]
Mark Dickinson9cae1782009-12-16 20:13:40 +00001/* Definitions of some C99 math library functions, for those platforms
2 that don't implement these functions already. */
3
Mark Dickinson12748b02009-12-21 15:22:00 +00004#include "Python.h"
Mark Dickinson9cae1782009-12-16 20:13:40 +00005#include <float.h>
Mark Dickinson1730fdc2009-12-21 15:40:33 +00006#include "_math.h"
Mark Dickinson12748b02009-12-21 15:22:00 +00007
8/* The following copyright notice applies to the original
9 implementations of acosh, asinh and atanh. */
10
11/*
12 * ====================================================
13 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
14 *
15 * Developed at SunPro, a Sun Microsystems, Inc. business.
16 * Permission to use, copy, modify, and distribute this
Antoine Pitrouc83ea132010-05-09 14:46:46 +000017 * software is freely granted, provided that this notice
Mark Dickinson12748b02009-12-21 15:22:00 +000018 * is preserved.
19 * ====================================================
20 */
21
22static const double ln2 = 6.93147180559945286227E-01;
23static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
24static const double two_pow_p28 = 268435456.0; /* 2**28 */
Mark Dickinson12748b02009-12-21 15:22:00 +000025
26/* acosh(x)
27 * Method :
28 * Based on
Antoine Pitrouc83ea132010-05-09 14:46:46 +000029 * acosh(x) = log [ x + sqrt(x*x-1) ]
Mark Dickinson12748b02009-12-21 15:22:00 +000030 * we have
Antoine Pitrouc83ea132010-05-09 14:46:46 +000031 * acosh(x) := log(x)+ln2, if x is large; else
32 * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
33 * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
Mark Dickinson12748b02009-12-21 15:22:00 +000034 *
35 * Special cases:
36 * acosh(x) is NaN with signal if x<1.
37 * acosh(NaN) is NaN without signal.
38 */
39
40double
41_Py_acosh(double x)
42{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043 if (Py_IS_NAN(x)) {
44 return x+x;
45 }
46 if (x < 1.) { /* x < 1; return a signaling NaN */
47 errno = EDOM;
Mark Dickinson12748b02009-12-21 15:22:00 +000048#ifdef Py_NAN
Antoine Pitrouc83ea132010-05-09 14:46:46 +000049 return Py_NAN;
Mark Dickinson12748b02009-12-21 15:22:00 +000050#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +000051 return (x-x)/(x-x);
Mark Dickinson12748b02009-12-21 15:22:00 +000052#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000053 }
54 else if (x >= two_pow_p28) { /* x > 2**28 */
55 if (Py_IS_INFINITY(x)) {
56 return x+x;
Mark Dickinsondab08962010-07-06 15:03:42 +000057 }
58 else {
Mark Dickinsond2d23782010-07-05 20:16:40 +000059 return log(x)+ln2; /* acosh(huge)=log(2x) */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000060 }
61 }
62 else if (x == 1.) {
Mark Dickinsond2d23782010-07-05 20:16:40 +000063 return 0.0; /* acosh(1) = 0 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000064 }
Mark Dickinsond2d23782010-07-05 20:16:40 +000065 else if (x > 2.) { /* 2 < x < 2**28 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000066 double t = x*x;
67 return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
68 }
69 else { /* 1 < x <= 2 */
70 double t = x - 1.0;
71 return m_log1p(t + sqrt(2.0*t + t*t));
72 }
Mark Dickinson12748b02009-12-21 15:22:00 +000073}
74
75
76/* asinh(x)
77 * Method :
Antoine Pitrouc83ea132010-05-09 14:46:46 +000078 * Based on
79 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
80 * we have
81 * asinh(x) := x if 1+x*x=1,
82 * := sign(x)*(log(x)+ln2)) for large |x|, else
83 * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
84 * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
Mark Dickinson12748b02009-12-21 15:22:00 +000085 */
86
87double
88_Py_asinh(double x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089{
90 double w;
91 double absx = fabs(x);
Mark Dickinson12748b02009-12-21 15:22:00 +000092
Antoine Pitrouc83ea132010-05-09 14:46:46 +000093 if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
94 return x+x;
95 }
96 if (absx < two_pow_m28) { /* |x| < 2**-28 */
Mark Dickinsond2d23782010-07-05 20:16:40 +000097 return x; /* return x inexact except 0 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 }
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
Mark Dickinson12748b02009-12-21 15:22:00 +0000111}
112
113/* atanh(x)
114 * Method :
115 * 1.Reduced x to positive by atanh(-x) = -atanh(x)
116 * 2.For x>=0.5
Mark Dickinsond2d23782010-07-05 20:16:40 +0000117 * 1 2x x
118 * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * -------)
119 * 2 1 - x 1 - x
Mark Dickinson12748b02009-12-21 15:22:00 +0000120 *
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
130double
131_Py_atanh(double x)
132{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000133 double absx;
134 double t;
Mark Dickinson12748b02009-12-21 15:22:00 +0000135
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 if (Py_IS_NAN(x)) {
137 return x+x;
138 }
139 absx = fabs(x);
140 if (absx >= 1.) { /* |x| >= 1 */
141 errno = EDOM;
Mark Dickinson12748b02009-12-21 15:22:00 +0000142#ifdef Py_NAN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000143 return Py_NAN;
Mark Dickinson12748b02009-12-21 15:22:00 +0000144#else
Benjamin Peterson5726b632016-12-03 12:32:38 -0800145 return x/0.0;
Mark Dickinson12748b02009-12-21 15:22:00 +0000146#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 }
148 if (absx < two_pow_m28) { /* |x| < 2**-28 */
149 return x;
150 }
151 if (absx < 0.5) { /* |x| < 0.5 */
152 t = absx+absx;
153 t = 0.5 * m_log1p(t + t*absx / (1.0 - absx));
154 }
155 else { /* 0.5 <= |x| <= 1.0 */
156 t = 0.5 * m_log1p((absx + absx) / (1.0 - absx));
157 }
158 return copysign(t, x);
Mark Dickinson12748b02009-12-21 15:22:00 +0000159}
Mark Dickinson9cae1782009-12-16 20:13:40 +0000160
161/* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed
162 to avoid the significant loss of precision that arises from direct
163 evaluation of the expression exp(x) - 1, for x near 0. */
164
165double
166_Py_expm1(double x)
167{
168 /* For abs(x) >= log(2), it's safe to evaluate exp(x) - 1 directly; this
169 also works fine for infinities and nans.
170
171 For smaller x, we can use a method due to Kahan that achieves close to
172 full accuracy.
173 */
174
175 if (fabs(x) < 0.7) {
Mark Dickinsondab08962010-07-06 15:03:42 +0000176 double u;
177 u = exp(x);
178 if (u == 1.0)
179 return x;
180 else
181 return (u - 1.0) * x / log(u);
Mark Dickinson9cae1782009-12-16 20:13:40 +0000182 }
183 else
Mark Dickinsondab08962010-07-06 15:03:42 +0000184 return exp(x) - 1.0;
Mark Dickinson9cae1782009-12-16 20:13:40 +0000185}
Mark Dickinson12748b02009-12-21 15:22:00 +0000186
187/* log1p(x) = log(1+x). The log1p function is designed to avoid the
188 significant loss of precision that arises from direct evaluation when x is
189 small. */
190
Mark Dickinson2de4be22012-08-18 12:31:34 +0100191#ifdef HAVE_LOG1P
192
193double
194_Py_log1p(double x)
195{
196 /* Some platforms supply a log1p function but don't respect the sign of
197 zero: log1p(-0.0) gives 0.0 instead of the correct result of -0.0.
198
199 To save fiddling with configure tests and platform checks, we handle the
200 special case of zero input directly on all platforms.
201 */
202 if (x == 0.0) {
203 return x;
204 }
205 else {
206 return log1p(x);
207 }
208}
209
210#else
211
Mark Dickinson12748b02009-12-21 15:22:00 +0000212double
213_Py_log1p(double x)
214{
215 /* For x small, we use the following approach. Let y be the nearest float
216 to 1+x, then
217
Mark Dickinsond2d23782010-07-05 20:16:40 +0000218 1+x = y * (1 - (y-1-x)/y)
Mark Dickinson12748b02009-12-21 15:22:00 +0000219
220 so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, the
221 second term is well approximated by (y-1-x)/y. If abs(x) >=
222 DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
223 then y-1-x will be exactly representable, and is computed exactly by
224 (y-1)-x.
225
226 If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
227 round-to-nearest then this method is slightly dangerous: 1+x could be
228 rounded up to 1+DBL_EPSILON instead of down to 1, and in that case
229 y-1-x will not be exactly representable any more and the result can be
230 off by many ulps. But this is easily fixed: for a floating-point
231 number |x| < DBL_EPSILON/2., the closest floating-point number to
232 log(1+x) is exactly x.
233 */
234
235 double y;
236 if (fabs(x) < DBL_EPSILON/2.) {
Mark Dickinsondab08962010-07-06 15:03:42 +0000237 return x;
238 }
239 else if (-0.5 <= x && x <= 1.) {
240 /* WARNING: it's possible than an overeager compiler
241 will incorrectly optimize the following two lines
242 to the equivalent of "return log(1.+x)". If this
243 happens, then results from log1p will be inaccurate
244 for small x. */
245 y = 1.+x;
246 return log(y)-((y-1.)-x)/y;
247 }
248 else {
249 /* NaNs and infinities should end up here */
250 return log(1.+x);
Mark Dickinson12748b02009-12-21 15:22:00 +0000251 }
252}
Mark Dickinson2de4be22012-08-18 12:31:34 +0100253
254#endif /* ifdef HAVE_LOG1P */