Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 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 | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | static object * |
| 39 | math_1(args, func) |
| 40 | object *args; |
| 41 | double (*func) FPROTO((double)); |
| 42 | { |
| 43 | double x; |
| 44 | if (!getdoublearg(args, &x)) |
| 45 | return NULL; |
| 46 | errno = 0; |
| 47 | x = (*func)(x); |
| 48 | if (errno != 0) |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 49 | return err_errno(RuntimeError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | else |
| 51 | return newfloatobject(x); |
| 52 | } |
| 53 | |
| 54 | static object * |
| 55 | math_2(args, func) |
| 56 | object *args; |
| 57 | double (*func) FPROTO((double, double)); |
| 58 | { |
| 59 | double x, y; |
| 60 | if (!get2doublearg(args, &x, &y)) |
| 61 | return NULL; |
| 62 | errno = 0; |
| 63 | x = (*func)(x, y); |
| 64 | if (errno != 0) |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 65 | return err_errno(RuntimeError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | else |
| 67 | return newfloatobject(x); |
| 68 | } |
| 69 | |
| 70 | #define FUNC1(stubname, func) \ |
| 71 | static object * stubname(self, args) object *self, *args; { \ |
| 72 | return math_1(args, func); \ |
| 73 | } |
| 74 | |
| 75 | #define FUNC2(stubname, func) \ |
| 76 | static object * stubname(self, args) object *self, *args; { \ |
| 77 | return math_2(args, func); \ |
| 78 | } |
| 79 | |
| 80 | FUNC1(math_acos, acos) |
| 81 | FUNC1(math_asin, asin) |
| 82 | FUNC1(math_atan, atan) |
| 83 | FUNC2(math_atan2, atan2) |
| 84 | FUNC1(math_ceil, ceil) |
| 85 | FUNC1(math_cos, cos) |
| 86 | FUNC1(math_cosh, cosh) |
| 87 | FUNC1(math_exp, exp) |
| 88 | FUNC1(math_fabs, fabs) |
| 89 | FUNC1(math_floor, floor) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 90 | #ifndef AMOEBA |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | /* XXX This one is not in the Amoeba library yet, so what the heck... */ |
| 92 | FUNC2(math_fmod, fmod) |
| 93 | #endif |
| 94 | FUNC1(math_log, log) |
| 95 | FUNC1(math_log10, log10) |
Guido van Rossum | 1492c27 | 1991-07-27 21:38:43 +0000 | [diff] [blame] | 96 | #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] | 97 | FUNC2(math_pow, power) |
| 98 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | FUNC2(math_pow, pow) |
Guido van Rossum | 76f2f2e | 1991-06-24 22:23:10 +0000 | [diff] [blame] | 100 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | FUNC1(math_sin, sin) |
| 102 | FUNC1(math_sinh, sinh) |
| 103 | FUNC1(math_sqrt, sqrt) |
| 104 | FUNC1(math_tan, tan) |
| 105 | FUNC1(math_tanh, tanh) |
| 106 | |
Guido van Rossum | 98d5433 | 1991-11-12 15:44:14 +0000 | [diff] [blame] | 107 | double frexp PROTO((double, int *)); |
| 108 | double ldexp PROTO((double, int)); |
| 109 | double modf PROTO((double, double *)); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 110 | |
| 111 | static object * |
| 112 | math_frexp(self, args) |
| 113 | object *self; |
| 114 | object *args; |
| 115 | { |
| 116 | object *v; |
| 117 | double x; |
| 118 | int i; |
| 119 | if (!getdoublearg(args, &x)) |
| 120 | return NULL; |
| 121 | errno = 0; |
| 122 | x = frexp(x, &i); |
| 123 | if (errno != 0) |
| 124 | return err_errno(RuntimeError); |
| 125 | v = newtupleobject(2); |
| 126 | if (v != NULL) { |
| 127 | settupleitem(v, 0, newfloatobject(x)); |
| 128 | settupleitem(v, 1, newintobject((long)i)); |
| 129 | if (err_occurred()) { |
| 130 | DECREF(v); |
| 131 | v = NULL; |
| 132 | } |
| 133 | } |
| 134 | return v; |
| 135 | } |
| 136 | |
| 137 | static object * |
| 138 | math_ldexp(self, args) |
| 139 | object *self; |
| 140 | object *args; |
| 141 | { |
| 142 | double x, y; |
| 143 | /* Cheat -- allow float as second argument */ |
| 144 | if (!get2doublearg(args, &x, &y)) |
| 145 | return NULL; |
| 146 | errno = 0; |
| 147 | x = ldexp(x, (int)y); |
| 148 | if (errno != 0) |
| 149 | return err_errno(RuntimeError); |
| 150 | else |
| 151 | return newfloatobject(x); |
| 152 | } |
| 153 | |
| 154 | static object * |
| 155 | math_modf(self, args) |
| 156 | object *self; |
| 157 | object *args; |
| 158 | { |
| 159 | object *v; |
| 160 | double x, y; |
| 161 | if (!getdoublearg(args, &x)) |
| 162 | return NULL; |
| 163 | errno = 0; |
| 164 | x = modf(x, &y); |
| 165 | if (errno != 0) |
| 166 | return err_errno(RuntimeError); |
| 167 | v = newtupleobject(2); |
| 168 | if (v != NULL) { |
| 169 | settupleitem(v, 0, newfloatobject(x)); |
| 170 | settupleitem(v, 1, newfloatobject(y)); |
| 171 | if (err_occurred()) { |
| 172 | DECREF(v); |
| 173 | v = NULL; |
| 174 | } |
| 175 | } |
| 176 | return v; |
| 177 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | |
| 179 | static struct methodlist math_methods[] = { |
| 180 | {"acos", math_acos}, |
| 181 | {"asin", math_asin}, |
| 182 | {"atan", math_atan}, |
| 183 | {"atan2", math_atan2}, |
| 184 | {"ceil", math_ceil}, |
| 185 | {"cos", math_cos}, |
| 186 | {"cosh", math_cosh}, |
| 187 | {"exp", math_exp}, |
| 188 | {"fabs", math_fabs}, |
| 189 | {"floor", math_floor}, |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 190 | #ifndef AMOEBA |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 191 | {"fmod", math_fmod}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | #endif |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 193 | {"frexp", math_frexp}, |
| 194 | {"ldexp", math_ldexp}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | {"log", math_log}, |
| 196 | {"log10", math_log10}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | {"modf", math_modf}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 198 | {"pow", math_pow}, |
| 199 | {"sin", math_sin}, |
| 200 | {"sinh", math_sinh}, |
| 201 | {"sqrt", math_sqrt}, |
| 202 | {"tan", math_tan}, |
| 203 | {"tanh", math_tanh}, |
| 204 | {NULL, NULL} /* sentinel */ |
| 205 | }; |
| 206 | |
| 207 | void |
| 208 | initmath() |
| 209 | { |
| 210 | object *m, *d, *v; |
Guido van Rossum | 738d4dd | 1990-10-26 14:59:30 +0000 | [diff] [blame] | 211 | |
| 212 | m = initmodule("math", math_methods); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | d = getmoduledict(m); |
Guido van Rossum | eb38d24 | 1990-11-18 17:36:45 +0000 | [diff] [blame] | 214 | dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0)); |
| 215 | DECREF(v); |
| 216 | dictinsert(d, "e", v = newfloatobject(exp(1.0))); |
| 217 | DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | } |