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