annotate Modules/mathmodule.c @ 44453:cba9ff760430 legacy-trunk

Fixed math.copysign() on Windows
author Christian Heimes <christian@cheimes.de>
date Fri, 04 Jan 2008 01:48:50 +0000
parents 6208b17d0dbe
children 94eeaeeb3ce9
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
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
15 /* 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
16 * 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
17 * 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
18 */
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
19 static int
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
20 is_error(double x)
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
21 {
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
22 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
23 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
24 if (errno == EDOM)
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
25 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
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 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
28 /* 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
29 * 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
30 * 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
31 * 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
32 * 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
33 * 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
34 * 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
35 * 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
36 * 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
37 */
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
38 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
39 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
40 "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
41 else
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
42 result = 0;
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
43 }
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
44 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
45 /* Unexpected math error */
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
46 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
47 return result;
498
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
48 }
56bb1067a880 Improve error handling.
Guido van Rossum <guido@python.org>
parents: 438
diff changeset
49
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
50 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
51 math_1(PyObject *arg, double (*func) (double))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
53 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
54 if (x == -1.0 && PyErr_Occurred())
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
57 PyFPE_START_PROTECT("in math_1", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
59 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
60 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
61 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
62 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
64 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
67 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
68 math_2(PyObject *args, double (*func) (double, double), char *funcname)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
70 PyObject *ox, *oy;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 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
72 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
73 return NULL;
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
74 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
75 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
76 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
79 PyFPE_START_PROTECT("in math_2", return 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 x = (*func)(x, y);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
81 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
82 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
83 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
84 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
86 return PyFloat_FromDouble(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
89 #define FUNC1(funcname, func, docstring) \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
90 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
91 return math_1(args, func); \
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
92 }\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
93 PyDoc_STRVAR(math_##funcname##_doc, docstring);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
95 #define FUNC2(funcname, func, docstring) \
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
96 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
97 return math_2(args, func, #funcname); \
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
98 }\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
99 PyDoc_STRVAR(math_##funcname##_doc, docstring);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
101 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
102 "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
103 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
104 "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
105 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
106 "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
107 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
108 "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
109 "Unlike atan(y/x), the signs of both x and y are considered.")
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
110
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
111 static PyObject * math_ceil(PyObject *self, PyObject *number) {
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
112 static PyObject *ceil_str = NULL;
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
113 PyObject *method;
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
114
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
115 if (ceil_str == NULL) {
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
116 ceil_str = PyString_FromString("__ceil__");
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
117 if (ceil_str == NULL)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
118 return NULL;
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
119 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
120
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
121 method = _PyType_Lookup(Py_Type(number), ceil_str);
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
122 if (method == NULL)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
123 return math_1(number, ceil);
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
124 else
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
125 return PyObject_CallFunction(method, "O", number);
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
126 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
127
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
128 PyDoc_STRVAR(math_ceil_doc,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
129 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
130 "This is the smallest integral value >= x.");
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
131
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
132 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
133 "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
134 FUNC1(cosh, cosh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
135 "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
136
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
137 #ifdef MS_WINDOWS
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
138 # define copysign _copysign
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
139 # define HAVE_COPYSIGN 1
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
140 #endif
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
141 #ifdef HAVE_COPYSIGN
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
142 FUNC2(copysign, copysign,
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
143 "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
144 #endif
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
145
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
146 FUNC1(exp, exp,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
147 "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
148 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
149 "fabs(x)\n\nReturn the absolute value of the float x.")
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
150
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
151 static PyObject * math_floor(PyObject *self, PyObject *number) {
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
152 static PyObject *floor_str = NULL;
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
153 PyObject *method;
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
154
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
155 if (floor_str == NULL) {
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
156 floor_str = PyString_FromString("__floor__");
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
157 if (floor_str == NULL)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
158 return NULL;
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
159 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
160
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
161 method = _PyType_Lookup(Py_Type(number), floor_str);
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
162 if (method == NULL)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
163 return math_1(number, floor);
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
164 else
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
165 return PyObject_CallFunction(method, "O", number);
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
166 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
167
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
168 PyDoc_STRVAR(math_floor_doc,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
169 "floor(x)\n\nReturn the floor of x as a float.\n"
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
170 "This is the largest integral value <= x.");
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 40362
diff changeset
171
18066
bde1cf5740ec annoying whitespace inconsistency
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 15181
diff changeset
172 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
173 "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
174 " x % y may differ.")
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
175 FUNC2(hypot, hypot,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
176 "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
177 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
178 "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
179 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
180 "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
181 FUNC1(sinh, sinh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
182 "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
183 FUNC1(sqrt, sqrt,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
184 "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
185 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
186 "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
187 FUNC1(tanh, tanh,
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
188 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
190 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
191 math_frexp(PyObject *self, PyObject *arg)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
192 {
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
193 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
194 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
195 if (x == -1.0 && PyErr_Occurred())
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
196 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
197 errno = 0;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
198 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
199 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
200 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
201 return NULL;
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
202 else
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
203 return Py_BuildValue("(di)", x, i);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
204 }
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
205
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
206 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
207 "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
208 "\n"
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
209 "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
210 "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
211 "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
212
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
213 static PyObject *
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
214 math_ldexp(PyObject *self, PyObject *args)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
215 {
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
216 double x;
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
217 int exp;
13326
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
218 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
219 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
220 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4780
diff changeset
221 PyFPE_START_PROTECT("ldexp", return 0)
12618
cc4350b1b91f Trent Mick:
Guido van Rossum <guido@python.org>
parents: 9608
diff changeset
222 x = ldexp(x, exp);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
223 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
224 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
225 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
226 return NULL;
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
227 else
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
228 return PyFloat_FromDouble(x);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
229 }
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
230
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
231 PyDoc_STRVAR(math_ldexp_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
232 "ldexp(x, i) -> x * (2**i)");
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
233
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
234 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
235 math_modf(PyObject *self, PyObject *arg)
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
236 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
237 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
238 if (x == -1.0 && PyErr_Occurred())
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
239 return NULL;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
240 errno = 0;
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
241 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
242 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
243 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
244 return NULL;
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
245 else
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
246 return Py_BuildValue("(dd)", x, y);
407
9b3d557665a9 Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents: 320
diff changeset
247 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
249 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
250 "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
251 "\n"
b4521436e8f8 Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents: 19048
diff changeset
252 "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
253 "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
254
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
255 /* 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
256 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
257 "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
258 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
259 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
260 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
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
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
263 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
264 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
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 /* 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
267 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
268 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
269 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
270 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
271 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
272 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
273 "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
274 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
275 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
276 /* Value is ~= x * 2**(e*SHIFT), so the log ~=
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
277 log(x) + log(2) * e * SHIFT.
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
278 CAUTION: e*SHIFT may overflow using int arithmetic,
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
279 so force use of double. */
19716
fb2d83aaff68 loghelper(): Try to nudge the compiler into doing mults in an order that
Tim Peters <tim.peters@gmail.com>
parents: 19713
diff changeset
280 x = func(x) + (e * (double)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
281 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
282 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
283
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
284 /* 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
285 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
286 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
287
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
288 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
289 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
290 {
26661
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
291 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
292 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
293 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
294 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
295
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26661
diff changeset
296 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
297 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
298
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
299 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
300 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
301 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
302
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
303 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
304 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
305 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
306 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
307 }
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
308
b12e326b4ea2 Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
309 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
310 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
311 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
312 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
313 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
314
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
315 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
316 "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
317 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
318
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
319 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
320 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
321 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
322 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
323 }
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19712
diff changeset
324
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
325 PyDoc_STRVAR(math_log10_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
326 "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
327
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
328 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
329 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
330
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
331 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
332 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
333 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
334 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
335 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
336 return NULL;
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
337 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
338 }
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
339
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
340 PyDoc_STRVAR(math_degrees_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
341 "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
342
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
343 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
344 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
345 {
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
346 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
347 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
348 return NULL;
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
349 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
350 }
a4d675b8b53c Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents: 19758
diff changeset
351
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
352 PyDoc_STRVAR(math_radians_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
353 "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
354
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
355 static PyObject *
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
356 math_isnan(PyObject *self, PyObject *arg)
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
357 {
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
358 double x = PyFloat_AsDouble(arg);
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
359 if (x == -1.0 && PyErr_Occurred())
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
360 return NULL;
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
361 return PyBool_FromLong((long)Py_IS_NAN(x));
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
362 }
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
363
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
364 PyDoc_STRVAR(math_isnan_doc,
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
365 "isnan(x) -> bool\n\
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
366 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
367
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
368 static PyObject *
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
369 math_isinf(PyObject *self, PyObject *arg)
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
370 {
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
371 double x = PyFloat_AsDouble(arg);
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
372 if (x == -1.0 && PyErr_Occurred())
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
373 return NULL;
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
374 return PyBool_FromLong((long)Py_IS_INFINITY(x));
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
375 }
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
376
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
377 PyDoc_STRVAR(math_isinf_doc,
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
378 "isinf(x) -> bool\n\
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
379 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
380
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
381
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
382 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
383 {"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
384 {"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
385 {"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
386 {"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
387 {"ceil", math_ceil, METH_O, math_ceil_doc},
44453
cba9ff760430 Fixed math.copysign() on Windows
Christian Heimes <christian@cheimes.de>
parents: 44440
diff changeset
388 #ifdef HAVE_COPYSIGN
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
389 {"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
390 #endif
40362
271133981e63 Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38491
diff changeset
391 {"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
392 {"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
393 {"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
394 {"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
395 {"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
396 {"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
397 {"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
398 {"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
399 {"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
400 {"isinf", math_isinf, METH_O, math_isinf_doc},
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
401 {"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
402 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
24717405dd89 ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents: 13309
diff changeset
403 {"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
404 {"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
405 {"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
406 {"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
407 {"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
408 {"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
409 {"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
410 {"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
411 {"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
412 {"tanh", math_tanh, METH_O, math_tanh_doc},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
416
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23427
diff changeset
417 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
418 "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
419 "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
420
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
421 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13331
diff changeset
422 initmath(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 {
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
424 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
425
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
426 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
427 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34872
diff changeset
428 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
429 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
430 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
431 goto finally;
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
432
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
433 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
434 goto finally;
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
435 if (PyDict_SetItemString(d, "pi", v) < 0)
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
436 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
437 Py_DECREF(v);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
438
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
439 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
440 goto finally;
4765
f8b36c0ec114 Correct 1-char typo / syntax error.
Guido van Rossum <guido@python.org>
parents: 4752
diff changeset
441 if (PyDict_SetItemString(d, "e", v) < 0)
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
442 goto finally;
4635
49745ddb4f69 Renamed
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
443 Py_DECREF(v);
4752
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
444
7367fb107108 Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents: 4635
diff changeset
445 finally:
14346
5397fdf88ec2 Do the absolute minimal amount of modifications to eradicate
Barry Warsaw <barry@python.org>
parents: 13912
diff changeset
446 return;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 }