blob: 8b1bccf8dad184efe5d7e7952bcface185733c49 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossume5372401993-03-16 12:15:04 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The 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>
30#ifndef errno
31extern int errno;
32#endif
33
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034#include "modsupport.h"
35
Guido van Rossum234f9421993-06-17 12:35:49 +000036#define getdoublearg(v, a) getargs(v, "d", a)
37#define get2doublearg(v, a, b) getargs(v, "(dd)", a, b)
38
Guido van Rossum3f5da241990-12-20 15:06:42 +000039#include <math.h>
40
Guido van Rossum9575a441993-04-07 14:06:14 +000041#ifdef i860
42/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
43#undef HUGE_VAL
44#endif
45
Guido van Rossum2d709b11992-01-14 18:37:27 +000046#ifndef __STDC__
Guido van Rossumdf840d91992-03-27 17:29:44 +000047extern double fmod PROTO((double, double));
Guido van Rossum2d709b11992-01-14 18:37:27 +000048#endif
49
Guido van Rossum8832b621991-12-16 15:44:24 +000050#ifdef HUGE_VAL
51#define CHECK(x) if (errno != 0) ; \
52 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
53 else errno = ERANGE
54#else
55#define CHECK(x) /* Don't know how to check */
56#endif
57
58static object *
59math_error()
60{
61 if (errno == EDOM)
62 err_setstr(ValueError, "math domain error");
63 else if (errno == ERANGE)
64 err_setstr(OverflowError, "math range error");
65 else
Guido van Rossum444db071992-02-26 15:26:56 +000066 err_errno(ValueError); /* Unexpected math error */
Guido van Rossum8832b621991-12-16 15:44:24 +000067 return NULL;
68}
69
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070static object *
71math_1(args, func)
72 object *args;
73 double (*func) FPROTO((double));
74{
75 double x;
76 if (!getdoublearg(args, &x))
77 return NULL;
78 errno = 0;
79 x = (*func)(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
84 return newfloatobject(x);
85}
86
87static object *
88math_2(args, func)
89 object *args;
90 double (*func) FPROTO((double, double));
91{
92 double x, y;
93 if (!get2doublearg(args, &x, &y))
94 return NULL;
95 errno = 0;
96 x = (*func)(x, y);
Guido van Rossum8832b621991-12-16 15:44:24 +000097 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000099 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 else
101 return newfloatobject(x);
102}
103
104#define FUNC1(stubname, func) \
105 static object * stubname(self, args) object *self, *args; { \
106 return math_1(args, func); \
107 }
108
109#define FUNC2(stubname, func) \
110 static object * stubname(self, args) object *self, *args; { \
111 return math_2(args, func); \
112 }
113
114FUNC1(math_acos, acos)
115FUNC1(math_asin, asin)
116FUNC1(math_atan, atan)
117FUNC2(math_atan2, atan2)
118FUNC1(math_ceil, ceil)
119FUNC1(math_cos, cos)
120FUNC1(math_cosh, cosh)
121FUNC1(math_exp, exp)
122FUNC1(math_fabs, fabs)
123FUNC1(math_floor, floor)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124FUNC2(math_fmod, fmod)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125FUNC1(math_log, log)
126FUNC1(math_log10, log10)
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 ... */
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000128FUNC2(math_pow, power)
129#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130FUNC2(math_pow, pow)
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000131#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132FUNC1(math_sin, sin)
133FUNC1(math_sinh, sinh)
134FUNC1(math_sqrt, sqrt)
135FUNC1(math_tan, tan)
136FUNC1(math_tanh, tanh)
137
Guido van Rossum98d54331991-11-12 15:44:14 +0000138double frexp PROTO((double, int *));
139double ldexp PROTO((double, int));
140double modf PROTO((double, double *));
Guido van Rossumd18ad581991-10-24 14:57:21 +0000141
142static object *
143math_frexp(self, args)
144 object *self;
145 object *args;
146{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000147 double x;
148 int i;
149 if (!getdoublearg(args, &x))
150 return NULL;
151 errno = 0;
152 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000153 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000154 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000155 return math_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000156 return mkvalue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000157}
158
159static object *
160math_ldexp(self, args)
161 object *self;
162 object *args;
163{
164 double x, y;
165 /* Cheat -- allow float as second argument */
166 if (!get2doublearg(args, &x, &y))
167 return NULL;
168 errno = 0;
169 x = ldexp(x, (int)y);
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
174 return newfloatobject(x);
175}
176
177static object *
178math_modf(self, args)
179 object *self;
180 object *args;
181{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000182 double x, y;
183 if (!getdoublearg(args, &x))
184 return NULL;
185 errno = 0;
186 x = modf(x, &y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000187 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000188 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000189 return math_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000190 return mkvalue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000191}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192
193static struct methodlist math_methods[] = {
194 {"acos", math_acos},
195 {"asin", math_asin},
196 {"atan", math_atan},
197 {"atan2", math_atan2},
198 {"ceil", math_ceil},
199 {"cos", math_cos},
200 {"cosh", math_cosh},
201 {"exp", math_exp},
202 {"fabs", math_fabs},
203 {"floor", math_floor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204 {"fmod", math_fmod},
Guido van Rossumd18ad581991-10-24 14:57:21 +0000205 {"frexp", math_frexp},
206 {"ldexp", math_ldexp},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 {"log", math_log},
208 {"log10", math_log10},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209 {"modf", math_modf},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 {"pow", math_pow},
211 {"sin", math_sin},
212 {"sinh", math_sinh},
213 {"sqrt", math_sqrt},
214 {"tan", math_tan},
215 {"tanh", math_tanh},
216 {NULL, NULL} /* sentinel */
217};
218
219void
220initmath()
221{
222 object *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000223
224 m = initmodule("math", math_methods);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225 d = getmoduledict(m);
Guido van Rossumeb38d241990-11-18 17:36:45 +0000226 dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
227 DECREF(v);
228 dictinsert(d, "e", v = newfloatobject(exp(1.0)));
229 DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230}