blob: c313f35a22ccc5cd15a4007b0e0208bf894e97bd [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Math module -- standard C math library functions, pi and e */
3
Barry Warsaw8b43b191996-12-09 22:32:36 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum7081cf51996-05-23 22:56:19 +00006#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +00007#ifndef __STDC__
Fred Drake40c48682000-07-03 18:11:56 +00008extern double fmod (double, double);
9extern double frexp (double, int *);
10extern double ldexp (double, int);
11extern double modf (double, double *);
Guido van Rossum7081cf51996-05-23 22:56:19 +000012#endif /* __STDC__ */
13#endif /* _MSC_VER */
14
Guido van Rossumb9418681995-01-12 11:28:16 +000015
Guido van Rossum9575a441993-04-07 14:06:14 +000016#ifdef i860
17/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
18#undef HUGE_VAL
19#endif
20
Guido van Rossum8832b621991-12-16 15:44:24 +000021#ifdef HUGE_VAL
22#define CHECK(x) if (errno != 0) ; \
23 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
24 else errno = ERANGE
25#else
26#define CHECK(x) /* Don't know how to check */
27#endif
28
Barry Warsaw8b43b191996-12-09 22:32:36 +000029static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000030math_error(void)
Guido van Rossum8832b621991-12-16 15:44:24 +000031{
32 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000033 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum8832b621991-12-16 15:44:24 +000034 else if (errno == ERANGE)
Barry Warsaw8b43b191996-12-09 22:32:36 +000035 PyErr_SetString(PyExc_OverflowError, "math range error");
Guido van Rossum8832b621991-12-16 15:44:24 +000036 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000037 /* Unexpected math error */
38 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum8832b621991-12-16 15:44:24 +000039 return NULL;
40}
41
Barry Warsaw8b43b191996-12-09 22:32:36 +000042static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000043math_1(PyObject *args, double (*func) (double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044{
45 double x;
Fred Drake40c48682000-07-03 18:11:56 +000046 if (! PyArg_ParseTuple(args, argsfmt, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047 return NULL;
48 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000049 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000051 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000052 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000054 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000056 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
Barry Warsaw8b43b191996-12-09 22:32:36 +000059static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000060math_2(PyObject *args, double (*func) (double, double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
62 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +000063 if (! PyArg_ParseTuple(args, argsfmt, &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 return NULL;
65 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000066 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000068 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000069 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000071 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000073 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
Fred Drake40c48682000-07-03 18:11:56 +000076#define FUNC1(funcname, func, docstring) \
77 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
78 return math_1(args, func, "d:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000079 }\
Fred Drake40c48682000-07-03 18:11:56 +000080 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081
Fred Drake40c48682000-07-03 18:11:56 +000082#define FUNC2(funcname, func, docstring) \
83 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
84 return math_2(args, func, "dd:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000085 }\
Fred Drake40c48682000-07-03 18:11:56 +000086 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087
Fred Drake40c48682000-07-03 18:11:56 +000088FUNC1(acos, acos,
Guido van Rossumc6e22901998-12-04 19:26:43 +000089 "acos(x)\n\nReturn the arc cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +000090FUNC1(asin, asin,
Guido van Rossumc6e22901998-12-04 19:26:43 +000091 "asin(x)\n\nReturn the arc sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +000092FUNC1(atan, atan,
Guido van Rossumc6e22901998-12-04 19:26:43 +000093 "atan(x)\n\nReturn the arc tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +000094FUNC2(atan2, atan2,
Guido van Rossumb057dd81998-12-08 16:27:10 +000095 "atan2(y, x)\n\nReturn atan(y/x).")
Fred Drake40c48682000-07-03 18:11:56 +000096FUNC1(ceil, ceil,
Guido van Rossumc6e22901998-12-04 19:26:43 +000097 "ceil(x)\n\nReturn the ceiling of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +000098FUNC1(cos, cos,
Guido van Rossumc6e22901998-12-04 19:26:43 +000099 "cos(x)\n\nReturn the cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000100FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000101 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000102FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000103 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000104FUNC1(fabs, fabs,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000105 "fabs(x)\n\nReturn the absolute value of the real x.")
Fred Drake40c48682000-07-03 18:11:56 +0000106FUNC1(floor, floor,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000107 "floor(x)\n\nReturn the floor of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000108 FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000109 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
110 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000111FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000112 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000113FUNC1(log, log,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000114 "log(x)\n\nReturn the natural logarithm of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000115FUNC1(log10, log10,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000116 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000117#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Fred Drake40c48682000-07-03 18:11:56 +0000118FUNC2(pow, power,
119 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000120#else
Fred Drake40c48682000-07-03 18:11:56 +0000121FUNC2(pow, pow,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000122 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000123#endif
Fred Drake40c48682000-07-03 18:11:56 +0000124FUNC1(sin, sin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000125 "sin(x)\n\nReturn the sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000126FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000127 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000128FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000129 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000130FUNC1(tan, tan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000131 "tan(x)\n\nReturn the tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000132FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000133 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135
Barry Warsaw8b43b191996-12-09 22:32:36 +0000136static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000137math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000138{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000139 double x;
140 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000141 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000142 return NULL;
143 errno = 0;
144 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000145 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000146 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000147 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000148 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000149}
150
Guido van Rossumc6e22901998-12-04 19:26:43 +0000151static char math_frexp_doc [] =
152"frexp(x)\n\
153\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000154Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000155m is a float and e is an int, such that x = m * 2.**e.\n\
156If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000157
158
Barry Warsaw8b43b191996-12-09 22:32:36 +0000159static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000160math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000161{
Guido van Rossumc5545052000-05-08 14:29:38 +0000162 double x;
163 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000164 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000165 return NULL;
166 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000167 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000168 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000169 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000170 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000171 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000172 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000173 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000174 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000175}
176
Guido van Rossumc6e22901998-12-04 19:26:43 +0000177static char math_ldexp_doc [] =
178"ldexp_doc(x, i)\n\
179\n\
180Return x * (2**i).";
181
182
Barry Warsaw8b43b191996-12-09 22:32:36 +0000183static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000184math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000185{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000186 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000187 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000188 return NULL;
189 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000191 {
192 extended e;
193 x = modf(x, &e);
194 y = e;
195 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000199 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000200 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000201 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000202 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000203}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204
Guido van Rossumc6e22901998-12-04 19:26:43 +0000205static char math_modf_doc [] =
206"modf(x)\n\
207\n\
208Return the fractional and integer parts of x. Both results carry the sign\n\
209of x. The integer part is returned as a real.";
210
211
Barry Warsaw8b43b191996-12-09 22:32:36 +0000212static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000213 {"acos", math_acos, METH_VARARGS, math_acos_doc},
214 {"asin", math_asin, METH_VARARGS, math_asin_doc},
215 {"atan", math_atan, METH_VARARGS, math_atan_doc},
216 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
217 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
218 {"cos", math_cos, METH_VARARGS, math_cos_doc},
219 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
220 {"exp", math_exp, METH_VARARGS, math_exp_doc},
221 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
222 {"floor", math_floor, METH_VARARGS, math_floor_doc},
223 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
224 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
225 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
226 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
227 {"log", math_log, METH_VARARGS, math_log_doc},
228 {"log10", math_log10, METH_VARARGS, math_log10_doc},
229 {"modf", math_modf, METH_VARARGS, math_modf_doc},
230 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000231 {"sin", math_sin, METH_VARARGS, math_sin_doc},
232 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
233 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
234 {"tan", math_tan, METH_VARARGS, math_tan_doc},
235 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 {NULL, NULL} /* sentinel */
237};
238
Guido van Rossumc6e22901998-12-04 19:26:43 +0000239
240static char module_doc [] =
241"This module is always available. It provides access to the\n\
242mathematical functions defined by the C standard.";
243
Guido van Rossum3886bb61998-12-04 18:50:17 +0000244DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000245initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000247 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000248
Guido van Rossumc6e22901998-12-04 19:26:43 +0000249 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000250 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000251
252 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
253 goto finally;
254 if (PyDict_SetItemString(d, "pi", v) < 0)
255 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000256 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000257
258 if (!(v = PyFloat_FromDouble(exp(1.0))))
259 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000260 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000261 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000262 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000263
264 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000265 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266}