blob: fcd88ad331c0a4d1b43ee4b032f4c2d4e485ee0a [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,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000109 "fmod(x,y)\n\nReturn x % y.")
Fred Drake40c48682000-07-03 18:11:56 +0000110FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000111 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000112FUNC1(log, log,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000113 "log(x)\n\nReturn the natural logarithm of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000114FUNC1(log10, log10,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000115 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000116#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Fred Drake40c48682000-07-03 18:11:56 +0000117FUNC2(pow, power,
118 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000119#else
Fred Drake40c48682000-07-03 18:11:56 +0000120FUNC2(pow, pow,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000121 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000122#endif
Fred Drake40c48682000-07-03 18:11:56 +0000123FUNC1(sin, sin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000124 "sin(x)\n\nReturn the sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000125FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000126 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000127FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000128 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000129FUNC1(tan, tan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000130 "tan(x)\n\nReturn the tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000131FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000132 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134
Barry Warsaw8b43b191996-12-09 22:32:36 +0000135static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000136math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000137{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000138 double x;
139 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000140 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000141 return NULL;
142 errno = 0;
143 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000144 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000145 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000146 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000147 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000148}
149
Guido van Rossumc6e22901998-12-04 19:26:43 +0000150static char math_frexp_doc [] =
151"frexp(x)\n\
152\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000153Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000154m is a float and e is an int, such that x = m * 2.**e.\n\
155If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000156
157
Barry Warsaw8b43b191996-12-09 22:32:36 +0000158static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000159math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000160{
Guido van Rossumc5545052000-05-08 14:29:38 +0000161 double x;
162 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000163 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000164 return NULL;
165 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000166 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000167 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000168 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000169 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000170 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000171 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000172 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000173 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000174}
175
Guido van Rossumc6e22901998-12-04 19:26:43 +0000176static char math_ldexp_doc [] =
177"ldexp_doc(x, i)\n\
178\n\
179Return x * (2**i).";
180
181
Barry Warsaw8b43b191996-12-09 22:32:36 +0000182static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000183math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000184{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000185 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000186 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000187 return NULL;
188 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000190 {
191 extended e;
192 x = modf(x, &e);
193 y = e;
194 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000196 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000198 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000199 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000200 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000201 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000202}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203
Guido van Rossumc6e22901998-12-04 19:26:43 +0000204static char math_modf_doc [] =
205"modf(x)\n\
206\n\
207Return the fractional and integer parts of x. Both results carry the sign\n\
208of x. The integer part is returned as a real.";
209
210
Barry Warsaw8b43b191996-12-09 22:32:36 +0000211static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000212 {"acos", math_acos, METH_VARARGS, math_acos_doc},
213 {"asin", math_asin, METH_VARARGS, math_asin_doc},
214 {"atan", math_atan, METH_VARARGS, math_atan_doc},
215 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
216 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
217 {"cos", math_cos, METH_VARARGS, math_cos_doc},
218 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
219 {"exp", math_exp, METH_VARARGS, math_exp_doc},
220 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
221 {"floor", math_floor, METH_VARARGS, math_floor_doc},
222 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
223 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
224 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
225 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
226 {"log", math_log, METH_VARARGS, math_log_doc},
227 {"log10", math_log10, METH_VARARGS, math_log10_doc},
228 {"modf", math_modf, METH_VARARGS, math_modf_doc},
229 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000230 {"sin", math_sin, METH_VARARGS, math_sin_doc},
231 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
232 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
233 {"tan", math_tan, METH_VARARGS, math_tan_doc},
234 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235 {NULL, NULL} /* sentinel */
236};
237
Guido van Rossumc6e22901998-12-04 19:26:43 +0000238
239static char module_doc [] =
240"This module is always available. It provides access to the\n\
241mathematical functions defined by the C standard.";
242
Guido van Rossum3886bb61998-12-04 18:50:17 +0000243DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000244initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000246 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000247
Guido van Rossumc6e22901998-12-04 19:26:43 +0000248 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000249 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000250
251 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
252 goto finally;
253 if (PyDict_SetItemString(d, "pi", v) < 0)
254 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000255 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000256
257 if (!(v = PyFloat_FromDouble(exp(1.0))))
258 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000259 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000260 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000261 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000262
263 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000264 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}