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