blob: 15211339bfcd46c7558fb229ebd9ba45a4ac1b1a [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 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{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000139 double x;
140 int i;
141 if (!getdoublearg(args, &x))
142 return NULL;
143 errno = 0;
144 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000145 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000146 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000147 return math_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000148 return mkvalue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000149}
150
151static object *
152math_ldexp(self, args)
153 object *self;
154 object *args;
155{
156 double x, y;
157 /* Cheat -- allow float as second argument */
158 if (!get2doublearg(args, &x, &y))
159 return NULL;
160 errno = 0;
161 x = ldexp(x, (int)y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000162 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000163 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000164 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000165 else
166 return newfloatobject(x);
167}
168
169static object *
170math_modf(self, args)
171 object *self;
172 object *args;
173{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000174 double x, y;
175 if (!getdoublearg(args, &x))
176 return NULL;
177 errno = 0;
178 x = modf(x, &y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000179 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000180 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000181 return math_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000182 return mkvalue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000183}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184
185static struct methodlist math_methods[] = {
186 {"acos", math_acos},
187 {"asin", math_asin},
188 {"atan", math_atan},
189 {"atan2", math_atan2},
190 {"ceil", math_ceil},
191 {"cos", math_cos},
192 {"cosh", math_cosh},
193 {"exp", math_exp},
194 {"fabs", math_fabs},
195 {"floor", math_floor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196 {"fmod", math_fmod},
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197 {"frexp", math_frexp},
198 {"ldexp", math_ldexp},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199 {"log", math_log},
200 {"log10", math_log10},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201 {"modf", math_modf},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 {"pow", math_pow},
203 {"sin", math_sin},
204 {"sinh", math_sinh},
205 {"sqrt", math_sqrt},
206 {"tan", math_tan},
207 {"tanh", math_tanh},
208 {NULL, NULL} /* sentinel */
209};
210
211void
212initmath()
213{
214 object *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000215
216 m = initmodule("math", math_methods);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217 d = getmoduledict(m);
Guido van Rossumeb38d241990-11-18 17:36:45 +0000218 dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
219 DECREF(v);
220 dictinsert(d, "e", v = newfloatobject(exp(1.0)));
221 DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222}