blob: 12effa5f46f13872f5852c4c8bc2cef07e513e79 [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 Rossum6964f731995-03-01 10:34:29 +000034#include "mymath.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
Guido van Rossum7081cf51996-05-23 22:56:19 +000036#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +000037#ifndef __STDC__
38extern double fmod PROTO((double, double));
39extern double frexp PROTO((double, int *));
40extern double ldexp PROTO((double, int));
41extern double modf PROTO((double, double *));
Guido van Rossum7081cf51996-05-23 22:56:19 +000042#endif /* __STDC__ */
43#endif /* _MSC_VER */
44
Guido van Rossumb9418681995-01-12 11:28:16 +000045
Guido van Rossum9575a441993-04-07 14:06:14 +000046#ifdef i860
47/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
48#undef HUGE_VAL
49#endif
50
Guido van Rossum8832b621991-12-16 15:44:24 +000051#ifdef HUGE_VAL
52#define CHECK(x) if (errno != 0) ; \
53 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
54 else errno = ERANGE
55#else
56#define CHECK(x) /* Don't know how to check */
57#endif
58
59static object *
60math_error()
61{
62 if (errno == EDOM)
63 err_setstr(ValueError, "math domain error");
64 else if (errno == ERANGE)
65 err_setstr(OverflowError, "math range error");
66 else
Guido van Rossum444db071992-02-26 15:26:56 +000067 err_errno(ValueError); /* Unexpected math error */
Guido van Rossum8832b621991-12-16 15:44:24 +000068 return NULL;
69}
70
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071static object *
72math_1(args, func)
73 object *args;
74 double (*func) FPROTO((double));
75{
76 double x;
77 if (!getdoublearg(args, &x))
78 return NULL;
79 errno = 0;
80 x = (*func)(x);
Guido van Rossum8832b621991-12-16 15:44:24 +000081 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000083 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 else
85 return newfloatobject(x);
86}
87
88static object *
89math_2(args, func)
90 object *args;
91 double (*func) FPROTO((double, double));
92{
93 double x, y;
94 if (!get2doublearg(args, &x, &y))
95 return NULL;
96 errno = 0;
97 x = (*func)(x, y);
Guido van Rossum8832b621991-12-16 15:44:24 +000098 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000100 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 else
102 return newfloatobject(x);
103}
104
105#define FUNC1(stubname, func) \
106 static object * stubname(self, args) object *self, *args; { \
107 return math_1(args, func); \
108 }
109
110#define FUNC2(stubname, func) \
111 static object * stubname(self, args) object *self, *args; { \
112 return math_2(args, func); \
113 }
114
115FUNC1(math_acos, acos)
116FUNC1(math_asin, asin)
117FUNC1(math_atan, atan)
118FUNC2(math_atan2, atan2)
119FUNC1(math_ceil, ceil)
120FUNC1(math_cos, cos)
121FUNC1(math_cosh, cosh)
122FUNC1(math_exp, exp)
Guido van Rossum6412b1d1996-08-08 19:10:21 +0000123#ifdef __MWERKS__
124double myfabs(double x) { return fabs(x); }
125FUNC1(math_fabs, myfabs)
126#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127FUNC1(math_fabs, fabs)
Guido van Rossum6412b1d1996-08-08 19:10:21 +0000128#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129FUNC1(math_floor, floor)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130FUNC2(math_fmod, fmod)
Guido van Rossum411a8bd1994-10-20 22:00:28 +0000131FUNC2(math_hypot, hypot)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132FUNC1(math_log, log)
133FUNC1(math_log10, log10)
Guido van Rossum1492c271991-07-27 21:38:43 +0000134#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 +0000135FUNC2(math_pow, power)
136#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137FUNC2(math_pow, pow)
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000138#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139FUNC1(math_sin, sin)
140FUNC1(math_sinh, sinh)
141FUNC1(math_sqrt, sqrt)
142FUNC1(math_tan, tan)
143FUNC1(math_tanh, tanh)
144
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145
Guido van Rossumd18ad581991-10-24 14:57:21 +0000146static object *
147math_frexp(self, args)
148 object *self;
149 object *args;
150{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000151 double x;
152 int i;
153 if (!getdoublearg(args, &x))
154 return NULL;
155 errno = 0;
156 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000157 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000158 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000159 return math_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000160 return mkvalue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000161}
162
163static object *
164math_ldexp(self, args)
165 object *self;
166 object *args;
167{
168 double x, y;
169 /* Cheat -- allow float as second argument */
170 if (!get2doublearg(args, &x, &y))
171 return NULL;
172 errno = 0;
173 x = ldexp(x, (int)y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000174 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000175 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000176 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000177 else
178 return newfloatobject(x);
179}
180
181static object *
182math_modf(self, args)
183 object *self;
184 object *args;
185{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000186 double x, y;
187 if (!getdoublearg(args, &x))
188 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 */
191{
192 extended e;
193 x = modf(x, &e);
194 y = e;
195}
196#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();
Guido van Rossume5372401993-03-16 12:15:04 +0000202 return mkvalue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000203}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204
205static struct methodlist math_methods[] = {
206 {"acos", math_acos},
207 {"asin", math_asin},
208 {"atan", math_atan},
209 {"atan2", math_atan2},
210 {"ceil", math_ceil},
211 {"cos", math_cos},
212 {"cosh", math_cosh},
213 {"exp", math_exp},
214 {"fabs", math_fabs},
215 {"floor", math_floor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216 {"fmod", math_fmod},
Guido van Rossumd18ad581991-10-24 14:57:21 +0000217 {"frexp", math_frexp},
Guido van Rossum411a8bd1994-10-20 22:00:28 +0000218 {"hypot", math_hypot},
Guido van Rossumd18ad581991-10-24 14:57:21 +0000219 {"ldexp", math_ldexp},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220 {"log", math_log},
221 {"log10", math_log10},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222 {"modf", math_modf},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 {"pow", math_pow},
224 {"sin", math_sin},
225 {"sinh", math_sinh},
226 {"sqrt", math_sqrt},
227 {"tan", math_tan},
228 {"tanh", math_tanh},
229 {NULL, NULL} /* sentinel */
230};
231
232void
233initmath()
234{
235 object *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000236
237 m = initmodule("math", math_methods);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238 d = getmoduledict(m);
Guido van Rossumeb38d241990-11-18 17:36:45 +0000239 dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
240 DECREF(v);
241 dictinsert(d, "e", v = newfloatobject(exp(1.0)));
242 DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243}