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