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