Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Math module -- standard C math library functions, pi and e */ |
| 3 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 4 | #include "Python.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 | |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 21 | #ifdef HUGE_VAL |
| 22 | #define CHECK(x) if (errno != 0) ; \ |
| 23 | else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
| 24 | else errno = ERANGE |
| 25 | #else |
| 26 | #define CHECK(x) /* Don't know how to check */ |
| 27 | #endif |
| 28 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 29 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 30 | math_error(void) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 31 | { |
| 32 | if (errno == EDOM) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 33 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 34 | else if (errno == ERANGE) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 35 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 36 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 37 | /* Unexpected math error */ |
| 38 | PyErr_SetFromErrno(PyExc_ValueError); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 39 | return NULL; |
| 40 | } |
| 41 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 42 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 43 | math_1(PyObject *args, double (*func) (double), char *argsfmt) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | { |
| 45 | double x; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 46 | if (! PyArg_ParseTuple(args, argsfmt, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | return NULL; |
| 48 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 49 | PyFPE_START_PROTECT("in math_1", return 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | x = (*func)(x); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 51 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 52 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 54 | return math_error(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 56 | return PyFloat_FromDouble(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 59 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 60 | math_2(PyObject *args, double (*func) (double, double), char *argsfmt) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | { |
| 62 | double x, y; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 63 | if (! PyArg_ParseTuple(args, argsfmt, &x, &y)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | return NULL; |
| 65 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 66 | PyFPE_START_PROTECT("in math_2", return 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | x = (*func)(x, y); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 68 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 69 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 71 | return math_error(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 73 | return PyFloat_FromDouble(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 76 | #define FUNC1(funcname, func, docstring) \ |
| 77 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 78 | return math_1(args, func, "d:" #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 79 | }\ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 80 | static char math_##funcname##_doc [] = docstring; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 82 | #define FUNC2(funcname, func, docstring) \ |
| 83 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 84 | return math_2(args, func, "dd:" #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 85 | }\ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 86 | static char math_##funcname##_doc [] = docstring; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 88 | FUNC1(acos, acos, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 89 | "acos(x)\n\nReturn the arc cosine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 90 | FUNC1(asin, asin, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 91 | "asin(x)\n\nReturn the arc sine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 92 | FUNC1(atan, atan, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 93 | "atan(x)\n\nReturn the arc tangent of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 94 | FUNC2(atan2, atan2, |
Guido van Rossum | b057dd8 | 1998-12-08 16:27:10 +0000 | [diff] [blame] | 95 | "atan2(y, x)\n\nReturn atan(y/x).") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 96 | FUNC1(ceil, ceil, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 97 | "ceil(x)\n\nReturn the ceiling of x as a real.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 98 | FUNC1(cos, cos, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 99 | "cos(x)\n\nReturn the cosine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 100 | FUNC1(cosh, cosh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 101 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 102 | FUNC1(exp, exp, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 103 | "exp(x)\n\nReturn e raised to the power of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 104 | FUNC1(fabs, fabs, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 105 | "fabs(x)\n\nReturn the absolute value of the real x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 106 | FUNC1(floor, floor, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 107 | "floor(x)\n\nReturn the floor of x as a real.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 108 | FUNC2(fmod, fmod, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 109 | "fmod(x,y)\n\nReturn x % y.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 110 | FUNC2(hypot, hypot, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 111 | "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] | 112 | FUNC1(log, log, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 113 | "log(x)\n\nReturn the natural logarithm of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 114 | FUNC1(log10, log10, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 115 | "log10(x)\n\nReturn the base-10 logarithm of x.") |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 116 | #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] | 117 | FUNC2(pow, power, |
| 118 | "pow(x,y)\n\nReturn x**y.") |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 119 | #else |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 120 | FUNC2(pow, pow, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 121 | "pow(x,y)\n\nReturn x**y.") |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 122 | #endif |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 123 | FUNC1(sin, sin, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 124 | "sin(x)\n\nReturn the sine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 125 | FUNC1(sinh, sinh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 126 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 127 | FUNC1(sqrt, sqrt, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 128 | "sqrt(x)\n\nReturn the square root of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 129 | FUNC1(tan, tan, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 130 | "tan(x)\n\nReturn the tangent of x.") |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 131 | FUNC1(tanh, tanh, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 132 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 134 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 135 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 136 | math_frexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 137 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 138 | double x; |
| 139 | int i; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 140 | if (! PyArg_ParseTuple(args, "d:frexp", &x)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 141 | return NULL; |
| 142 | errno = 0; |
| 143 | x = frexp(x, &i); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 144 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 145 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 146 | return math_error(); |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 147 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 150 | static char math_frexp_doc [] = |
| 151 | "frexp(x)\n\ |
| 152 | \n\ |
Tim Peters | 489d54e | 2000-07-03 22:41:34 +0000 | [diff] [blame] | 153 | Return the mantissa and exponent of x, as pair (m, e).\n\ |
Tim Peters | c9c02c4 | 2000-07-02 20:17:08 +0000 | [diff] [blame] | 154 | m is a float and e is an int, such that x = m * 2.**e.\n\ |
| 155 | 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] | 156 | |
| 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_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 160 | { |
Guido van Rossum | c554505 | 2000-05-08 14:29:38 +0000 | [diff] [blame] | 161 | double x; |
| 162 | int exp; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 163 | if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 164 | return NULL; |
| 165 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 166 | PyFPE_START_PROTECT("ldexp", return 0) |
Guido van Rossum | c554505 | 2000-05-08 14:29:38 +0000 | [diff] [blame] | 167 | x = ldexp(x, exp); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 168 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 169 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 170 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 171 | return math_error(); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 172 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 173 | return PyFloat_FromDouble(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 176 | static char math_ldexp_doc [] = |
| 177 | "ldexp_doc(x, i)\n\ |
| 178 | \n\ |
| 179 | Return x * (2**i)."; |
| 180 | |
| 181 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 182 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 183 | math_modf(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 184 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 185 | double x, y; |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 186 | if (! PyArg_ParseTuple(args, "d:modf", &x)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | errno = 0; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 190 | { |
| 191 | extended e; |
| 192 | x = modf(x, &e); |
| 193 | y = e; |
| 194 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #else |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 196 | x = modf(x, &y); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 197 | #endif |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 198 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 199 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 200 | return math_error(); |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 201 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 202 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 204 | static char math_modf_doc [] = |
| 205 | "modf(x)\n\ |
| 206 | \n\ |
| 207 | Return the fractional and integer parts of x. Both results carry the sign\n\ |
| 208 | of x. The integer part is returned as a real."; |
| 209 | |
| 210 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 211 | static PyMethodDef math_methods[] = { |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 212 | {"acos", math_acos, METH_VARARGS, math_acos_doc}, |
| 213 | {"asin", math_asin, METH_VARARGS, math_asin_doc}, |
| 214 | {"atan", math_atan, METH_VARARGS, math_atan_doc}, |
| 215 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
| 216 | {"ceil", math_ceil, METH_VARARGS, math_ceil_doc}, |
| 217 | {"cos", math_cos, METH_VARARGS, math_cos_doc}, |
| 218 | {"cosh", math_cosh, METH_VARARGS, math_cosh_doc}, |
| 219 | {"exp", math_exp, METH_VARARGS, math_exp_doc}, |
| 220 | {"fabs", math_fabs, METH_VARARGS, math_fabs_doc}, |
| 221 | {"floor", math_floor, METH_VARARGS, math_floor_doc}, |
| 222 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
| 223 | {"frexp", math_frexp, METH_VARARGS, math_frexp_doc}, |
| 224 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
| 225 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
| 226 | {"log", math_log, METH_VARARGS, math_log_doc}, |
| 227 | {"log10", math_log10, METH_VARARGS, math_log10_doc}, |
| 228 | {"modf", math_modf, METH_VARARGS, math_modf_doc}, |
| 229 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 230 | {"sin", math_sin, METH_VARARGS, math_sin_doc}, |
| 231 | {"sinh", math_sinh, METH_VARARGS, math_sinh_doc}, |
| 232 | {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc}, |
| 233 | {"tan", math_tan, METH_VARARGS, math_tan_doc}, |
| 234 | {"tanh", math_tanh, METH_VARARGS, math_tanh_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | {NULL, NULL} /* sentinel */ |
| 236 | }; |
| 237 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 238 | |
| 239 | static char module_doc [] = |
| 240 | "This module is always available. It provides access to the\n\ |
| 241 | mathematical functions defined by the C standard."; |
| 242 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 243 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 244 | initmath(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 246 | PyObject *m, *d, *v; |
Guido van Rossum | 738d4dd | 1990-10-26 14:59:30 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 248 | m = Py_InitModule3("math", math_methods, module_doc); |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 249 | d = PyModule_GetDict(m); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 250 | |
| 251 | if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0))) |
| 252 | goto finally; |
| 253 | if (PyDict_SetItemString(d, "pi", v) < 0) |
| 254 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 255 | Py_DECREF(v); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 256 | |
| 257 | if (!(v = PyFloat_FromDouble(exp(1.0)))) |
| 258 | goto finally; |
Guido van Rossum | 4c4cbf3 | 1996-12-18 14:12:22 +0000 | [diff] [blame] | 259 | if (PyDict_SetItemString(d, "e", v) < 0) |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 260 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 261 | Py_DECREF(v); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 262 | |
| 263 | finally: |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 264 | return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | } |