blob: 00aa5eb90341f32afb52cafce057653d0c44fdb4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
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 Rossum3f5da241990-12-20 15:06:42 +000036#include <math.h>
37
Guido van Rossum2d709b11992-01-14 18:37:27 +000038#ifndef __STDC__
Guido van Rossumdf840d91992-03-27 17:29:44 +000039extern double fmod PROTO((double, double));
Guido van Rossum2d709b11992-01-14 18:37:27 +000040#endif
41
Guido van Rossum8832b621991-12-16 15:44:24 +000042#ifdef HUGE_VAL
43#define CHECK(x) if (errno != 0) ; \
44 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
45 else errno = ERANGE
46#else
47#define CHECK(x) /* Don't know how to check */
48#endif
49
50static object *
51math_error()
52{
53 if (errno == EDOM)
54 err_setstr(ValueError, "math domain error");
55 else if (errno == ERANGE)
56 err_setstr(OverflowError, "math range error");
57 else
Guido van Rossum444db071992-02-26 15:26:56 +000058 err_errno(ValueError); /* Unexpected math error */
Guido van Rossum8832b621991-12-16 15:44:24 +000059 return NULL;
60}
61
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062static object *
63math_1(args, func)
64 object *args;
65 double (*func) FPROTO((double));
66{
67 double x;
68 if (!getdoublearg(args, &x))
69 return NULL;
70 errno = 0;
71 x = (*func)(x);
Guido van Rossum8832b621991-12-16 15:44:24 +000072 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000074 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 else
76 return newfloatobject(x);
77}
78
79static object *
80math_2(args, func)
81 object *args;
82 double (*func) FPROTO((double, double));
83{
84 double x, y;
85 if (!get2doublearg(args, &x, &y))
86 return NULL;
87 errno = 0;
88 x = (*func)(x, y);
Guido van Rossum8832b621991-12-16 15:44:24 +000089 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000091 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 else
93 return newfloatobject(x);
94}
95
96#define FUNC1(stubname, func) \
97 static object * stubname(self, args) object *self, *args; { \
98 return math_1(args, func); \
99 }
100
101#define FUNC2(stubname, func) \
102 static object * stubname(self, args) object *self, *args; { \
103 return math_2(args, func); \
104 }
105
106FUNC1(math_acos, acos)
107FUNC1(math_asin, asin)
108FUNC1(math_atan, atan)
109FUNC2(math_atan2, atan2)
110FUNC1(math_ceil, ceil)
111FUNC1(math_cos, cos)
112FUNC1(math_cosh, cosh)
113FUNC1(math_exp, exp)
114FUNC1(math_fabs, fabs)
115FUNC1(math_floor, floor)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116FUNC2(math_fmod, fmod)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117FUNC1(math_log, log)
118FUNC1(math_log10, log10)
Guido van Rossum1492c271991-07-27 21:38:43 +0000119#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 +0000120FUNC2(math_pow, power)
121#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122FUNC2(math_pow, pow)
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000123#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124FUNC1(math_sin, sin)
125FUNC1(math_sinh, sinh)
126FUNC1(math_sqrt, sqrt)
127FUNC1(math_tan, tan)
128FUNC1(math_tanh, tanh)
129
Guido van Rossum98d54331991-11-12 15:44:14 +0000130double frexp PROTO((double, int *));
131double ldexp PROTO((double, int));
132double modf PROTO((double, double *));
Guido van Rossumd18ad581991-10-24 14:57:21 +0000133
134static object *
135math_frexp(self, args)
136 object *self;
137 object *args;
138{
139 object *v;
140 double x;
141 int i;
142 if (!getdoublearg(args, &x))
143 return NULL;
144 errno = 0;
145 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000146 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000147 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000148 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000149 v = newtupleobject(2);
150 if (v != NULL) {
151 settupleitem(v, 0, newfloatobject(x));
152 settupleitem(v, 1, newintobject((long)i));
153 if (err_occurred()) {
154 DECREF(v);
155 v = NULL;
156 }
157 }
158 return v;
159}
160
161static object *
162math_ldexp(self, args)
163 object *self;
164 object *args;
165{
166 double x, y;
167 /* Cheat -- allow float as second argument */
168 if (!get2doublearg(args, &x, &y))
169 return NULL;
170 errno = 0;
171 x = ldexp(x, (int)y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000172 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000173 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000174 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000175 else
176 return newfloatobject(x);
177}
178
179static object *
180math_modf(self, args)
181 object *self;
182 object *args;
183{
184 object *v;
185 double x, y;
186 if (!getdoublearg(args, &x))
187 return NULL;
188 errno = 0;
189 x = modf(x, &y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000190 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000191 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000192 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000193 v = newtupleobject(2);
194 if (v != NULL) {
195 settupleitem(v, 0, newfloatobject(x));
196 settupleitem(v, 1, newfloatobject(y));
197 if (err_occurred()) {
198 DECREF(v);
199 v = NULL;
200 }
201 }
202 return v;
203}
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},
218 {"ldexp", math_ldexp},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219 {"log", math_log},
220 {"log10", math_log10},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221 {"modf", math_modf},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222 {"pow", math_pow},
223 {"sin", math_sin},
224 {"sinh", math_sinh},
225 {"sqrt", math_sqrt},
226 {"tan", math_tan},
227 {"tanh", math_tanh},
228 {NULL, NULL} /* sentinel */
229};
230
231void
232initmath()
233{
234 object *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000235
236 m = initmodule("math", math_methods);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237 d = getmoduledict(m);
Guido van Rossumeb38d241990-11-18 17:36:45 +0000238 dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
239 DECREF(v);
240 dictinsert(d, "e", v = newfloatobject(exp(1.0)));
241 DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242}