annotate Modules/mathmodule.c @ 45919:855c4290dfd7 legacy-trunk

Fix the tcl-8.4.18.1 path and make sure we cd into the right directory when building tcl/tk.
author Trent Nelson <trent.nelson@snakebite.org>
date Wed, 19 Mar 2008 08:06:03 +0000
parents ce9816f53690
children 5bc7b20dc04a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Math module -- standard C math library functions, pi and e */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
3 #include "Python.h"
34872
0143d990d60c Make that a C, not a C++, comment.
Michael W. Hudson <mwh@python.net>
parents: 34871
diff changeset
4 #include "longintrepr.h" /* just for SHIFT */
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
44940
12099143724f Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents: 44488
diff changeset
15 #ifdef _OSF_SOURCE
12099143724f Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents: 44488
diff changeset
16 /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */
12099143724f Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents: 44488
diff changeset
17 extern double copysign(double, double);
12099143724f Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents: 44488
diff changeset
18 #endif
12099143724f Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents: 44488
diff changeset
19
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
20 /* Call is_error when errno != 0, and where x is the result libm
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
21 * returned. is_error will usually set up an exception and return
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
22 * true (1), but may return false (0) without setting up an exception.
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
23 */
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
24 static int
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
25 is_error(double x)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
26 {
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
27 int result = 1; /* presumption of guilt */
15181
2ed358446f2f Repaired a comment and asserted a precondition.
Tim Peters <tim.peters@gmail.com>
parents: 15170
diff changeset
28 assert(errno); /* non-zero errno is a precondition for calling */
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
29 if (errno == EDOM)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
30 PyErr_SetString(PyExc_ValueError, "math domain error");
19750
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
31
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
32 else if (errno == ERANGE) {
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
33 /* ANSI C generally requires libm functions to set ERANGE
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
34 * on overflow, but also generally *allows* them to set
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
35 * ERANGE on underflow too. There's no consistency about
19750
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
36 * the latter across platforms.
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
37 * Alas, C99 never requires that errno be set.
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
38 * Here we suppress the underflow errors (libm functions
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
39 * should return a zero on underflow, and +- HUGE_VAL on
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
40 * overflow, so testing the result for zero suffices to
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
41 * distinguish the cases).
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
42 */
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
43 if (x)
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
44 PyErr_SetString(PyExc_OverflowError,
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
45 "math range error");
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
46 else
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
47 result = 0;
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
48 }
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
49 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
50 /* Unexpected math error */
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
51 PyErr_SetFromErrno(PyExc_ValueError);
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
52 return result;
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
53 }
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
54
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
55 static PyObject *
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
56 math_1(PyObject *arg, double (*func) (double))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
58 double x = PyFloat_AsDouble(arg);
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
59 if (x == -1.0 && PyErr_Occurred())
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
62 PyFPE_START_PROTECT("in math_1", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
64 PyFPE_END_PROTECT(x)
31763
3473dea3aa4c Patch #871657: Set EDOM for `nan' return values on FreeBSD and OpenBSD.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 30968
diff changeset
65 Py_SET_ERRNO_ON_MATH_ERROR(x);
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
66 if (errno && is_error(x))
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
67 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
69 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
72 static PyObject *
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
73 math_2(PyObject *args, double (*func) (double, double), char *funcname)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
75 PyObject *ox, *oy;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 double x, y;
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
77 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
78 return NULL;
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
79 x = PyFloat_AsDouble(ox);
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
80 y = PyFloat_AsDouble(oy);
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
81 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
84 PyFPE_START_PROTECT("in math_2", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 x = (*func)(x, y);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
86 PyFPE_END_PROTECT(x)
31763
3473dea3aa4c Patch #871657: Set EDOM for `nan' return values on FreeBSD and OpenBSD.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 30968
diff changeset
87 Py_SET_ERRNO_ON_MATH_ERROR(x);
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
88 if (errno && is_error(x))
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
89 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
91 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
94 #define FUNC1(funcname, func, docstring) \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
95 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
96 return math_1(args, func); \
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
97 }\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
98 PyDoc_STRVAR(math_##funcname##_doc, docstring);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
100 #define FUNC2(funcname, func, docstring) \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
101 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
102 return math_2(args, func, #funcname); \
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
103 }\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
104 PyDoc_STRVAR(math_##funcname##_doc, docstring);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
106 FUNC1(acos, acos,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
107 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
108 FUNC1(asin, asin,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
109 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
110 FUNC1(atan, atan,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
111 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
19758
64bcef1702ad Revert parts of patch #453627, documenting the resulting test failures
Martin v. Löwis <martin@v.loewis.de>
parents: 19750
diff changeset
112 FUNC2(atan2, atan2,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
113 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
114 "Unlike atan(y/x), the signs of both x and y are considered.")
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44453
diff changeset
115 FUNC1(ceil, ceil,
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44453
diff changeset
116 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44453
diff changeset
117 "This is the smallest integral value >= x.")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
118 FUNC1(cos, cos,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
119 "cos(x)\n\nReturn the cosine of x (measured in radians).")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
120 FUNC1(cosh, cosh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
121 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
122
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
123 #ifdef MS_WINDOWS
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
124 # define copysign _copysign
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
125 # define HAVE_COPYSIGN 1
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
126 #endif
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
127 #ifdef HAVE_COPYSIGN
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
128 FUNC2(copysign, copysign,
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
129 "copysign(x,y)\n\nReturn x with the sign of y.");
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
130 #endif
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
131
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
132 FUNC1(exp, exp,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
133 "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
134 FUNC1(fabs, fabs,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
135 "fabs(x)\n\nReturn the absolute value of the float x.")
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44453
diff changeset
136 FUNC1(floor, floor,
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44453
diff changeset
137 "floor(x)\n\nReturn the floor of x as a float.\n"
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44453
diff changeset
138 "This is the largest integral value <= x.")
18066
bde1cf5740ec annoying whitespace inconsistency
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 15181
diff changeset
139 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
140 "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
141 " x % y may differ.")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
142 FUNC2(hypot, hypot,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
143 "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
144 FUNC2(pow, pow,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
145 "pow(x,y)\n\nReturn x**y (x to the power of y).")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
146 FUNC1(sin, sin,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
147 "sin(x)\n\nReturn the sine of x (measured in radians).")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
148 FUNC1(sinh, sinh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
149 "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
150 FUNC1(sqrt, sqrt,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
151 "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
152 FUNC1(tan, tan,
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
153 "tan(x)\n\nReturn the tangent of x (measured in radians).")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
154 FUNC1(tanh, tanh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
155 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
157 static PyObject *
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
158 math_trunc(PyObject *self, PyObject *number)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
159 {
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
160 return PyObject_CallMethod(number, "__trunc__", NULL);
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
161 }
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
162
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
163 PyDoc_STRVAR(math_trunc_doc,
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
164 "trunc(x:Real) -> Integral\n"
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
165 "\n"
45102
ce9816f53690 Neaten-up a bit.
Raymond Hettinger <python@rcn.com>
parents: 45079
diff changeset
166 "Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.");
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
167
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
168 static PyObject *
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
169 math_frexp(PyObject *self, PyObject *arg)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
170 {
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
171 int i;
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
172 double x = PyFloat_AsDouble(arg);
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
173 if (x == -1.0 && PyErr_Occurred())
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
174 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
175 errno = 0;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
176 x = frexp(x, &i);
31763
3473dea3aa4c Patch #871657: Set EDOM for `nan' return values on FreeBSD and OpenBSD.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 30968
diff changeset
177 Py_SET_ERRNO_ON_MATH_ERROR(x);
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
178 if (errno && is_error(x))
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
179 return NULL;
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
180 else
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
181 return Py_BuildValue("(di)", x, i);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
182 }
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
183
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
184 PyDoc_STRVAR(math_frexp_doc,
19712
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
185 "frexp(x)\n"
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
186 "\n"
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
187 "Return the mantissa and exponent of x, as pair (m, e).\n"
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
188 "m is a float and e is an int, such that x = m * 2.**e.\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
189 "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
190
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
191 static PyObject *
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
192 math_ldexp(PyObject *self, PyObject *args)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
193 {
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
194 double x;
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
195 int exp;
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
196 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
197 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
198 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
199 PyFPE_START_PROTECT("ldexp", return 0)
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
200 x = ldexp(x, exp);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
201 PyFPE_END_PROTECT(x)
31763
3473dea3aa4c Patch #871657: Set EDOM for `nan' return values on FreeBSD and OpenBSD.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 30968
diff changeset
202 Py_SET_ERRNO_ON_MATH_ERROR(x);
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
203 if (errno && is_error(x))
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
204 return NULL;
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
205 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
206 return PyFloat_FromDouble(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
207 }
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
208
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
209 PyDoc_STRVAR(math_ldexp_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
210 "ldexp(x, i) -> x * (2**i)");
9593
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 PyObject *
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
213 math_modf(PyObject *self, PyObject *arg)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
214 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
215 double y, x = PyFloat_AsDouble(arg);
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
216 if (x == -1.0 && PyErr_Occurred())
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
217 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
218 errno = 0;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
219 x = modf(x, &y);
31763
3473dea3aa4c Patch #871657: Set EDOM for `nan' return values on FreeBSD and OpenBSD.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 30968
diff changeset
220 Py_SET_ERRNO_ON_MATH_ERROR(x);
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
221 if (errno && is_error(x))
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
222 return NULL;
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
223 else
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
224 return Py_BuildValue("(dd)", x, y);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
225 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
227 PyDoc_STRVAR(math_modf_doc,
19712
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
228 "modf(x)\n"
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
229 "\n"
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
230 "Return the fractional and integer parts of x. Both results carry the sign\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
231 "of x. The integer part is returned as a real.");
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
232
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
233 /* A decent logarithm is easy to compute even for huge longs, but libm can't
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
234 do that by itself -- loghelper can. func is log or log10, and name is
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
235 "log" or "log10". Note that overflow isn't possible: a long can contain
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
236 no more than INT_MAX * SHIFT bits, so has value certainly less than
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
237 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
238 small enough to fit in an IEEE single. log and log10 are even smaller.
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
239 */
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
240
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
241 static PyObject*
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
242 loghelper(PyObject* arg, double (*func)(double), char *funcname)
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
243 {
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
244 /* If it is long, do it ourselves. */
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
245 if (PyLong_Check(arg)) {
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
246 double x;
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
247 int e;
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
248 x = _PyLong_AsScaledDouble(arg, &e);
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
249 if (x <= 0.0) {
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
250 PyErr_SetString(PyExc_ValueError,
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
251 "math domain error");
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
252 return NULL;
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
253 }
44947
8e435bdd4bc8 setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents: 44940
diff changeset
254 /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
8e435bdd4bc8 setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents: 44940
diff changeset
255 log(x) + log(2) * e * PyLong_SHIFT.
8e435bdd4bc8 setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents: 44940
diff changeset
256 CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
257 so force use of double. */
44947
8e435bdd4bc8 setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents: 44940
diff changeset
258 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
259 return PyFloat_FromDouble(x);
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
260 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
261
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
262 /* Else let libm handle it by itself. */
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
263 return math_1(arg, func);
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
264 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
265
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
266 static PyObject *
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
267 math_log(PyObject *self, PyObject *args)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
268 {
26661
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
269 PyObject *arg;
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
270 PyObject *base = NULL;
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
271 PyObject *num, *den;
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
272 PyObject *ans;
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
273
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26661
diff changeset
274 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
26661
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
275 return NULL;
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
276
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
277 num = loghelper(arg, log, "log");
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
278 if (num == NULL || base == NULL)
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
279 return num;
26661
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
280
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
281 den = loghelper(base, log, "log");
26661
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
282 if (den == NULL) {
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
283 Py_DECREF(num);
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
284 return NULL;
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
285 }
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
286
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
287 ans = PyNumber_Divide(num, den);
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
288 Py_DECREF(num);
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
289 Py_DECREF(den);
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
290 return ans;
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
291 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
292
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
293 PyDoc_STRVAR(math_log_doc,
26661
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
294 "log(x[, base]) -> the logarithm of x to the given base.\n\
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
295 If the base not specified, returns the natural logarithm (base e) of x.");
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
296
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
297 static PyObject *
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
298 math_log10(PyObject *self, PyObject *arg)
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
299 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
300 return loghelper(arg, log10, "log10");
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
301 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
302
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
303 PyDoc_STRVAR(math_log10_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
304 "log10(x) -> the base 10 logarithm of x.");
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
305
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
306 static const double degToRad = Py_MATH_PI / 180.0;
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
307 static const double radToDeg = 180.0 / Py_MATH_PI;
23427
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
308
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
309 static PyObject *
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
310 math_degrees(PyObject *self, PyObject *arg)
23427
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
311 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
312 double x = PyFloat_AsDouble(arg);
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
313 if (x == -1.0 && PyErr_Occurred())
23427
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
314 return NULL;
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
315 return PyFloat_FromDouble(x * radToDeg);
23427
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
316 }
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
317
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
318 PyDoc_STRVAR(math_degrees_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
319 "degrees(x) -> converts angle x from radians to degrees");
23427
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
320
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
321 static PyObject *
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
322 math_radians(PyObject *self, PyObject *arg)
23427
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
323 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
324 double x = PyFloat_AsDouble(arg);
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
325 if (x == -1.0 && PyErr_Occurred())
23427
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
326 return NULL;
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
327 return PyFloat_FromDouble(x * degToRad);
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
328 }
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
329
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
330 PyDoc_STRVAR(math_radians_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
331 "radians(x) -> converts angle x from degrees to radians");
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
332
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
333 static PyObject *
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
334 math_isnan(PyObject *self, PyObject *arg)
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
335 {
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
336 double x = PyFloat_AsDouble(arg);
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
337 if (x == -1.0 && PyErr_Occurred())
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
338 return NULL;
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
339 return PyBool_FromLong((long)Py_IS_NAN(x));
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
340 }
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
341
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
342 PyDoc_STRVAR(math_isnan_doc,
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
343 "isnan(x) -> bool\n\
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
344 Checks if float x is not a number (NaN)");
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
345
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
346 static PyObject *
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
347 math_isinf(PyObject *self, PyObject *arg)
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
348 {
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
349 double x = PyFloat_AsDouble(arg);
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
350 if (x == -1.0 && PyErr_Occurred())
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
351 return NULL;
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
352 return PyBool_FromLong((long)Py_IS_INFINITY(x));
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
353 }
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
354
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
355 PyDoc_STRVAR(math_isinf_doc,
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
356 "isinf(x) -> bool\n\
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
357 Checks if float x is infinite (positive or negative)");
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
358
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
359
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
360 static PyMethodDef math_methods[] = {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
361 {"acos", math_acos, METH_O, math_acos_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
362 {"asin", math_asin, METH_O, math_asin_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
363 {"atan", math_atan, METH_O, math_atan_doc},
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
364 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
365 {"ceil", math_ceil, METH_O, math_ceil_doc},
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
366 #ifdef HAVE_COPYSIGN
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
367 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
368 #endif
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
369 {"cos", math_cos, METH_O, math_cos_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
370 {"cosh", math_cosh, METH_O, math_cosh_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
371 {"degrees", math_degrees, METH_O, math_degrees_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
372 {"exp", math_exp, METH_O, math_exp_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
373 {"fabs", math_fabs, METH_O, math_fabs_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
374 {"floor", math_floor, METH_O, math_floor_doc},
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
375 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
376 {"frexp", math_frexp, METH_O, math_frexp_doc},
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
377 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
378 {"isinf", math_isinf, METH_O, math_isinf_doc},
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
379 {"isnan", math_isnan, METH_O, math_isnan_doc},
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
380 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
381 {"log", math_log, METH_VARARGS, math_log_doc},
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
382 {"log10", math_log10, METH_O, math_log10_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
383 {"modf", math_modf, METH_O, math_modf_doc},
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
384 {"pow", math_pow, METH_VARARGS, math_pow_doc},
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
385 {"radians", math_radians, METH_O, math_radians_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
386 {"sin", math_sin, METH_O, math_sin_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
387 {"sinh", math_sinh, METH_O, math_sinh_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
388 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
389 {"tan", math_tan, METH_O, math_tan_doc},
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
390 {"tanh", math_tanh, METH_O, math_tanh_doc},
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44947
diff changeset
391 {"trunc", math_trunc, METH_O, math_trunc_doc},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
395
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
396 PyDoc_STRVAR(module_doc,
19712
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
397 "This module is always available. It provides access to the\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
398 "mathematical functions defined by the C standard.");
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
399
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
400 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13331
diff changeset
401 initmath(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 {
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
403 PyObject *m, *d, *v;
19048
e282d6b2b758 Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents: 18066
diff changeset
404
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
405 m = Py_InitModule3("math", math_methods, module_doc);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34872
diff changeset
406 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34872
diff changeset
407 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
408 d = PyModule_GetDict(m);
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
409 if (d == NULL)
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
410 goto finally;
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
411
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
412 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
413 goto finally;
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
414 if (PyDict_SetItemString(d, "pi", v) < 0)
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
415 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
416 Py_DECREF(v);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
417
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
418 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
419 goto finally;
4765
f8b36c0ec114 Correct 1-char typo / syntax error.
Guido van Rossum <guido@python.org>
parents: 4752
diff changeset
420 if (PyDict_SetItemString(d, "e", v) < 0)
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
421 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
422 Py_DECREF(v);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
423
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
424 finally:
14346
5397fdf88ec2 Do the absolute minimal amount of modifications to eradicate
Barry Warsaw <barry@python.org>
parents: 13912
diff changeset
425 return;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 }