Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Math module -- standard C math library functions, pi and e */ |
| 2 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 3 | /* Here are some comments from Tim Peters, extracted from the |
| 4 | discussion attached to http://bugs.python.org/issue1640. They |
| 5 | describe the general aims of the math module with respect to |
| 6 | special values, IEEE-754 floating-point exceptions, and Python |
| 7 | exceptions. |
| 8 | |
| 9 | These are the "spirit of 754" rules: |
| 10 | |
| 11 | 1. If the mathematical result is a real number, but of magnitude too |
| 12 | large to approximate by a machine float, overflow is signaled and the |
| 13 | result is an infinity (with the appropriate sign). |
| 14 | |
| 15 | 2. If the mathematical result is a real number, but of magnitude too |
| 16 | small to approximate by a machine float, underflow is signaled and the |
| 17 | result is a zero (with the appropriate sign). |
| 18 | |
| 19 | 3. At a singularity (a value x such that the limit of f(y) as y |
| 20 | approaches x exists and is an infinity), "divide by zero" is signaled |
| 21 | and the result is an infinity (with the appropriate sign). This is |
| 22 | complicated a little by that the left-side and right-side limits may |
| 23 | not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0 |
| 24 | from the positive or negative directions. In that specific case, the |
| 25 | sign of the zero determines the result of 1/0. |
| 26 | |
| 27 | 4. At a point where a function has no defined result in the extended |
| 28 | reals (i.e., the reals plus an infinity or two), invalid operation is |
| 29 | signaled and a NaN is returned. |
| 30 | |
| 31 | And these are what Python has historically /tried/ to do (but not |
| 32 | always successfully, as platform libm behavior varies a lot): |
| 33 | |
| 34 | For #1, raise OverflowError. |
| 35 | |
| 36 | For #2, return a zero (with the appropriate sign if that happens by |
| 37 | accident ;-)). |
| 38 | |
| 39 | For #3 and #4, raise ValueError. It may have made sense to raise |
| 40 | Python's ZeroDivisionError in #3, but historically that's only been |
| 41 | raised for division by zero and mod by zero. |
| 42 | |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | In general, on an IEEE-754 platform the aim is to follow the C99 |
| 47 | standard, including Annex 'F', whenever possible. Where the |
| 48 | standard recommends raising the 'divide-by-zero' or 'invalid' |
| 49 | floating-point exceptions, Python should raise a ValueError. Where |
| 50 | the standard recommends raising 'overflow', Python should raise an |
| 51 | OverflowError. In all other circumstances a value should be |
| 52 | returned. |
| 53 | */ |
| 54 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 55 | #include "Python.h" |
Michael W. Hudson | 9ef852c | 2005-04-06 13:05:18 +0000 | [diff] [blame] | 56 | #include "longintrepr.h" /* just for SHIFT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | |
Neal Norwitz | 5f95a79 | 2008-01-25 08:04:16 +0000 | [diff] [blame] | 58 | #ifdef _OSF_SOURCE |
| 59 | /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
| 60 | extern double copysign(double, double); |
| 61 | #endif |
| 62 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 63 | /* Call is_error when errno != 0, and where x is the result libm |
| 64 | * returned. is_error will usually set up an exception and return |
| 65 | * true (1), but may return false (0) without setting up an exception. |
| 66 | */ |
| 67 | static int |
| 68 | is_error(double x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 69 | { |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 70 | int result = 1; /* presumption of guilt */ |
Tim Peters | 2bf405a | 2000-10-12 19:42:00 +0000 | [diff] [blame] | 71 | assert(errno); /* non-zero errno is a precondition for calling */ |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 72 | if (errno == EDOM) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 73 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 74 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 75 | else if (errno == ERANGE) { |
| 76 | /* ANSI C generally requires libm functions to set ERANGE |
| 77 | * on overflow, but also generally *allows* them to set |
| 78 | * ERANGE on underflow too. There's no consistency about |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 79 | * the latter across platforms. |
| 80 | * Alas, C99 never requires that errno be set. |
| 81 | * Here we suppress the underflow errors (libm functions |
| 82 | * should return a zero on underflow, and +- HUGE_VAL on |
| 83 | * overflow, so testing the result for zero suffices to |
| 84 | * distinguish the cases). |
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 | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 98 | /* |
| 99 | math_1 is used to wrap a libm function f that takes a double |
| 100 | arguments and returns a double. |
| 101 | |
| 102 | The error reporting follows these rules, which are designed to do |
| 103 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 104 | platforms. |
| 105 | |
| 106 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 107 | - an infinite result from finite inputs causes OverflowError to be |
| 108 | raised if can_overflow is 1, or raises ValueError if can_overflow |
| 109 | is 0. |
| 110 | - if the result is finite and errno == EDOM then ValueError is |
| 111 | raised |
| 112 | - if the result is finite and nonzero and errno == ERANGE then |
| 113 | OverflowError is raised |
| 114 | |
| 115 | The last rule is used to catch overflow on platforms which follow |
| 116 | C89 but for which HUGE_VAL is not an infinity. |
| 117 | |
| 118 | For the majority of one-argument functions these rules are enough |
| 119 | to ensure that Python's functions behave as specified in 'Annex F' |
| 120 | of the C99 standard, with the 'invalid' and 'divide-by-zero' |
| 121 | floating-point exceptions mapping to Python's ValueError and the |
| 122 | 'overflow' floating-point exception mapping to OverflowError. |
| 123 | math_1 only works for functions that don't have singularities *and* |
| 124 | the possibility of overflow; fortunately, that covers everything we |
| 125 | care about right now. |
| 126 | */ |
| 127 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 128 | static PyObject * |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 129 | math_1(PyObject *arg, double (*func) (double), int can_overflow) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 131 | double x, r; |
| 132 | x = PyFloat_AsDouble(arg); |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 133 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 136 | PyFPE_START_PROTECT("in math_1", return 0); |
| 137 | r = (*func)(x); |
| 138 | PyFPE_END_PROTECT(r); |
| 139 | if (Py_IS_NAN(r)) { |
| 140 | if (!Py_IS_NAN(x)) |
| 141 | errno = EDOM; |
| 142 | else |
| 143 | errno = 0; |
| 144 | } |
| 145 | else if (Py_IS_INFINITY(r)) { |
| 146 | if (Py_IS_FINITE(x)) |
| 147 | errno = can_overflow ? ERANGE : EDOM; |
| 148 | else |
| 149 | errno = 0; |
| 150 | } |
| 151 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 152 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | else |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 154 | return PyFloat_FromDouble(r); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 157 | /* |
| 158 | math_2 is used to wrap a libm function f that takes two double |
| 159 | arguments and returns a double. |
| 160 | |
| 161 | The error reporting follows these rules, which are designed to do |
| 162 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 163 | platforms. |
| 164 | |
| 165 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 166 | - an infinite result from finite inputs causes OverflowError to be |
| 167 | raised. |
| 168 | - if the result is finite and errno == EDOM then ValueError is |
| 169 | raised |
| 170 | - if the result is finite and nonzero and errno == ERANGE then |
| 171 | OverflowError is raised |
| 172 | |
| 173 | The last rule is used to catch overflow on platforms which follow |
| 174 | C89 but for which HUGE_VAL is not an infinity. |
| 175 | |
| 176 | For most two-argument functions (copysign, fmod, hypot, atan2) |
| 177 | these rules are enough to ensure that Python's functions behave as |
| 178 | specified in 'Annex F' of the C99 standard, with the 'invalid' and |
| 179 | 'divide-by-zero' floating-point exceptions mapping to Python's |
| 180 | ValueError and the 'overflow' floating-point exception mapping to |
| 181 | OverflowError. |
| 182 | */ |
| 183 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 184 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 185 | math_2(PyObject *args, double (*func) (double, double), char *funcname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 187 | PyObject *ox, *oy; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 188 | double x, y, r; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 189 | if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
| 190 | return NULL; |
| 191 | x = PyFloat_AsDouble(ox); |
| 192 | y = PyFloat_AsDouble(oy); |
| 193 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | return NULL; |
| 195 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 196 | PyFPE_START_PROTECT("in math_2", return 0); |
| 197 | r = (*func)(x, y); |
| 198 | PyFPE_END_PROTECT(r); |
| 199 | if (Py_IS_NAN(r)) { |
| 200 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 201 | errno = EDOM; |
| 202 | else |
| 203 | errno = 0; |
| 204 | } |
| 205 | else if (Py_IS_INFINITY(r)) { |
| 206 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 207 | errno = ERANGE; |
| 208 | else |
| 209 | errno = 0; |
| 210 | } |
| 211 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 212 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | else |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 214 | return PyFloat_FromDouble(r); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 217 | #define FUNC1(funcname, func, can_overflow, docstring) \ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 218 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 219 | return math_1(args, func, can_overflow); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 220 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 221 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 223 | #define FUNC2(funcname, func, docstring) \ |
| 224 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 225 | return math_2(args, func, #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 226 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 227 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 229 | FUNC1(acos, acos, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 230 | "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 231 | FUNC1(acosh, acosh, 0, |
| 232 | "acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.") |
| 233 | FUNC1(asin, asin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 234 | "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 235 | FUNC1(asinh, asinh, 0, |
| 236 | "asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.") |
| 237 | FUNC1(atan, atan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 238 | "atan(x)\n\nReturn the arc tangent (measured in radians) of x.") |
Martin v. Löwis | 387c547 | 2001-09-06 08:16:17 +0000 | [diff] [blame] | 239 | FUNC2(atan2, atan2, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 240 | "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
| 241 | "Unlike atan(y/x), the signs of both x and y are considered.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 242 | FUNC1(atanh, atanh, 0, |
| 243 | "atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.") |
| 244 | FUNC1(ceil, ceil, 0, |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 245 | "ceil(x)\n\nReturn the ceiling of x as a float.\n" |
| 246 | "This is the smallest integral value >= x.") |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 247 | FUNC2(copysign, copysign, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 248 | "copysign(x,y)\n\nReturn x with the sign of y.") |
| 249 | FUNC1(cos, cos, 0, |
| 250 | "cos(x)\n\nReturn the cosine of x (measured in radians).") |
| 251 | FUNC1(cosh, cosh, 1, |
| 252 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
| 253 | FUNC1(exp, exp, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 254 | "exp(x)\n\nReturn e raised to the power of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 255 | FUNC1(fabs, fabs, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 256 | "fabs(x)\n\nReturn the absolute value of the float x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 257 | FUNC1(floor, floor, 0, |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 258 | "floor(x)\n\nReturn the floor of x as a float.\n" |
| 259 | "This is the largest integral value <= x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 260 | FUNC1(log1p, log1p, 1, |
| 261 | "log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n\ |
| 262 | The result is computed in a way which is accurate for x near zero.") |
| 263 | FUNC1(sin, sin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 264 | "sin(x)\n\nReturn the sine of x (measured in radians).") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 265 | FUNC1(sinh, sinh, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 266 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 267 | FUNC1(sqrt, sqrt, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 268 | "sqrt(x)\n\nReturn the square root of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 269 | FUNC1(tan, tan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 270 | "tan(x)\n\nReturn the tangent of x (measured in radians).") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 271 | FUNC1(tanh, tanh, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 272 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 273 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 274 | static PyObject * |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 275 | math_trunc(PyObject *self, PyObject *number) |
| 276 | { |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 277 | return PyObject_CallMethod(number, "__trunc__", NULL); |
| 278 | } |
| 279 | |
| 280 | PyDoc_STRVAR(math_trunc_doc, |
| 281 | "trunc(x:Real) -> Integral\n" |
| 282 | "\n" |
Raymond Hettinger | fe424f7 | 2008-02-02 05:24:44 +0000 | [diff] [blame] | 283 | "Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method."); |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 284 | |
| 285 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 286 | math_frexp(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 287 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 288 | int i; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 289 | double x = PyFloat_AsDouble(arg); |
| 290 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 291 | return NULL; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 292 | /* deal with special cases directly, to sidestep platform |
| 293 | differences */ |
| 294 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
| 295 | i = 0; |
| 296 | } |
| 297 | else { |
| 298 | PyFPE_START_PROTECT("in math_frexp", return 0); |
| 299 | x = frexp(x, &i); |
| 300 | PyFPE_END_PROTECT(x); |
| 301 | } |
| 302 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 305 | PyDoc_STRVAR(math_frexp_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 306 | "frexp(x)\n" |
| 307 | "\n" |
| 308 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 309 | "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] | 310 | "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] | 311 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 312 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 313 | math_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 314 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 315 | double x, r; |
Guido van Rossum | c554505 | 2000-05-08 14:29:38 +0000 | [diff] [blame] | 316 | int exp; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 317 | if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 318 | return NULL; |
| 319 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 320 | PyFPE_START_PROTECT("in math_ldexp", return 0) |
| 321 | r = ldexp(x, exp); |
| 322 | PyFPE_END_PROTECT(r) |
| 323 | if (Py_IS_FINITE(x) && Py_IS_INFINITY(r)) |
| 324 | errno = ERANGE; |
| 325 | /* Windows MSVC8 sets errno = EDOM on ldexp(NaN, i); |
| 326 | we unset it to avoid raising a ValueError here. */ |
| 327 | if (errno == EDOM) |
| 328 | errno = 0; |
| 329 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 330 | return NULL; |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 331 | else |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 332 | return PyFloat_FromDouble(r); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 335 | PyDoc_STRVAR(math_ldexp_doc, |
| 336 | "ldexp(x, i) -> x * (2**i)"); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 337 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 338 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 339 | math_modf(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 340 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 341 | double y, x = PyFloat_AsDouble(arg); |
| 342 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 345 | PyFPE_START_PROTECT("in math_modf", return 0); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 346 | x = modf(x, &y); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 347 | PyFPE_END_PROTECT(x); |
| 348 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 349 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 350 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 351 | PyDoc_STRVAR(math_modf_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 352 | "modf(x)\n" |
| 353 | "\n" |
| 354 | "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] | 355 | "of x. The integer part is returned as a real."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 356 | |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 357 | /* A decent logarithm is easy to compute even for huge longs, but libm can't |
| 358 | do that by itself -- loghelper can. func is log or log10, and name is |
| 359 | "log" or "log10". Note that overflow isn't possible: a long can contain |
| 360 | no more than INT_MAX * SHIFT bits, so has value certainly less than |
| 361 | 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
| 362 | small enough to fit in an IEEE single. log and log10 are even smaller. |
| 363 | */ |
| 364 | |
| 365 | static PyObject* |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 366 | loghelper(PyObject* arg, double (*func)(double), char *funcname) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 367 | { |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 368 | /* If it is long, do it ourselves. */ |
| 369 | if (PyLong_Check(arg)) { |
| 370 | double x; |
| 371 | int e; |
| 372 | x = _PyLong_AsScaledDouble(arg, &e); |
| 373 | if (x <= 0.0) { |
| 374 | PyErr_SetString(PyExc_ValueError, |
| 375 | "math domain error"); |
| 376 | return NULL; |
| 377 | } |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 378 | /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~= |
| 379 | log(x) + log(2) * e * PyLong_SHIFT. |
| 380 | CAUTION: e*PyLong_SHIFT may overflow using int arithmetic, |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 381 | so force use of double. */ |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 382 | x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 383 | return PyFloat_FromDouble(x); |
| 384 | } |
| 385 | |
| 386 | /* Else let libm handle it by itself. */ |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 387 | return math_1(arg, func, 0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static PyObject * |
| 391 | math_log(PyObject *self, PyObject *args) |
| 392 | { |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 393 | PyObject *arg; |
| 394 | PyObject *base = NULL; |
| 395 | PyObject *num, *den; |
| 396 | PyObject *ans; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 397 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 398 | if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 399 | return NULL; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 400 | |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 401 | num = loghelper(arg, log, "log"); |
| 402 | if (num == NULL || base == NULL) |
| 403 | return num; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 404 | |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 405 | den = loghelper(base, log, "log"); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 406 | if (den == NULL) { |
| 407 | Py_DECREF(num); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | ans = PyNumber_Divide(num, den); |
| 412 | Py_DECREF(num); |
| 413 | Py_DECREF(den); |
| 414 | return ans; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 417 | PyDoc_STRVAR(math_log_doc, |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 418 | "log(x[, base]) -> the logarithm of x to the given base.\n\ |
| 419 | 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] | 420 | |
| 421 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 422 | math_log10(PyObject *self, PyObject *arg) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 423 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 424 | return loghelper(arg, log10, "log10"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 427 | PyDoc_STRVAR(math_log10_doc, |
| 428 | "log10(x) -> the base 10 logarithm of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 429 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 430 | static PyObject * |
| 431 | math_fmod(PyObject *self, PyObject *args) |
| 432 | { |
| 433 | PyObject *ox, *oy; |
| 434 | double r, x, y; |
| 435 | if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
| 436 | return NULL; |
| 437 | x = PyFloat_AsDouble(ox); |
| 438 | y = PyFloat_AsDouble(oy); |
| 439 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 440 | return NULL; |
| 441 | /* fmod(x, +/-Inf) returns x for finite x. */ |
| 442 | if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
| 443 | return PyFloat_FromDouble(x); |
| 444 | errno = 0; |
| 445 | PyFPE_START_PROTECT("in math_fmod", return 0); |
| 446 | r = fmod(x, y); |
| 447 | PyFPE_END_PROTECT(r); |
| 448 | if (Py_IS_NAN(r)) { |
| 449 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 450 | errno = EDOM; |
| 451 | else |
| 452 | errno = 0; |
| 453 | } |
| 454 | if (errno && is_error(r)) |
| 455 | return NULL; |
| 456 | else |
| 457 | return PyFloat_FromDouble(r); |
| 458 | } |
| 459 | |
| 460 | PyDoc_STRVAR(math_fmod_doc, |
| 461 | "fmod(x,y)\n\nReturn fmod(x, y), according to platform C." |
| 462 | " x % y may differ."); |
| 463 | |
| 464 | static PyObject * |
| 465 | math_hypot(PyObject *self, PyObject *args) |
| 466 | { |
| 467 | PyObject *ox, *oy; |
| 468 | double r, x, y; |
| 469 | if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
| 470 | return NULL; |
| 471 | x = PyFloat_AsDouble(ox); |
| 472 | y = PyFloat_AsDouble(oy); |
| 473 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 474 | return NULL; |
| 475 | /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */ |
| 476 | if (Py_IS_INFINITY(x)) |
| 477 | return PyFloat_FromDouble(fabs(x)); |
| 478 | if (Py_IS_INFINITY(y)) |
| 479 | return PyFloat_FromDouble(fabs(y)); |
| 480 | errno = 0; |
| 481 | PyFPE_START_PROTECT("in math_hypot", return 0); |
| 482 | r = hypot(x, y); |
| 483 | PyFPE_END_PROTECT(r); |
| 484 | if (Py_IS_NAN(r)) { |
| 485 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 486 | errno = EDOM; |
| 487 | else |
| 488 | errno = 0; |
| 489 | } |
| 490 | else if (Py_IS_INFINITY(r)) { |
| 491 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 492 | errno = ERANGE; |
| 493 | else |
| 494 | errno = 0; |
| 495 | } |
| 496 | if (errno && is_error(r)) |
| 497 | return NULL; |
| 498 | else |
| 499 | return PyFloat_FromDouble(r); |
| 500 | } |
| 501 | |
| 502 | PyDoc_STRVAR(math_hypot_doc, |
| 503 | "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y)."); |
| 504 | |
| 505 | /* pow can't use math_2, but needs its own wrapper: the problem is |
| 506 | that an infinite result can arise either as a result of overflow |
| 507 | (in which case OverflowError should be raised) or as a result of |
| 508 | e.g. 0.**-5. (for which ValueError needs to be raised.) |
| 509 | */ |
| 510 | |
| 511 | static PyObject * |
| 512 | math_pow(PyObject *self, PyObject *args) |
| 513 | { |
| 514 | PyObject *ox, *oy; |
| 515 | double r, x, y; |
Mark Dickinson | e941d97 | 2008-04-19 18:51:48 +0000 | [diff] [blame] | 516 | int y_is_odd; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 517 | |
| 518 | if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
| 519 | return NULL; |
| 520 | x = PyFloat_AsDouble(ox); |
| 521 | y = PyFloat_AsDouble(oy); |
| 522 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 523 | return NULL; |
Mark Dickinson | a1293eb | 2008-04-19 19:41:52 +0000 | [diff] [blame^] | 524 | |
| 525 | /* deal directly with various special cases, to cope with problems on |
| 526 | various platforms whose semantics don't exactly match C99 */ |
| 527 | |
| 528 | /* 1**x, x**0, and (-1)**(+-infinity) return 1., even if x is NaN or |
| 529 | an infinity. */ |
| 530 | if (x == 1. || y == 0. || (x == -1. && Py_IS_INFINITY(y))) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 531 | return PyFloat_FromDouble(1.); |
Mark Dickinson | a1293eb | 2008-04-19 19:41:52 +0000 | [diff] [blame^] | 532 | /* otherwise, return a NaN if either input was a NaN */ |
| 533 | if (Py_IS_NAN(x)) |
| 534 | return PyFloat_FromDouble(x); |
| 535 | if (Py_IS_NAN(y)) |
| 536 | return PyFloat_FromDouble(y); |
Mark Dickinson | e941d97 | 2008-04-19 18:51:48 +0000 | [diff] [blame] | 537 | /* inf ** (nonzero, non-NaN) is one of +-0, +-infinity */ |
| 538 | if (Py_IS_INFINITY(x) && !Py_IS_NAN(y)) { |
| 539 | y_is_odd = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
| 540 | if (y > 0.) |
| 541 | r = y_is_odd ? x : fabs(x); |
| 542 | else |
| 543 | r = y_is_odd ? copysign(0., x) : 0.; |
| 544 | return PyFloat_FromDouble(r); |
| 545 | } |
| 546 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 547 | errno = 0; |
| 548 | PyFPE_START_PROTECT("in math_pow", return 0); |
| 549 | r = pow(x, y); |
| 550 | PyFPE_END_PROTECT(r); |
| 551 | if (Py_IS_NAN(r)) { |
Mark Dickinson | a1293eb | 2008-04-19 19:41:52 +0000 | [diff] [blame^] | 552 | errno = EDOM; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 553 | } |
| 554 | /* an infinite result arises either from: |
| 555 | |
| 556 | (A) (+/-0.)**negative, |
| 557 | (B) overflow of x**y with both x and y finite (and x nonzero) |
| 558 | (C) (+/-inf)**positive, or |
| 559 | (D) x**inf with |x| > 1, or x**-inf with |x| < 1. |
| 560 | |
| 561 | In case (A) we want ValueError to be raised. In case (B) |
| 562 | OverflowError should be raised. In cases (C) and (D) the infinite |
| 563 | result should be returned. |
| 564 | */ |
| 565 | else if (Py_IS_INFINITY(r)) { |
| 566 | if (x == 0.) |
| 567 | errno = EDOM; |
| 568 | else if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 569 | errno = ERANGE; |
| 570 | else |
| 571 | errno = 0; |
| 572 | } |
| 573 | |
| 574 | if (errno && is_error(r)) |
| 575 | return NULL; |
| 576 | else |
| 577 | return PyFloat_FromDouble(r); |
| 578 | } |
| 579 | |
| 580 | PyDoc_STRVAR(math_pow_doc, |
| 581 | "pow(x,y)\n\nReturn x**y (x to the power of y)."); |
| 582 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 583 | static const double degToRad = Py_MATH_PI / 180.0; |
| 584 | static const double radToDeg = 180.0 / Py_MATH_PI; |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 585 | |
| 586 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 587 | math_degrees(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 588 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 589 | double x = PyFloat_AsDouble(arg); |
| 590 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 591 | return NULL; |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 592 | return PyFloat_FromDouble(x * radToDeg); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 595 | PyDoc_STRVAR(math_degrees_doc, |
| 596 | "degrees(x) -> converts angle x from radians to degrees"); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 597 | |
| 598 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 599 | math_radians(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 600 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 601 | double x = PyFloat_AsDouble(arg); |
| 602 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 603 | return NULL; |
| 604 | return PyFloat_FromDouble(x * degToRad); |
| 605 | } |
| 606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 607 | PyDoc_STRVAR(math_radians_doc, |
| 608 | "radians(x) -> converts angle x from degrees to radians"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 609 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 610 | static PyObject * |
| 611 | math_isnan(PyObject *self, PyObject *arg) |
| 612 | { |
| 613 | double x = PyFloat_AsDouble(arg); |
| 614 | if (x == -1.0 && PyErr_Occurred()) |
| 615 | return NULL; |
| 616 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 617 | } |
| 618 | |
| 619 | PyDoc_STRVAR(math_isnan_doc, |
| 620 | "isnan(x) -> bool\n\ |
| 621 | Checks if float x is not a number (NaN)"); |
| 622 | |
| 623 | static PyObject * |
| 624 | math_isinf(PyObject *self, PyObject *arg) |
| 625 | { |
| 626 | double x = PyFloat_AsDouble(arg); |
| 627 | if (x == -1.0 && PyErr_Occurred()) |
| 628 | return NULL; |
| 629 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 630 | } |
| 631 | |
| 632 | PyDoc_STRVAR(math_isinf_doc, |
| 633 | "isinf(x) -> bool\n\ |
| 634 | Checks if float x is infinite (positive or negative)"); |
| 635 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 636 | static PyMethodDef math_methods[] = { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 637 | {"acos", math_acos, METH_O, math_acos_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 638 | {"acosh", math_acosh, METH_O, math_acosh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 639 | {"asin", math_asin, METH_O, math_asin_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 640 | {"asinh", math_asinh, METH_O, math_asinh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 641 | {"atan", math_atan, METH_O, math_atan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 642 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 643 | {"atanh", math_atanh, METH_O, math_atanh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 644 | {"ceil", math_ceil, METH_O, math_ceil_doc}, |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 645 | {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 646 | {"cos", math_cos, METH_O, math_cos_doc}, |
| 647 | {"cosh", math_cosh, METH_O, math_cosh_doc}, |
| 648 | {"degrees", math_degrees, METH_O, math_degrees_doc}, |
| 649 | {"exp", math_exp, METH_O, math_exp_doc}, |
| 650 | {"fabs", math_fabs, METH_O, math_fabs_doc}, |
| 651 | {"floor", math_floor, METH_O, math_floor_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 652 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 653 | {"frexp", math_frexp, METH_O, math_frexp_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 654 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 655 | {"isinf", math_isinf, METH_O, math_isinf_doc}, |
| 656 | {"isnan", math_isnan, METH_O, math_isnan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 657 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
| 658 | {"log", math_log, METH_VARARGS, math_log_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 659 | {"log1p", math_log1p, METH_O, math_log1p_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 660 | {"log10", math_log10, METH_O, math_log10_doc}, |
| 661 | {"modf", math_modf, METH_O, math_modf_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 662 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 663 | {"radians", math_radians, METH_O, math_radians_doc}, |
| 664 | {"sin", math_sin, METH_O, math_sin_doc}, |
| 665 | {"sinh", math_sinh, METH_O, math_sinh_doc}, |
| 666 | {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
| 667 | {"tan", math_tan, METH_O, math_tan_doc}, |
| 668 | {"tanh", math_tanh, METH_O, math_tanh_doc}, |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 669 | {"trunc", math_trunc, METH_O, math_trunc_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 670 | {NULL, NULL} /* sentinel */ |
| 671 | }; |
| 672 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 674 | PyDoc_STRVAR(module_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 675 | "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] | 676 | "mathematical functions defined by the C standard."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 677 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 678 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 679 | initmath(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 680 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 681 | PyObject *m; |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 682 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 683 | m = Py_InitModule3("math", math_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 684 | if (m == NULL) |
| 685 | goto finally; |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 686 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 687 | PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
| 688 | PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 689 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 690 | finally: |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 691 | return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 692 | } |