Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Math module -- standard C math library functions, pi and e */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 801f473 | 1990-12-20 23:09:14 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Guido van Rossum | 801f473 | 1990-12-20 23:09:14 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 31 | #define getdoublearg(v, a) getargs(v, "d", a) |
| 32 | #define get2doublearg(v, a, b) getargs(v, "(dd)", a, b) |
| 33 | |
Guido van Rossum | 6964f73 | 1995-03-01 10:34:29 +0000 | [diff] [blame] | 34 | #include "mymath.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 7081cf5 | 1996-05-23 22:56:19 +0000 | [diff] [blame] | 36 | #ifndef _MSC_VER |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 37 | #ifndef __STDC__ |
| 38 | extern double fmod PROTO((double, double)); |
| 39 | extern double frexp PROTO((double, int *)); |
| 40 | extern double ldexp PROTO((double, int)); |
| 41 | extern double modf PROTO((double, double *)); |
Guido van Rossum | 7081cf5 | 1996-05-23 22:56:19 +0000 | [diff] [blame] | 42 | #endif /* __STDC__ */ |
| 43 | #endif /* _MSC_VER */ |
| 44 | |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 9caac91 | 1995-06-14 22:17:37 +0000 | [diff] [blame] | 46 | #if defined(HAVE_HYPOT) && !defined(NeXT) |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 47 | extern double hypot PROTO((double, double)); |
Guido van Rossum | 3206268 | 1996-01-12 01:39:11 +0000 | [diff] [blame] | 48 | #else |
| 49 | double hypot(x,y) |
| 50 | double x; |
| 51 | double y; |
| 52 | { |
| 53 | double yx; |
| 54 | |
| 55 | x = fabs(x); |
| 56 | y = fabs(y); |
| 57 | if (x < y) { |
| 58 | double temp = x; |
| 59 | x = y; |
| 60 | y = temp; |
| 61 | } |
| 62 | if (x == 0.) |
| 63 | return 0.; |
| 64 | else { |
| 65 | yx = y/x; |
| 66 | return x*sqrt(1.+yx*yx); |
| 67 | } |
| 68 | } |
Guido van Rossum | b941868 | 1995-01-12 11:28:16 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 71 | #ifdef i860 |
| 72 | /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
| 73 | #undef HUGE_VAL |
| 74 | #endif |
| 75 | |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 76 | #ifdef HUGE_VAL |
| 77 | #define CHECK(x) if (errno != 0) ; \ |
| 78 | else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
| 79 | else errno = ERANGE |
| 80 | #else |
| 81 | #define CHECK(x) /* Don't know how to check */ |
| 82 | #endif |
| 83 | |
| 84 | static object * |
| 85 | math_error() |
| 86 | { |
| 87 | if (errno == EDOM) |
| 88 | err_setstr(ValueError, "math domain error"); |
| 89 | else if (errno == ERANGE) |
| 90 | err_setstr(OverflowError, "math range error"); |
| 91 | else |
Guido van Rossum | 444db07 | 1992-02-26 15:26:56 +0000 | [diff] [blame] | 92 | err_errno(ValueError); /* Unexpected math error */ |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
| 95 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | static object * |
| 97 | math_1(args, func) |
| 98 | object *args; |
| 99 | double (*func) FPROTO((double)); |
| 100 | { |
| 101 | double x; |
| 102 | if (!getdoublearg(args, &x)) |
| 103 | return NULL; |
| 104 | errno = 0; |
| 105 | x = (*func)(x); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 106 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 108 | return math_error(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | else |
| 110 | return newfloatobject(x); |
| 111 | } |
| 112 | |
| 113 | static object * |
| 114 | math_2(args, func) |
| 115 | object *args; |
| 116 | double (*func) FPROTO((double, double)); |
| 117 | { |
| 118 | double x, y; |
| 119 | if (!get2doublearg(args, &x, &y)) |
| 120 | return NULL; |
| 121 | errno = 0; |
| 122 | x = (*func)(x, y); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 123 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 125 | return math_error(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | else |
| 127 | return newfloatobject(x); |
| 128 | } |
| 129 | |
| 130 | #define FUNC1(stubname, func) \ |
| 131 | static object * stubname(self, args) object *self, *args; { \ |
| 132 | return math_1(args, func); \ |
| 133 | } |
| 134 | |
| 135 | #define FUNC2(stubname, func) \ |
| 136 | static object * stubname(self, args) object *self, *args; { \ |
| 137 | return math_2(args, func); \ |
| 138 | } |
| 139 | |
| 140 | FUNC1(math_acos, acos) |
| 141 | FUNC1(math_asin, asin) |
| 142 | FUNC1(math_atan, atan) |
| 143 | FUNC2(math_atan2, atan2) |
| 144 | FUNC1(math_ceil, ceil) |
| 145 | FUNC1(math_cos, cos) |
| 146 | FUNC1(math_cosh, cosh) |
| 147 | FUNC1(math_exp, exp) |
| 148 | FUNC1(math_fabs, fabs) |
| 149 | FUNC1(math_floor, floor) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | FUNC2(math_fmod, fmod) |
Guido van Rossum | 411a8bd | 1994-10-20 22:00:28 +0000 | [diff] [blame] | 151 | FUNC2(math_hypot, hypot) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | FUNC1(math_log, log) |
| 153 | FUNC1(math_log10, log10) |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 154 | #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */ |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 155 | FUNC2(math_pow, power) |
| 156 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | FUNC2(math_pow, pow) |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 158 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | FUNC1(math_sin, sin) |
| 160 | FUNC1(math_sinh, sinh) |
| 161 | FUNC1(math_sqrt, sqrt) |
| 162 | FUNC1(math_tan, tan) |
| 163 | FUNC1(math_tanh, tanh) |
| 164 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 166 | static object * |
| 167 | math_frexp(self, args) |
| 168 | object *self; |
| 169 | object *args; |
| 170 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 171 | double x; |
| 172 | int i; |
| 173 | if (!getdoublearg(args, &x)) |
| 174 | return NULL; |
| 175 | errno = 0; |
| 176 | x = frexp(x, &i); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 177 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 178 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 179 | return math_error(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 180 | return mkvalue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static object * |
| 184 | math_ldexp(self, args) |
| 185 | object *self; |
| 186 | object *args; |
| 187 | { |
| 188 | double x, y; |
| 189 | /* Cheat -- allow float as second argument */ |
| 190 | if (!get2doublearg(args, &x, &y)) |
| 191 | return NULL; |
| 192 | errno = 0; |
| 193 | x = ldexp(x, (int)y); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 194 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 195 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 196 | return math_error(); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 197 | else |
| 198 | return newfloatobject(x); |
| 199 | } |
| 200 | |
| 201 | static object * |
| 202 | math_modf(self, args) |
| 203 | object *self; |
| 204 | object *args; |
| 205 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 206 | double x, y; |
| 207 | if (!getdoublearg(args, &x)) |
| 208 | return NULL; |
| 209 | errno = 0; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 210 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 211 | { |
| 212 | extended e; |
| 213 | x = modf(x, &e); |
| 214 | y = e; |
| 215 | } |
| 216 | #else |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 217 | x = modf(x, &y); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | #endif |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 219 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 220 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 221 | return math_error(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 222 | return mkvalue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 223 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | |
| 225 | static struct methodlist math_methods[] = { |
| 226 | {"acos", math_acos}, |
| 227 | {"asin", math_asin}, |
| 228 | {"atan", math_atan}, |
| 229 | {"atan2", math_atan2}, |
| 230 | {"ceil", math_ceil}, |
| 231 | {"cos", math_cos}, |
| 232 | {"cosh", math_cosh}, |
| 233 | {"exp", math_exp}, |
| 234 | {"fabs", math_fabs}, |
| 235 | {"floor", math_floor}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | {"fmod", math_fmod}, |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 237 | {"frexp", math_frexp}, |
Guido van Rossum | 411a8bd | 1994-10-20 22:00:28 +0000 | [diff] [blame] | 238 | {"hypot", math_hypot}, |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 239 | {"ldexp", math_ldexp}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | {"log", math_log}, |
| 241 | {"log10", math_log10}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 242 | {"modf", math_modf}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | {"pow", math_pow}, |
| 244 | {"sin", math_sin}, |
| 245 | {"sinh", math_sinh}, |
| 246 | {"sqrt", math_sqrt}, |
| 247 | {"tan", math_tan}, |
| 248 | {"tanh", math_tanh}, |
| 249 | {NULL, NULL} /* sentinel */ |
| 250 | }; |
| 251 | |
| 252 | void |
| 253 | initmath() |
| 254 | { |
| 255 | object *m, *d, *v; |
Guido van Rossum | 738d4dd | 1990-10-26 14:59:30 +0000 | [diff] [blame] | 256 | |
| 257 | m = initmodule("math", math_methods); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 258 | d = getmoduledict(m); |
Guido van Rossum | eb38d24 | 1990-11-18 17:36:45 +0000 | [diff] [blame] | 259 | dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0)); |
| 260 | DECREF(v); |
| 261 | dictinsert(d, "e", v = newfloatobject(exp(1.0))); |
| 262 | DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | } |