Mercurial > cpython-lru_cache-weakref
annotate Modules/mathmodule.c @ 13255:916fb024ce94 legacy-trunk
Change copyright notice.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 30 Jun 2000 23:50:40 +0000 |
| parents | 2a6dc65c060f |
| children | 87ee82e5c56d |
| 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 | |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12669
diff
changeset
|
7 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12669
diff
changeset
|
8 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12669
diff
changeset
|
9 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12669
diff
changeset
|
10 All rights reserved. |
| 145 | 11 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12669
diff
changeset
|
12 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12669
diff
changeset
|
13 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 145 | 14 |
| 15 ******************************************************************/ | |
| 16 | |
| 6 | 17 /* Math module -- standard C math library functions, pi and e */ |
| 18 | |
| 4635 | 19 #include "Python.h" |
| 6 | 20 |
| 2455 | 21 #include "mymath.h" |
| 86 | 22 |
| 3597 | 23 #ifndef _MSC_VER |
|
2125
bd061dc30842
rearrange declarations somewhat
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
24 #ifndef __STDC__ |
| 4635 | 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 *)); | |
| 3597 | 29 #endif /* __STDC__ */ |
| 30 #endif /* _MSC_VER */ | |
| 31 | |
|
2125
bd061dc30842
rearrange declarations somewhat
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
32 |
|
1271
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
33 #ifdef i860 |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
34 /* 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
|
35 #undef HUGE_VAL |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
36 #endif |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
37 |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
38 #ifdef HUGE_VAL |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
39 #define CHECK(x) if (errno != 0) ; \ |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
40 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
41 else errno = ERANGE |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
42 #else |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
43 #define CHECK(x) /* Don't know how to check */ |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
44 #endif |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
45 |
| 4635 | 46 static PyObject * |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
47 math_error() |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
48 { |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
49 if (errno == EDOM) |
| 4635 | 50 PyErr_SetString(PyExc_ValueError, "math domain error"); |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
51 else if (errno == ERANGE) |
| 4635 | 52 PyErr_SetString(PyExc_OverflowError, "math range error"); |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
53 else |
| 4635 | 54 /* Unexpected math error */ |
| 55 PyErr_SetFromErrno(PyExc_ValueError); | |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
56 return NULL; |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
57 } |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
58 |
| 4635 | 59 static PyObject * |
| 6 | 60 math_1(args, func) |
| 4635 | 61 PyObject *args; |
| 62 double (*func) Py_FPROTO((double)); | |
| 6 | 63 { |
| 64 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
|
65 if (! PyArg_Parse(args, "d", &x)) |
| 6 | 66 return NULL; |
| 67 errno = 0; | |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4780
diff
changeset
|
68 PyFPE_START_PROTECT("in math_1", return 0) |
| 6 | 69 x = (*func)(x); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
70 PyFPE_END_PROTECT(x) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
71 CHECK(x); |
| 6 | 72 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
73 return math_error(); |
| 6 | 74 else |
| 4635 | 75 return PyFloat_FromDouble(x); |
| 6 | 76 } |
| 77 | |
| 4635 | 78 static PyObject * |
| 6 | 79 math_2(args, func) |
| 4635 | 80 PyObject *args; |
| 81 double (*func) Py_FPROTO((double, double)); | |
| 6 | 82 { |
| 83 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
|
84 if (! PyArg_Parse(args, "(dd)", &x, &y)) |
| 6 | 85 return NULL; |
| 86 errno = 0; | |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4780
diff
changeset
|
87 PyFPE_START_PROTECT("in math_2", return 0) |
| 6 | 88 x = (*func)(x, y); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
89 PyFPE_END_PROTECT(x) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
90 CHECK(x); |
| 6 | 91 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
92 return math_error(); |
| 6 | 93 else |
| 4635 | 94 return PyFloat_FromDouble(x); |
| 6 | 95 } |
| 96 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
97 #define FUNC1(stubname, func, docstring_name, docstring) \ |
| 4635 | 98 static PyObject * stubname(self, args) PyObject *self, *args; { \ |
| 6 | 99 return math_1(args, func); \ |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
100 }\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
101 static char docstring_name [] = docstring; |
| 6 | 102 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
103 #define FUNC2(stubname, func, docstring_name, docstring) \ |
| 4635 | 104 static PyObject * stubname(self, args) PyObject *self, *args; { \ |
| 6 | 105 return math_2(args, func); \ |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
106 }\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
107 static char docstring_name [] = docstring; |
| 6 | 108 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
109 FUNC1(math_acos, acos, math_acos_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
110 "acos(x)\n\nReturn the arc cosine of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
111 FUNC1(math_asin, asin, math_asin_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
112 "asin(x)\n\nReturn the arc sine of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
113 FUNC1(math_atan, atan, math_atan_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
114 "atan(x)\n\nReturn the arc tangent of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
115 FUNC2(math_atan2, atan2, math_atan2_doc, |
|
9608
5a36bad1d248
Correctly document atan2.
Guido van Rossum <guido@python.org>
parents:
9593
diff
changeset
|
116 "atan2(y, x)\n\nReturn atan(y/x).") |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
117 FUNC1(math_ceil, ceil, math_ceil_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
118 "ceil(x)\n\nReturn the ceiling of x as a real.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
119 FUNC1(math_cos, cos, math_cos_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
120 "cos(x)\n\nReturn the cosine of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
121 FUNC1(math_cosh, cosh, math_cosh_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
122 "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
123 FUNC1(math_exp, exp, math_exp_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
124 "exp(x)\n\nReturn e raised to the power of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
125 FUNC1(math_fabs, fabs, math_fabs_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
126 "fabs(x)\n\nReturn the absolute value of the real x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
127 FUNC1(math_floor, floor, math_floor_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
128 "floor(x)\n\nReturn the floor of x as a real.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
129 FUNC2(math_fmod, fmod, math_fmod_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
130 "fmod(x,y)\n\nReturn x % y.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
131 FUNC2(math_hypot, hypot, math_hypot_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
132 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
133 FUNC1(math_log, log, math_log_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
134 "log(x)\n\nReturn the natural logarithm of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
135 FUNC1(math_log10, log10, math_log10_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
136 "log10(x)\n\nReturn the base-10 logarithm of x.") |
|
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 ... */ |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
138 FUNC2(math_pow, power, math_pow_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
139 "power(x,y)\n\nReturn x**y.") |
|
289
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
140 #else |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
141 FUNC2(math_pow, pow, math_pow_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
142 "pow(x,y)\n\nReturn x**y.") |
|
289
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
143 #endif |
|
12669
2a6dc65c060f
The addition of rint() (by Peter Schneider-Kamp; I forgot to mention
Guido van Rossum <guido@python.org>
parents:
12663
diff
changeset
|
144 #ifdef HAVE_RINT |
|
12663
1ea8cf885f7b
Added math.rint() -- round according to current IEEE754 mode
Guido van Rossum <guido@python.org>
parents:
12618
diff
changeset
|
145 FUNC1(math_rint, rint, math_rint_doc, |
|
1ea8cf885f7b
Added math.rint() -- round according to current IEEE754 mode
Guido van Rossum <guido@python.org>
parents:
12618
diff
changeset
|
146 "rint(x)\n\nReturn the integer nearest to x as a real.") |
|
12669
2a6dc65c060f
The addition of rint() (by Peter Schneider-Kamp; I forgot to mention
Guido van Rossum <guido@python.org>
parents:
12663
diff
changeset
|
147 #endif |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
148 FUNC1(math_sin, sin, math_sin_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
149 "sin(x)\n\nReturn the sine of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
150 FUNC1(math_sinh, sinh, math_sinh_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
151 "sinh(x)\n\nReturn the hyperbolic sine of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
152 FUNC1(math_sqrt, sqrt, math_sqrt_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
153 "sqrt(x)\n\nReturn the square root of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
154 FUNC1(math_tan, tan, math_tan_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
155 "tan(x)\n\nReturn the tangent of x.") |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
156 FUNC1(math_tanh, tanh, math_tanh_doc, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
157 "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
| 6 | 158 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
159 |
| 4635 | 160 static PyObject * |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
161 math_frexp(self, args) |
| 4635 | 162 PyObject *self; |
| 163 PyObject *args; | |
|
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 double x; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
166 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
|
167 if (! PyArg_Parse(args, "d", &x)) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
168 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
169 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
170 x = frexp(x, &i); |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
171 CHECK(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
172 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
173 return math_error(); |
| 4635 | 174 return Py_BuildValue("(di)", x, i); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
175 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
176 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
177 static char math_frexp_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
178 "frexp(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
179 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
180 Return the matissa and exponent for x. The mantissa is positive."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
181 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
182 |
| 4635 | 183 static PyObject * |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
184 math_ldexp(self, args) |
| 4635 | 185 PyObject *self; |
| 186 PyObject *args; | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
187 { |
| 12618 | 188 double x; |
| 189 int exp; | |
| 190 if (! PyArg_Parse(args, "(di)", &x, &exp)) | |
|
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; |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4780
diff
changeset
|
193 PyFPE_START_PROTECT("ldexp", return 0) |
| 12618 | 194 x = ldexp(x, exp); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
195 PyFPE_END_PROTECT(x) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
196 CHECK(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
197 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
198 return math_error(); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
199 else |
| 4635 | 200 return PyFloat_FromDouble(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
201 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
202 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
203 static char math_ldexp_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
204 "ldexp_doc(x, i)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
205 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
206 Return x * (2**i)."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
207 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
208 |
| 4635 | 209 static PyObject * |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
210 math_modf(self, args) |
| 4635 | 211 PyObject *self; |
| 212 PyObject *args; | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
213 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
214 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
|
215 if (! PyArg_Parse(args, "d", &x)) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
216 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
217 errno = 0; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
218 #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
|
219 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
220 extended e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
221 x = modf(x, &e); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
222 y = e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
223 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
224 #else |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
225 x = modf(x, &y); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
226 #endif |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
227 CHECK(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
228 if (errno != 0) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
229 return math_error(); |
| 4635 | 230 return Py_BuildValue("(dd)", x, y); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
231 } |
| 6 | 232 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
233 static char math_modf_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
234 "modf(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
235 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
236 Return the fractional and integer parts of x. Both results carry the sign\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
237 of x. The integer part is returned as a real."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
238 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
239 |
| 4635 | 240 static PyMethodDef math_methods[] = { |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
241 {"acos", math_acos, 0, math_acos_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
242 {"asin", math_asin, 0, math_asin_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
243 {"atan", math_atan, 0, math_atan_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
244 {"atan2", math_atan2, 0, math_atan2_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
245 {"ceil", math_ceil, 0, math_ceil_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
246 {"cos", math_cos, 0, math_cos_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
247 {"cosh", math_cosh, 0, math_cosh_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
248 {"exp", math_exp, 0, math_exp_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
249 {"fabs", math_fabs, 0, math_fabs_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
250 {"floor", math_floor, 0, math_floor_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
251 {"fmod", math_fmod, 0, math_fmod_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
252 {"frexp", math_frexp, 0, math_frexp_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
253 {"hypot", math_hypot, 0, math_hypot_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
254 {"ldexp", math_ldexp, 0, math_ldexp_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
255 {"log", math_log, 0, math_log_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
256 {"log10", math_log10, 0, math_log10_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
257 {"modf", math_modf, 0, math_modf_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
258 {"pow", math_pow, 0, math_pow_doc}, |
|
12669
2a6dc65c060f
The addition of rint() (by Peter Schneider-Kamp; I forgot to mention
Guido van Rossum <guido@python.org>
parents:
12663
diff
changeset
|
259 #ifdef HAVE_RINT |
|
12663
1ea8cf885f7b
Added math.rint() -- round according to current IEEE754 mode
Guido van Rossum <guido@python.org>
parents:
12618
diff
changeset
|
260 {"rint", math_rint, 0, math_rint_doc}, |
|
12669
2a6dc65c060f
The addition of rint() (by Peter Schneider-Kamp; I forgot to mention
Guido van Rossum <guido@python.org>
parents:
12663
diff
changeset
|
261 #endif |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
262 {"sin", math_sin, 0, math_sin_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
263 {"sinh", math_sinh, 0, math_sinh_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
264 {"sqrt", math_sqrt, 0, math_sqrt_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
265 {"tan", math_tan, 0, math_tan_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
266 {"tanh", math_tanh, 0, math_tanh_doc}, |
| 6 | 267 {NULL, NULL} /* sentinel */ |
| 268 }; | |
| 269 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
270 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
271 static char module_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
272 "This module is always available. It provides access to the\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
273 mathematical functions defined by the C standard."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
274 |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
5554
diff
changeset
|
275 DL_EXPORT(void) |
| 6 | 276 initmath() |
| 277 { | |
| 4635 | 278 PyObject *m, *d, *v; |
|
42
4835c2d38b84
Take out duplication of code in initmodule().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
279 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
280 m = Py_InitModule3("math", math_methods, module_doc); |
| 4635 | 281 d = PyModule_GetDict(m); |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
282 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
283 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0))) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
284 goto finally; |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
285 if (PyDict_SetItemString(d, "pi", v) < 0) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
286 goto finally; |
| 4635 | 287 Py_DECREF(v); |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
288 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
289 if (!(v = PyFloat_FromDouble(exp(1.0)))) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
290 goto finally; |
|
4765
f8b36c0ec114
Correct 1-char typo / syntax error.
Guido van Rossum <guido@python.org>
parents:
4752
diff
changeset
|
291 if (PyDict_SetItemString(d, "e", v) < 0) |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
292 goto finally; |
| 4635 | 293 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
|
294 return; |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
295 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
296 finally: |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
297 Py_FatalError("can't initialize math module"); |
| 6 | 298 } |
