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 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Michael W. Hudson | 9ef852c | 2005-04-06 13:05:18 +0000 | [diff] [blame] | 4 | #include "longintrepr.h" /* just for SHIFT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 7081cf5 | 1996-05-23 22:56:19 +0000 | [diff] [blame] | 6 | #ifndef _MSC_VER |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 7 | #ifndef __STDC__ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 8 | extern double fmod (double, double); |
| 9 | extern double frexp (double, int *); |
| 10 | extern double ldexp (double, int); |
| 11 | extern double modf (double, double *); |
Guido van Rossum | 7081cf5 | 1996-05-23 22:56:19 +0000 | [diff] [blame] | 12 | #endif /* __STDC__ */ |
| 13 | #endif /* _MSC_VER */ |
| 14 | |
Neal Norwitz | 5f95a79 | 2008-01-25 08:04:16 +0000 | [diff] [blame] | 15 | #ifdef _OSF_SOURCE |
| 16 | /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
| 17 | extern double copysign(double, double); |
| 18 | #endif |
| 19 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 20 | /* Call is_error when errno != 0, and where x is the result libm |
| 21 | * returned. is_error will usually set up an exception and return |
| 22 | * true (1), but may return false (0) without setting up an exception. |
| 23 | */ |
| 24 | static int |
| 25 | is_error(double x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 26 | { |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 27 | int result = 1; /* presumption of guilt */ |
Tim Peters | 2bf405a | 2000-10-12 19:42:00 +0000 | [diff] [blame] | 28 | assert(errno); /* non-zero errno is a precondition for calling */ |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 29 | if (errno == EDOM) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 30 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 31 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 32 | else if (errno == ERANGE) { |
| 33 | /* ANSI C generally requires libm functions to set ERANGE |
| 34 | * on overflow, but also generally *allows* them to set |
| 35 | * ERANGE on underflow too. There's no consistency about |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 36 | * the latter across platforms. |
| 37 | * Alas, C99 never requires that errno be set. |
| 38 | * Here we suppress the underflow errors (libm functions |
| 39 | * should return a zero on underflow, and +- HUGE_VAL on |
| 40 | * overflow, so testing the result for zero suffices to |
| 41 | * distinguish the cases). |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 42 | */ |
| 43 | if (x) |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 44 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 45 | "math range error"); |
| 46 | else |
| 47 | result = 0; |
| 48 | } |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 49 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 50 | /* Unexpected math error */ |
| 51 | PyErr_SetFromErrno(PyExc_ValueError); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 52 | return result; |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 55 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 56 | math_1(PyObject *arg, double (*func) (double)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 58 | double x = PyFloat_AsDouble(arg); |
| 59 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | return NULL; |
| 61 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 62 | PyFPE_START_PROTECT("in math_1", return 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | x = (*func)(x); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 64 | PyFPE_END_PROTECT(x) |
Hye-Shik Chang | 77d9a3e | 2004-03-22 08:43:55 +0000 | [diff] [blame] | 65 | Py_SET_ERRNO_ON_MATH_ERROR(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 66 | if (errno && is_error(x)) |
| 67 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 69 | return PyFloat_FromDouble(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 72 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 73 | math_2(PyObject *args, double (*func) (double, double), char *funcname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 75 | PyObject *ox, *oy; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | double x, y; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 77 | if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
| 78 | return NULL; |
| 79 | x = PyFloat_AsDouble(ox); |
| 80 | y = PyFloat_AsDouble(oy); |
| 81 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | return NULL; |
| 83 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 84 | PyFPE_START_PROTECT("in math_2", return 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | x = (*func)(x, y); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 86 | PyFPE_END_PROTECT(x) |
Hye-Shik Chang | 77d9a3e | 2004-03-22 08:43:55 +0000 | [diff] [blame] | 87 | Py_SET_ERRNO_ON_MATH_ERROR(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 88 | if (errno && is_error(x)) |
| 89 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 91 | return PyFloat_FromDouble(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 94 | #define FUNC1(funcname, func, docstring) \ |
| 95 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 96 | return math_1(args, func); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 97 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 98 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 100 | #define FUNC2(funcname, func, docstring) \ |
| 101 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 102 | return math_2(args, func, #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 103 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 104 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 106 | FUNC1(acos, acos, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 107 | "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 108 | FUNC1(asin, asin, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 109 | "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 110 | FUNC1(atan, atan, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 111 | "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] | 112 | FUNC2(atan2, atan2, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 113 | "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
| 114 | "Unlike atan(y/x), the signs of both x and y are considered.") |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 115 | FUNC1(ceil, ceil, |
| 116 | "ceil(x)\n\nReturn the ceiling of x as a float.\n" |
| 117 | "This is the smallest integral value >= x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 118 | FUNC1(cos, cos, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 119 | "cos(x)\n\nReturn the cosine of x (measured in radians).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 120 | FUNC1(cosh, cosh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 121 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
Christian Heimes | 66ecda4 | 2008-01-04 01:48:50 +0000 | [diff] [blame] | 122 | |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 123 | #ifdef MS_WINDOWS |
Christian Heimes | 66ecda4 | 2008-01-04 01:48:50 +0000 | [diff] [blame] | 124 | # define copysign _copysign |
| 125 | # define HAVE_COPYSIGN 1 |
| 126 | #endif |
| 127 | #ifdef HAVE_COPYSIGN |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 128 | FUNC2(copysign, copysign, |
Christian Heimes | 66ecda4 | 2008-01-04 01:48:50 +0000 | [diff] [blame] | 129 | "copysign(x,y)\n\nReturn x with the sign of y."); |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 130 | #endif |
Christian Heimes | 66ecda4 | 2008-01-04 01:48:50 +0000 | [diff] [blame] | 131 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 132 | FUNC1(exp, exp, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 133 | "exp(x)\n\nReturn e raised to the power of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 134 | FUNC1(fabs, fabs, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 135 | "fabs(x)\n\nReturn the absolute value of the float x.") |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 136 | FUNC1(floor, floor, |
| 137 | "floor(x)\n\nReturn the floor of x as a float.\n" |
| 138 | "This is the largest integral value <= x.") |
Peter Schneider-Kamp | 07c1922 | 2001-06-10 16:45:08 +0000 | [diff] [blame] | 139 | FUNC2(fmod, fmod, |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 140 | "fmod(x,y)\n\nReturn fmod(x, y), according to platform C." |
| 141 | " x % y may differ.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 142 | FUNC2(hypot, hypot, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 143 | "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 144 | FUNC2(pow, pow, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 145 | "pow(x,y)\n\nReturn x**y (x to the power of y).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 146 | FUNC1(sin, sin, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 147 | "sin(x)\n\nReturn the sine of x (measured in radians).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 148 | FUNC1(sinh, sinh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 149 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 150 | FUNC1(sqrt, sqrt, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 151 | "sqrt(x)\n\nReturn the square root of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 152 | FUNC1(tan, tan, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 153 | "tan(x)\n\nReturn the tangent of x (measured in radians).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 154 | FUNC1(tanh, tanh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 155 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 157 | static PyObject * |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 158 | math_trunc(PyObject *self, PyObject *number) |
| 159 | { |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 160 | return PyObject_CallMethod(number, "__trunc__", NULL); |
| 161 | } |
| 162 | |
| 163 | PyDoc_STRVAR(math_trunc_doc, |
| 164 | "trunc(x:Real) -> Integral\n" |
| 165 | "\n" |
Raymond Hettinger | fe424f7 | 2008-02-02 05:24:44 +0000 | [diff] [blame] | 166 | "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] | 167 | |
| 168 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 169 | math_frexp(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 170 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 171 | int i; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 172 | double x = PyFloat_AsDouble(arg); |
| 173 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | errno = 0; |
| 176 | x = frexp(x, &i); |
Hye-Shik Chang | 77d9a3e | 2004-03-22 08:43:55 +0000 | [diff] [blame] | 177 | Py_SET_ERRNO_ON_MATH_ERROR(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 178 | if (errno && is_error(x)) |
| 179 | return NULL; |
| 180 | else |
| 181 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 184 | PyDoc_STRVAR(math_frexp_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 185 | "frexp(x)\n" |
| 186 | "\n" |
| 187 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 188 | "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] | 189 | "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] | 190 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 191 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 192 | math_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 193 | { |
Guido van Rossum | c554505 | 2000-05-08 14:29:38 +0000 | [diff] [blame] | 194 | double x; |
| 195 | int exp; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 196 | if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 197 | return NULL; |
| 198 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 199 | PyFPE_START_PROTECT("ldexp", return 0) |
Guido van Rossum | c554505 | 2000-05-08 14:29:38 +0000 | [diff] [blame] | 200 | x = ldexp(x, exp); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 201 | PyFPE_END_PROTECT(x) |
Hye-Shik Chang | 77d9a3e | 2004-03-22 08:43:55 +0000 | [diff] [blame] | 202 | Py_SET_ERRNO_ON_MATH_ERROR(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 203 | if (errno && is_error(x)) |
| 204 | return NULL; |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 205 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 206 | return PyFloat_FromDouble(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 209 | PyDoc_STRVAR(math_ldexp_doc, |
| 210 | "ldexp(x, i) -> x * (2**i)"); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 211 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 212 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 213 | math_modf(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 214 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 215 | double y, x = PyFloat_AsDouble(arg); |
| 216 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 217 | return NULL; |
| 218 | errno = 0; |
| 219 | x = modf(x, &y); |
Hye-Shik Chang | 77d9a3e | 2004-03-22 08:43:55 +0000 | [diff] [blame] | 220 | Py_SET_ERRNO_ON_MATH_ERROR(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 221 | if (errno && is_error(x)) |
| 222 | return NULL; |
| 223 | else |
| 224 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 225 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 227 | PyDoc_STRVAR(math_modf_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 228 | "modf(x)\n" |
| 229 | "\n" |
| 230 | "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] | 231 | "of x. The integer part is returned as a real."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 232 | |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 233 | /* A decent logarithm is easy to compute even for huge longs, but libm can't |
| 234 | do that by itself -- loghelper can. func is log or log10, and name is |
| 235 | "log" or "log10". Note that overflow isn't possible: a long can contain |
| 236 | no more than INT_MAX * SHIFT bits, so has value certainly less than |
| 237 | 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
| 238 | small enough to fit in an IEEE single. log and log10 are even smaller. |
| 239 | */ |
| 240 | |
| 241 | static PyObject* |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 242 | loghelper(PyObject* arg, double (*func)(double), char *funcname) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 243 | { |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 244 | /* If it is long, do it ourselves. */ |
| 245 | if (PyLong_Check(arg)) { |
| 246 | double x; |
| 247 | int e; |
| 248 | x = _PyLong_AsScaledDouble(arg, &e); |
| 249 | if (x <= 0.0) { |
| 250 | PyErr_SetString(PyExc_ValueError, |
| 251 | "math domain error"); |
| 252 | return NULL; |
| 253 | } |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 254 | /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~= |
| 255 | log(x) + log(2) * e * PyLong_SHIFT. |
| 256 | CAUTION: e*PyLong_SHIFT may overflow using int arithmetic, |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 257 | so force use of double. */ |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 258 | x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 259 | return PyFloat_FromDouble(x); |
| 260 | } |
| 261 | |
| 262 | /* Else let libm handle it by itself. */ |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 263 | return math_1(arg, func); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static PyObject * |
| 267 | math_log(PyObject *self, PyObject *args) |
| 268 | { |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 269 | PyObject *arg; |
| 270 | PyObject *base = NULL; |
| 271 | PyObject *num, *den; |
| 272 | PyObject *ans; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 273 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 274 | if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 275 | return NULL; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 276 | |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 277 | num = loghelper(arg, log, "log"); |
| 278 | if (num == NULL || base == NULL) |
| 279 | return num; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 280 | |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 281 | den = loghelper(base, log, "log"); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 282 | if (den == NULL) { |
| 283 | Py_DECREF(num); |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | ans = PyNumber_Divide(num, den); |
| 288 | Py_DECREF(num); |
| 289 | Py_DECREF(den); |
| 290 | return ans; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 293 | PyDoc_STRVAR(math_log_doc, |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 294 | "log(x[, base]) -> the logarithm of x to the given base.\n\ |
| 295 | 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] | 296 | |
| 297 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 298 | math_log10(PyObject *self, PyObject *arg) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 299 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 300 | return loghelper(arg, log10, "log10"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 303 | PyDoc_STRVAR(math_log10_doc, |
| 304 | "log10(x) -> the base 10 logarithm of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 305 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 306 | static const double degToRad = Py_MATH_PI / 180.0; |
| 307 | static const double radToDeg = 180.0 / Py_MATH_PI; |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 308 | |
| 309 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 310 | math_degrees(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 311 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 312 | double x = PyFloat_AsDouble(arg); |
| 313 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 314 | return NULL; |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 315 | return PyFloat_FromDouble(x * radToDeg); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 318 | PyDoc_STRVAR(math_degrees_doc, |
| 319 | "degrees(x) -> converts angle x from radians to degrees"); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 320 | |
| 321 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 322 | math_radians(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 323 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 324 | double x = PyFloat_AsDouble(arg); |
| 325 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 326 | return NULL; |
| 327 | return PyFloat_FromDouble(x * degToRad); |
| 328 | } |
| 329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 330 | PyDoc_STRVAR(math_radians_doc, |
| 331 | "radians(x) -> converts angle x from degrees to radians"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 332 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 333 | static PyObject * |
| 334 | math_isnan(PyObject *self, PyObject *arg) |
| 335 | { |
| 336 | double x = PyFloat_AsDouble(arg); |
| 337 | if (x == -1.0 && PyErr_Occurred()) |
| 338 | return NULL; |
| 339 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 340 | } |
| 341 | |
| 342 | PyDoc_STRVAR(math_isnan_doc, |
| 343 | "isnan(x) -> bool\n\ |
| 344 | Checks if float x is not a number (NaN)"); |
| 345 | |
| 346 | static PyObject * |
| 347 | math_isinf(PyObject *self, PyObject *arg) |
| 348 | { |
| 349 | double x = PyFloat_AsDouble(arg); |
| 350 | if (x == -1.0 && PyErr_Occurred()) |
| 351 | return NULL; |
| 352 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 353 | } |
| 354 | |
| 355 | PyDoc_STRVAR(math_isinf_doc, |
| 356 | "isinf(x) -> bool\n\ |
| 357 | Checks if float x is infinite (positive or negative)"); |
| 358 | |
| 359 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 360 | static PyMethodDef math_methods[] = { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 361 | {"acos", math_acos, METH_O, math_acos_doc}, |
| 362 | {"asin", math_asin, METH_O, math_asin_doc}, |
| 363 | {"atan", math_atan, METH_O, math_atan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 364 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 365 | {"ceil", math_ceil, METH_O, math_ceil_doc}, |
Christian Heimes | 66ecda4 | 2008-01-04 01:48:50 +0000 | [diff] [blame] | 366 | #ifdef HAVE_COPYSIGN |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 367 | {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
| 368 | #endif |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 369 | {"cos", math_cos, METH_O, math_cos_doc}, |
| 370 | {"cosh", math_cosh, METH_O, math_cosh_doc}, |
| 371 | {"degrees", math_degrees, METH_O, math_degrees_doc}, |
| 372 | {"exp", math_exp, METH_O, math_exp_doc}, |
| 373 | {"fabs", math_fabs, METH_O, math_fabs_doc}, |
| 374 | {"floor", math_floor, METH_O, math_floor_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 375 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 376 | {"frexp", math_frexp, METH_O, math_frexp_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 377 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 378 | {"isinf", math_isinf, METH_O, math_isinf_doc}, |
| 379 | {"isnan", math_isnan, METH_O, math_isnan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 380 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
| 381 | {"log", math_log, METH_VARARGS, math_log_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 382 | {"log10", math_log10, METH_O, math_log10_doc}, |
| 383 | {"modf", math_modf, METH_O, math_modf_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 384 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 385 | {"radians", math_radians, METH_O, math_radians_doc}, |
| 386 | {"sin", math_sin, METH_O, math_sin_doc}, |
| 387 | {"sinh", math_sinh, METH_O, math_sinh_doc}, |
| 388 | {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
| 389 | {"tan", math_tan, METH_O, math_tan_doc}, |
| 390 | {"tanh", math_tanh, METH_O, math_tanh_doc}, |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 391 | {"trunc", math_trunc, METH_O, math_trunc_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | {NULL, NULL} /* sentinel */ |
| 393 | }; |
| 394 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 396 | PyDoc_STRVAR(module_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 397 | "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] | 398 | "mathematical functions defined by the C standard."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 399 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 400 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 401 | initmath(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 402 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 403 | PyObject *m, *d, *v; |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 404 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 405 | m = Py_InitModule3("math", math_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 406 | if (m == NULL) |
| 407 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 408 | d = PyModule_GetDict(m); |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 409 | if (d == NULL) |
| 410 | goto finally; |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 411 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 412 | if (!(v = PyFloat_FromDouble(Py_MATH_PI))) |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 413 | goto finally; |
| 414 | if (PyDict_SetItemString(d, "pi", v) < 0) |
| 415 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 416 | Py_DECREF(v); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 417 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 418 | if (!(v = PyFloat_FromDouble(Py_MATH_E))) |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 419 | goto finally; |
Guido van Rossum | 4c4cbf3 | 1996-12-18 14:12:22 +0000 | [diff] [blame] | 420 | if (PyDict_SetItemString(d, "e", v) < 0) |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 421 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 422 | Py_DECREF(v); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 423 | |
| 424 | finally: |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 425 | return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 426 | } |