1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Math module -- standard C math library functions, pi and e */
27 #include "allobjects.h"
31 #define getdoublearg(v, a) getargs(v, "d", a)
32 #define get2doublearg(v, a, b) getargs(v, "(dd)", a, b)
37 extern double fmod
PROTO((double, double));
38 extern double frexp
PROTO((double, int *));
39 extern double ldexp
PROTO((double, int));
40 extern double modf
PROTO((double, double *));
44 extern double hypot
PROTO((double, double));
48 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
53 #define CHECK(x) if (errno != 0) ; \
54 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
57 #define CHECK(x) /* Don't know how to check */
64 err_setstr(ValueError
, "math domain error");
65 else if (errno
== ERANGE
)
66 err_setstr(OverflowError
, "math range error");
68 err_errno(ValueError
); /* Unexpected math error */
75 double (*func
) FPROTO((double));
78 if (!getdoublearg(args
, &x
))
86 return newfloatobject(x
);
92 double (*func
) FPROTO((double, double));
95 if (!get2doublearg(args
, &x
, &y
))
103 return newfloatobject(x
);
106 #define FUNC1(stubname, func) \
107 static object * stubname(self, args) object *self, *args; { \
108 return math_1(args, func); \
111 #define FUNC2(stubname, func) \
112 static object * stubname(self, args) object *self, *args; { \
113 return math_2(args, func); \
116 FUNC1(math_acos
, acos
)
117 FUNC1(math_asin
, asin
)
118 FUNC1(math_atan
, atan
)
119 FUNC2(math_atan2
, atan2
)
120 FUNC1(math_ceil
, ceil
)
122 FUNC1(math_cosh
, cosh
)
124 FUNC1(math_fabs
, fabs
)
125 FUNC1(math_floor
, floor
)
126 FUNC2(math_fmod
, fmod
)
128 FUNC2(math_hypot
, hypot
)
131 FUNC1(math_log10
, log10
)
132 #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
133 FUNC2(math_pow
, power
)
138 FUNC1(math_sinh
, sinh
)
139 FUNC1(math_sqrt
, sqrt
)
141 FUNC1(math_tanh
, tanh
)
145 math_frexp(self
, args
)
151 if (!getdoublearg(args
, &x
))
158 return mkvalue("(di)", x
, i
);
162 math_ldexp(self
, args
)
167 /* Cheat -- allow float as second argument */
168 if (!get2doublearg(args
, &x
, &y
))
171 x
= ldexp(x
, (int)y
);
176 return newfloatobject(x
);
180 math_modf(self
, args
)
185 if (!getdoublearg(args
, &x
))
188 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
200 return mkvalue("(dd)", x
, y
);
203 static struct methodlist math_methods
[] = {
207 {"atan2", math_atan2
},
213 {"floor", math_floor
},
215 {"frexp", math_frexp
},
217 {"hypot", math_hypot
},
219 {"ldexp", math_ldexp
},
221 {"log10", math_log10
},
229 {NULL
, NULL
} /* sentinel */
237 m
= initmodule("math", math_methods
);
238 d
= getmoduledict(m
);
239 dictinsert(d
, "pi", v
= newfloatobject(atan(1.0) * 4.0));
241 dictinsert(d
, "e", v
= newfloatobject(exp(1.0)));