Mercurial > cpython-lru_cache-weakref
annotate Modules/mathmodule.c @ 44239:369a4e06fd30
Added another test case for kwonly methods
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Sat, 08 Dec 2007 22:32:47 +0000 |
| parents | 1c98b445d847 |
| children | 86a2679dca46 |
| rev | line source |
|---|---|
| 6 | 1 /* Math module -- standard C math library functions, pi and e */ |
| 2 | |
| 4635 | 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 | 5 |
| 3597 | 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 | 12 #endif /* __STDC__ */ |
| 13 #endif /* _MSC_VER */ | |
| 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 | 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 | 45 /* Unexpected math error */ |
| 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 | 50 static PyObject * |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
51 math_1(PyObject *arg, double (*func) (double)) |
| 6 | 52 { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
53 double x = PyFloat_AsDouble(arg); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
54 if (x == -1.0 && PyErr_Occurred()) |
| 6 | 55 return NULL; |
| 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 | 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 | 63 else |
| 4635 | 64 return PyFloat_FromDouble(x); |
| 6 | 65 } |
| 66 | |
| 4635 | 67 static PyObject * |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
68 math_2(PyObject *args, double (*func) (double, double), char *funcname) |
| 6 | 69 { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
70 PyObject *ox, *oy; |
| 6 | 71 double x, y; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
72 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
73 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
74 x = PyFloat_AsDouble(ox); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
75 y = PyFloat_AsDouble(oy); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
76 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 6 | 77 return NULL; |
| 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 | 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 | 85 else |
| 4635 | 86 return PyFloat_FromDouble(x); |
| 6 | 87 } |
| 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) { \ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
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 | 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) { \ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
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 | 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.") |
|
42754
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
110 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
111 static PyObject * math_ceil(PyObject *self, PyObject *number) { |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
112 static PyObject *ceil_str = NULL; |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
113 PyObject *method; |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
114 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
115 if (ceil_str == NULL) { |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
116 ceil_str = PyUnicode_FromString("__ceil__"); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
117 if (ceil_str == NULL) |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
118 return NULL; |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
119 } |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
120 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
121 method = _PyType_Lookup(Py_Type(number), ceil_str); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
122 if (method == NULL) |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
123 return math_1(number, ceil); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
124 else |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
125 return PyObject_CallFunction(method, "O", number); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
126 } |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
127 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
128 PyDoc_STRVAR(math_ceil_doc, |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
129 "ceil(x)\n\nReturn the ceiling of x as a float.\n" |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
130 "This is the smallest integral value >= x."); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
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.") |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
136 FUNC1(exp, exp, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
137 "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
|
138 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
|
139 "fabs(x)\n\nReturn the absolute value of the float x.") |
|
42754
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
140 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
141 static PyObject * math_floor(PyObject *self, PyObject *number) { |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
142 static PyObject *floor_str = NULL; |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
143 PyObject *method; |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
144 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
145 if (floor_str == NULL) { |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
146 floor_str = PyUnicode_FromString("__floor__"); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
147 if (floor_str == NULL) |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
148 return NULL; |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
149 } |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
150 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
151 method = _PyType_Lookup(Py_Type(number), floor_str); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
152 if (method == NULL) |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
153 return math_1(number, floor); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
154 else |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
155 return PyObject_CallFunction(method, "O", number); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
156 } |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
157 |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
158 PyDoc_STRVAR(math_floor_doc, |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
159 "floor(x)\n\nReturn the floor of x as a float.\n" |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
160 "This is the largest integral value <= x."); |
|
1c98b445d847
Fix math.ceil() and math.floor() to fall back to __ceil__ and __floor__
Guido van Rossum <guido@python.org>
parents:
42334
diff
changeset
|
161 |
|
18066
bde1cf5740ec
annoying whitespace inconsistency
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
15181
diff
changeset
|
162 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
|
163 "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
|
164 " x % y may differ.") |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
165 FUNC2(hypot, hypot, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
166 "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
|
167 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
|
168 "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
|
169 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
|
170 "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
|
171 FUNC1(sinh, sinh, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
172 "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
|
173 FUNC1(sqrt, sqrt, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
174 "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
|
175 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
|
176 "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
|
177 FUNC1(tanh, tanh, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
178 "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
| 6 | 179 |
| 4635 | 180 static PyObject * |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
181 math_frexp(PyObject *self, PyObject *arg) |
|
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 int i; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
184 double x = PyFloat_AsDouble(arg); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
185 if (x == -1.0 && PyErr_Occurred()) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
186 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
187 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
188 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
|
189 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
|
190 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
|
191 return NULL; |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
192 else |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
193 return Py_BuildValue("(di)", x, i); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
194 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
195 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
196 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
|
197 "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
|
198 "\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
199 "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
|
200 "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
|
201 "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
|
202 |
| 4635 | 203 static PyObject * |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
204 math_ldexp(PyObject *self, PyObject *args) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
205 { |
| 12618 | 206 double x; |
| 207 int exp; | |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
208 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
|
209 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
210 errno = 0; |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4780
diff
changeset
|
211 PyFPE_START_PROTECT("ldexp", return 0) |
| 12618 | 212 x = ldexp(x, exp); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 return NULL; |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
217 else |
| 4635 | 218 return PyFloat_FromDouble(x); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
219 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
220 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
221 PyDoc_STRVAR(math_ldexp_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
222 "ldexp(x, i) -> x * (2**i)"); |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
223 |
| 4635 | 224 static PyObject * |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
225 math_modf(PyObject *self, PyObject *arg) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
226 { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
227 double y, x = PyFloat_AsDouble(arg); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
228 if (x == -1.0 && PyErr_Occurred()) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
229 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
230 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
231 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
|
232 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
|
233 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
|
234 return NULL; |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
235 else |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
236 return Py_BuildValue("(dd)", x, y); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
237 } |
| 6 | 238 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
239 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
|
240 "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
|
241 "\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
242 "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
|
243 "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
|
244 |
|
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
|
245 /* 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
|
246 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
|
247 "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
|
248 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
|
249 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
|
250 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
|
251 */ |
|
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 |
|
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 static PyObject* |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
254 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
|
255 { |
|
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 /* 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 "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
|
264 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
|
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 /* 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
|
267 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
|
268 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
|
269 so force use of double. */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
40450
diff
changeset
|
270 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
|
271 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
|
272 } |
|
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 |
|
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 /* Else let libm handle it by itself. */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
275 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
|
276 } |
|
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 |
|
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 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
|
279 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
|
280 { |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
281 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
|
282 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
|
283 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
|
284 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
|
285 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26661
diff
changeset
|
286 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
|
287 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
|
288 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
289 num = loghelper(arg, log, "log"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
290 if (num == NULL || base == NULL) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
291 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
|
292 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
293 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
|
294 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
|
295 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
|
296 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
|
297 } |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
298 |
|
37191
4bfe8afe8f65
Get rid of remnants of integer division
Neal Norwitz <nnorwitz@gmail.com>
parents:
36324
diff
changeset
|
299 ans = PyNumber_TrueDivide(num, den); |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
300 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
|
301 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
|
302 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
|
303 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
304 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
305 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
|
306 "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
|
307 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
|
308 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
309 static PyObject * |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
310 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
|
311 { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
312 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
|
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_log10_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
316 "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
|
317 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
318 /* XXX(nnorwitz): Should we use the platform M_PI or something more accurate |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
319 like: 3.14159265358979323846264338327950288 */ |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
320 static const double degToRad = 3.141592653589793238462643383 / 180.0; |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
321 |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
322 static PyObject * |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
323 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
|
324 { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
325 double x = PyFloat_AsDouble(arg); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
326 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
|
327 return NULL; |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
328 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
|
329 } |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
330 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
331 PyDoc_STRVAR(math_degrees_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
332 "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
|
333 |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
334 static PyObject * |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
335 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
|
336 { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
337 double x = PyFloat_AsDouble(arg); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
338 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
|
339 return NULL; |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
340 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
|
341 } |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
342 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
343 PyDoc_STRVAR(math_radians_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
344 "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
|
345 |
| 4635 | 346 static PyMethodDef math_methods[] = { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
347 {"acos", math_acos, METH_O, math_acos_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
348 {"asin", math_asin, METH_O, math_asin_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
349 {"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
|
350 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
351 {"ceil", math_ceil, METH_O, math_ceil_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
352 {"cos", math_cos, METH_O, math_cos_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
353 {"cosh", math_cosh, METH_O, math_cosh_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
354 {"degrees", math_degrees, METH_O, math_degrees_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
355 {"exp", math_exp, METH_O, math_exp_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
356 {"fabs", math_fabs, METH_O, math_fabs_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
357 {"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
|
358 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
359 {"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
|
360 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
|
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
361 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
|
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
362 {"log", math_log, METH_VARARGS, math_log_doc}, |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
363 {"log10", math_log10, METH_O, math_log10_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
364 {"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
|
365 {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
366 {"radians", math_radians, METH_O, math_radians_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
367 {"sin", math_sin, METH_O, math_sin_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
368 {"sinh", math_sinh, METH_O, math_sinh_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
369 {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
370 {"tan", math_tan, METH_O, math_tan_doc}, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
371 {"tanh", math_tanh, METH_O, math_tanh_doc}, |
| 6 | 372 {NULL, NULL} /* sentinel */ |
| 373 }; | |
| 374 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
375 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
376 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
|
377 "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
|
378 "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
|
379 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
380 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13331
diff
changeset
|
381 initmath(void) |
| 6 | 382 { |
| 4635 | 383 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
|
384 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
385 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
|
386 if (m == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
34872
diff
changeset
|
387 goto finally; |
| 4635 | 388 d = PyModule_GetDict(m); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
389 if (d == NULL) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
390 goto finally; |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
391 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
392 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0))) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
393 goto finally; |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
394 if (PyDict_SetItemString(d, "pi", v) < 0) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
395 goto finally; |
| 4635 | 396 Py_DECREF(v); |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
397 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
398 if (!(v = PyFloat_FromDouble(exp(1.0)))) |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
399 goto finally; |
|
4765
f8b36c0ec114
Correct 1-char typo / syntax error.
Guido van Rossum <guido@python.org>
parents:
4752
diff
changeset
|
400 if (PyDict_SetItemString(d, "e", v) < 0) |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
401 goto finally; |
| 4635 | 402 Py_DECREF(v); |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
403 |
|
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
404 finally: |
|
14346
5397fdf88ec2
Do the absolute minimal amount of modifications to eradicate
Barry Warsaw <barry@python.org>
parents:
13912
diff
changeset
|
405 return; |
| 6 | 406 } |
