annotate Modules/mathmodule.c @ 13184:efecbbf72765 legacy-trunk

Describe a bit about writing test cases for Python...
author Skip Montanaro <skip@pobox.com>
date Fri, 30 Jun 2000 06:08:35 +0000
parents 2a6dc65c060f
children 916fb024ce94
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
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
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
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
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
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
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
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
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
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
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 92
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* Math module -- standard C math library functions, pi and e */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
34 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
2455
b42ab77f2226 include mymath.h
Guido van Rossum <guido@python.org>
parents: 2213
diff changeset
36 #include "mymath.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 75
diff changeset
37
3597
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
38 #ifndef _MSC_VER
2125
bd061dc30842 rearrange declarations somewhat
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
39 #ifndef __STDC__
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
40 extern double fmod Py_PROTO((double, double));
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
41 extern double frexp Py_PROTO((double, int *));
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
42 extern double ldexp Py_PROTO((double, int));
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
43 extern double modf Py_PROTO((double, double *));
3597
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
44 #endif /* __STDC__ */
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
45 #endif /* _MSC_VER */
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
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
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
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
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
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
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
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
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
69 /* Unexpected math error */
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
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
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
74 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 math_1(args, func)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
76 PyObject *args;
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
77 double (*func) Py_FPROTO((double));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
83 PyFPE_START_PROTECT("in math_1", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
85 PyFPE_END_PROTECT(x)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
86 CHECK(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
88 return math_error();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
90 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
93 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 math_2(args, func)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
95 PyObject *args;
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
96 double (*func) Py_FPROTO((double, double));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 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
99 if (! PyArg_Parse(args, "(dd)", &x, &y))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
102 PyFPE_START_PROTECT("in math_2", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 x = (*func)(x, y);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
104 PyFPE_END_PROTECT(x)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
105 CHECK(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
107 return math_error();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
109 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
112 #define FUNC1(stubname, func, docstring_name, docstring) \
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
113 static PyObject * stubname(self, args) PyObject *self, *args; { \
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 return math_1(args, func); \
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
115 }\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
116 static char docstring_name [] = docstring;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
118 #define FUNC2(stubname, func, docstring_name, docstring) \
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
119 static PyObject * stubname(self, args) PyObject *self, *args; { \
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return math_2(args, func); \
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
121 }\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
122 static char docstring_name [] = docstring;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
124 FUNC1(math_acos, acos, math_acos_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
125 "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
126 FUNC1(math_asin, asin, math_asin_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
127 "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
128 FUNC1(math_atan, atan, math_atan_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
129 "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
130 FUNC2(math_atan2, atan2, math_atan2_doc,
9608
5a36bad1d248 Correctly document atan2.
Guido van Rossum <guido@python.org>
parents: 9593
diff changeset
131 "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
132 FUNC1(math_ceil, ceil, math_ceil_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
133 "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
134 FUNC1(math_cos, cos, math_cos_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
135 "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
136 FUNC1(math_cosh, cosh, math_cosh_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
137 "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
138 FUNC1(math_exp, exp, math_exp_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
139 "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
140 FUNC1(math_fabs, fabs, math_fabs_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
141 "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
142 FUNC1(math_floor, floor, math_floor_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
143 "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
144 FUNC2(math_fmod, fmod, math_fmod_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
145 "fmod(x,y)\n\nReturn x % y.")
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
146 FUNC2(math_hypot, hypot, math_hypot_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
147 "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
148 FUNC1(math_log, log, math_log_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
149 "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
150 FUNC1(math_log10, log10, math_log10_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
151 "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
152 #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
153 FUNC2(math_pow, power, math_pow_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
154 "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
155 #else
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
156 FUNC2(math_pow, pow, math_pow_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
157 "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
158 #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
159 #ifdef HAVE_RINT
12663
1ea8cf885f7b Added math.rint() -- round according to current IEEE754 mode
Guido van Rossum <guido@python.org>
parents: 12618
diff changeset
160 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
161 "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
162 #endif
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
163 FUNC1(math_sin, sin, math_sin_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
164 "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
165 FUNC1(math_sinh, sinh, math_sinh_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
166 "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
167 FUNC1(math_sqrt, sqrt, math_sqrt_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
168 "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
169 FUNC1(math_tan, tan, math_tan_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
170 "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
171 FUNC1(math_tanh, tanh, math_tanh_doc,
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
172 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
174
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
175 static PyObject *
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
176 math_frexp(self, args)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
177 PyObject *self;
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
178 PyObject *args;
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
179 {
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
180 double x;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
181 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
182 if (! PyArg_Parse(args, "d", &x))
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
183 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
184 errno = 0;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
185 x = frexp(x, &i);
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
186 CHECK(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
187 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
188 return math_error();
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
189 return Py_BuildValue("(di)", x, i);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
190 }
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
191
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
192 static char math_frexp_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
193 "frexp(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
194 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
195 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
196
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
197
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
198 static PyObject *
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
199 math_ldexp(self, args)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
200 PyObject *self;
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
201 PyObject *args;
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
202 {
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
203 double x;
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
204 int exp;
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
205 if (! PyArg_Parse(args, "(di)", &x, &exp))
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
206 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
207 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
208 PyFPE_START_PROTECT("ldexp", return 0)
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
209 x = ldexp(x, exp);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
210 PyFPE_END_PROTECT(x)
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();
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
214 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
215 return PyFloat_FromDouble(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
216 }
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
217
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
218 static char math_ldexp_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
219 "ldexp_doc(x, i)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
220 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
221 Return x * (2**i).";
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
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
224 static PyObject *
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
225 math_modf(self, args)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
226 PyObject *self;
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
227 PyObject *args;
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
228 {
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
229 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
230 if (! PyArg_Parse(args, "d", &x))
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
231 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
232 errno = 0;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
233 #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
234 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
235 extended e;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
236 x = modf(x, &e);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
237 y = e;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
238 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
239 #else
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
240 x = modf(x, &y);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
241 #endif
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
242 CHECK(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
243 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
244 return math_error();
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
245 return Py_BuildValue("(dd)", x, y);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
246 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
248 static char math_modf_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
249 "modf(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
250 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
251 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
252 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
253
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
254
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
255 static PyMethodDef math_methods[] = {
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
256 {"acos", math_acos, 0, math_acos_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
257 {"asin", math_asin, 0, math_asin_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
258 {"atan", math_atan, 0, math_atan_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
259 {"atan2", math_atan2, 0, math_atan2_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
260 {"ceil", math_ceil, 0, math_ceil_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
261 {"cos", math_cos, 0, math_cos_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
262 {"cosh", math_cosh, 0, math_cosh_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
263 {"exp", math_exp, 0, math_exp_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
264 {"fabs", math_fabs, 0, math_fabs_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
265 {"floor", math_floor, 0, math_floor_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
266 {"fmod", math_fmod, 0, math_fmod_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
267 {"frexp", math_frexp, 0, math_frexp_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
268 {"hypot", math_hypot, 0, math_hypot_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
269 {"ldexp", math_ldexp, 0, math_ldexp_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
270 {"log", math_log, 0, math_log_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
271 {"log10", math_log10, 0, math_log10_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
272 {"modf", math_modf, 0, math_modf_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
273 {"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
274 #ifdef HAVE_RINT
12663
1ea8cf885f7b Added math.rint() -- round according to current IEEE754 mode
Guido van Rossum <guido@python.org>
parents: 12618
diff changeset
275 {"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
276 #endif
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
277 {"sin", math_sin, 0, math_sin_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
278 {"sinh", math_sinh, 0, math_sinh_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
279 {"sqrt", math_sqrt, 0, math_sqrt_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
280 {"tan", math_tan, 0, math_tan_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
281 {"tanh", math_tanh, 0, math_tanh_doc},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
285
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
286 static char module_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
287 "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
288 mathematical functions defined by the C standard.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
289
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 5554
diff changeset
290 DL_EXPORT(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 initmath()
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 {
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
293 PyObject *m, *d, *v;
42
4835c2d38b84 Take out duplication of code in initmodule().
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
294
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
295 m = Py_InitModule3("math", math_methods, module_doc);
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
296 d = PyModule_GetDict(m);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
297
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
298 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
299 goto finally;
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
300 if (PyDict_SetItemString(d, "pi", v) < 0)
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
301 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
302 Py_DECREF(v);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
303
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
304 if (!(v = PyFloat_FromDouble(exp(1.0))))
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
305 goto finally;
4765
f8b36c0ec114 Correct 1-char typo / syntax error.
Guido van Rossum <guido@python.org>
parents: 4752
diff changeset
306 if (PyDict_SetItemString(d, "e", v) < 0)
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
307 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
308 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
309 return;
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
310
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
311 finally:
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
312 Py_FatalError("can't initialize math module");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 }