Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | bab9d03 | 1992-04-05 14:26:55 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 3 | Netherlands. |
| 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> |
| 30 | #ifndef errno |
| 31 | extern int errno; |
| 32 | #endif |
| 33 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | #include "modsupport.h" |
| 35 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | #include <math.h> |
| 37 | |
Guido van Rossum | 2d709b1 | 1992-01-14 18:37:27 +0000 | [diff] [blame] | 38 | #ifndef __STDC__ |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 39 | extern double fmod PROTO((double, double)); |
Guido van Rossum | 2d709b1 | 1992-01-14 18:37:27 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 42 | #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 | |
| 50 | static object * |
| 51 | math_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 Rossum | 444db07 | 1992-02-26 15:26:56 +0000 | [diff] [blame] | 58 | err_errno(ValueError); /* Unexpected math error */ |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
| 61 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | static object * |
| 63 | math_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 Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 72 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 74 | return math_error(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | else |
| 76 | return newfloatobject(x); |
| 77 | } |
| 78 | |
| 79 | static object * |
| 80 | math_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 Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 89 | CHECK(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 91 | return math_error(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | 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 | |
| 106 | FUNC1(math_acos, acos) |
| 107 | FUNC1(math_asin, asin) |
| 108 | FUNC1(math_atan, atan) |
| 109 | FUNC2(math_atan2, atan2) |
| 110 | FUNC1(math_ceil, ceil) |
| 111 | FUNC1(math_cos, cos) |
| 112 | FUNC1(math_cosh, cosh) |
| 113 | FUNC1(math_exp, exp) |
| 114 | FUNC1(math_fabs, fabs) |
| 115 | FUNC1(math_floor, floor) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | FUNC2(math_fmod, fmod) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | FUNC1(math_log, log) |
| 118 | FUNC1(math_log10, log10) |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 119 | #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] | 120 | FUNC2(math_pow, power) |
| 121 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 122 | FUNC2(math_pow, pow) |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 123 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | FUNC1(math_sin, sin) |
| 125 | FUNC1(math_sinh, sinh) |
| 126 | FUNC1(math_sqrt, sqrt) |
| 127 | FUNC1(math_tan, tan) |
| 128 | FUNC1(math_tanh, tanh) |
| 129 | |
Guido van Rossum | 98d5433 | 1991-11-12 15:44:14 +0000 | [diff] [blame] | 130 | double frexp PROTO((double, int *)); |
| 131 | double ldexp PROTO((double, int)); |
| 132 | double modf PROTO((double, double *)); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 133 | |
| 134 | static object * |
| 135 | math_frexp(self, args) |
| 136 | object *self; |
| 137 | object *args; |
| 138 | { |
| 139 | object *v; |
| 140 | double x; |
| 141 | int i; |
| 142 | if (!getdoublearg(args, &x)) |
| 143 | return NULL; |
| 144 | errno = 0; |
| 145 | x = frexp(x, &i); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 146 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 147 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 148 | return math_error(); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 149 | v = newtupleobject(2); |
| 150 | if (v != NULL) { |
| 151 | settupleitem(v, 0, newfloatobject(x)); |
| 152 | settupleitem(v, 1, newintobject((long)i)); |
| 153 | if (err_occurred()) { |
| 154 | DECREF(v); |
| 155 | v = NULL; |
| 156 | } |
| 157 | } |
| 158 | return v; |
| 159 | } |
| 160 | |
| 161 | static object * |
| 162 | math_ldexp(self, args) |
| 163 | object *self; |
| 164 | object *args; |
| 165 | { |
| 166 | double x, y; |
| 167 | /* Cheat -- allow float as second argument */ |
| 168 | if (!get2doublearg(args, &x, &y)) |
| 169 | return NULL; |
| 170 | errno = 0; |
| 171 | x = ldexp(x, (int)y); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 172 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 173 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 174 | return math_error(); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 175 | else |
| 176 | return newfloatobject(x); |
| 177 | } |
| 178 | |
| 179 | static object * |
| 180 | math_modf(self, args) |
| 181 | object *self; |
| 182 | object *args; |
| 183 | { |
| 184 | object *v; |
| 185 | double x, y; |
| 186 | if (!getdoublearg(args, &x)) |
| 187 | return NULL; |
| 188 | errno = 0; |
| 189 | x = modf(x, &y); |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 190 | CHECK(x); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 191 | if (errno != 0) |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 192 | return math_error(); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 193 | v = newtupleobject(2); |
| 194 | if (v != NULL) { |
| 195 | settupleitem(v, 0, newfloatobject(x)); |
| 196 | settupleitem(v, 1, newfloatobject(y)); |
| 197 | if (err_occurred()) { |
| 198 | DECREF(v); |
| 199 | v = NULL; |
| 200 | } |
| 201 | } |
| 202 | return v; |
| 203 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 204 | |
| 205 | static struct methodlist math_methods[] = { |
| 206 | {"acos", math_acos}, |
| 207 | {"asin", math_asin}, |
| 208 | {"atan", math_atan}, |
| 209 | {"atan2", math_atan2}, |
| 210 | {"ceil", math_ceil}, |
| 211 | {"cos", math_cos}, |
| 212 | {"cosh", math_cosh}, |
| 213 | {"exp", math_exp}, |
| 214 | {"fabs", math_fabs}, |
| 215 | {"floor", math_floor}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 216 | {"fmod", math_fmod}, |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 217 | {"frexp", math_frexp}, |
| 218 | {"ldexp", math_ldexp}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | {"log", math_log}, |
| 220 | {"log10", math_log10}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | {"modf", math_modf}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | {"pow", math_pow}, |
| 223 | {"sin", math_sin}, |
| 224 | {"sinh", math_sinh}, |
| 225 | {"sqrt", math_sqrt}, |
| 226 | {"tan", math_tan}, |
| 227 | {"tanh", math_tanh}, |
| 228 | {NULL, NULL} /* sentinel */ |
| 229 | }; |
| 230 | |
| 231 | void |
| 232 | initmath() |
| 233 | { |
| 234 | object *m, *d, *v; |
Guido van Rossum | 738d4dd | 1990-10-26 14:59:30 +0000 | [diff] [blame] | 235 | |
| 236 | m = initmodule("math", math_methods); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | d = getmoduledict(m); |
Guido van Rossum | eb38d24 | 1990-11-18 17:36:45 +0000 | [diff] [blame] | 238 | dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0)); |
| 239 | DECREF(v); |
| 240 | dictinsert(d, "e", v = newfloatobject(exp(1.0))); |
| 241 | DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 242 | } |