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