Mercurial > cpython-lru_cache-weakref
annotate Modules/mathmodule.c @ 4963:5ded2bc1908b legacy-trunk
"ported" Guido's embedding demo to the mac, and wrote some minimal
notes on embedding on the mac.
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Wed, 15 Jan 1997 16:53:37 +0000 |
| parents | abef5006f84b |
| children | a48f3678f9e1 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2061
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2055
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2055
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4178
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Math module -- standard C math library functions, pi and e */ |
| 33 | |
| 4635 | 34 #include "Python.h" |
| 6 | 35 |
| 2455 | 36 #include "mymath.h" |
| 86 | 37 |
| 3597 | 38 #ifndef _MSC_VER |
|
2125
bd061dc30842
rearrange declarations somewhat
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
39 #ifndef __STDC__ |
| 4635 | 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 *)); | |
| 3597 | 44 #endif /* __STDC__ */ |
| 45 #endif /* _MSC_VER */ | |
| 46 | |
|
2125
bd061dc30842
rearrange declarations somewhat
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
47 |
|
1271
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
48 #ifdef i860 |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
49 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
50 #undef HUGE_VAL |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
51 #endif |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
52 |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
53 #ifdef HUGE_VAL |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
54 #define CHECK(x) if (errno != 0) ; \ |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
55 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
56 else errno = ERANGE |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
57 #else |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
58 #define CHECK(x) /* Don't know how to check */ |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
59 #endif |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
60 |
| 4635 | 61 static PyObject * |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
62 math_error() |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
63 { |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
64 if (errno == EDOM) |
| 4635 | 65 PyErr_SetString(PyExc_ValueError, "math domain error"); |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
66 else if (errno == ERANGE) |
| 4635 | 67 PyErr_SetString(PyExc_OverflowError, "math range error"); |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
68 else |
| 4635 | 69 /* Unexpected math error */ |
| 70 PyErr_SetFromErrno(PyExc_ValueError); | |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
71 return NULL; |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
72 } |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
73 |
| 4635 | 74 static PyObject * |
| 6 | 75 math_1(args, func) |
| 4635 | 76 PyObject *args; |
| 77 double (*func) Py_FPROTO((double)); | |
| 6 | 78 { |
| 79 double x; | |
|
4780
abef5006f84b
Removed references to getdoublearg and get2doublearg rename macros and
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4766
diff
changeset
|
80 if (! PyArg_Parse(args, "d", &x)) |
| 6 | 81 return NULL; |
| 82 errno = 0; | |
| 83 x = (*func)(x); | |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
84 CHECK(x); |
| 6 | 85 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
86 return math_error(); |
| 6 | 87 else |
| 4635 | 88 return PyFloat_FromDouble(x); |
| 6 | 89 } |
| 90 | |
| 4635 | 91 static PyObject * |
| 6 | 92 math_2(args, func) |
| 4635 | 93 PyObject *args; |
| 94 double (*func) Py_FPROTO((double, double)); | |
| 6 | 95 { |
| 96 double x, y; | |
|
4780
abef5006f84b
Removed references to getdoublearg and get2doublearg rename macros and
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4766
diff
changeset
|
97 if (! PyArg_Parse(args, "(dd)", &x, &y)) |
| 6 | 98 return NULL; |
| 99 errno = 0; | |
| 100 x = (*func)(x, y); | |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
101 CHECK(x); |
| 6 | 102 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
103 return math_error(); |
| 6 | 104 else |
| 4635 | 105 return PyFloat_FromDouble(x); |
| 6 | 106 } |
| 107 | |
| 108 #define FUNC1(stubname, func) \ | |
| 4635 | 109 static PyObject * stubname(self, args) PyObject *self, *args; { \ |
| 6 | 110 return math_1(args, func); \ |
| 111 } | |
| 112 | |
| 113 #define FUNC2(stubname, func) \ | |
| 4635 | 114 static PyObject * stubname(self, args) PyObject *self, *args; { \ |
| 6 | 115 return math_2(args, func); \ |
| 116 } | |
| 117 | |
| 118 FUNC1(math_acos, acos) | |
| 119 FUNC1(math_asin, asin) | |
| 120 FUNC1(math_atan, atan) | |
| 121 FUNC2(math_atan2, atan2) | |
| 122 FUNC1(math_ceil, ceil) | |
| 123 FUNC1(math_cos, cos) | |
| 124 FUNC1(math_cosh, cosh) | |
| 125 FUNC1(math_exp, exp) | |
|
3974
fc505f02ad8a
Hack for Mac (where fabs is not usable as a function pointer).
Guido van Rossum <guido@python.org>
parents:
3881
diff
changeset
|
126 #ifdef __MWERKS__ |
|
fc505f02ad8a
Hack for Mac (where fabs is not usable as a function pointer).
Guido van Rossum <guido@python.org>
parents:
3881
diff
changeset
|
127 double myfabs(double x) { return fabs(x); } |
|
fc505f02ad8a
Hack for Mac (where fabs is not usable as a function pointer).
Guido van Rossum <guido@python.org>
parents:
3881
diff
changeset
|
128 FUNC1(math_fabs, myfabs) |
|
fc505f02ad8a
Hack for Mac (where fabs is not usable as a function pointer).
Guido van Rossum <guido@python.org>
parents:
3881
diff
changeset
|
129 #else |
| 6 | 130 FUNC1(math_fabs, fabs) |
|
3974
fc505f02ad8a
Hack for Mac (where fabs is not usable as a function pointer).
Guido van Rossum <guido@python.org>
parents:
3881
diff
changeset
|
131 #endif |
| 6 | 132 FUNC1(math_floor, floor) |
| 133 FUNC2(math_fmod, fmod) | |
| 1986 | 134 FUNC2(math_hypot, hypot) |
| 6 | 135 FUNC1(math_log, log) |
| 136 FUNC1(math_log10, log10) | |
|
320
26426c42a162
Add some err_errno calls that were missing
Guido van Rossum <guido@python.org>
parents:
289
diff
changeset
|
137 #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */ |
|
289
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
138 FUNC2(math_pow, power) |
|
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
139 #else |
| 6 | 140 FUNC2(math_pow, pow) |
|
289
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
141 #endif |
| 6 | 142 FUNC1(math_sin, sin) |
| 143 FUNC1(math_sinh, sinh) | |
| 144 FUNC1(math_sqrt, sqrt) | |
| 145 FUNC1(math_tan, tan) | |
| 146 FUNC1(math_tanh, tanh) | |
| 147 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
148 |
| 4635 | 149 static PyObject * |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
150 math_frexp(self, args) |
| 4635 | 151 PyObject *self; |
| 152 PyObject *args; | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
153 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
154 double x; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
155 int i; |
|
4780
abef5006f84b
Removed references to getdoublearg and get2doublearg rename macros and
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4766
diff
changeset
|
156 if (! PyArg_Parse(args, "d", &x)) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
157 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
158 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
159 x = frexp(x, &i); |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
160 CHECK(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
161 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
162 return math_error(); |
| 4635 | 163 return Py_BuildValue("(di)", x, i); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
164 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
165 |
| 4635 | 166 static PyObject * |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
167 math_ldexp(self, args) |
| 4635 | 168 PyObject *self; |
| 169 PyObject *args; | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
170 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
171 double x, y; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
172 /* Cheat -- allow float as second argument */ |
|
4780
abef5006f84b
Removed references to getdoublearg and get2doublearg rename macros and
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4766
diff
changeset
|
173 if (! PyArg_Parse(args, "(dd)", &x, &y)) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
174 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
175 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
176 x = ldexp(x, (int)y); |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
177 CHECK(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
178 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
179 return math_error(); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
180 else |
| 4635 | 181 return PyFloat_FromDouble(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
182 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
183 |
| 4635 | 184 static PyObject * |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
185 math_modf(self, args) |
| 4635 | 186 PyObject *self; |
| 187 PyObject *args; | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
188 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
189 double x, y; |
|
4780
abef5006f84b
Removed references to getdoublearg and get2doublearg rename macros and
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4766
diff
changeset
|
190 if (! PyArg_Parse(args, "d", &x)) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
191 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
192 errno = 0; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
193 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
194 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
195 extended e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
196 x = modf(x, &e); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
197 y = e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
198 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
199 #else |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
200 x = modf(x, &y); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
201 #endif |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
202 CHECK(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
203 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
204 return math_error(); |
| 4635 | 205 return Py_BuildValue("(dd)", x, y); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
206 } |
| 6 | 207 |
| 4635 | 208 static PyMethodDef math_methods[] = { |
| 6 | 209 {"acos", math_acos}, |
| 210 {"asin", math_asin}, | |
| 211 {"atan", math_atan}, | |
| 212 {"atan2", math_atan2}, | |
| 213 {"ceil", math_ceil}, | |
| 214 {"cos", math_cos}, | |
| 215 {"cosh", math_cosh}, | |
| 216 {"exp", math_exp}, | |
| 217 {"fabs", math_fabs}, | |
| 218 {"floor", math_floor}, | |
| 219 {"fmod", math_fmod}, | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
220 {"frexp", math_frexp}, |
| 1986 | 221 {"hypot", math_hypot}, |
| 6 | 222 {"ldexp", math_ldexp}, |
| 223 {"log", math_log}, | |
| 224 {"log10", math_log10}, | |
| 225 {"modf", math_modf}, | |
| 226 {"pow", math_pow}, | |
| 227 {"sin", math_sin}, | |
| 228 {"sinh", math_sinh}, | |
| 229 {"sqrt", math_sqrt}, | |
| 230 {"tan", math_tan}, | |
| 231 {"tanh", math_tanh}, | |
| 232 {NULL, NULL} /* sentinel */ | |
| 233 }; | |
| 234 | |
| 235 void | |
| 236 initmath() | |
| 237 { | |
| 4635 | 238 PyObject *m, *d, *v; |
|
42
4835c2d38b84
Take out duplication of code in initmodule().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
239 |
| 4635 | 240 m = Py_InitModule("math", math_methods); |
| 241 d = PyModule_GetDict(m); | |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
242 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
243 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0))) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
244 goto finally; |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
245 if (PyDict_SetItemString(d, "pi", v) < 0) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
246 goto finally; |
| 4635 | 247 Py_DECREF(v); |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
248 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
249 if (!(v = PyFloat_FromDouble(exp(1.0)))) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
250 goto finally; |
|
4765
f8b36c0ec114
Correct 1-char typo / syntax error.
Guido van Rossum <guido@python.org>
parents:
4752
diff
changeset
|
251 if (PyDict_SetItemString(d, "e", v) < 0) |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
252 goto finally; |
| 4635 | 253 Py_DECREF(v); |
|
4766
9604d6784675
Correct *another* mistake (initmath() always fell through to fatal error).
Guido van Rossum <guido@python.org>
parents:
4765
diff
changeset
|
254 return; |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
255 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
256 finally: |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
257 Py_FatalError("can't initialize math module"); |
| 6 | 258 } |
