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