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" |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame^] | 4 | #include "longintrepr.h" |
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 | |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 16 | #ifdef i860 |
| 17 | /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
| 18 | #undef HUGE_VAL |
| 19 | #endif |
| 20 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 21 | /* RED_FLAG 12-Oct-2000 Tim |
Tim Peters | 2bf405a | 2000-10-12 19:42:00 +0000 | [diff] [blame] | 22 | * What CHECK does if errno == 0 and x is a NaN is a platform-dependent crap |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 23 | * shoot. Most (but not all!) platforms will end up setting errno to ERANGE |
| 24 | * then, but EDOM is probably better. |
| 25 | */ |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 26 | #ifdef HUGE_VAL |
| 27 | #define CHECK(x) if (errno != 0) ; \ |
| 28 | else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
| 29 | else errno = ERANGE |
| 30 | #else |
| 31 | #define CHECK(x) /* Don't know how to check */ |
| 32 | #endif |
| 33 | |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 34 | /* Call is_error when errno != 0, and where x is the result libm |
| 35 | * returned. is_error will usually set up an exception and return |
| 36 | * true (1), but may return false (0) without setting up an exception. |
| 37 | */ |
| 38 | static int |
| 39 | is_error(double x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 40 | { |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 41 | int result = 1; /* presumption of guilt */ |
Tim Peters | 2bf405a | 2000-10-12 19:42:00 +0000 | [diff] [blame] | 42 | assert(errno); /* non-zero errno is a precondition for calling */ |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 43 | if (errno == EDOM) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 44 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 45 | else if (errno == ERANGE) { |
| 46 | /* ANSI C generally requires libm functions to set ERANGE |
| 47 | * on overflow, but also generally *allows* them to set |
| 48 | * ERANGE on underflow too. There's no consistency about |
| 49 | * the latter across platforms. Here we suppress the |
| 50 | * underflow errors (libm functions should return a zero |
| 51 | * on underflow, and +- HUGE_VAL on overflow, so testing |
| 52 | * the result for zero suffices to distinguish the cases). |
| 53 | */ |
| 54 | if (x) |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 55 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 56 | "math range error"); |
| 57 | else |
| 58 | result = 0; |
| 59 | } |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 60 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 61 | /* Unexpected math error */ |
| 62 | PyErr_SetFromErrno(PyExc_ValueError); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 63 | return result; |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 66 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 67 | math_1(PyObject *args, double (*func) (double), char *argsfmt) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | { |
| 69 | double x; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 70 | if (! PyArg_ParseTuple(args, argsfmt, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | return NULL; |
| 72 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 73 | PyFPE_START_PROTECT("in math_1", return 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | x = (*func)(x); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 75 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 76 | CHECK(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 77 | if (errno && is_error(x)) |
| 78 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 80 | return PyFloat_FromDouble(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 83 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 84 | math_2(PyObject *args, double (*func) (double, double), char *argsfmt) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | { |
| 86 | double x, y; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 87 | if (! PyArg_ParseTuple(args, argsfmt, &x, &y)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 90 | PyFPE_START_PROTECT("in math_2", return 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | x = (*func)(x, y); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 92 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 93 | CHECK(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 94 | if (errno && is_error(x)) |
| 95 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 97 | return PyFloat_FromDouble(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 100 | #define FUNC1(funcname, func, docstring) \ |
| 101 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 102 | return math_1(args, func, "d:" #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 103 | }\ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 104 | static char 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 | #define FUNC2(funcname, func, docstring) \ |
| 107 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 108 | return math_2(args, func, "dd:" #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 109 | }\ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 110 | static char math_##funcname##_doc [] = docstring; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 112 | FUNC1(acos, acos, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 113 | "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 114 | FUNC1(asin, asin, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 115 | "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 116 | FUNC1(atan, atan, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 117 | "atan(x)\n\nReturn the arc tangent (measured in radians) of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 118 | FUNC2(atan2, atan2, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 119 | "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
| 120 | "Unlike atan(y/x), the signs of both x and y are considered.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 121 | FUNC1(ceil, ceil, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 122 | "ceil(x)\n\nReturn the ceiling of x as a float.\n" |
| 123 | "This is the smallest integral value >= x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 124 | FUNC1(cos, cos, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 125 | "cos(x)\n\nReturn the cosine of x (measured in radians).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 126 | FUNC1(cosh, cosh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 127 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 128 | FUNC1(exp, exp, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 129 | "exp(x)\n\nReturn e raised to the power of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 130 | FUNC1(fabs, fabs, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 131 | "fabs(x)\n\nReturn the absolute value of the float x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 132 | FUNC1(floor, floor, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 133 | "floor(x)\n\nReturn the floor of x as a float.\n" |
| 134 | "This is the largest integral value <= x.") |
Peter Schneider-Kamp | 07c1922 | 2001-06-10 16:45:08 +0000 | [diff] [blame] | 135 | FUNC2(fmod, fmod, |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 136 | "fmod(x,y)\n\nReturn fmod(x, y), according to platform C." |
| 137 | " x % y may differ.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 138 | FUNC2(hypot, hypot, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 139 | "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).") |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 140 | #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 141 | FUNC2(pow, power, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 142 | "pow(x,y)\n\nReturn x**y (x to the power of y).") |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 143 | #else |
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).") |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 146 | #endif |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 147 | FUNC1(sin, sin, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 148 | "sin(x)\n\nReturn the sine of x (measured in radians).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 149 | FUNC1(sinh, sinh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 150 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 151 | FUNC1(sqrt, sqrt, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 152 | "sqrt(x)\n\nReturn the square root of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 153 | FUNC1(tan, tan, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 154 | "tan(x)\n\nReturn the tangent of x (measured in radians).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 155 | FUNC1(tanh, tanh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 156 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 158 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 159 | math_frexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 160 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 161 | double x; |
| 162 | int i; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 163 | if (! PyArg_ParseTuple(args, "d:frexp", &x)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 164 | return NULL; |
| 165 | errno = 0; |
| 166 | x = frexp(x, &i); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 167 | CHECK(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 168 | if (errno && is_error(x)) |
| 169 | return NULL; |
| 170 | else |
| 171 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 174 | static char math_frexp_doc [] = |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 175 | "frexp(x)\n" |
| 176 | "\n" |
| 177 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 178 | "m is a float and e is an int, such that x = m * 2.**e.\n" |
| 179 | "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] | 180 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 181 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 182 | math_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 183 | { |
Guido van Rossum | c554505 | 2000-05-08 14:29:38 +0000 | [diff] [blame] | 184 | double x; |
| 185 | int exp; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 186 | if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 189 | PyFPE_START_PROTECT("ldexp", return 0) |
Guido van Rossum | c554505 | 2000-05-08 14:29:38 +0000 | [diff] [blame] | 190 | x = ldexp(x, exp); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 191 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 192 | CHECK(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 193 | if (errno && is_error(x)) |
| 194 | return NULL; |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 195 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 196 | return PyFloat_FromDouble(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 199 | static char math_ldexp_doc [] = |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 200 | "ldexp(x, i) -> x * (2**i)"; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 201 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 202 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 203 | math_modf(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 204 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 205 | double x, y; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 206 | if (! PyArg_ParseTuple(args, "d:modf", &x)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 207 | return NULL; |
| 208 | errno = 0; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 210 | { |
| 211 | extended e; |
| 212 | x = modf(x, &e); |
| 213 | y = e; |
| 214 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #else |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 216 | x = modf(x, &y); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | #endif |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 218 | CHECK(x); |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 219 | if (errno && is_error(x)) |
| 220 | return NULL; |
| 221 | else |
| 222 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 223 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 225 | static char math_modf_doc [] = |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 226 | "modf(x)\n" |
| 227 | "\n" |
| 228 | "Return the fractional and integer parts of x. Both results carry the sign\n" |
| 229 | "of x. The integer part is returned as a real."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 230 | |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame^] | 231 | /* A decent logarithm is easy to compute even for huge longs, but libm can't |
| 232 | do that by itself -- loghelper can. func is log or log10, and name is |
| 233 | "log" or "log10". Note that overflow isn't possible: a long can contain |
| 234 | no more than INT_MAX * SHIFT bits, so has value certainly less than |
| 235 | 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
| 236 | small enough to fit in an IEEE single. log and log10 are even smaller. |
| 237 | */ |
| 238 | |
| 239 | static PyObject* |
| 240 | loghelper(PyObject* args, double (*func)(double), char *name) |
| 241 | { |
| 242 | PyObject *arg; |
| 243 | char format[16]; |
| 244 | |
| 245 | /* See whether this is a long. */ |
| 246 | format[0] = 'O'; |
| 247 | format[1] = ':'; |
| 248 | strcpy(format + 2, name); |
| 249 | if (! PyArg_ParseTuple(args, format, &arg)) |
| 250 | return NULL; |
| 251 | |
| 252 | /* If it is long, do it ourselves. */ |
| 253 | if (PyLong_Check(arg)) { |
| 254 | double x; |
| 255 | int e; |
| 256 | x = _PyLong_AsScaledDouble(arg, &e); |
| 257 | if (x <= 0.0) { |
| 258 | PyErr_SetString(PyExc_ValueError, |
| 259 | "math domain error"); |
| 260 | return NULL; |
| 261 | } |
| 262 | /* Value is ~= x * 2**(e*SHIFT), so the log ~= |
| 263 | log(x) + log(2) * e * SHIFT. |
| 264 | CAUTION: e*SHIFT may overflow using int arithmetic, |
| 265 | so force use of double. */ |
| 266 | x = func(x) + func(2.0) * (double)e * (double)SHIFT; |
| 267 | return PyFloat_FromDouble(x); |
| 268 | } |
| 269 | |
| 270 | /* Else let libm handle it by itself. */ |
| 271 | format[0] = 'd'; |
| 272 | return math_1(args, func, format); |
| 273 | } |
| 274 | |
| 275 | static PyObject * |
| 276 | math_log(PyObject *self, PyObject *args) |
| 277 | { |
| 278 | return loghelper(args, log, "log"); |
| 279 | } |
| 280 | |
| 281 | static char math_log_doc[] = |
| 282 | "log(x) -> the natural logarithm (base e) of x."; |
| 283 | |
| 284 | static PyObject * |
| 285 | math_log10(PyObject *self, PyObject *args) |
| 286 | { |
| 287 | return loghelper(args, log10, "log10"); |
| 288 | } |
| 289 | |
| 290 | static char math_log10_doc[] = |
| 291 | "log10(x) -> the base 10 logarithm of x."; |
| 292 | |
| 293 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 294 | static PyMethodDef math_methods[] = { |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 295 | {"acos", math_acos, METH_VARARGS, math_acos_doc}, |
| 296 | {"asin", math_asin, METH_VARARGS, math_asin_doc}, |
| 297 | {"atan", math_atan, METH_VARARGS, math_atan_doc}, |
| 298 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
| 299 | {"ceil", math_ceil, METH_VARARGS, math_ceil_doc}, |
| 300 | {"cos", math_cos, METH_VARARGS, math_cos_doc}, |
| 301 | {"cosh", math_cosh, METH_VARARGS, math_cosh_doc}, |
| 302 | {"exp", math_exp, METH_VARARGS, math_exp_doc}, |
| 303 | {"fabs", math_fabs, METH_VARARGS, math_fabs_doc}, |
| 304 | {"floor", math_floor, METH_VARARGS, math_floor_doc}, |
| 305 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
| 306 | {"frexp", math_frexp, METH_VARARGS, math_frexp_doc}, |
| 307 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
| 308 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
| 309 | {"log", math_log, METH_VARARGS, math_log_doc}, |
| 310 | {"log10", math_log10, METH_VARARGS, math_log10_doc}, |
| 311 | {"modf", math_modf, METH_VARARGS, math_modf_doc}, |
| 312 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 313 | {"sin", math_sin, METH_VARARGS, math_sin_doc}, |
| 314 | {"sinh", math_sinh, METH_VARARGS, math_sinh_doc}, |
| 315 | {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc}, |
| 316 | {"tan", math_tan, METH_VARARGS, math_tan_doc}, |
| 317 | {"tanh", math_tanh, METH_VARARGS, math_tanh_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | {NULL, NULL} /* sentinel */ |
| 319 | }; |
| 320 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 321 | |
| 322 | static char module_doc [] = |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 323 | "This module is always available. It provides access to the\n" |
| 324 | "mathematical functions defined by the C standard."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 326 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 327 | initmath(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 329 | PyObject *m, *d, *v; |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 330 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 331 | m = Py_InitModule3("math", math_methods, module_doc); |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 332 | d = PyModule_GetDict(m); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 333 | |
| 334 | if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0))) |
| 335 | goto finally; |
| 336 | if (PyDict_SetItemString(d, "pi", v) < 0) |
| 337 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 338 | Py_DECREF(v); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 339 | |
| 340 | if (!(v = PyFloat_FromDouble(exp(1.0)))) |
| 341 | goto finally; |
Guido van Rossum | 4c4cbf3 | 1996-12-18 14:12:22 +0000 | [diff] [blame] | 342 | if (PyDict_SetItemString(d, "e", v) < 0) |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 343 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 344 | Py_DECREF(v); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 345 | |
| 346 | finally: |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 347 | return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | } |