1 /* Math module -- standard C math library functions, pi and e */
4 #include "longintrepr.h" /* just for SHIFT */
8 extern double fmod (double, double);
9 extern double frexp (double, int *);
10 extern double ldexp (double, int);
11 extern double modf (double, double *);
15 /* Call is_error when errno != 0, and where x is the result libm
16 * returned. is_error will usually set up an exception and return
17 * true (1), but may return false (0) without setting up an exception.
22 int result
= 1; /* presumption of guilt */
23 assert(errno
); /* non-zero errno is a precondition for calling */
25 PyErr_SetString(PyExc_ValueError
, "math domain error");
27 else if (errno
== ERANGE
) {
28 /* ANSI C generally requires libm functions to set ERANGE
29 * on overflow, but also generally *allows* them to set
30 * ERANGE on underflow too. There's no consistency about
31 * the latter across platforms.
32 * Alas, C99 never requires that errno be set.
33 * Here we suppress the underflow errors (libm functions
34 * should return a zero on underflow, and +- HUGE_VAL on
35 * overflow, so testing the result for zero suffices to
36 * distinguish the cases).
39 PyErr_SetString(PyExc_OverflowError
,
45 /* Unexpected math error */
46 PyErr_SetFromErrno(PyExc_ValueError
);
51 math_1(PyObject
*arg
, double (*func
) (double))
53 double x
= PyFloat_AsDouble(arg
);
54 if (x
== -1.0 && PyErr_Occurred())
57 PyFPE_START_PROTECT("in math_1", return 0)
60 Py_SET_ERRNO_ON_MATH_ERROR(x
);
61 if (errno
&& is_error(x
))
64 return PyFloat_FromDouble(x
);
68 math_2(PyObject
*args
, double (*func
) (double, double), char *funcname
)
72 if (! PyArg_UnpackTuple(args
, funcname
, 2, 2, &ox
, &oy
))
74 x
= PyFloat_AsDouble(ox
);
75 y
= PyFloat_AsDouble(oy
);
76 if ((x
== -1.0 || y
== -1.0) && PyErr_Occurred())
79 PyFPE_START_PROTECT("in math_2", return 0)
82 Py_SET_ERRNO_ON_MATH_ERROR(x
);
83 if (errno
&& is_error(x
))
86 return PyFloat_FromDouble(x
);
89 #define FUNC1(funcname, func, docstring) \
90 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
91 return math_1(args, func); \
93 PyDoc_STRVAR(math_##funcname##_doc, docstring);
95 #define FUNC2(funcname, func, docstring) \
96 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
97 return math_2(args, func, #funcname); \
99 PyDoc_STRVAR(math_##funcname##_doc, docstring);
102 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
104 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
106 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
108 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
109 "Unlike atan(y/x), the signs of both x and y are considered.")
111 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
112 "This is the smallest integral value >= x.")
114 "cos(x)\n\nReturn the cosine of x (measured in radians).")
116 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
118 "exp(x)\n\nReturn e raised to the power of x.")
120 "fabs(x)\n\nReturn the absolute value of the float x.")
122 "floor(x)\n\nReturn the floor of x as a float.\n"
123 "This is the largest integral value <= x.")
125 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
126 " x % y may differ.")
128 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
130 "pow(x,y)\n\nReturn x**y (x to the power of y).")
132 "sin(x)\n\nReturn the sine of x (measured in radians).")
134 "sinh(x)\n\nReturn the hyperbolic sine of x.")
136 "sqrt(x)\n\nReturn the square root of x.")
138 "tan(x)\n\nReturn the tangent of x (measured in radians).")
140 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
143 math_frexp(PyObject
*self
, PyObject
*arg
)
146 double x
= PyFloat_AsDouble(arg
);
147 if (x
== -1.0 && PyErr_Occurred())
151 Py_SET_ERRNO_ON_MATH_ERROR(x
);
152 if (errno
&& is_error(x
))
155 return Py_BuildValue("(di)", x
, i
);
158 PyDoc_STRVAR(math_frexp_doc
,
161 "Return the mantissa and exponent of x, as pair (m, e).\n"
162 "m is a float and e is an int, such that x = m * 2.**e.\n"
163 "If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.");
166 math_ldexp(PyObject
*self
, PyObject
*args
)
170 if (! PyArg_ParseTuple(args
, "di:ldexp", &x
, &exp
))
173 PyFPE_START_PROTECT("ldexp", return 0)
176 Py_SET_ERRNO_ON_MATH_ERROR(x
);
177 if (errno
&& is_error(x
))
180 return PyFloat_FromDouble(x
);
183 PyDoc_STRVAR(math_ldexp_doc
,
184 "ldexp(x, i) -> x * (2**i)");
187 math_modf(PyObject
*self
, PyObject
*arg
)
189 double y
, x
= PyFloat_AsDouble(arg
);
190 if (x
== -1.0 && PyErr_Occurred())
194 Py_SET_ERRNO_ON_MATH_ERROR(x
);
195 if (errno
&& is_error(x
))
198 return Py_BuildValue("(dd)", x
, y
);
201 PyDoc_STRVAR(math_modf_doc
,
204 "Return the fractional and integer parts of x. Both results carry the sign\n"
205 "of x. The integer part is returned as a real.");
207 /* A decent logarithm is easy to compute even for huge longs, but libm can't
208 do that by itself -- loghelper can. func is log or log10, and name is
209 "log" or "log10". Note that overflow isn't possible: a long can contain
210 no more than INT_MAX * SHIFT bits, so has value certainly less than
211 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
212 small enough to fit in an IEEE single. log and log10 are even smaller.
216 loghelper(PyObject
* arg
, double (*func
)(double), char *funcname
)
218 /* If it is long, do it ourselves. */
219 if (PyLong_Check(arg
)) {
222 x
= _PyLong_AsScaledDouble(arg
, &e
);
224 PyErr_SetString(PyExc_ValueError
,
225 "math domain error");
228 /* Value is ~= x * 2**(e*SHIFT), so the log ~=
229 log(x) + log(2) * e * SHIFT.
230 CAUTION: e*SHIFT may overflow using int arithmetic,
231 so force use of double. */
232 x
= func(x
) + (e
* (double)SHIFT
) * func(2.0);
233 return PyFloat_FromDouble(x
);
236 /* Else let libm handle it by itself. */
237 return math_1(arg
, func
);
241 math_log(PyObject
*self
, PyObject
*args
)
244 PyObject
*base
= NULL
;
248 if (!PyArg_UnpackTuple(args
, "log", 1, 2, &arg
, &base
))
251 num
= loghelper(arg
, log
, "log");
252 if (num
== NULL
|| base
== NULL
)
255 den
= loghelper(base
, log
, "log");
261 ans
= PyNumber_Divide(num
, den
);
267 PyDoc_STRVAR(math_log_doc
,
268 "log(x[, base]) -> the logarithm of x to the given base.\n\
269 If the base not specified, returns the natural logarithm (base e) of x.");
272 math_log10(PyObject
*self
, PyObject
*arg
)
274 return loghelper(arg
, log10
, "log10");
277 PyDoc_STRVAR(math_log10_doc
,
278 "log10(x) -> the base 10 logarithm of x.");
280 /* XXX(nnorwitz): Should we use the platform M_PI or something more accurate
281 like: 3.14159265358979323846264338327950288 */
282 static const double degToRad
= 3.141592653589793238462643383 / 180.0;
285 math_degrees(PyObject
*self
, PyObject
*arg
)
287 double x
= PyFloat_AsDouble(arg
);
288 if (x
== -1.0 && PyErr_Occurred())
290 return PyFloat_FromDouble(x
/ degToRad
);
293 PyDoc_STRVAR(math_degrees_doc
,
294 "degrees(x) -> converts angle x from radians to degrees");
297 math_radians(PyObject
*self
, PyObject
*arg
)
299 double x
= PyFloat_AsDouble(arg
);
300 if (x
== -1.0 && PyErr_Occurred())
302 return PyFloat_FromDouble(x
* degToRad
);
305 PyDoc_STRVAR(math_radians_doc
,
306 "radians(x) -> converts angle x from degrees to radians");
308 static PyMethodDef math_methods
[] = {
309 {"acos", math_acos
, METH_O
, math_acos_doc
},
310 {"asin", math_asin
, METH_O
, math_asin_doc
},
311 {"atan", math_atan
, METH_O
, math_atan_doc
},
312 {"atan2", math_atan2
, METH_VARARGS
, math_atan2_doc
},
313 {"ceil", math_ceil
, METH_O
, math_ceil_doc
},
314 {"cos", math_cos
, METH_O
, math_cos_doc
},
315 {"cosh", math_cosh
, METH_O
, math_cosh_doc
},
316 {"degrees", math_degrees
, METH_O
, math_degrees_doc
},
317 {"exp", math_exp
, METH_O
, math_exp_doc
},
318 {"fabs", math_fabs
, METH_O
, math_fabs_doc
},
319 {"floor", math_floor
, METH_O
, math_floor_doc
},
320 {"fmod", math_fmod
, METH_VARARGS
, math_fmod_doc
},
321 {"frexp", math_frexp
, METH_O
, math_frexp_doc
},
322 {"hypot", math_hypot
, METH_VARARGS
, math_hypot_doc
},
323 {"ldexp", math_ldexp
, METH_VARARGS
, math_ldexp_doc
},
324 {"log", math_log
, METH_VARARGS
, math_log_doc
},
325 {"log10", math_log10
, METH_O
, math_log10_doc
},
326 {"modf", math_modf
, METH_O
, math_modf_doc
},
327 {"pow", math_pow
, METH_VARARGS
, math_pow_doc
},
328 {"radians", math_radians
, METH_O
, math_radians_doc
},
329 {"sin", math_sin
, METH_O
, math_sin_doc
},
330 {"sinh", math_sinh
, METH_O
, math_sinh_doc
},
331 {"sqrt", math_sqrt
, METH_O
, math_sqrt_doc
},
332 {"tan", math_tan
, METH_O
, math_tan_doc
},
333 {"tanh", math_tanh
, METH_O
, math_tanh_doc
},
334 {NULL
, NULL
} /* sentinel */
338 PyDoc_STRVAR(module_doc
,
339 "This module is always available. It provides access to the\n"
340 "mathematical functions defined by the C standard.");
347 m
= Py_InitModule3("math", math_methods
, module_doc
);
350 d
= PyModule_GetDict(m
);
354 if (!(v
= PyFloat_FromDouble(atan(1.0) * 4.0)))
356 if (PyDict_SetItemString(d
, "pi", v
) < 0)
360 if (!(v
= PyFloat_FromDouble(exp(1.0))))
362 if (PyDict_SetItemString(d
, "e", v
) < 0)