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, |
Mark Dickinson | 66bada5 | 2008-06-18 10:04:31 +0000 | [diff] [blame] | 184 | "math domain error"); /* invalid arg */ |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 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, |
Mark Dickinson | 66bada5 | 2008-06-18 10:04:31 +0000 | [diff] [blame] | 190 | "math range error"); /* overflow */ |
Mark Dickinson | b63aff1 | 2008-05-09 14:10:27 +0000 | [diff] [blame] | 191 | else |
| 192 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 66bada5 | 2008-06-18 10:04:31 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 385 | Note 3: The intermediate values lo, yr, and hi are declared volatile so |
| 386 | aggressive compilers won't algebraically reduce lo to always be exactly 0.0. |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 389 | prevents double rounding because any addition or subtraction of two doubles |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 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 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 399 | Note 5: The signature of math.fsum() differs from __builtin__.sum() |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 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 */ |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 410 | _fsum_realloc(double **p_ptr, Py_ssize_t n, |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 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 */ |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 428 | PyErr_SetString(PyExc_MemoryError, "math.fsum partials"); |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 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* |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 467 | math_fsum(PyObject *self, PyObject *seq) |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 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; |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 472 | double xsave, special_sum = 0.0, inf_sum = 0.0; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 473 | volatile double hi, yr, lo; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 474 | |
| 475 | iter = PyObject_GetIter(seq); |
| 476 | if (iter == NULL) |
| 477 | return NULL; |
| 478 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 479 | PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL) |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 480 | |
| 481 | for(;;) { /* for x in iterable */ |
| 482 | assert(0 <= n && n <= m); |
| 483 | assert((m == NUM_PARTIALS && p == ps) || |
| 484 | (m > NUM_PARTIALS && p != NULL)); |
| 485 | |
| 486 | item = PyIter_Next(iter); |
| 487 | if (item == NULL) { |
| 488 | if (PyErr_Occurred()) |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 489 | goto _fsum_error; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | x = PyFloat_AsDouble(item); |
| 493 | Py_DECREF(item); |
| 494 | if (PyErr_Occurred()) |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 495 | goto _fsum_error; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 496 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 497 | xsave = x; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 498 | for (i = j = 0; j < n; j++) { /* for y in partials */ |
| 499 | y = p[j]; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 500 | if (fabs(x) < fabs(y)) { |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 501 | t = x; x = y; y = t; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 502 | } |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 503 | hi = x + y; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 504 | yr = hi - x; |
| 505 | lo = y - yr; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 506 | if (lo != 0.0) |
| 507 | p[i++] = lo; |
| 508 | x = hi; |
| 509 | } |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 510 | |
| 511 | n = i; /* ps[i:] = [x] */ |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 512 | if (x != 0.0) { |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 513 | if (! Py_IS_FINITE(x)) { |
| 514 | /* a nonfinite x could arise either as |
| 515 | a result of intermediate overflow, or |
| 516 | as a result of a nan or inf in the |
| 517 | summands */ |
| 518 | if (Py_IS_FINITE(xsave)) { |
| 519 | PyErr_SetString(PyExc_OverflowError, |
| 520 | "intermediate overflow in fsum"); |
| 521 | goto _fsum_error; |
| 522 | } |
| 523 | if (Py_IS_INFINITY(xsave)) |
| 524 | inf_sum += xsave; |
| 525 | special_sum += xsave; |
| 526 | /* reset partials */ |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 527 | n = 0; |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 528 | } |
| 529 | else if (n >= m && _fsum_realloc(&p, n, ps, &m)) |
| 530 | goto _fsum_error; |
| 531 | else |
| 532 | p[n++] = x; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 536 | if (special_sum != 0.0) { |
| 537 | if (Py_IS_NAN(inf_sum)) |
| 538 | PyErr_SetString(PyExc_ValueError, |
| 539 | "-inf + inf in fsum"); |
| 540 | else |
| 541 | sum = PyFloat_FromDouble(special_sum); |
| 542 | goto _fsum_error; |
| 543 | } |
| 544 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 545 | hi = 0.0; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 546 | if (n > 0) { |
| 547 | hi = p[--n]; |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 548 | /* sum_exact(ps, hi) from the top, stop when the sum becomes |
| 549 | inexact. */ |
| 550 | while (n > 0) { |
| 551 | x = hi; |
| 552 | y = p[--n]; |
| 553 | assert(fabs(y) < fabs(x)); |
| 554 | hi = x + y; |
| 555 | yr = hi - x; |
| 556 | lo = y - yr; |
| 557 | if (lo != 0.0) |
| 558 | break; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 559 | } |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 560 | /* Make half-even rounding work across multiple partials. |
| 561 | Needed so that sum([1e-16, 1, 1e16]) will round-up the last |
| 562 | digit to two instead of down to zero (the 1e-16 makes the 1 |
| 563 | slightly closer to two). With a potential 1 ULP rounding |
| 564 | error fixed-up, math.fsum() can guarantee commutativity. */ |
| 565 | if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || |
| 566 | (lo > 0.0 && p[n-1] > 0.0))) { |
| 567 | y = lo * 2.0; |
| 568 | x = hi + y; |
| 569 | yr = x - hi; |
| 570 | if (y == yr) |
| 571 | hi = x; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 574 | sum = PyFloat_FromDouble(hi); |
| 575 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 576 | _fsum_error: |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 577 | PyFPE_END_PROTECT(hi) |
| 578 | Py_DECREF(iter); |
| 579 | if (p != ps) |
| 580 | PyMem_Free(p); |
| 581 | return sum; |
| 582 | } |
| 583 | |
| 584 | #undef NUM_PARTIALS |
| 585 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 586 | PyDoc_STRVAR(math_fsum_doc, |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 587 | "sum(iterable)\n\n\ |
| 588 | Return an accurate floating point sum of values in the iterable.\n\ |
| 589 | Assumes IEEE-754 floating point arithmetic."); |
| 590 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 591 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 592 | math_factorial(PyObject *self, PyObject *arg) |
| 593 | { |
| 594 | long i, x; |
| 595 | PyObject *result, *iobj, *newresult; |
| 596 | |
| 597 | if (PyFloat_Check(arg)) { |
| 598 | double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); |
| 599 | if (dx != floor(dx)) { |
| 600 | PyErr_SetString(PyExc_ValueError, |
| 601 | "factorial() only accepts integral values"); |
| 602 | return NULL; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | x = PyLong_AsLong(arg); |
| 607 | if (x == -1 && PyErr_Occurred()) |
| 608 | return NULL; |
| 609 | if (x < 0) { |
| 610 | PyErr_SetString(PyExc_ValueError, |
| 611 | "factorial() not defined for negative values"); |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | result = (PyObject *)PyLong_FromLong(1); |
| 616 | if (result == NULL) |
| 617 | return NULL; |
| 618 | for (i=1 ; i<=x ; i++) { |
| 619 | iobj = (PyObject *)PyLong_FromLong(i); |
| 620 | if (iobj == NULL) |
| 621 | goto error; |
| 622 | newresult = PyNumber_Multiply(result, iobj); |
| 623 | Py_DECREF(iobj); |
| 624 | if (newresult == NULL) |
| 625 | goto error; |
| 626 | Py_DECREF(result); |
| 627 | result = newresult; |
| 628 | } |
| 629 | return result; |
| 630 | |
| 631 | error: |
| 632 | Py_DECREF(result); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 633 | return NULL; |
| 634 | } |
| 635 | |
| 636 | PyDoc_STRVAR(math_factorial_doc, "Return n!"); |
| 637 | |
| 638 | static PyObject * |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 639 | math_trunc(PyObject *self, PyObject *number) |
| 640 | { |
| 641 | static PyObject *trunc_str = NULL; |
| 642 | PyObject *trunc; |
| 643 | |
| 644 | if (Py_TYPE(number)->tp_dict == NULL) { |
| 645 | if (PyType_Ready(Py_TYPE(number)) < 0) |
| 646 | return NULL; |
| 647 | } |
| 648 | |
| 649 | if (trunc_str == NULL) { |
| 650 | trunc_str = PyUnicode_InternFromString("__trunc__"); |
| 651 | if (trunc_str == NULL) |
| 652 | return NULL; |
| 653 | } |
| 654 | |
| 655 | trunc = _PyType_Lookup(Py_TYPE(number), trunc_str); |
| 656 | if (trunc == NULL) { |
| 657 | PyErr_Format(PyExc_TypeError, |
| 658 | "type %.100s doesn't define __trunc__ method", |
| 659 | Py_TYPE(number)->tp_name); |
| 660 | return NULL; |
| 661 | } |
| 662 | return PyObject_CallFunctionObjArgs(trunc, number, NULL); |
| 663 | } |
| 664 | |
| 665 | PyDoc_STRVAR(math_trunc_doc, |
| 666 | "trunc(x:Real) -> Integral\n" |
| 667 | "\n" |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 668 | "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] | 669 | |
| 670 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 671 | math_frexp(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 672 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 673 | int i; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 674 | double x = PyFloat_AsDouble(arg); |
| 675 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 676 | return NULL; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 677 | /* deal with special cases directly, to sidestep platform |
| 678 | differences */ |
| 679 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
| 680 | i = 0; |
| 681 | } |
| 682 | else { |
| 683 | PyFPE_START_PROTECT("in math_frexp", return 0); |
| 684 | x = frexp(x, &i); |
| 685 | PyFPE_END_PROTECT(x); |
| 686 | } |
| 687 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 690 | PyDoc_STRVAR(math_frexp_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 691 | "frexp(x)\n" |
| 692 | "\n" |
| 693 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 694 | "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] | 695 | "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] | 696 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 697 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 698 | math_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 699 | { |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 700 | double x, r; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 701 | PyObject *oexp; |
| 702 | long exp; |
| 703 | if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 704 | return NULL; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 705 | |
| 706 | if (PyLong_Check(oexp)) { |
| 707 | /* on overflow, replace exponent with either LONG_MAX |
| 708 | or LONG_MIN, depending on the sign. */ |
| 709 | exp = PyLong_AsLong(oexp); |
| 710 | if (exp == -1 && PyErr_Occurred()) { |
| 711 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 712 | if (Py_SIZE(oexp) < 0) { |
| 713 | exp = LONG_MIN; |
| 714 | } |
| 715 | else { |
| 716 | exp = LONG_MAX; |
| 717 | } |
| 718 | PyErr_Clear(); |
| 719 | } |
| 720 | else { |
| 721 | /* propagate any unexpected exception */ |
| 722 | return NULL; |
| 723 | } |
| 724 | } |
| 725 | } |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 726 | else { |
| 727 | PyErr_SetString(PyExc_TypeError, |
| 728 | "Expected an int or long as second argument " |
| 729 | "to ldexp."); |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | if (x == 0. || !Py_IS_FINITE(x)) { |
| 734 | /* NaNs, zeros and infinities are returned unchanged */ |
| 735 | r = x; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 736 | errno = 0; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 737 | } else if (exp > INT_MAX) { |
| 738 | /* overflow */ |
| 739 | r = copysign(Py_HUGE_VAL, x); |
| 740 | errno = ERANGE; |
| 741 | } else if (exp < INT_MIN) { |
| 742 | /* underflow to +-0 */ |
| 743 | r = copysign(0., x); |
| 744 | errno = 0; |
| 745 | } else { |
| 746 | errno = 0; |
| 747 | PyFPE_START_PROTECT("in math_ldexp", return 0); |
| 748 | r = ldexp(x, (int)exp); |
| 749 | PyFPE_END_PROTECT(r); |
| 750 | if (Py_IS_INFINITY(r)) |
| 751 | errno = ERANGE; |
| 752 | } |
| 753 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 754 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 755 | return NULL; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 756 | return PyFloat_FromDouble(r); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 759 | PyDoc_STRVAR(math_ldexp_doc, |
| 760 | "ldexp(x, i) -> x * (2**i)"); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 761 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 762 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 763 | math_modf(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 764 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 765 | double y, x = PyFloat_AsDouble(arg); |
| 766 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 767 | return NULL; |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 768 | /* some platforms don't do the right thing for NaNs and |
| 769 | infinities, so we take care of special cases directly. */ |
| 770 | if (!Py_IS_FINITE(x)) { |
| 771 | if (Py_IS_INFINITY(x)) |
| 772 | return Py_BuildValue("(dd)", copysign(0., x), x); |
| 773 | else if (Py_IS_NAN(x)) |
| 774 | return Py_BuildValue("(dd)", x, x); |
| 775 | } |
| 776 | |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 777 | errno = 0; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 778 | PyFPE_START_PROTECT("in math_modf", return 0); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 779 | x = modf(x, &y); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 780 | PyFPE_END_PROTECT(x); |
| 781 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 782 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 784 | PyDoc_STRVAR(math_modf_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 785 | "modf(x)\n" |
| 786 | "\n" |
| 787 | "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] | 788 | "of x. The integer part is returned as a real."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 789 | |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 790 | /* A decent logarithm is easy to compute even for huge longs, but libm can't |
| 791 | do that by itself -- loghelper can. func is log or log10, and name is |
| 792 | "log" or "log10". Note that overflow isn't possible: a long can contain |
| 793 | no more than INT_MAX * SHIFT bits, so has value certainly less than |
| 794 | 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
| 795 | small enough to fit in an IEEE single. log and log10 are even smaller. |
| 796 | */ |
| 797 | |
| 798 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 799 | loghelper(PyObject* arg, double (*func)(double), char *funcname) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 800 | { |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 801 | /* If it is long, do it ourselves. */ |
| 802 | if (PyLong_Check(arg)) { |
| 803 | double x; |
| 804 | int e; |
| 805 | x = _PyLong_AsScaledDouble(arg, &e); |
| 806 | if (x <= 0.0) { |
| 807 | PyErr_SetString(PyExc_ValueError, |
| 808 | "math domain error"); |
| 809 | return NULL; |
| 810 | } |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 811 | /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~= |
| 812 | log(x) + log(2) * e * PyLong_SHIFT. |
| 813 | CAUTION: e*PyLong_SHIFT may overflow using int arithmetic, |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 814 | so force use of double. */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 815 | x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 816 | return PyFloat_FromDouble(x); |
| 817 | } |
| 818 | |
| 819 | /* Else let libm handle it by itself. */ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 820 | return math_1(arg, func, 0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static PyObject * |
| 824 | math_log(PyObject *self, PyObject *args) |
| 825 | { |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 826 | PyObject *arg; |
| 827 | PyObject *base = NULL; |
| 828 | PyObject *num, *den; |
| 829 | PyObject *ans; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 830 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 831 | if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 832 | return NULL; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 833 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 834 | num = loghelper(arg, log, "log"); |
| 835 | if (num == NULL || base == NULL) |
| 836 | return num; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 837 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 838 | den = loghelper(base, log, "log"); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 839 | if (den == NULL) { |
| 840 | Py_DECREF(num); |
| 841 | return NULL; |
| 842 | } |
| 843 | |
Neal Norwitz | bcc0db8 | 2006-03-24 08:14:36 +0000 | [diff] [blame] | 844 | ans = PyNumber_TrueDivide(num, den); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 845 | Py_DECREF(num); |
| 846 | Py_DECREF(den); |
| 847 | return ans; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 850 | PyDoc_STRVAR(math_log_doc, |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 851 | "log(x[, base]) -> the logarithm of x to the given base.\n\ |
| 852 | 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] | 853 | |
| 854 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 855 | math_log10(PyObject *self, PyObject *arg) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 856 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 857 | return loghelper(arg, log10, "log10"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 860 | PyDoc_STRVAR(math_log10_doc, |
| 861 | "log10(x) -> the base 10 logarithm of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 862 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 863 | static PyObject * |
| 864 | math_fmod(PyObject *self, PyObject *args) |
| 865 | { |
| 866 | PyObject *ox, *oy; |
| 867 | double r, x, y; |
| 868 | if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
| 869 | return NULL; |
| 870 | x = PyFloat_AsDouble(ox); |
| 871 | y = PyFloat_AsDouble(oy); |
| 872 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 873 | return NULL; |
| 874 | /* fmod(x, +/-Inf) returns x for finite x. */ |
| 875 | if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
| 876 | return PyFloat_FromDouble(x); |
| 877 | errno = 0; |
| 878 | PyFPE_START_PROTECT("in math_fmod", return 0); |
| 879 | r = fmod(x, y); |
| 880 | PyFPE_END_PROTECT(r); |
| 881 | if (Py_IS_NAN(r)) { |
| 882 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 883 | errno = EDOM; |
| 884 | else |
| 885 | errno = 0; |
| 886 | } |
| 887 | if (errno && is_error(r)) |
| 888 | return NULL; |
| 889 | else |
| 890 | return PyFloat_FromDouble(r); |
| 891 | } |
| 892 | |
| 893 | PyDoc_STRVAR(math_fmod_doc, |
| 894 | "fmod(x,y)\n\nReturn fmod(x, y), according to platform C." |
| 895 | " x % y may differ."); |
| 896 | |
| 897 | static PyObject * |
| 898 | math_hypot(PyObject *self, PyObject *args) |
| 899 | { |
| 900 | PyObject *ox, *oy; |
| 901 | double r, x, y; |
| 902 | if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
| 903 | return NULL; |
| 904 | x = PyFloat_AsDouble(ox); |
| 905 | y = PyFloat_AsDouble(oy); |
| 906 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 907 | return NULL; |
| 908 | /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */ |
| 909 | if (Py_IS_INFINITY(x)) |
| 910 | return PyFloat_FromDouble(fabs(x)); |
| 911 | if (Py_IS_INFINITY(y)) |
| 912 | return PyFloat_FromDouble(fabs(y)); |
| 913 | errno = 0; |
| 914 | PyFPE_START_PROTECT("in math_hypot", return 0); |
| 915 | r = hypot(x, y); |
| 916 | PyFPE_END_PROTECT(r); |
| 917 | if (Py_IS_NAN(r)) { |
| 918 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 919 | errno = EDOM; |
| 920 | else |
| 921 | errno = 0; |
| 922 | } |
| 923 | else if (Py_IS_INFINITY(r)) { |
| 924 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 925 | errno = ERANGE; |
| 926 | else |
| 927 | errno = 0; |
| 928 | } |
| 929 | if (errno && is_error(r)) |
| 930 | return NULL; |
| 931 | else |
| 932 | return PyFloat_FromDouble(r); |
| 933 | } |
| 934 | |
| 935 | PyDoc_STRVAR(math_hypot_doc, |
| 936 | "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y)."); |
| 937 | |
| 938 | /* pow can't use math_2, but needs its own wrapper: the problem is |
| 939 | that an infinite result can arise either as a result of overflow |
| 940 | (in which case OverflowError should be raised) or as a result of |
| 941 | e.g. 0.**-5. (for which ValueError needs to be raised.) |
| 942 | */ |
| 943 | |
| 944 | static PyObject * |
| 945 | math_pow(PyObject *self, PyObject *args) |
| 946 | { |
| 947 | PyObject *ox, *oy; |
| 948 | double r, x, y; |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 949 | int odd_y; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 950 | |
| 951 | if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
| 952 | return NULL; |
| 953 | x = PyFloat_AsDouble(ox); |
| 954 | y = PyFloat_AsDouble(oy); |
| 955 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 956 | return NULL; |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 957 | |
| 958 | /* deal directly with IEEE specials, to cope with problems on various |
| 959 | platforms whose semantics don't exactly match C99 */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 960 | r = 0.; /* silence compiler warning */ |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 961 | if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { |
| 962 | errno = 0; |
| 963 | if (Py_IS_NAN(x)) |
| 964 | r = y == 0. ? 1. : x; /* NaN**0 = 1 */ |
| 965 | else if (Py_IS_NAN(y)) |
| 966 | r = x == 1. ? 1. : y; /* 1**NaN = 1 */ |
| 967 | else if (Py_IS_INFINITY(x)) { |
| 968 | odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
| 969 | if (y > 0.) |
| 970 | r = odd_y ? x : fabs(x); |
| 971 | else if (y == 0.) |
| 972 | r = 1.; |
| 973 | else /* y < 0. */ |
| 974 | r = odd_y ? copysign(0., x) : 0.; |
| 975 | } |
| 976 | else if (Py_IS_INFINITY(y)) { |
| 977 | if (fabs(x) == 1.0) |
| 978 | r = 1.; |
| 979 | else if (y > 0. && fabs(x) > 1.0) |
| 980 | r = y; |
| 981 | else if (y < 0. && fabs(x) < 1.0) { |
| 982 | r = -y; /* result is +inf */ |
| 983 | if (x == 0.) /* 0**-inf: divide-by-zero */ |
| 984 | errno = EDOM; |
| 985 | } |
| 986 | else |
| 987 | r = 0.; |
| 988 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 989 | } |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 990 | else { |
| 991 | /* let libm handle finite**finite */ |
| 992 | errno = 0; |
| 993 | PyFPE_START_PROTECT("in math_pow", return 0); |
| 994 | r = pow(x, y); |
| 995 | PyFPE_END_PROTECT(r); |
| 996 | /* a NaN result should arise only from (-ve)**(finite |
| 997 | non-integer); in this case we want to raise ValueError. */ |
| 998 | if (!Py_IS_FINITE(r)) { |
| 999 | if (Py_IS_NAN(r)) { |
| 1000 | errno = EDOM; |
| 1001 | } |
| 1002 | /* |
| 1003 | an infinite result here arises either from: |
| 1004 | (A) (+/-0.)**negative (-> divide-by-zero) |
| 1005 | (B) overflow of x**y with x and y finite |
| 1006 | */ |
| 1007 | else if (Py_IS_INFINITY(r)) { |
| 1008 | if (x == 0.) |
| 1009 | errno = EDOM; |
| 1010 | else |
| 1011 | errno = ERANGE; |
| 1012 | } |
| 1013 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | if (errno && is_error(r)) |
| 1017 | return NULL; |
| 1018 | else |
| 1019 | return PyFloat_FromDouble(r); |
| 1020 | } |
| 1021 | |
| 1022 | PyDoc_STRVAR(math_pow_doc, |
| 1023 | "pow(x,y)\n\nReturn x**y (x to the power of y)."); |
| 1024 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1025 | static const double degToRad = Py_MATH_PI / 180.0; |
| 1026 | static const double radToDeg = 180.0 / Py_MATH_PI; |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1027 | |
| 1028 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1029 | math_degrees(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1030 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1031 | double x = PyFloat_AsDouble(arg); |
| 1032 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1033 | return NULL; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1034 | return PyFloat_FromDouble(x * radToDeg); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1037 | PyDoc_STRVAR(math_degrees_doc, |
| 1038 | "degrees(x) -> converts angle x from radians to degrees"); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1039 | |
| 1040 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1041 | math_radians(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1042 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1043 | double x = PyFloat_AsDouble(arg); |
| 1044 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1045 | return NULL; |
| 1046 | return PyFloat_FromDouble(x * degToRad); |
| 1047 | } |
| 1048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1049 | PyDoc_STRVAR(math_radians_doc, |
| 1050 | "radians(x) -> converts angle x from degrees to radians"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1051 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1052 | static PyObject * |
| 1053 | math_isnan(PyObject *self, PyObject *arg) |
| 1054 | { |
| 1055 | double x = PyFloat_AsDouble(arg); |
| 1056 | if (x == -1.0 && PyErr_Occurred()) |
| 1057 | return NULL; |
| 1058 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 1059 | } |
| 1060 | |
| 1061 | PyDoc_STRVAR(math_isnan_doc, |
| 1062 | "isnan(x) -> bool\n\ |
| 1063 | Checks if float x is not a number (NaN)"); |
| 1064 | |
| 1065 | static PyObject * |
| 1066 | math_isinf(PyObject *self, PyObject *arg) |
| 1067 | { |
| 1068 | double x = PyFloat_AsDouble(arg); |
| 1069 | if (x == -1.0 && PyErr_Occurred()) |
| 1070 | return NULL; |
| 1071 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 1072 | } |
| 1073 | |
| 1074 | PyDoc_STRVAR(math_isinf_doc, |
| 1075 | "isinf(x) -> bool\n\ |
| 1076 | Checks if float x is infinite (positive or negative)"); |
| 1077 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1078 | static PyMethodDef math_methods[] = { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1079 | {"acos", math_acos, METH_O, math_acos_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1080 | {"acosh", math_acosh, METH_O, math_acosh_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1081 | {"asin", math_asin, METH_O, math_asin_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1082 | {"asinh", math_asinh, METH_O, math_asinh_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1083 | {"atan", math_atan, METH_O, math_atan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1084 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1085 | {"atanh", math_atanh, METH_O, math_atanh_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1086 | {"ceil", math_ceil, METH_O, math_ceil_doc}, |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1087 | {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1088 | {"cos", math_cos, METH_O, math_cos_doc}, |
| 1089 | {"cosh", math_cosh, METH_O, math_cosh_doc}, |
| 1090 | {"degrees", math_degrees, METH_O, math_degrees_doc}, |
| 1091 | {"exp", math_exp, METH_O, math_exp_doc}, |
| 1092 | {"fabs", math_fabs, METH_O, math_fabs_doc}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1093 | {"factorial", math_factorial, METH_O, math_factorial_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1094 | {"floor", math_floor, METH_O, math_floor_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1095 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1096 | {"frexp", math_frexp, METH_O, math_frexp_doc}, |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1097 | {"fsum", math_fsum, METH_O, math_fsum_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1098 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1099 | {"isinf", math_isinf, METH_O, math_isinf_doc}, |
| 1100 | {"isnan", math_isnan, METH_O, math_isnan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1101 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
| 1102 | {"log", math_log, METH_VARARGS, math_log_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1103 | {"log1p", math_log1p, METH_O, math_log1p_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1104 | {"log10", math_log10, METH_O, math_log10_doc}, |
| 1105 | {"modf", math_modf, METH_O, math_modf_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1106 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1107 | {"radians", math_radians, METH_O, math_radians_doc}, |
| 1108 | {"sin", math_sin, METH_O, math_sin_doc}, |
| 1109 | {"sinh", math_sinh, METH_O, math_sinh_doc}, |
| 1110 | {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
| 1111 | {"tan", math_tan, METH_O, math_tan_doc}, |
| 1112 | {"tanh", math_tanh, METH_O, math_tanh_doc}, |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1113 | {"trunc", math_trunc, METH_O, math_trunc_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1114 | {NULL, NULL} /* sentinel */ |
| 1115 | }; |
| 1116 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1118 | PyDoc_STRVAR(module_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1119 | "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] | 1120 | "mathematical functions defined by the C standard."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1121 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1122 | |
| 1123 | static struct PyModuleDef mathmodule = { |
| 1124 | PyModuleDef_HEAD_INIT, |
| 1125 | "math", |
| 1126 | module_doc, |
| 1127 | -1, |
| 1128 | math_methods, |
| 1129 | NULL, |
| 1130 | NULL, |
| 1131 | NULL, |
| 1132 | NULL |
| 1133 | }; |
| 1134 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1135 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1136 | PyInit_math(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1137 | { |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1138 | PyObject *m; |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 1139 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1140 | m = PyModule_Create(&mathmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1141 | if (m == NULL) |
| 1142 | goto finally; |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1143 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1144 | PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
| 1145 | PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1146 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1147 | finally: |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1148 | return m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1149 | } |