Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Math module -- standard C math library functions, pi and e */ |
| 12 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 6964f73 | 1995-03-01 10:34:29 +0000 | [diff] [blame] | 15 | #include "mymath.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 7081cf5 | 1996-05-23 22:56:19 +0000 | [diff] [blame] | 17 | #ifndef _MSC_VER |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 18 | #ifndef __STDC__ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 19 | extern double fmod Py_PROTO((double, double)); |
| 20 | extern double frexp Py_PROTO((double, int *)); |
| 21 | extern double ldexp Py_PROTO((double, int)); |
| 22 | extern double modf Py_PROTO((double, double *)); |
Guido van Rossum | 7081cf5 | 1996-05-23 22:56:19 +0000 | [diff] [blame] | 23 | #endif /* __STDC__ */ |
| 24 | #endif /* _MSC_VER */ |
| 25 | |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 27 | #ifdef i860 |
| 28 | /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
| 29 | #undef HUGE_VAL |
| 30 | #endif |
| 31 | |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 32 | #ifdef HUGE_VAL |
| 33 | #define CHECK(x) if (errno != 0) ; \ |
| 34 | else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
| 35 | else errno = ERANGE |
| 36 | #else |
| 37 | #define CHECK(x) /* Don't know how to check */ |
| 38 | #endif |
| 39 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 40 | static PyObject * |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 41 | math_error() |
| 42 | { |
| 43 | if (errno == EDOM) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 44 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 45 | else if (errno == ERANGE) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 46 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 47 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 48 | /* Unexpected math error */ |
| 49 | PyErr_SetFromErrno(PyExc_ValueError); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 50 | return NULL; |
| 51 | } |
| 52 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 53 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | math_1(args, func) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 55 | PyObject *args; |
| 56 | double (*func) Py_FPROTO((double)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | { |
| 58 | double x; |
Roger E. Masse | 20c6381 | 1996-12-18 21:59:01 +0000 | [diff] [blame] | 59 | if (! PyArg_Parse(args, "d", &x)) |
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) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 65 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 67 | return math_error(); |
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 * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | math_2(args, func) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 74 | PyObject *args; |
| 75 | double (*func) Py_FPROTO((double, double)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | { |
| 77 | double x, y; |
Roger E. Masse | 20c6381 | 1996-12-18 21:59:01 +0000 | [diff] [blame] | 78 | if (! PyArg_Parse(args, "(dd)", &x, &y)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | return NULL; |
| 80 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 81 | PyFPE_START_PROTECT("in math_2", return 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | x = (*func)(x, y); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 83 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 84 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 86 | return math_error(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | else |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 88 | return PyFloat_FromDouble(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 91 | #define FUNC1(stubname, func, docstring_name, docstring) \ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 92 | static PyObject * stubname(self, args) PyObject *self, *args; { \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | return math_1(args, func); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 94 | }\ |
| 95 | static char docstring_name [] = docstring; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 97 | #define FUNC2(stubname, func, docstring_name, docstring) \ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 98 | static PyObject * stubname(self, args) PyObject *self, *args; { \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | return math_2(args, func); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 100 | }\ |
| 101 | static char docstring_name [] = docstring; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 103 | FUNC1(math_acos, acos, math_acos_doc, |
| 104 | "acos(x)\n\nReturn the arc cosine of x.") |
| 105 | FUNC1(math_asin, asin, math_asin_doc, |
| 106 | "asin(x)\n\nReturn the arc sine of x.") |
| 107 | FUNC1(math_atan, atan, math_atan_doc, |
| 108 | "atan(x)\n\nReturn the arc tangent of x.") |
| 109 | FUNC2(math_atan2, atan2, math_atan2_doc, |
Guido van Rossum | b057dd8 | 1998-12-08 16:27:10 +0000 | [diff] [blame] | 110 | "atan2(y, x)\n\nReturn atan(y/x).") |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 111 | FUNC1(math_ceil, ceil, math_ceil_doc, |
| 112 | "ceil(x)\n\nReturn the ceiling of x as a real.") |
| 113 | FUNC1(math_cos, cos, math_cos_doc, |
| 114 | "cos(x)\n\nReturn the cosine of x.") |
| 115 | FUNC1(math_cosh, cosh, math_cosh_doc, |
| 116 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
| 117 | FUNC1(math_exp, exp, math_exp_doc, |
| 118 | "exp(x)\n\nReturn e raised to the power of x.") |
| 119 | FUNC1(math_fabs, fabs, math_fabs_doc, |
| 120 | "fabs(x)\n\nReturn the absolute value of the real x.") |
| 121 | FUNC1(math_floor, floor, math_floor_doc, |
| 122 | "floor(x)\n\nReturn the floor of x as a real.") |
| 123 | FUNC2(math_fmod, fmod, math_fmod_doc, |
| 124 | "fmod(x,y)\n\nReturn x % y.") |
| 125 | FUNC2(math_hypot, hypot, math_hypot_doc, |
| 126 | "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).") |
| 127 | FUNC1(math_log, log, math_log_doc, |
| 128 | "log(x)\n\nReturn the natural logarithm of x.") |
| 129 | FUNC1(math_log10, log10, math_log10_doc, |
| 130 | "log10(x)\n\nReturn the base-10 logarithm of x.") |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 131 | #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 132 | FUNC2(math_pow, power, math_pow_doc, |
| 133 | "power(x,y)\n\nReturn x**y.") |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 134 | #else |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 135 | FUNC2(math_pow, pow, math_pow_doc, |
| 136 | "pow(x,y)\n\nReturn x**y.") |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 137 | #endif |
Guido van Rossum | c9a5f34 | 2000-05-11 18:42:27 +0000 | [diff] [blame] | 138 | #ifdef HAVE_RINT |
Guido van Rossum | 71260b8 | 2000-05-11 18:19:42 +0000 | [diff] [blame] | 139 | FUNC1(math_rint, rint, math_rint_doc, |
| 140 | "rint(x)\n\nReturn the integer nearest to x as a real.") |
Guido van Rossum | c9a5f34 | 2000-05-11 18:42:27 +0000 | [diff] [blame] | 141 | #endif |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 142 | FUNC1(math_sin, sin, math_sin_doc, |
| 143 | "sin(x)\n\nReturn the sine of x.") |
| 144 | FUNC1(math_sinh, sinh, math_sinh_doc, |
| 145 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
| 146 | FUNC1(math_sqrt, sqrt, math_sqrt_doc, |
| 147 | "sqrt(x)\n\nReturn the square root of x.") |
| 148 | FUNC1(math_tan, tan, math_tan_doc, |
| 149 | "tan(x)\n\nReturn the tangent of x.") |
| 150 | FUNC1(math_tanh, tanh, math_tanh_doc, |
| 151 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 153 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 154 | static PyObject * |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 155 | math_frexp(self, args) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 156 | PyObject *self; |
| 157 | PyObject *args; |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 158 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 159 | double x; |
| 160 | int i; |
Roger E. Masse | 20c6381 | 1996-12-18 21:59:01 +0000 | [diff] [blame] | 161 | if (! PyArg_Parse(args, "d", &x)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | errno = 0; |
| 164 | x = frexp(x, &i); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 165 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 166 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 167 | return math_error(); |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 168 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 171 | static char math_frexp_doc [] = |
| 172 | "frexp(x)\n\ |
| 173 | \n\ |
Tim Peters | c9c02c4 | 2000-07-02 20:17:08 +0000 | [diff] [blame^] | 174 | Return the matissa and exponent of x, as pair (m, e).\n\ |
| 175 | m is a float and e is an int, such that x = m * 2.**e.\n\ |
| 176 | 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] | 177 | |
| 178 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 179 | static PyObject * |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 180 | math_ldexp(self, args) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 181 | PyObject *self; |
| 182 | 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; |
| 186 | if (! PyArg_Parse(args, "(di)", &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); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 193 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 194 | return math_error(); |
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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 199 | static char math_ldexp_doc [] = |
| 200 | "ldexp_doc(x, i)\n\ |
| 201 | \n\ |
| 202 | Return x * (2**i)."; |
| 203 | |
| 204 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 205 | static PyObject * |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 206 | math_modf(self, args) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 207 | PyObject *self; |
| 208 | PyObject *args; |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 209 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 210 | double x, y; |
Roger E. Masse | 20c6381 | 1996-12-18 21:59:01 +0000 | [diff] [blame] | 211 | if (! PyArg_Parse(args, "d", &x)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 212 | return NULL; |
| 213 | errno = 0; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 214 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 215 | { |
| 216 | extended e; |
| 217 | x = modf(x, &e); |
| 218 | y = e; |
| 219 | } |
| 220 | #else |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 221 | x = modf(x, &y); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | #endif |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 223 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 224 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 225 | return math_error(); |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 226 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 227 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 229 | static char math_modf_doc [] = |
| 230 | "modf(x)\n\ |
| 231 | \n\ |
| 232 | Return the fractional and integer parts of x. Both results carry the sign\n\ |
| 233 | of x. The integer part is returned as a real."; |
| 234 | |
| 235 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 236 | static PyMethodDef math_methods[] = { |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 237 | {"acos", math_acos, 0, math_acos_doc}, |
| 238 | {"asin", math_asin, 0, math_asin_doc}, |
| 239 | {"atan", math_atan, 0, math_atan_doc}, |
| 240 | {"atan2", math_atan2, 0, math_atan2_doc}, |
| 241 | {"ceil", math_ceil, 0, math_ceil_doc}, |
| 242 | {"cos", math_cos, 0, math_cos_doc}, |
| 243 | {"cosh", math_cosh, 0, math_cosh_doc}, |
| 244 | {"exp", math_exp, 0, math_exp_doc}, |
| 245 | {"fabs", math_fabs, 0, math_fabs_doc}, |
| 246 | {"floor", math_floor, 0, math_floor_doc}, |
| 247 | {"fmod", math_fmod, 0, math_fmod_doc}, |
| 248 | {"frexp", math_frexp, 0, math_frexp_doc}, |
| 249 | {"hypot", math_hypot, 0, math_hypot_doc}, |
| 250 | {"ldexp", math_ldexp, 0, math_ldexp_doc}, |
| 251 | {"log", math_log, 0, math_log_doc}, |
| 252 | {"log10", math_log10, 0, math_log10_doc}, |
| 253 | {"modf", math_modf, 0, math_modf_doc}, |
| 254 | {"pow", math_pow, 0, math_pow_doc}, |
Guido van Rossum | c9a5f34 | 2000-05-11 18:42:27 +0000 | [diff] [blame] | 255 | #ifdef HAVE_RINT |
Guido van Rossum | 71260b8 | 2000-05-11 18:19:42 +0000 | [diff] [blame] | 256 | {"rint", math_rint, 0, math_rint_doc}, |
Guido van Rossum | c9a5f34 | 2000-05-11 18:42:27 +0000 | [diff] [blame] | 257 | #endif |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 258 | {"sin", math_sin, 0, math_sin_doc}, |
| 259 | {"sinh", math_sinh, 0, math_sinh_doc}, |
| 260 | {"sqrt", math_sqrt, 0, math_sqrt_doc}, |
| 261 | {"tan", math_tan, 0, math_tan_doc}, |
| 262 | {"tanh", math_tanh, 0, math_tanh_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | {NULL, NULL} /* sentinel */ |
| 264 | }; |
| 265 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 266 | |
| 267 | static char module_doc [] = |
| 268 | "This module is always available. It provides access to the\n\ |
| 269 | mathematical functions defined by the C standard."; |
| 270 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 271 | DL_EXPORT(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 272 | initmath() |
| 273 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 274 | PyObject *m, *d, *v; |
Guido van Rossum | 738d4dd | 1990-10-26 14:59:30 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 276 | m = Py_InitModule3("math", math_methods, module_doc); |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 277 | d = PyModule_GetDict(m); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 278 | |
| 279 | if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0))) |
| 280 | goto finally; |
| 281 | if (PyDict_SetItemString(d, "pi", v) < 0) |
| 282 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 283 | Py_DECREF(v); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 284 | |
| 285 | if (!(v = PyFloat_FromDouble(exp(1.0)))) |
| 286 | goto finally; |
Guido van Rossum | 4c4cbf3 | 1996-12-18 14:12:22 +0000 | [diff] [blame] | 287 | if (PyDict_SetItemString(d, "e", v) < 0) |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 288 | goto finally; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 289 | Py_DECREF(v); |
Guido van Rossum | 4a48809 | 1996-12-18 14:14:33 +0000 | [diff] [blame] | 290 | return; |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 291 | |
| 292 | finally: |
| 293 | Py_FatalError("can't initialize math module"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | } |