blob: a20197a96ed915ddebba25486563e0ceadf41ed9 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Math module -- standard C math library functions, pi and e */
12
Barry Warsaw8b43b191996-12-09 22:32:36 +000013#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Guido van Rossum6964f731995-03-01 10:34:29 +000015#include "mymath.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016
Guido van Rossum7081cf51996-05-23 22:56:19 +000017#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +000018#ifndef __STDC__
Fred Drake40c48682000-07-03 18:11:56 +000019extern double fmod (double, double);
20extern double frexp (double, int *);
21extern double ldexp (double, int);
22extern double modf (double, double *);
Guido van Rossum7081cf51996-05-23 22:56:19 +000023#endif /* __STDC__ */
24#endif /* _MSC_VER */
25
Guido van Rossumb9418681995-01-12 11:28:16 +000026
Guido van Rossum9575a441993-04-07 14:06:14 +000027#ifdef i860
28/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
29#undef HUGE_VAL
30#endif
31
Guido van Rossum8832b621991-12-16 15:44:24 +000032#ifdef HUGE_VAL
33#define CHECK(x) if (errno != 0) ; \
34 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
35 else errno = ERANGE
36#else
37#define CHECK(x) /* Don't know how to check */
38#endif
39
Barry Warsaw8b43b191996-12-09 22:32:36 +000040static PyObject *
Guido van Rossum8832b621991-12-16 15:44:24 +000041math_error()
42{
43 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000044 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum8832b621991-12-16 15:44:24 +000045 else if (errno == ERANGE)
Barry Warsaw8b43b191996-12-09 22:32:36 +000046 PyErr_SetString(PyExc_OverflowError, "math range error");
Guido van Rossum8832b621991-12-16 15:44:24 +000047 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000048 /* Unexpected math error */
49 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum8832b621991-12-16 15:44:24 +000050 return NULL;
51}
52
Barry Warsaw8b43b191996-12-09 22:32:36 +000053static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000054math_1(PyObject *args, double (*func) (double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055{
56 double x;
Fred Drake40c48682000-07-03 18:11:56 +000057 if (! PyArg_ParseTuple(args, argsfmt, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 return NULL;
59 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000060 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000062 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000063 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000065 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000067 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Barry Warsaw8b43b191996-12-09 22:32:36 +000070static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000071math_2(PyObject *args, double (*func) (double, double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072{
73 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +000074 if (! PyArg_ParseTuple(args, argsfmt, &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 return NULL;
76 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000077 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000079 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000080 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000082 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000084 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085}
86
Fred Drake40c48682000-07-03 18:11:56 +000087#define FUNC1(funcname, func, docstring) \
88 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
89 return math_1(args, func, "d:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000090 }\
Fred Drake40c48682000-07-03 18:11:56 +000091 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092
Fred Drake40c48682000-07-03 18:11:56 +000093#define FUNC2(funcname, func, docstring) \
94 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
95 return math_2(args, func, "dd:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000096 }\
Fred Drake40c48682000-07-03 18:11:56 +000097 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098
Fred Drake40c48682000-07-03 18:11:56 +000099FUNC1(acos, acos,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000100 "acos(x)\n\nReturn the arc cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000101FUNC1(asin, asin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000102 "asin(x)\n\nReturn the arc sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000103FUNC1(atan, atan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000104 "atan(x)\n\nReturn the arc tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000105FUNC2(atan2, atan2,
Guido van Rossumb057dd81998-12-08 16:27:10 +0000106 "atan2(y, x)\n\nReturn atan(y/x).")
Fred Drake40c48682000-07-03 18:11:56 +0000107FUNC1(ceil, ceil,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000108 "ceil(x)\n\nReturn the ceiling of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000109FUNC1(cos, cos,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000110 "cos(x)\n\nReturn the cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000111FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000112 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000113FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000114 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000115FUNC1(fabs, fabs,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000116 "fabs(x)\n\nReturn the absolute value of the real x.")
Fred Drake40c48682000-07-03 18:11:56 +0000117FUNC1(floor, floor,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000118 "floor(x)\n\nReturn the floor of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000119 FUNC2(fmod, fmod,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000120 "fmod(x,y)\n\nReturn x % y.")
Fred Drake40c48682000-07-03 18:11:56 +0000121FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000122 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000123FUNC1(log, log,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000124 "log(x)\n\nReturn the natural logarithm of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000125FUNC1(log10, log10,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000126 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000127#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Fred Drake40c48682000-07-03 18:11:56 +0000128FUNC2(pow, power,
129 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000130#else
Fred Drake40c48682000-07-03 18:11:56 +0000131FUNC2(pow, pow,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000132 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000133#endif
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000134#ifdef HAVE_RINT
Fred Drake40c48682000-07-03 18:11:56 +0000135FUNC1(rint, rint,
Guido van Rossum71260b82000-05-11 18:19:42 +0000136 "rint(x)\n\nReturn the integer nearest to x as a real.")
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000137#endif
Fred Drake40c48682000-07-03 18:11:56 +0000138FUNC1(sin, sin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000139 "sin(x)\n\nReturn the sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000140FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000141 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000142FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000143 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000144FUNC1(tan, tan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000145 "tan(x)\n\nReturn the tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000146FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000147 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149
Barry Warsaw8b43b191996-12-09 22:32:36 +0000150static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000151math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000152{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000153 double x;
154 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000155 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000156 return NULL;
157 errno = 0;
158 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000159 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000160 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000161 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000162 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000163}
164
Guido van Rossumc6e22901998-12-04 19:26:43 +0000165static char math_frexp_doc [] =
166"frexp(x)\n\
167\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000168Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000169m is a float and e is an int, such that x = m * 2.**e.\n\
170If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000171
172
Barry Warsaw8b43b191996-12-09 22:32:36 +0000173static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000174math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000175{
Guido van Rossumc5545052000-05-08 14:29:38 +0000176 double x;
177 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000178 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000179 return NULL;
180 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000181 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000182 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000183 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000184 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000185 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000186 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000187 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000188 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000189}
190
Guido van Rossumc6e22901998-12-04 19:26:43 +0000191static char math_ldexp_doc [] =
192"ldexp_doc(x, i)\n\
193\n\
194Return x * (2**i).";
195
196
Barry Warsaw8b43b191996-12-09 22:32:36 +0000197static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000198math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000199{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000200 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000201 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000202 return NULL;
203 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000205 {
206 extended e;
207 x = modf(x, &e);
208 y = e;
209 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000211 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000213 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000214 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000215 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000216 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000217}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218
Guido van Rossumc6e22901998-12-04 19:26:43 +0000219static char math_modf_doc [] =
220"modf(x)\n\
221\n\
222Return the fractional and integer parts of x. Both results carry the sign\n\
223of x. The integer part is returned as a real.";
224
225
Barry Warsaw8b43b191996-12-09 22:32:36 +0000226static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000227 {"acos", math_acos, METH_VARARGS, math_acos_doc},
228 {"asin", math_asin, METH_VARARGS, math_asin_doc},
229 {"atan", math_atan, METH_VARARGS, math_atan_doc},
230 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
231 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
232 {"cos", math_cos, METH_VARARGS, math_cos_doc},
233 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
234 {"exp", math_exp, METH_VARARGS, math_exp_doc},
235 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
236 {"floor", math_floor, METH_VARARGS, math_floor_doc},
237 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
238 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
239 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
240 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
241 {"log", math_log, METH_VARARGS, math_log_doc},
242 {"log10", math_log10, METH_VARARGS, math_log10_doc},
243 {"modf", math_modf, METH_VARARGS, math_modf_doc},
244 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000245#ifdef HAVE_RINT
Fred Drake40c48682000-07-03 18:11:56 +0000246 {"rint", math_rint, METH_VARARGS, math_rint_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000247#endif
Fred Drake40c48682000-07-03 18:11:56 +0000248 {"sin", math_sin, METH_VARARGS, math_sin_doc},
249 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
250 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
251 {"tan", math_tan, METH_VARARGS, math_tan_doc},
252 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253 {NULL, NULL} /* sentinel */
254};
255
Guido van Rossumc6e22901998-12-04 19:26:43 +0000256
257static char module_doc [] =
258"This module is always available. It provides access to the\n\
259mathematical functions defined by the C standard.";
260
Guido van Rossum3886bb61998-12-04 18:50:17 +0000261DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262initmath()
263{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000264 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000265
Guido van Rossumc6e22901998-12-04 19:26:43 +0000266 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000267 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000268
269 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
270 goto finally;
271 if (PyDict_SetItemString(d, "pi", v) < 0)
272 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000273 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000274
275 if (!(v = PyFloat_FromDouble(exp(1.0))))
276 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000277 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000278 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000279 Py_DECREF(v);
Guido van Rossum4a488091996-12-18 14:14:33 +0000280 return;
Barry Warsawfc93f751996-12-17 00:47:03 +0000281
282 finally:
283 Py_FatalError("can't initialize math module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284}