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