Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
Mark Dickinson | e05e840 | 2009-05-04 13:30:43 +0000 | [diff] [blame] | 3 | #ifdef X87_DOUBLE_ROUNDING |
| 4 | /* On x86 platforms using an x87 FPU, this function is called from the |
| 5 | Py_FORCE_DOUBLE macro (defined in pymath.h) to force a floating-point |
| 6 | number out of an 80-bit x87 FPU register and into a 64-bit memory location, |
| 7 | thus rounding from extended precision to double precision. */ |
| 8 | double _Py_force_double(double x) |
| 9 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 10 | volatile double y; |
| 11 | y = x; |
| 12 | return y; |
Mark Dickinson | e05e840 | 2009-05-04 13:30:43 +0000 | [diff] [blame] | 13 | } |
| 14 | #endif |
| 15 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 16 | #ifndef HAVE_HYPOT |
| 17 | double hypot(double x, double y) |
| 18 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 19 | double yx; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 21 | x = fabs(x); |
| 22 | y = fabs(y); |
| 23 | if (x < y) { |
| 24 | double temp = x; |
| 25 | x = y; |
| 26 | y = temp; |
| 27 | } |
| 28 | if (x == 0.) |
| 29 | return 0.; |
| 30 | else { |
| 31 | yx = y/x; |
| 32 | return x*sqrt(1.+yx*yx); |
| 33 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 34 | } |
| 35 | #endif /* HAVE_HYPOT */ |
| 36 | |
| 37 | #ifndef HAVE_COPYSIGN |
Mark Dickinson | 9cbb143 | 2009-04-18 14:19:58 +0000 | [diff] [blame] | 38 | double |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 39 | copysign(double x, double y) |
| 40 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 41 | /* use atan2 to distinguish -0. from 0. */ |
| 42 | if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) { |
| 43 | return fabs(x); |
| 44 | } else { |
| 45 | return -fabs(x); |
| 46 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 47 | } |
| 48 | #endif /* HAVE_COPYSIGN */ |
| 49 | |
| 50 | #ifndef HAVE_LOG1P |
Andrew MacIntyre | f8db822 | 2008-09-22 14:11:41 +0000 | [diff] [blame] | 51 | #include <float.h> |
| 52 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 53 | double |
| 54 | log1p(double x) |
| 55 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 56 | /* For x small, we use the following approach. Let y be the nearest |
| 57 | float to 1+x, then |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 58 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 59 | 1+x = y * (1 - (y-1-x)/y) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 61 | so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, |
| 62 | the second term is well approximated by (y-1-x)/y. If abs(x) >= |
| 63 | DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest |
| 64 | then y-1-x will be exactly representable, and is computed exactly |
| 65 | by (y-1)-x. |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 67 | If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be |
| 68 | round-to-nearest then this method is slightly dangerous: 1+x could |
| 69 | be rounded up to 1+DBL_EPSILON instead of down to 1, and in that |
| 70 | case y-1-x will not be exactly representable any more and the |
| 71 | result can be off by many ulps. But this is easily fixed: for a |
| 72 | floating-point number |x| < DBL_EPSILON/2., the closest |
| 73 | floating-point number to log(1+x) is exactly x. |
| 74 | */ |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 76 | double y; |
| 77 | if (fabs(x) < DBL_EPSILON/2.) { |
| 78 | return x; |
| 79 | } else if (-0.5 <= x && x <= 1.) { |
| 80 | /* WARNING: it's possible than an overeager compiler |
| 81 | will incorrectly optimize the following two lines |
| 82 | to the equivalent of "return log(1.+x)". If this |
| 83 | happens, then results from log1p will be inaccurate |
| 84 | for small x. */ |
| 85 | y = 1.+x; |
| 86 | return log(y)-((y-1.)-x)/y; |
| 87 | } else { |
| 88 | /* NaNs and infinities should end up here */ |
| 89 | return log(1.+x); |
| 90 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 91 | } |
| 92 | #endif /* HAVE_LOG1P */ |
| 93 | |
| 94 | /* |
| 95 | * ==================================================== |
| 96 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 97 | * |
| 98 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 99 | * Permission to use, copy, modify, and distribute this |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 100 | * software is freely granted, provided that this notice |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 101 | * is preserved. |
| 102 | * ==================================================== |
| 103 | */ |
| 104 | |
| 105 | static const double ln2 = 6.93147180559945286227E-01; |
| 106 | static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */ |
| 107 | static const double two_pow_p28 = 268435456.0; /* 2**28 */ |
| 108 | static const double zero = 0.0; |
| 109 | |
| 110 | /* asinh(x) |
| 111 | * Method : |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 112 | * Based on |
| 113 | * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ] |
| 114 | * we have |
| 115 | * asinh(x) := x if 1+x*x=1, |
| 116 | * := sign(x)*(log(x)+ln2)) for large |x|, else |
| 117 | * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else |
| 118 | * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2))) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 119 | */ |
| 120 | |
| 121 | #ifndef HAVE_ASINH |
| 122 | double |
| 123 | asinh(double x) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 124 | { |
| 125 | double w; |
| 126 | double absx = fabs(x); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 128 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) { |
| 129 | return x+x; |
| 130 | } |
| 131 | if (absx < two_pow_m28) { /* |x| < 2**-28 */ |
| 132 | return x; /* return x inexact except 0 */ |
| 133 | } |
| 134 | if (absx > two_pow_p28) { /* |x| > 2**28 */ |
| 135 | w = log(absx)+ln2; |
| 136 | } |
| 137 | else if (absx > 2.0) { /* 2 < |x| < 2**28 */ |
| 138 | w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx)); |
| 139 | } |
| 140 | else { /* 2**-28 <= |x| < 2= */ |
| 141 | double t = x*x; |
| 142 | w = log1p(absx + t / (1.0 + sqrt(1.0 + t))); |
| 143 | } |
| 144 | return copysign(w, x); |
| 145 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 146 | } |
| 147 | #endif /* HAVE_ASINH */ |
| 148 | |
| 149 | /* acosh(x) |
| 150 | * Method : |
| 151 | * Based on |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 152 | * acosh(x) = log [ x + sqrt(x*x-1) ] |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 153 | * we have |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 154 | * acosh(x) := log(x)+ln2, if x is large; else |
| 155 | * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else |
| 156 | * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1. |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 157 | * |
| 158 | * Special cases: |
| 159 | * acosh(x) is NaN with signal if x<1. |
| 160 | * acosh(NaN) is NaN without signal. |
| 161 | */ |
| 162 | |
| 163 | #ifndef HAVE_ACOSH |
| 164 | double |
| 165 | acosh(double x) |
| 166 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 167 | if (Py_IS_NAN(x)) { |
| 168 | return x+x; |
| 169 | } |
| 170 | if (x < 1.) { /* x < 1; return a signaling NaN */ |
| 171 | errno = EDOM; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 172 | #ifdef Py_NAN |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 173 | return Py_NAN; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 174 | #else |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 175 | return (x-x)/(x-x); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 176 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 177 | } |
| 178 | else if (x >= two_pow_p28) { /* x > 2**28 */ |
| 179 | if (Py_IS_INFINITY(x)) { |
| 180 | return x+x; |
| 181 | } else { |
| 182 | return log(x)+ln2; /* acosh(huge)=log(2x) */ |
| 183 | } |
| 184 | } |
| 185 | else if (x == 1.) { |
| 186 | return 0.0; /* acosh(1) = 0 */ |
| 187 | } |
| 188 | else if (x > 2.) { /* 2 < x < 2**28 */ |
| 189 | double t = x*x; |
| 190 | return log(2.0*x - 1.0 / (x + sqrt(t - 1.0))); |
| 191 | } |
| 192 | else { /* 1 < x <= 2 */ |
| 193 | double t = x - 1.0; |
| 194 | return log1p(t + sqrt(2.0*t + t*t)); |
| 195 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 196 | } |
| 197 | #endif /* HAVE_ACOSH */ |
| 198 | |
| 199 | /* atanh(x) |
| 200 | * Method : |
| 201 | * 1.Reduced x to positive by atanh(-x) = -atanh(x) |
| 202 | * 2.For x>=0.5 |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 203 | * 1 2x x |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 204 | * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 205 | * 2 1 - x 1 - x |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 206 | * |
| 207 | * For x<0.5 |
| 208 | * atanh(x) = 0.5*log1p(2x+2x*x/(1-x)) |
| 209 | * |
| 210 | * Special cases: |
| 211 | * atanh(x) is NaN if |x| >= 1 with signal; |
| 212 | * atanh(NaN) is that NaN with no signal; |
| 213 | * |
| 214 | */ |
| 215 | |
| 216 | #ifndef HAVE_ATANH |
| 217 | double |
| 218 | atanh(double x) |
| 219 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 220 | double absx; |
| 221 | double t; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 222 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 223 | if (Py_IS_NAN(x)) { |
| 224 | return x+x; |
| 225 | } |
| 226 | absx = fabs(x); |
| 227 | if (absx >= 1.) { /* |x| >= 1 */ |
| 228 | errno = EDOM; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 229 | #ifdef Py_NAN |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 230 | return Py_NAN; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 231 | #else |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 232 | return x/zero; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 233 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 234 | } |
| 235 | if (absx < two_pow_m28) { /* |x| < 2**-28 */ |
| 236 | return x; |
| 237 | } |
| 238 | if (absx < 0.5) { /* |x| < 0.5 */ |
| 239 | t = absx+absx; |
| 240 | t = 0.5 * log1p(t + t*absx / (1.0 - absx)); |
| 241 | } |
| 242 | else { /* 0.5 <= |x| <= 1.0 */ |
| 243 | t = 0.5 * log1p((absx + absx) / (1.0 - absx)); |
| 244 | } |
| 245 | return copysign(t, x); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 246 | } |
| 247 | #endif /* HAVE_ATANH */ |