blob: ae7dc7fad701964fbaf97d4721d517e55d56b320 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Math module -- standard C math library functions, pi and e */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum801f4731990-12-20 23:09:14 +000029#include <errno.h>
Guido van Rossum801f4731990-12-20 23:09:14 +000030
Guido van Rossum234f9421993-06-17 12:35:49 +000031#define getdoublearg(v, a) getargs(v, "d", a)
32#define get2doublearg(v, a, b) getargs(v, "(dd)", a, b)
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include <math.h>
35
Guido van Rossumb9418681995-01-12 11:28:16 +000036#ifndef __STDC__
37extern double fmod PROTO((double, double));
38extern double frexp PROTO((double, int *));
39extern double ldexp PROTO((double, int));
40extern double modf PROTO((double, double *));
41#endif
42
43#ifdef HAVE_HYPOT
44extern double hypot PROTO((double, double));
45#endif
46
Guido van Rossum9575a441993-04-07 14:06:14 +000047#ifdef i860
48/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
49#undef HUGE_VAL
50#endif
51
Guido van Rossum8832b621991-12-16 15:44:24 +000052#ifdef HUGE_VAL
53#define CHECK(x) if (errno != 0) ; \
54 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
55 else errno = ERANGE
56#else
57#define CHECK(x) /* Don't know how to check */
58#endif
59
60static object *
61math_error()
62{
63 if (errno == EDOM)
64 err_setstr(ValueError, "math domain error");
65 else if (errno == ERANGE)
66 err_setstr(OverflowError, "math range error");
67 else
Guido van Rossum444db071992-02-26 15:26:56 +000068 err_errno(ValueError); /* Unexpected math error */
Guido van Rossum8832b621991-12-16 15:44:24 +000069 return NULL;
70}
71
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072static object *
73math_1(args, func)
74 object *args;
75 double (*func) FPROTO((double));
76{
77 double x;
78 if (!getdoublearg(args, &x))
79 return NULL;
80 errno = 0;
81 x = (*func)(x);
Guido van Rossum8832b621991-12-16 15:44:24 +000082 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000084 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 else
86 return newfloatobject(x);
87}
88
89static object *
90math_2(args, func)
91 object *args;
92 double (*func) FPROTO((double, double));
93{
94 double x, y;
95 if (!get2doublearg(args, &x, &y))
96 return NULL;
97 errno = 0;
98 x = (*func)(x, y);
Guido van Rossum8832b621991-12-16 15:44:24 +000099 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000101 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 else
103 return newfloatobject(x);
104}
105
106#define FUNC1(stubname, func) \
107 static object * stubname(self, args) object *self, *args; { \
108 return math_1(args, func); \
109 }
110
111#define FUNC2(stubname, func) \
112 static object * stubname(self, args) object *self, *args; { \
113 return math_2(args, func); \
114 }
115
116FUNC1(math_acos, acos)
117FUNC1(math_asin, asin)
118FUNC1(math_atan, atan)
119FUNC2(math_atan2, atan2)
120FUNC1(math_ceil, ceil)
121FUNC1(math_cos, cos)
122FUNC1(math_cosh, cosh)
123FUNC1(math_exp, exp)
124FUNC1(math_fabs, fabs)
125FUNC1(math_floor, floor)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126FUNC2(math_fmod, fmod)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000127#ifdef HAVE_HYPOT
Guido van Rossum411a8bd1994-10-20 22:00:28 +0000128FUNC2(math_hypot, hypot)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000129#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130FUNC1(math_log, log)
131FUNC1(math_log10, log10)
Guido van Rossum1492c271991-07-27 21:38:43 +0000132#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000133FUNC2(math_pow, power)
134#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135FUNC2(math_pow, pow)
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000136#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137FUNC1(math_sin, sin)
138FUNC1(math_sinh, sinh)
139FUNC1(math_sqrt, sqrt)
140FUNC1(math_tan, tan)
141FUNC1(math_tanh, tanh)
142
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143#ifndef macintosh
144
Guido van Rossumd18ad581991-10-24 14:57:21 +0000145
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146#endif
147
Guido van Rossumd18ad581991-10-24 14:57:21 +0000148static object *
149math_frexp(self, args)
150 object *self;
151 object *args;
152{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000153 double x;
154 int i;
155 if (!getdoublearg(args, &x))
156 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();
Guido van Rossume5372401993-03-16 12:15:04 +0000162 return mkvalue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000163}
164
165static object *
166math_ldexp(self, args)
167 object *self;
168 object *args;
169{
170 double x, y;
171 /* Cheat -- allow float as second argument */
172 if (!get2doublearg(args, &x, &y))
173 return NULL;
174 errno = 0;
175 x = ldexp(x, (int)y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000176 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000177 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000178 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000179 else
180 return newfloatobject(x);
181}
182
183static object *
184math_modf(self, args)
185 object *self;
186 object *args;
187{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000188 double x, y;
189 if (!getdoublearg(args, &x))
190 return NULL;
191 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
193{
194 extended e;
195 x = modf(x, &e);
196 y = e;
197}
198#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000199 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000200#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000201 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000202 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000203 return math_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000204 return mkvalue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000205}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206
207static struct methodlist math_methods[] = {
208 {"acos", math_acos},
209 {"asin", math_asin},
210 {"atan", math_atan},
211 {"atan2", math_atan2},
212 {"ceil", math_ceil},
213 {"cos", math_cos},
214 {"cosh", math_cosh},
215 {"exp", math_exp},
216 {"fabs", math_fabs},
217 {"floor", math_floor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218 {"fmod", math_fmod},
Guido van Rossumd18ad581991-10-24 14:57:21 +0000219 {"frexp", math_frexp},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000220#ifdef HAVE_HYPOT
Guido van Rossum411a8bd1994-10-20 22:00:28 +0000221 {"hypot", math_hypot},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000222#endif
Guido van Rossumd18ad581991-10-24 14:57:21 +0000223 {"ldexp", math_ldexp},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224 {"log", math_log},
225 {"log10", math_log10},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226 {"modf", math_modf},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227 {"pow", math_pow},
228 {"sin", math_sin},
229 {"sinh", math_sinh},
230 {"sqrt", math_sqrt},
231 {"tan", math_tan},
232 {"tanh", math_tanh},
233 {NULL, NULL} /* sentinel */
234};
235
236void
237initmath()
238{
239 object *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000240
241 m = initmodule("math", math_methods);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242 d = getmoduledict(m);
Guido van Rossumeb38d241990-11-18 17:36:45 +0000243 dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
244 DECREF(v);
245 dictinsert(d, "e", v = newfloatobject(exp(1.0)));
246 DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247}