blob: 93b24fc2a71f1cbb12abac899d6c5fe2ab2d8612 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Math module -- standard C math library functions, pi and e */
2
3#include <stdio.h>
4#include <math.h>
5
6#include "PROTO.h"
7#include "object.h"
8#include "intobject.h"
9#include "tupleobject.h"
10#include "floatobject.h"
11#include "dictobject.h"
12#include "methodobject.h"
13#include "moduleobject.h"
14#include "objimpl.h"
15#include "import.h"
16#include "modsupport.h"
17
18static int
19getdoublearg(args, px)
20 register object *args;
21 double *px;
22{
23 if (args == NULL)
24 return err_badarg();
25 if (is_floatobject(args)) {
26 *px = getfloatvalue(args);
27 return 1;
28 }
29 if (is_intobject(args)) {
30 *px = getintvalue(args);
31 return 1;
32 }
33 return err_badarg();
34}
35
36static int
37get2doublearg(args, px, py)
38 register object *args;
39 double *px, *py;
40{
41 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2)
42 return err_badarg();
43 return getdoublearg(gettupleitem(args, 0), px) &&
44 getdoublearg(gettupleitem(args, 1), py);
45}
46
47static object *
48math_1(args, func)
49 object *args;
50 double (*func) FPROTO((double));
51{
52 double x;
53 if (!getdoublearg(args, &x))
54 return NULL;
55 errno = 0;
56 x = (*func)(x);
57 if (errno != 0)
58 return NULL;
59 else
60 return newfloatobject(x);
61}
62
63static object *
64math_2(args, func)
65 object *args;
66 double (*func) FPROTO((double, double));
67{
68 double x, y;
69 if (!get2doublearg(args, &x, &y))
70 return NULL;
71 errno = 0;
72 x = (*func)(x, y);
73 if (errno != 0)
74 return NULL;
75 else
76 return newfloatobject(x);
77}
78
79#define FUNC1(stubname, func) \
80 static object * stubname(self, args) object *self, *args; { \
81 return math_1(args, func); \
82 }
83
84#define FUNC2(stubname, func) \
85 static object * stubname(self, args) object *self, *args; { \
86 return math_2(args, func); \
87 }
88
89FUNC1(math_acos, acos)
90FUNC1(math_asin, asin)
91FUNC1(math_atan, atan)
92FUNC2(math_atan2, atan2)
93FUNC1(math_ceil, ceil)
94FUNC1(math_cos, cos)
95FUNC1(math_cosh, cosh)
96FUNC1(math_exp, exp)
97FUNC1(math_fabs, fabs)
98FUNC1(math_floor, floor)
99#if 0
100/* XXX This one is not in the Amoeba library yet, so what the heck... */
101FUNC2(math_fmod, fmod)
102#endif
103FUNC1(math_log, log)
104FUNC1(math_log10, log10)
105FUNC2(math_pow, pow)
106FUNC1(math_sin, sin)
107FUNC1(math_sinh, sinh)
108FUNC1(math_sqrt, sqrt)
109FUNC1(math_tan, tan)
110FUNC1(math_tanh, tanh)
111
112#if 0
113/* What about these? */
114double frexp(double x, int *i);
115double ldexp(double x, int n);
116double modf(double x, double *i);
117#endif
118
119static struct methodlist math_methods[] = {
120 {"acos", math_acos},
121 {"asin", math_asin},
122 {"atan", math_atan},
123 {"atan2", math_atan2},
124 {"ceil", math_ceil},
125 {"cos", math_cos},
126 {"cosh", math_cosh},
127 {"exp", math_exp},
128 {"fabs", math_fabs},
129 {"floor", math_floor},
130#if 0
131 {"fmod", math_fmod},
132 {"frexp", math_freqp},
133 {"ldexp", math_ldexp},
134#endif
135 {"log", math_log},
136 {"log10", math_log10},
137#if 0
138 {"modf", math_modf},
139#endif
140 {"pow", math_pow},
141 {"sin", math_sin},
142 {"sinh", math_sinh},
143 {"sqrt", math_sqrt},
144 {"tan", math_tan},
145 {"tanh", math_tanh},
146 {NULL, NULL} /* sentinel */
147};
148
149void
150initmath()
151{
152 object *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000153
154 m = initmodule("math", math_methods);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 d = getmoduledict(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 dictinsert(d, "pi", newfloatobject(atan(1.0) * 4.0));
157 dictinsert(d, "e", newfloatobject(exp(1.0)));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}