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