annotate Modules/mathmodule.c @ 14914:79cdeeeefafa legacy-trunk

Removed the implib_dir instance variable because it is unused. Removed get_ext_libname() because it is unused. Fixed get_libraries() to append an '_d' to the python debug import library. If MSVC is used, do not add 'pythonxx.lib' to the list of libraries, because this is handled better by a pragma in config.h. This should fix bug #115595, but it needs some more testing.
author Thomas Heller <theller@ctypes.org>
date Thu, 28 Sep 2000 19:28:35 +0000
parents 284508c653c7
children 09c7ac188ce2
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
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Math module -- standard C math library functions, pi and e */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
4 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
3597
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
6 #ifndef _MSC_VER
2125
bd061dc30842 rearrange declarations somewhat
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
7 #ifndef __STDC__
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
8 extern double fmod (double, double);
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
9 extern double frexp (double, int *);
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
10 extern double ldexp (double, int);
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
11 extern double modf (double, double *);
3597
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
12 #endif /* __STDC__ */
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
13 #endif /* _MSC_VER */
e07597389d64 Microsoft C changes
Guido van Rossum <guido@python.org>
parents: 3339
diff changeset
14
2125
bd061dc30842 rearrange declarations somewhat
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
15
1271
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
16 #ifdef i860
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
17 /* 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
18 #undef HUGE_VAL
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
19 #endif
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
20
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
21 #ifdef HUGE_VAL
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
22 #define CHECK(x) if (errno != 0) ; \
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
23 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
24 else errno = ERANGE
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
25 #else
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
26 #define CHECK(x) /* Don't know how to check */
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
27 #endif
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
28
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
29 static PyObject *
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13331
diff changeset
30 math_error(void)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
31 {
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
32 if (errno == EDOM)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
33 PyErr_SetString(PyExc_ValueError, "math domain error");
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
34 else if (errno == ERANGE)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
35 PyErr_SetString(PyExc_OverflowError, "math range error");
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
36 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
37 /* Unexpected math error */
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
38 PyErr_SetFromErrno(PyExc_ValueError);
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
39 return NULL;
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
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
42 static PyObject *
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
43 math_1(PyObject *args, double (*func) (double), char *argsfmt)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 double x;
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
46 if (! PyArg_ParseTuple(args, argsfmt, &x))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
49 PyFPE_START_PROTECT("in math_1", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
51 PyFPE_END_PROTECT(x)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
52 CHECK(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
54 return math_error();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
56 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
59 static PyObject *
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
60 math_2(PyObject *args, double (*func) (double, double), char *argsfmt)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 double x, y;
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
63 if (! PyArg_ParseTuple(args, argsfmt, &x, &y))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
66 PyFPE_START_PROTECT("in math_2", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 x = (*func)(x, y);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
68 PyFPE_END_PROTECT(x)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
69 CHECK(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
71 return math_error();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
73 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
76 #define FUNC1(funcname, func, docstring) \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
77 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
78 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
79 }\
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
80 static char math_##funcname##_doc [] = docstring;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
82 #define FUNC2(funcname, func, docstring) \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
83 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
84 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
85 }\
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
86 static char math_##funcname##_doc [] = docstring;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
88 FUNC1(acos, acos,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
89 "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
90 FUNC1(asin, asin,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
91 "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
92 FUNC1(atan, atan,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
93 "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
94 FUNC2(atan2, atan2,
9608
5a36bad1d248 Correctly document atan2.
Guido van Rossum <guido@python.org>
parents: 9593
diff changeset
95 "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
96 FUNC1(ceil, ceil,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
97 "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
98 FUNC1(cos, cos,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
99 "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
100 FUNC1(cosh, cosh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
101 "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
102 FUNC1(exp, exp,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
103 "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
104 FUNC1(fabs, fabs,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
105 "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
106 FUNC1(floor, floor,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
107 "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
108 FUNC2(fmod, fmod,
14616
284508c653c7 Fixed legit gripe from c.l.py that math.fmod docs aren't confusing enough.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
109 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
284508c653c7 Fixed legit gripe from c.l.py that math.fmod docs aren't confusing enough.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
110 " x % y may differ.")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
111 FUNC2(hypot, hypot,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
112 "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
113 FUNC1(log, log,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
114 "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
115 FUNC1(log10, log10,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
116 "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
117 #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
118 FUNC2(pow, power,
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
119 "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
120 #else
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
121 FUNC2(pow, pow,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
122 "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
123 #endif
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
124 FUNC1(sin, sin,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
125 "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
126 FUNC1(sinh, sinh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
127 "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
128 FUNC1(sqrt, sqrt,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
129 "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
130 FUNC1(tan, tan,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
131 "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
132 FUNC1(tanh, tanh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
133 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
135
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
136 static PyObject *
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
137 math_frexp(PyObject *self, PyObject *args)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
138 {
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
139 double x;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
140 int i;
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
141 if (! PyArg_ParseTuple(args, "d:frexp", &x))
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
142 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
143 errno = 0;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
144 x = frexp(x, &i);
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
145 CHECK(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
146 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
147 return math_error();
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
148 return Py_BuildValue("(di)", x, i);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
149 }
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
150
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
151 static char math_frexp_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
152 "frexp(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
153 \n\
13331
7e3d60455958 Typo repair in docstring -- my fault.
Tim Peters <tim.peters@gmail.com>
parents: 13326
diff changeset
154 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
155 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
156 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
157
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
158
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
159 static PyObject *
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
160 math_ldexp(PyObject *self, PyObject *args)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
161 {
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
162 double x;
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
163 int exp;
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
164 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
165 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
166 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
167 PyFPE_START_PROTECT("ldexp", return 0)
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
168 x = ldexp(x, exp);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
169 PyFPE_END_PROTECT(x)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
170 CHECK(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
171 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
172 return math_error();
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
173 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
174 return PyFloat_FromDouble(x);
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_ldexp_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
178 "ldexp_doc(x, i)\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 x * (2**i).";
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
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
183 static PyObject *
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
184 math_modf(PyObject *self, PyObject *args)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
185 {
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
186 double x, y;
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
187 if (! PyArg_ParseTuple(args, "d:modf", &x))
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
188 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
189 errno = 0;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
190 #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
191 {
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
192 extended e;
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
193 x = modf(x, &e);
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
194 y = e;
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
195 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
196 #else
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
197 x = modf(x, &y);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
198 #endif
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
199 CHECK(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
200 if (errno != 0)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
201 return math_error();
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
202 return Py_BuildValue("(dd)", x, y);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
203 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
205 static char math_modf_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
206 "modf(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
207 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
208 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
209 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
210
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
211
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
212 static PyMethodDef math_methods[] = {
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
213 {"acos", math_acos, METH_VARARGS, math_acos_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
214 {"asin", math_asin, METH_VARARGS, math_asin_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
215 {"atan", math_atan, METH_VARARGS, math_atan_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
216 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
217 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
218 {"cos", math_cos, METH_VARARGS, math_cos_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
219 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
220 {"exp", math_exp, METH_VARARGS, math_exp_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
221 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
222 {"floor", math_floor, METH_VARARGS, math_floor_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
223 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
224 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
225 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
226 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
227 {"log", math_log, METH_VARARGS, math_log_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
228 {"log10", math_log10, METH_VARARGS, math_log10_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
229 {"modf", math_modf, METH_VARARGS, math_modf_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
230 {"pow", math_pow, METH_VARARGS, math_pow_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
231 {"sin", math_sin, METH_VARARGS, math_sin_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
232 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
233 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
234 {"tan", math_tan, METH_VARARGS, math_tan_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
235 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
239
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
240 static char module_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
241 "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
242 mathematical functions defined by the C standard.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
243
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 5554
diff changeset
244 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13331
diff changeset
245 initmath(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 {
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
247 PyObject *m, *d, *v;
42
4835c2d38b84 Take out duplication of code in initmodule().
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
248
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
249 m = Py_InitModule3("math", math_methods, module_doc);
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
250 d = PyModule_GetDict(m);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
251
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
252 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
253 goto finally;
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
254 if (PyDict_SetItemString(d, "pi", v) < 0)
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
255 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
256 Py_DECREF(v);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
257
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
258 if (!(v = PyFloat_FromDouble(exp(1.0))))
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
259 goto finally;
4765
f8b36c0ec114 Correct 1-char typo / syntax error.
Guido van Rossum <guido@python.org>
parents: 4752
diff changeset
260 if (PyDict_SetItemString(d, "e", v) < 0)
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
261 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
262 Py_DECREF(v);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
263
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
264 finally:
14346
5397fdf88ec2 Do the absolute minimal amount of modifications to eradicate
Barry Warsaw <barry@python.org>
parents: 13912
diff changeset
265 return;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 }