1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Math module -- standard C math library functions, pi and e */
17 extern double fmod (double, double);
18 extern double frexp (double, int *);
19 extern double ldexp (double, int);
20 extern double modf (double, double *);
26 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
31 #define CHECK(x) if (errno != 0) ; \
32 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
35 #define CHECK(x) /* Don't know how to check */
42 PyErr_SetString(PyExc_ValueError
, "math domain error");
43 else if (errno
== ERANGE
)
44 PyErr_SetString(PyExc_OverflowError
, "math range error");
46 /* Unexpected math error */
47 PyErr_SetFromErrno(PyExc_ValueError
);
52 math_1(PyObject
*args
, double (*func
) (double), char *argsfmt
)
55 if (! PyArg_ParseTuple(args
, argsfmt
, &x
))
58 PyFPE_START_PROTECT("in math_1", return 0)
65 return PyFloat_FromDouble(x
);
69 math_2(PyObject
*args
, double (*func
) (double, double), char *argsfmt
)
72 if (! PyArg_ParseTuple(args
, argsfmt
, &x
, &y
))
75 PyFPE_START_PROTECT("in math_2", return 0)
82 return PyFloat_FromDouble(x
);
85 #define FUNC1(funcname, func, docstring) \
86 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
87 return math_1(args, func, "d:" #funcname); \
89 static char math_##funcname##_doc [] = docstring;
91 #define FUNC2(funcname, func, docstring) \
92 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
93 return math_2(args, func, "dd:" #funcname); \
95 static char math_##funcname##_doc [] = docstring;
98 "acos(x)\n\nReturn the arc cosine of x.")
100 "asin(x)\n\nReturn the arc sine of x.")
102 "atan(x)\n\nReturn the arc tangent of x.")
104 "atan2(y, x)\n\nReturn atan(y/x).")
106 "ceil(x)\n\nReturn the ceiling of x as a real.")
108 "cos(x)\n\nReturn the cosine of x.")
110 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
112 "exp(x)\n\nReturn e raised to the power of x.")
114 "fabs(x)\n\nReturn the absolute value of the real x.")
116 "floor(x)\n\nReturn the floor of x as a real.")
118 "fmod(x,y)\n\nReturn x % y.")
120 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
122 "log(x)\n\nReturn the natural logarithm of x.")
124 "log10(x)\n\nReturn the base-10 logarithm of x.")
125 #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
127 "pow(x,y)\n\nReturn x**y.")
130 "pow(x,y)\n\nReturn x**y.")
133 "sin(x)\n\nReturn the sine of x.")
135 "sinh(x)\n\nReturn the hyperbolic sine of x.")
137 "sqrt(x)\n\nReturn the square root of x.")
139 "tan(x)\n\nReturn the tangent of x.")
141 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
145 math_frexp(PyObject
*self
, PyObject
*args
)
149 if (! PyArg_ParseTuple(args
, "d:frexp", &x
))
156 return Py_BuildValue("(di)", x
, i
);
159 static char math_frexp_doc
[] =
162 Return the mantissa and exponent of x, as pair (m, e).\n\
163 m is a float and e is an int, such that x = m * 2.**e.\n\
164 If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
168 math_ldexp(PyObject
*self
, PyObject
*args
)
172 if (! PyArg_ParseTuple(args
, "di:ldexp", &x
, &exp
))
175 PyFPE_START_PROTECT("ldexp", return 0)
182 return PyFloat_FromDouble(x
);
185 static char math_ldexp_doc
[] =
192 math_modf(PyObject
*self
, PyObject
*args
)
195 if (! PyArg_ParseTuple(args
, "d:modf", &x
))
198 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
210 return Py_BuildValue("(dd)", x
, y
);
213 static char math_modf_doc
[] =
216 Return the fractional and integer parts of x. Both results carry the sign\n\
217 of x. The integer part is returned as a real.";
220 static PyMethodDef math_methods
[] = {
221 {"acos", math_acos
, METH_VARARGS
, math_acos_doc
},
222 {"asin", math_asin
, METH_VARARGS
, math_asin_doc
},
223 {"atan", math_atan
, METH_VARARGS
, math_atan_doc
},
224 {"atan2", math_atan2
, METH_VARARGS
, math_atan2_doc
},
225 {"ceil", math_ceil
, METH_VARARGS
, math_ceil_doc
},
226 {"cos", math_cos
, METH_VARARGS
, math_cos_doc
},
227 {"cosh", math_cosh
, METH_VARARGS
, math_cosh_doc
},
228 {"exp", math_exp
, METH_VARARGS
, math_exp_doc
},
229 {"fabs", math_fabs
, METH_VARARGS
, math_fabs_doc
},
230 {"floor", math_floor
, METH_VARARGS
, math_floor_doc
},
231 {"fmod", math_fmod
, METH_VARARGS
, math_fmod_doc
},
232 {"frexp", math_frexp
, METH_VARARGS
, math_frexp_doc
},
233 {"hypot", math_hypot
, METH_VARARGS
, math_hypot_doc
},
234 {"ldexp", math_ldexp
, METH_VARARGS
, math_ldexp_doc
},
235 {"log", math_log
, METH_VARARGS
, math_log_doc
},
236 {"log10", math_log10
, METH_VARARGS
, math_log10_doc
},
237 {"modf", math_modf
, METH_VARARGS
, math_modf_doc
},
238 {"pow", math_pow
, METH_VARARGS
, math_pow_doc
},
239 {"sin", math_sin
, METH_VARARGS
, math_sin_doc
},
240 {"sinh", math_sinh
, METH_VARARGS
, math_sinh_doc
},
241 {"sqrt", math_sqrt
, METH_VARARGS
, math_sqrt_doc
},
242 {"tan", math_tan
, METH_VARARGS
, math_tan_doc
},
243 {"tanh", math_tanh
, METH_VARARGS
, math_tanh_doc
},
244 {NULL
, NULL
} /* sentinel */
248 static char module_doc
[] =
249 "This module is always available. It provides access to the\n\
250 mathematical functions defined by the C standard.";
257 m
= Py_InitModule3("math", math_methods
, module_doc
);
258 d
= PyModule_GetDict(m
);
260 if (!(v
= PyFloat_FromDouble(atan(1.0) * 4.0)))
262 if (PyDict_SetItemString(d
, "pi", v
) < 0)
266 if (!(v
= PyFloat_FromDouble(exp(1.0))))
268 if (PyDict_SetItemString(d
, "e", v
) < 0)
274 Py_FatalError("can't initialize math module");