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) |
| 90 | #if 0 |
| 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 | |
| 107 | #if 0 |
| 108 | /* What about these? */ |
| 109 | double frexp(double x, int *i); |
| 110 | double ldexp(double x, int n); |
| 111 | double modf(double x, double *i); |
| 112 | #endif |
| 113 | |
| 114 | static struct methodlist math_methods[] = { |
| 115 | {"acos", math_acos}, |
| 116 | {"asin", math_asin}, |
| 117 | {"atan", math_atan}, |
| 118 | {"atan2", math_atan2}, |
| 119 | {"ceil", math_ceil}, |
| 120 | {"cos", math_cos}, |
| 121 | {"cosh", math_cosh}, |
| 122 | {"exp", math_exp}, |
| 123 | {"fabs", math_fabs}, |
| 124 | {"floor", math_floor}, |
| 125 | #if 0 |
| 126 | {"fmod", math_fmod}, |
| 127 | {"frexp", math_freqp}, |
| 128 | {"ldexp", math_ldexp}, |
| 129 | #endif |
| 130 | {"log", math_log}, |
| 131 | {"log10", math_log10}, |
| 132 | #if 0 |
| 133 | {"modf", math_modf}, |
| 134 | #endif |
| 135 | {"pow", math_pow}, |
| 136 | {"sin", math_sin}, |
| 137 | {"sinh", math_sinh}, |
| 138 | {"sqrt", math_sqrt}, |
| 139 | {"tan", math_tan}, |
| 140 | {"tanh", math_tanh}, |
| 141 | {NULL, NULL} /* sentinel */ |
| 142 | }; |
| 143 | |
| 144 | void |
| 145 | initmath() |
| 146 | { |
| 147 | object *m, *d, *v; |
Guido van Rossum | 738d4dd | 1990-10-26 14:59:30 +0000 | [diff] [blame] | 148 | |
| 149 | m = initmodule("math", math_methods); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | d = getmoduledict(m); |
Guido van Rossum | eb38d24 | 1990-11-18 17:36:45 +0000 | [diff] [blame] | 151 | dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0)); |
| 152 | DECREF(v); |
| 153 | dictinsert(d, "e", v = newfloatobject(exp(1.0))); |
| 154 | DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | } |