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