Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Math module -- standard C math library functions, pi and e */ |
| 2 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 3 | /* Here are some comments from Tim Peters, extracted from the |
| 4 | discussion attached to http://bugs.python.org/issue1640. They |
| 5 | describe the general aims of the math module with respect to |
| 6 | special values, IEEE-754 floating-point exceptions, and Python |
| 7 | exceptions. |
| 8 | |
| 9 | These are the "spirit of 754" rules: |
| 10 | |
| 11 | 1. If the mathematical result is a real number, but of magnitude too |
| 12 | large to approximate by a machine float, overflow is signaled and the |
| 13 | result is an infinity (with the appropriate sign). |
| 14 | |
| 15 | 2. If the mathematical result is a real number, but of magnitude too |
| 16 | small to approximate by a machine float, underflow is signaled and the |
| 17 | result is a zero (with the appropriate sign). |
| 18 | |
| 19 | 3. At a singularity (a value x such that the limit of f(y) as y |
| 20 | approaches x exists and is an infinity), "divide by zero" is signaled |
| 21 | and the result is an infinity (with the appropriate sign). This is |
| 22 | complicated a little by that the left-side and right-side limits may |
| 23 | not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0 |
| 24 | from the positive or negative directions. In that specific case, the |
| 25 | sign of the zero determines the result of 1/0. |
| 26 | |
| 27 | 4. At a point where a function has no defined result in the extended |
| 28 | reals (i.e., the reals plus an infinity or two), invalid operation is |
| 29 | signaled and a NaN is returned. |
| 30 | |
| 31 | And these are what Python has historically /tried/ to do (but not |
| 32 | always successfully, as platform libm behavior varies a lot): |
| 33 | |
| 34 | For #1, raise OverflowError. |
| 35 | |
| 36 | For #2, return a zero (with the appropriate sign if that happens by |
| 37 | accident ;-)). |
| 38 | |
| 39 | For #3 and #4, raise ValueError. It may have made sense to raise |
| 40 | Python's ZeroDivisionError in #3, but historically that's only been |
| 41 | raised for division by zero and mod by zero. |
| 42 | |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | In general, on an IEEE-754 platform the aim is to follow the C99 |
| 47 | standard, including Annex 'F', whenever possible. Where the |
| 48 | standard recommends raising the 'divide-by-zero' or 'invalid' |
| 49 | floating-point exceptions, Python should raise a ValueError. Where |
| 50 | the standard recommends raising 'overflow', Python should raise an |
| 51 | OverflowError. In all other circumstances a value should be |
| 52 | returned. |
| 53 | */ |
| 54 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 55 | #include "Python.h" |
Michael W. Hudson | 9ef852c | 2005-04-06 13:05:18 +0000 | [diff] [blame] | 56 | #include "longintrepr.h" /* just for SHIFT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | |
Neal Norwitz | 5f95a79 | 2008-01-25 08:04:16 +0000 | [diff] [blame] | 58 | #ifdef _OSF_SOURCE |
| 59 | /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
| 60 | extern double copysign(double, double); |
| 61 | #endif |
| 62 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 63 | /* Call is_error when errno != 0, and where x is the result libm |
| 64 | * returned. is_error will usually set up an exception and return |
| 65 | * true (1), but may return false (0) without setting up an exception. |
| 66 | */ |
| 67 | static int |
| 68 | is_error(double x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 69 | { |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 70 | int result = 1; /* presumption of guilt */ |
Tim Peters | 2bf405a | 2000-10-12 19:42:00 +0000 | [diff] [blame] | 71 | assert(errno); /* non-zero errno is a precondition for calling */ |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 72 | if (errno == EDOM) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 73 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 74 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 75 | else if (errno == ERANGE) { |
| 76 | /* ANSI C generally requires libm functions to set ERANGE |
| 77 | * on overflow, but also generally *allows* them to set |
| 78 | * ERANGE on underflow too. There's no consistency about |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 79 | * the latter across platforms. |
| 80 | * Alas, C99 never requires that errno be set. |
| 81 | * Here we suppress the underflow errors (libm functions |
| 82 | * should return a zero on underflow, and +- HUGE_VAL on |
| 83 | * overflow, so testing the result for zero suffices to |
| 84 | * distinguish the cases). |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 85 | */ |
| 86 | if (x) |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 87 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 88 | "math range error"); |
| 89 | else |
| 90 | result = 0; |
| 91 | } |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 92 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 93 | /* Unexpected math error */ |
| 94 | PyErr_SetFromErrno(PyExc_ValueError); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 95 | return result; |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 98 | /* |
Mark Dickinson | 92483cd | 2008-04-20 21:39:04 +0000 | [diff] [blame] | 99 | wrapper for atan2 that deals directly with special cases before |
| 100 | delegating to the platform libm for the remaining cases. This |
| 101 | is necessary to get consistent behaviour across platforms. |
| 102 | Windows, FreeBSD and alpha Tru64 are amongst platforms that don't |
| 103 | always follow C99. |
| 104 | */ |
| 105 | |
| 106 | static double |
| 107 | m_atan2(double y, double x) |
| 108 | { |
| 109 | if (Py_IS_NAN(x) || Py_IS_NAN(y)) |
| 110 | return Py_NAN; |
| 111 | if (Py_IS_INFINITY(y)) { |
| 112 | if (Py_IS_INFINITY(x)) { |
| 113 | if (copysign(1., x) == 1.) |
| 114 | /* atan2(+-inf, +inf) == +-pi/4 */ |
| 115 | return copysign(0.25*Py_MATH_PI, y); |
| 116 | else |
| 117 | /* atan2(+-inf, -inf) == +-pi*3/4 */ |
| 118 | return copysign(0.75*Py_MATH_PI, y); |
| 119 | } |
| 120 | /* atan2(+-inf, x) == +-pi/2 for finite x */ |
| 121 | return copysign(0.5*Py_MATH_PI, y); |
| 122 | } |
| 123 | if (Py_IS_INFINITY(x) || y == 0.) { |
| 124 | if (copysign(1., x) == 1.) |
| 125 | /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */ |
| 126 | return copysign(0., y); |
| 127 | else |
| 128 | /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */ |
| 129 | return copysign(Py_MATH_PI, y); |
| 130 | } |
| 131 | return atan2(y, x); |
| 132 | } |
| 133 | |
| 134 | /* |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 135 | math_1 is used to wrap a libm function f that takes a double |
| 136 | arguments and returns a double. |
| 137 | |
| 138 | The error reporting follows these rules, which are designed to do |
| 139 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 140 | platforms. |
| 141 | |
| 142 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 143 | - an infinite result from finite inputs causes OverflowError to be |
| 144 | raised if can_overflow is 1, or raises ValueError if can_overflow |
| 145 | is 0. |
| 146 | - if the result is finite and errno == EDOM then ValueError is |
| 147 | raised |
| 148 | - if the result is finite and nonzero and errno == ERANGE then |
| 149 | OverflowError is raised |
| 150 | |
| 151 | The last rule is used to catch overflow on platforms which follow |
| 152 | C89 but for which HUGE_VAL is not an infinity. |
| 153 | |
| 154 | For the majority of one-argument functions these rules are enough |
| 155 | to ensure that Python's functions behave as specified in 'Annex F' |
| 156 | of the C99 standard, with the 'invalid' and 'divide-by-zero' |
| 157 | floating-point exceptions mapping to Python's ValueError and the |
| 158 | 'overflow' floating-point exception mapping to OverflowError. |
| 159 | math_1 only works for functions that don't have singularities *and* |
| 160 | the possibility of overflow; fortunately, that covers everything we |
| 161 | care about right now. |
| 162 | */ |
| 163 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 164 | static PyObject * |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 165 | math_1(PyObject *arg, double (*func) (double), int can_overflow) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 167 | double x, r; |
| 168 | x = PyFloat_AsDouble(arg); |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 169 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | return NULL; |
| 171 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 172 | PyFPE_START_PROTECT("in math_1", return 0); |
| 173 | r = (*func)(x); |
| 174 | PyFPE_END_PROTECT(r); |
| 175 | if (Py_IS_NAN(r)) { |
| 176 | if (!Py_IS_NAN(x)) |
| 177 | errno = EDOM; |
| 178 | else |
| 179 | errno = 0; |
| 180 | } |
| 181 | else if (Py_IS_INFINITY(r)) { |
| 182 | if (Py_IS_FINITE(x)) |
| 183 | errno = can_overflow ? ERANGE : EDOM; |
| 184 | else |
| 185 | errno = 0; |
| 186 | } |
| 187 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 188 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | else |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 190 | return PyFloat_FromDouble(r); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 193 | /* |
| 194 | math_2 is used to wrap a libm function f that takes two double |
| 195 | arguments and returns a double. |
| 196 | |
| 197 | The error reporting follows these rules, which are designed to do |
| 198 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 199 | platforms. |
| 200 | |
| 201 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 202 | - an infinite result from finite inputs causes OverflowError to be |
| 203 | raised. |
| 204 | - if the result is finite and errno == EDOM then ValueError is |
| 205 | raised |
| 206 | - if the result is finite and nonzero and errno == ERANGE then |
| 207 | OverflowError is raised |
| 208 | |
| 209 | The last rule is used to catch overflow on platforms which follow |
| 210 | C89 but for which HUGE_VAL is not an infinity. |
| 211 | |
| 212 | For most two-argument functions (copysign, fmod, hypot, atan2) |
| 213 | these rules are enough to ensure that Python's functions behave as |
| 214 | specified in 'Annex F' of the C99 standard, with the 'invalid' and |
| 215 | 'divide-by-zero' floating-point exceptions mapping to Python's |
| 216 | ValueError and the 'overflow' floating-point exception mapping to |
| 217 | OverflowError. |
| 218 | */ |
| 219 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 220 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 221 | math_2(PyObject *args, double (*func) (double, double), char *funcname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 223 | PyObject *ox, *oy; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 224 | double x, y, r; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 225 | if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
| 226 | return NULL; |
| 227 | x = PyFloat_AsDouble(ox); |
| 228 | y = PyFloat_AsDouble(oy); |
| 229 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 230 | return NULL; |
| 231 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 232 | PyFPE_START_PROTECT("in math_2", return 0); |
| 233 | r = (*func)(x, y); |
| 234 | PyFPE_END_PROTECT(r); |
| 235 | if (Py_IS_NAN(r)) { |
| 236 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 237 | errno = EDOM; |
| 238 | else |
| 239 | errno = 0; |
| 240 | } |
| 241 | else if (Py_IS_INFINITY(r)) { |
| 242 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 243 | errno = ERANGE; |
| 244 | else |
| 245 | errno = 0; |
| 246 | } |
| 247 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 248 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | else |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 250 | return PyFloat_FromDouble(r); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 253 | #define FUNC1(funcname, func, can_overflow, docstring) \ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 254 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 255 | return math_1(args, func, can_overflow); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 256 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 257 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 258 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 259 | #define FUNC2(funcname, func, docstring) \ |
| 260 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 261 | return math_2(args, func, #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 262 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 263 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 264 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 265 | FUNC1(acos, acos, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 266 | "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 267 | FUNC1(acosh, acosh, 0, |
| 268 | "acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.") |
| 269 | FUNC1(asin, asin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 270 | "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 271 | FUNC1(asinh, asinh, 0, |
| 272 | "asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.") |
| 273 | FUNC1(atan, atan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 274 | "atan(x)\n\nReturn the arc tangent (measured in radians) of x.") |
Mark Dickinson | 92483cd | 2008-04-20 21:39:04 +0000 | [diff] [blame] | 275 | FUNC2(atan2, m_atan2, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 276 | "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
| 277 | "Unlike atan(y/x), the signs of both x and y are considered.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 278 | FUNC1(atanh, atanh, 0, |
| 279 | "atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.") |
| 280 | FUNC1(ceil, ceil, 0, |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 281 | "ceil(x)\n\nReturn the ceiling of x as a float.\n" |
| 282 | "This is the smallest integral value >= x.") |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 283 | FUNC2(copysign, copysign, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 284 | "copysign(x,y)\n\nReturn x with the sign of y.") |
| 285 | FUNC1(cos, cos, 0, |
| 286 | "cos(x)\n\nReturn the cosine of x (measured in radians).") |
| 287 | FUNC1(cosh, cosh, 1, |
| 288 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
| 289 | FUNC1(exp, exp, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 290 | "exp(x)\n\nReturn e raised to the power of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 291 | FUNC1(fabs, fabs, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 292 | "fabs(x)\n\nReturn the absolute value of the float x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 293 | FUNC1(floor, floor, 0, |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 294 | "floor(x)\n\nReturn the floor of x as a float.\n" |
| 295 | "This is the largest integral value <= x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 296 | FUNC1(log1p, log1p, 1, |
| 297 | "log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n\ |
| 298 | The result is computed in a way which is accurate for x near zero.") |
| 299 | FUNC1(sin, sin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 300 | "sin(x)\n\nReturn the sine of x (measured in radians).") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 301 | FUNC1(sinh, sinh, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 302 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 303 | FUNC1(sqrt, sqrt, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 304 | "sqrt(x)\n\nReturn the square root of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 305 | FUNC1(tan, tan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 306 | "tan(x)\n\nReturn the tangent of x (measured in radians).") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 307 | FUNC1(tanh, tanh, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 308 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 310 | /* Precision summation function as msum() by Raymond Hettinger in |
| 311 | <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>, |
| 312 | enhanced with the exact partials sum and roundoff from Mark |
| 313 | Dickinson's post at <http://bugs.python.org/file10357/msum4.py>. |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 314 | See those links for more details, proofs and other references. |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 315 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 316 | Note 1: IEEE 754R floating point semantics are assumed, |
| 317 | but the current implementation does not re-establish special |
| 318 | value semantics across iterations (i.e. handling -Inf + Inf). |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 319 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 320 | Note 2: No provision is made for intermediate overflow handling; |
Raymond Hettinger | 2a9179a | 2008-05-29 08:38:23 +0000 | [diff] [blame] | 321 | therefore, sum([1e+308, 1e-308, 1e+308]) returns 1e+308 while |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 322 | sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the |
| 323 | overflow of the first partial sum. |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 324 | |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 325 | Note 3: The itermediate values lo, yr, and hi are declared volatile so |
| 326 | aggressive compilers won't algebraicly reduce lo to always be exactly 0.0. |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 327 | |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 328 | Note 4: Intermediate values and partial sums are declared as long doubles |
| 329 | as a way to eliminate double rounding environments where the operations |
| 330 | are carried-out in extended precision but stored in double precision |
| 331 | variables. In some cases, this doesn't help because the compiler |
| 332 | treats long doubles as doubles (i.e. the MS compiler for Win32 builds). |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 333 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 334 | Note 5: The signature of math.sum() differs from __builtin__.sum() |
| 335 | because the start argument doesn't make sense in the context of |
| 336 | accurate summation. Since the partials table is collapsed before |
| 337 | returning a result, sum(seq2, start=sum(seq1)) may not equal the |
| 338 | accurate result returned by sum(itertools.chain(seq1, seq2)). |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 339 | */ |
| 340 | |
| 341 | #define NUM_PARTIALS 32 /* initial partials array size, on stack */ |
| 342 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 343 | /* Extend the partials array p[] by doubling its size. */ |
| 344 | static int /* non-zero on error */ |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 345 | _sum_realloc(long double **p_ptr, Py_ssize_t n, |
| 346 | long double *ps, Py_ssize_t *m_ptr) |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 347 | { |
| 348 | void *v = NULL; |
| 349 | Py_ssize_t m = *m_ptr; |
| 350 | |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 351 | m += m; /* long double */ |
| 352 | if (n < m && m < (PY_SSIZE_T_MAX / sizeof(long double))) { |
| 353 | long double *p = *p_ptr; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 354 | if (p == ps) { |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 355 | v = PyMem_Malloc(sizeof(long double) * m); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 356 | if (v != NULL) |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 357 | memcpy(v, ps, sizeof(long double) * n); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 358 | } |
| 359 | else |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 360 | v = PyMem_Realloc(p, sizeof(long double) * m); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 361 | } |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 362 | if (v == NULL) { /* size overflow or no memory */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 363 | PyErr_SetString(PyExc_MemoryError, "math sum partials"); |
| 364 | return 1; |
| 365 | } |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 366 | *p_ptr = (long double*) v; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 367 | *m_ptr = m; |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* Full precision summation of a sequence of floats. |
| 372 | |
| 373 | def msum(iterable): |
| 374 | partials = [] # sorted, non-overlapping partial sums |
| 375 | for x in iterable: |
| 376 | i = 0 |
| 377 | for y in partials: |
| 378 | if abs(x) < abs(y): |
| 379 | x, y = y, x |
| 380 | hi = x + y |
| 381 | lo = y - (hi - x) |
| 382 | if lo: |
| 383 | partials[i] = lo |
| 384 | i += 1 |
| 385 | x = hi |
| 386 | partials[i:] = [x] |
| 387 | return sum_exact(partials) |
| 388 | |
| 389 | Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo |
| 390 | are exactly equal to x+y. The inner loop applies hi/lo summation to each |
| 391 | partial so that the list of partial sums remains exact. |
| 392 | |
| 393 | Sum_exact() adds the partial sums exactly and correctly rounds the final |
| 394 | result (using the round-half-to-even rule). The items in partials remain |
| 395 | non-zero, non-special, non-overlapping and strictly increasing in |
| 396 | magnitude, but possibly not all having the same sign. |
| 397 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 398 | Depends on IEEE 754 arithmetic guarantees and half-even rounding. |
| 399 | */ |
| 400 | |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 401 | static PyObject* |
| 402 | math_sum(PyObject *self, PyObject *seq) |
| 403 | { |
| 404 | PyObject *item, *iter, *sum = NULL; |
| 405 | Py_ssize_t i, j, n = 0, m = NUM_PARTIALS; |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 406 | long double x, y, t, ps[NUM_PARTIALS], *p = ps; |
| 407 | volatile long double hi, yr, lo; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 408 | |
| 409 | iter = PyObject_GetIter(seq); |
| 410 | if (iter == NULL) |
| 411 | return NULL; |
| 412 | |
| 413 | PyFPE_START_PROTECT("sum", Py_DECREF(iter); return NULL) |
| 414 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 415 | for(;;) { /* for x in iterable */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 416 | assert(0 <= n && n <= m); |
| 417 | assert((m == NUM_PARTIALS && p == ps) || |
| 418 | (m > NUM_PARTIALS && p != NULL)); |
| 419 | |
| 420 | item = PyIter_Next(iter); |
| 421 | if (item == NULL) { |
| 422 | if (PyErr_Occurred()) |
| 423 | goto _sum_error; |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 424 | break; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 425 | } |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 426 | x = (long double)PyFloat_AsDouble(item); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 427 | Py_DECREF(item); |
| 428 | if (PyErr_Occurred()) |
| 429 | goto _sum_error; |
| 430 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 431 | for (i = j = 0; j < n; j++) { /* for y in partials */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 432 | y = p[j]; |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 433 | if (fabs(x) < fabs(y)) { |
| 434 | t = x; x = y; y = t; |
| 435 | } |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 436 | hi = x + y; |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 437 | yr = hi - x; |
| 438 | lo = y - yr; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 439 | if (lo != 0.0) |
| 440 | p[i++] = lo; |
| 441 | x = hi; |
| 442 | } |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 443 | |
| 444 | n = i; /* ps[i:] = [x] */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 445 | if (x != 0.0) { |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 446 | /* If non-finite, reset partials, effectively |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 447 | adding subsequent items without roundoff |
| 448 | and yielding correct non-finite results, |
| 449 | provided IEEE 754 rules are observed */ |
| 450 | if (! Py_IS_FINITE(x)) |
| 451 | n = 0; |
| 452 | else if (n >= m && _sum_realloc(&p, n, ps, &m)) |
| 453 | goto _sum_error; |
| 454 | p[n++] = x; |
| 455 | } |
| 456 | } |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 457 | |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 458 | hi = 0.0; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 459 | if (n > 0) { |
| 460 | hi = p[--n]; |
| 461 | if (Py_IS_FINITE(hi)) { |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 462 | /* sum_exact(ps, hi) from the top, stop when the sum becomes inexact. */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 463 | while (n > 0) { |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 464 | x = hi; |
| 465 | y = p[--n]; |
| 466 | assert(fabs(y) < fabs(x)); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 467 | hi = x + y; |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 468 | yr = hi - x; |
| 469 | lo = y - yr; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 470 | if (lo != 0.0) |
| 471 | break; |
| 472 | } |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 473 | /* Make half-even rounding work across multiple partials. Needed |
| 474 | so that sum([1e-16, 1, 1e16]) will round-up the last digit to |
| 475 | two instead of down to zero (the 1e-16 makes the 1 slightly |
| 476 | closer to two). With a potential 1 ULP rounding error fixed-up, |
| 477 | math.sum() can guarantee commutativity. */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 478 | if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || |
| 479 | (lo > 0.0 && p[n-1] > 0.0))) { |
| 480 | y = lo * 2.0; |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 481 | x = hi + y; |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 482 | yr = x - hi; |
| 483 | if (y == yr) |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 484 | hi = x; |
| 485 | } |
| 486 | } |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 487 | else { /* raise exception corresponding to a special value */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 488 | errno = Py_IS_NAN(hi) ? EDOM : ERANGE; |
| 489 | if (is_error(hi)) |
| 490 | goto _sum_error; |
| 491 | } |
| 492 | } |
Raymond Hettinger | 7b1ed66 | 2008-06-09 09:29:17 +0000 | [diff] [blame^] | 493 | sum = PyFloat_FromDouble((double)hi); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 494 | |
| 495 | _sum_error: |
| 496 | PyFPE_END_PROTECT(hi) |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 497 | Py_DECREF(iter); |
| 498 | if (p != ps) |
| 499 | PyMem_Free(p); |
| 500 | return sum; |
| 501 | } |
| 502 | |
| 503 | #undef NUM_PARTIALS |
| 504 | |
| 505 | PyDoc_STRVAR(math_sum_doc, |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 506 | "sum(iterable)\n\n\ |
| 507 | Return an accurate floating point sum of values in the iterable.\n\ |
| 508 | Assumes IEEE-754 floating point arithmetic."); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 509 | |
Raymond Hettinger | ecbdd2e | 2008-06-09 06:54:45 +0000 | [diff] [blame] | 510 | |
| 511 | static PyObject * |
| 512 | math_factorial(PyObject *self, PyObject *arg) |
| 513 | { |
| 514 | long i, x; |
| 515 | PyObject *result, *iobj, *newresult; |
| 516 | |
| 517 | if (PyFloat_Check(arg)) { |
| 518 | double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); |
| 519 | if (dx != floor(dx)) { |
| 520 | PyErr_SetString(PyExc_ValueError, |
| 521 | "factorial() only accepts integral values"); |
| 522 | return NULL; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | x = PyInt_AsLong(arg); |
| 527 | if (x == -1 && PyErr_Occurred()) |
| 528 | return NULL; |
| 529 | if (x < 0) { |
| 530 | PyErr_SetString(PyExc_ValueError, |
| 531 | "factorial() not defined for negative values"); |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | result = (PyObject *)PyInt_FromLong(1); |
| 536 | if (result == NULL) |
| 537 | return NULL; |
| 538 | for (i=1 ; i<=x ; i++) { |
| 539 | iobj = (PyObject *)PyInt_FromLong(i); |
| 540 | if (iobj == NULL) |
| 541 | goto error; |
| 542 | newresult = PyNumber_Multiply(result, iobj); |
| 543 | Py_DECREF(iobj); |
| 544 | if (newresult == NULL) |
| 545 | goto error; |
| 546 | Py_DECREF(result); |
| 547 | result = newresult; |
| 548 | } |
| 549 | return result; |
| 550 | |
| 551 | error: |
| 552 | Py_DECREF(result); |
| 553 | Py_XDECREF(iobj); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | PyDoc_STRVAR(math_factorial_doc, "Return n!"); |
| 558 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 559 | static PyObject * |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 560 | math_trunc(PyObject *self, PyObject *number) |
| 561 | { |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 562 | return PyObject_CallMethod(number, "__trunc__", NULL); |
| 563 | } |
| 564 | |
| 565 | PyDoc_STRVAR(math_trunc_doc, |
| 566 | "trunc(x:Real) -> Integral\n" |
| 567 | "\n" |
Raymond Hettinger | fe424f7 | 2008-02-02 05:24:44 +0000 | [diff] [blame] | 568 | "Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method."); |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 569 | |
| 570 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 571 | math_frexp(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 572 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 573 | int i; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 574 | double x = PyFloat_AsDouble(arg); |
| 575 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 576 | return NULL; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 577 | /* deal with special cases directly, to sidestep platform |
| 578 | differences */ |
| 579 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
| 580 | i = 0; |
| 581 | } |
| 582 | else { |
| 583 | PyFPE_START_PROTECT("in math_frexp", return 0); |
| 584 | x = frexp(x, &i); |
| 585 | PyFPE_END_PROTECT(x); |
| 586 | } |
| 587 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 590 | PyDoc_STRVAR(math_frexp_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 591 | "frexp(x)\n" |
| 592 | "\n" |
| 593 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 594 | "m is a float and e is an int, such that x = m * 2.**e.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 595 | "If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 596 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 597 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 598 | math_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 599 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 600 | double x, r; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 601 | PyObject *oexp; |
| 602 | long exp; |
| 603 | if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 604 | return NULL; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 605 | |
| 606 | if (PyLong_Check(oexp)) { |
| 607 | /* on overflow, replace exponent with either LONG_MAX |
| 608 | or LONG_MIN, depending on the sign. */ |
| 609 | exp = PyLong_AsLong(oexp); |
| 610 | if (exp == -1 && PyErr_Occurred()) { |
| 611 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 612 | if (Py_SIZE(oexp) < 0) { |
| 613 | exp = LONG_MIN; |
| 614 | } |
| 615 | else { |
| 616 | exp = LONG_MAX; |
| 617 | } |
| 618 | PyErr_Clear(); |
| 619 | } |
| 620 | else { |
| 621 | /* propagate any unexpected exception */ |
| 622 | return NULL; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | else if (PyInt_Check(oexp)) { |
| 627 | exp = PyInt_AS_LONG(oexp); |
| 628 | } |
| 629 | else { |
| 630 | PyErr_SetString(PyExc_TypeError, |
| 631 | "Expected an int or long as second argument " |
| 632 | "to ldexp."); |
| 633 | return NULL; |
| 634 | } |
| 635 | |
| 636 | if (x == 0. || !Py_IS_FINITE(x)) { |
| 637 | /* NaNs, zeros and infinities are returned unchanged */ |
| 638 | r = x; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 639 | errno = 0; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 640 | } else if (exp > INT_MAX) { |
| 641 | /* overflow */ |
| 642 | r = copysign(Py_HUGE_VAL, x); |
| 643 | errno = ERANGE; |
| 644 | } else if (exp < INT_MIN) { |
| 645 | /* underflow to +-0 */ |
| 646 | r = copysign(0., x); |
| 647 | errno = 0; |
| 648 | } else { |
| 649 | errno = 0; |
| 650 | PyFPE_START_PROTECT("in math_ldexp", return 0); |
| 651 | r = ldexp(x, (int)exp); |
| 652 | PyFPE_END_PROTECT(r); |
| 653 | if (Py_IS_INFINITY(r)) |
| 654 | errno = ERANGE; |
| 655 | } |
| 656 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 657 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 658 | return NULL; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 659 | return PyFloat_FromDouble(r); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 662 | PyDoc_STRVAR(math_ldexp_doc, |
| 663 | "ldexp(x, i) -> x * (2**i)"); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 664 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 665 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 666 | math_modf(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 667 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 668 | double y, x = PyFloat_AsDouble(arg); |
| 669 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 670 | return NULL; |
Mark Dickinson | b2f7090 | 2008-04-20 01:39:24 +0000 | [diff] [blame] | 671 | /* some platforms don't do the right thing for NaNs and |
| 672 | infinities, so we take care of special cases directly. */ |
| 673 | if (!Py_IS_FINITE(x)) { |
| 674 | if (Py_IS_INFINITY(x)) |
| 675 | return Py_BuildValue("(dd)", copysign(0., x), x); |
| 676 | else if (Py_IS_NAN(x)) |
| 677 | return Py_BuildValue("(dd)", x, x); |
| 678 | } |
| 679 | |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 680 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 681 | PyFPE_START_PROTECT("in math_modf", return 0); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 682 | x = modf(x, &y); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 683 | PyFPE_END_PROTECT(x); |
| 684 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 685 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 686 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 687 | PyDoc_STRVAR(math_modf_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 688 | "modf(x)\n" |
| 689 | "\n" |
| 690 | "Return the fractional and integer parts of x. Both results carry the sign\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 691 | "of x. The integer part is returned as a real."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 692 | |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 693 | /* A decent logarithm is easy to compute even for huge longs, but libm can't |
| 694 | do that by itself -- loghelper can. func is log or log10, and name is |
| 695 | "log" or "log10". Note that overflow isn't possible: a long can contain |
| 696 | no more than INT_MAX * SHIFT bits, so has value certainly less than |
| 697 | 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
| 698 | small enough to fit in an IEEE single. log and log10 are even smaller. |
| 699 | */ |
| 700 | |
| 701 | static PyObject* |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 702 | loghelper(PyObject* arg, double (*func)(double), char *funcname) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 703 | { |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 704 | /* If it is long, do it ourselves. */ |
| 705 | if (PyLong_Check(arg)) { |
| 706 | double x; |
| 707 | int e; |
| 708 | x = _PyLong_AsScaledDouble(arg, &e); |
| 709 | if (x <= 0.0) { |
| 710 | PyErr_SetString(PyExc_ValueError, |
| 711 | "math domain error"); |
| 712 | return NULL; |
| 713 | } |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 714 | /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~= |
| 715 | log(x) + log(2) * e * PyLong_SHIFT. |
| 716 | CAUTION: e*PyLong_SHIFT may overflow using int arithmetic, |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 717 | so force use of double. */ |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 718 | x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 719 | return PyFloat_FromDouble(x); |
| 720 | } |
| 721 | |
| 722 | /* Else let libm handle it by itself. */ |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 723 | return math_1(arg, func, 0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static PyObject * |
| 727 | math_log(PyObject *self, PyObject *args) |
| 728 | { |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 729 | PyObject *arg; |
| 730 | PyObject *base = NULL; |
| 731 | PyObject *num, *den; |
| 732 | PyObject *ans; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 733 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 734 | if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 735 | return NULL; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 736 | |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 737 | num = loghelper(arg, log, "log"); |
| 738 | if (num == NULL || base == NULL) |
| 739 | return num; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 740 | |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 741 | den = loghelper(base, log, "log"); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 742 | if (den == NULL) { |
| 743 | Py_DECREF(num); |
| 744 | return NULL; |
| 745 | } |
| 746 | |
| 747 | ans = PyNumber_Divide(num, den); |
| 748 | Py_DECREF(num); |
| 749 | Py_DECREF(den); |
| 750 | return ans; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 753 | PyDoc_STRVAR(math_log_doc, |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 754 | "log(x[, base]) -> the logarithm of x to the given base.\n\ |
| 755 | If the base not specified, returns the natural logarithm (base e) of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 756 | |
| 757 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 758 | math_log10(PyObject *self, PyObject *arg) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 759 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 760 | return loghelper(arg, log10, "log10"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 763 | PyDoc_STRVAR(math_log10_doc, |
| 764 | "log10(x) -> the base 10 logarithm of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 765 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 766 | static PyObject * |
| 767 | math_fmod(PyObject *self, PyObject *args) |
| 768 | { |
| 769 | PyObject *ox, *oy; |
| 770 | double r, x, y; |
| 771 | if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
| 772 | return NULL; |
| 773 | x = PyFloat_AsDouble(ox); |
| 774 | y = PyFloat_AsDouble(oy); |
| 775 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 776 | return NULL; |
| 777 | /* fmod(x, +/-Inf) returns x for finite x. */ |
| 778 | if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
| 779 | return PyFloat_FromDouble(x); |
| 780 | errno = 0; |
| 781 | PyFPE_START_PROTECT("in math_fmod", return 0); |
| 782 | r = fmod(x, y); |
| 783 | PyFPE_END_PROTECT(r); |
| 784 | if (Py_IS_NAN(r)) { |
| 785 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 786 | errno = EDOM; |
| 787 | else |
| 788 | errno = 0; |
| 789 | } |
| 790 | if (errno && is_error(r)) |
| 791 | return NULL; |
| 792 | else |
| 793 | return PyFloat_FromDouble(r); |
| 794 | } |
| 795 | |
| 796 | PyDoc_STRVAR(math_fmod_doc, |
| 797 | "fmod(x,y)\n\nReturn fmod(x, y), according to platform C." |
| 798 | " x % y may differ."); |
| 799 | |
| 800 | static PyObject * |
| 801 | math_hypot(PyObject *self, PyObject *args) |
| 802 | { |
| 803 | PyObject *ox, *oy; |
| 804 | double r, x, y; |
| 805 | if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
| 806 | return NULL; |
| 807 | x = PyFloat_AsDouble(ox); |
| 808 | y = PyFloat_AsDouble(oy); |
| 809 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 810 | return NULL; |
| 811 | /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */ |
| 812 | if (Py_IS_INFINITY(x)) |
| 813 | return PyFloat_FromDouble(fabs(x)); |
| 814 | if (Py_IS_INFINITY(y)) |
| 815 | return PyFloat_FromDouble(fabs(y)); |
| 816 | errno = 0; |
| 817 | PyFPE_START_PROTECT("in math_hypot", return 0); |
| 818 | r = hypot(x, y); |
| 819 | PyFPE_END_PROTECT(r); |
| 820 | if (Py_IS_NAN(r)) { |
| 821 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 822 | errno = EDOM; |
| 823 | else |
| 824 | errno = 0; |
| 825 | } |
| 826 | else if (Py_IS_INFINITY(r)) { |
| 827 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 828 | errno = ERANGE; |
| 829 | else |
| 830 | errno = 0; |
| 831 | } |
| 832 | if (errno && is_error(r)) |
| 833 | return NULL; |
| 834 | else |
| 835 | return PyFloat_FromDouble(r); |
| 836 | } |
| 837 | |
| 838 | PyDoc_STRVAR(math_hypot_doc, |
| 839 | "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y)."); |
| 840 | |
| 841 | /* pow can't use math_2, but needs its own wrapper: the problem is |
| 842 | that an infinite result can arise either as a result of overflow |
| 843 | (in which case OverflowError should be raised) or as a result of |
| 844 | e.g. 0.**-5. (for which ValueError needs to be raised.) |
| 845 | */ |
| 846 | |
| 847 | static PyObject * |
| 848 | math_pow(PyObject *self, PyObject *args) |
| 849 | { |
| 850 | PyObject *ox, *oy; |
| 851 | double r, x, y; |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 852 | int odd_y; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 853 | |
| 854 | if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
| 855 | return NULL; |
| 856 | x = PyFloat_AsDouble(ox); |
| 857 | y = PyFloat_AsDouble(oy); |
| 858 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 859 | return NULL; |
Mark Dickinson | a1293eb | 2008-04-19 19:41:52 +0000 | [diff] [blame] | 860 | |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 861 | /* deal directly with IEEE specials, to cope with problems on various |
| 862 | platforms whose semantics don't exactly match C99 */ |
Mark Dickinson | 0da94c8 | 2008-04-21 01:55:50 +0000 | [diff] [blame] | 863 | r = 0.; /* silence compiler warning */ |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 864 | if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { |
| 865 | errno = 0; |
| 866 | if (Py_IS_NAN(x)) |
| 867 | r = y == 0. ? 1. : x; /* NaN**0 = 1 */ |
| 868 | else if (Py_IS_NAN(y)) |
| 869 | r = x == 1. ? 1. : y; /* 1**NaN = 1 */ |
| 870 | else if (Py_IS_INFINITY(x)) { |
| 871 | odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
| 872 | if (y > 0.) |
| 873 | r = odd_y ? x : fabs(x); |
| 874 | else if (y == 0.) |
| 875 | r = 1.; |
| 876 | else /* y < 0. */ |
| 877 | r = odd_y ? copysign(0., x) : 0.; |
| 878 | } |
| 879 | else if (Py_IS_INFINITY(y)) { |
| 880 | if (fabs(x) == 1.0) |
| 881 | r = 1.; |
| 882 | else if (y > 0. && fabs(x) > 1.0) |
| 883 | r = y; |
| 884 | else if (y < 0. && fabs(x) < 1.0) { |
| 885 | r = -y; /* result is +inf */ |
| 886 | if (x == 0.) /* 0**-inf: divide-by-zero */ |
| 887 | errno = EDOM; |
| 888 | } |
| 889 | else |
| 890 | r = 0.; |
| 891 | } |
Mark Dickinson | e941d97 | 2008-04-19 18:51:48 +0000 | [diff] [blame] | 892 | } |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 893 | else { |
| 894 | /* let libm handle finite**finite */ |
| 895 | errno = 0; |
| 896 | PyFPE_START_PROTECT("in math_pow", return 0); |
| 897 | r = pow(x, y); |
| 898 | PyFPE_END_PROTECT(r); |
| 899 | /* a NaN result should arise only from (-ve)**(finite |
| 900 | non-integer); in this case we want to raise ValueError. */ |
| 901 | if (!Py_IS_FINITE(r)) { |
| 902 | if (Py_IS_NAN(r)) { |
| 903 | errno = EDOM; |
| 904 | } |
| 905 | /* |
| 906 | an infinite result here arises either from: |
| 907 | (A) (+/-0.)**negative (-> divide-by-zero) |
| 908 | (B) overflow of x**y with x and y finite |
| 909 | */ |
| 910 | else if (Py_IS_INFINITY(r)) { |
| 911 | if (x == 0.) |
| 912 | errno = EDOM; |
| 913 | else |
| 914 | errno = ERANGE; |
| 915 | } |
| 916 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | if (errno && is_error(r)) |
| 920 | return NULL; |
| 921 | else |
| 922 | return PyFloat_FromDouble(r); |
| 923 | } |
| 924 | |
| 925 | PyDoc_STRVAR(math_pow_doc, |
| 926 | "pow(x,y)\n\nReturn x**y (x to the power of y)."); |
| 927 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 928 | static const double degToRad = Py_MATH_PI / 180.0; |
| 929 | static const double radToDeg = 180.0 / Py_MATH_PI; |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 930 | |
| 931 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 932 | math_degrees(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 933 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 934 | double x = PyFloat_AsDouble(arg); |
| 935 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 936 | return NULL; |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 937 | return PyFloat_FromDouble(x * radToDeg); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 940 | PyDoc_STRVAR(math_degrees_doc, |
| 941 | "degrees(x) -> converts angle x from radians to degrees"); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 942 | |
| 943 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 944 | math_radians(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 945 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 946 | double x = PyFloat_AsDouble(arg); |
| 947 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 948 | return NULL; |
| 949 | return PyFloat_FromDouble(x * degToRad); |
| 950 | } |
| 951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 952 | PyDoc_STRVAR(math_radians_doc, |
| 953 | "radians(x) -> converts angle x from degrees to radians"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 954 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 955 | static PyObject * |
| 956 | math_isnan(PyObject *self, PyObject *arg) |
| 957 | { |
| 958 | double x = PyFloat_AsDouble(arg); |
| 959 | if (x == -1.0 && PyErr_Occurred()) |
| 960 | return NULL; |
| 961 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 962 | } |
| 963 | |
| 964 | PyDoc_STRVAR(math_isnan_doc, |
| 965 | "isnan(x) -> bool\n\ |
| 966 | Checks if float x is not a number (NaN)"); |
| 967 | |
| 968 | static PyObject * |
| 969 | math_isinf(PyObject *self, PyObject *arg) |
| 970 | { |
| 971 | double x = PyFloat_AsDouble(arg); |
| 972 | if (x == -1.0 && PyErr_Occurred()) |
| 973 | return NULL; |
| 974 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 975 | } |
| 976 | |
| 977 | PyDoc_STRVAR(math_isinf_doc, |
| 978 | "isinf(x) -> bool\n\ |
| 979 | Checks if float x is infinite (positive or negative)"); |
| 980 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 981 | static PyMethodDef math_methods[] = { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 982 | {"acos", math_acos, METH_O, math_acos_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 983 | {"acosh", math_acosh, METH_O, math_acosh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 984 | {"asin", math_asin, METH_O, math_asin_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 985 | {"asinh", math_asinh, METH_O, math_asinh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 986 | {"atan", math_atan, METH_O, math_atan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 987 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 988 | {"atanh", math_atanh, METH_O, math_atanh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 989 | {"ceil", math_ceil, METH_O, math_ceil_doc}, |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 990 | {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 991 | {"cos", math_cos, METH_O, math_cos_doc}, |
| 992 | {"cosh", math_cosh, METH_O, math_cosh_doc}, |
| 993 | {"degrees", math_degrees, METH_O, math_degrees_doc}, |
| 994 | {"exp", math_exp, METH_O, math_exp_doc}, |
| 995 | {"fabs", math_fabs, METH_O, math_fabs_doc}, |
Raymond Hettinger | ecbdd2e | 2008-06-09 06:54:45 +0000 | [diff] [blame] | 996 | {"factorial", math_factorial, METH_O, math_factorial_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 997 | {"floor", math_floor, METH_O, math_floor_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 998 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 999 | {"frexp", math_frexp, METH_O, math_frexp_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1000 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 1001 | {"isinf", math_isinf, METH_O, math_isinf_doc}, |
| 1002 | {"isnan", math_isnan, METH_O, math_isnan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1003 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
| 1004 | {"log", math_log, METH_VARARGS, math_log_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1005 | {"log1p", math_log1p, METH_O, math_log1p_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1006 | {"log10", math_log10, METH_O, math_log10_doc}, |
| 1007 | {"modf", math_modf, METH_O, math_modf_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1008 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1009 | {"radians", math_radians, METH_O, math_radians_doc}, |
| 1010 | {"sin", math_sin, METH_O, math_sin_doc}, |
| 1011 | {"sinh", math_sinh, METH_O, math_sinh_doc}, |
| 1012 | {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 1013 | {"sum", math_sum, METH_O, math_sum_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1014 | {"tan", math_tan, METH_O, math_tan_doc}, |
| 1015 | {"tanh", math_tanh, METH_O, math_tanh_doc}, |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 1016 | {"trunc", math_trunc, METH_O, math_trunc_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1017 | {NULL, NULL} /* sentinel */ |
| 1018 | }; |
| 1019 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1021 | PyDoc_STRVAR(module_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1022 | "This module is always available. It provides access to the\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1023 | "mathematical functions defined by the C standard."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1024 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1025 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1026 | initmath(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1027 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1028 | PyObject *m; |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 1029 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1030 | m = Py_InitModule3("math", math_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1031 | if (m == NULL) |
| 1032 | goto finally; |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1033 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1034 | PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
| 1035 | PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1036 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1037 | finally: |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 1038 | return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1039 | } |