Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame^] | 1 | /* Math module -- standard C math library functions, pi and e */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include "PROTO.h" |
| 7 | #include "object.h" |
| 8 | #include "intobject.h" |
| 9 | #include "tupleobject.h" |
| 10 | #include "floatobject.h" |
| 11 | #include "dictobject.h" |
| 12 | #include "methodobject.h" |
| 13 | #include "moduleobject.h" |
| 14 | #include "objimpl.h" |
| 15 | #include "import.h" |
| 16 | #include "modsupport.h" |
| 17 | |
| 18 | static int |
| 19 | getdoublearg(args, px) |
| 20 | register object *args; |
| 21 | double *px; |
| 22 | { |
| 23 | if (args == NULL) |
| 24 | return err_badarg(); |
| 25 | if (is_floatobject(args)) { |
| 26 | *px = getfloatvalue(args); |
| 27 | return 1; |
| 28 | } |
| 29 | if (is_intobject(args)) { |
| 30 | *px = getintvalue(args); |
| 31 | return 1; |
| 32 | } |
| 33 | return err_badarg(); |
| 34 | } |
| 35 | |
| 36 | static int |
| 37 | get2doublearg(args, px, py) |
| 38 | register object *args; |
| 39 | double *px, *py; |
| 40 | { |
| 41 | if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) |
| 42 | return err_badarg(); |
| 43 | return getdoublearg(gettupleitem(args, 0), px) && |
| 44 | getdoublearg(gettupleitem(args, 1), py); |
| 45 | } |
| 46 | |
| 47 | static object * |
| 48 | math_1(args, func) |
| 49 | object *args; |
| 50 | double (*func) FPROTO((double)); |
| 51 | { |
| 52 | double x; |
| 53 | if (!getdoublearg(args, &x)) |
| 54 | return NULL; |
| 55 | errno = 0; |
| 56 | x = (*func)(x); |
| 57 | if (errno != 0) |
| 58 | return NULL; |
| 59 | else |
| 60 | return newfloatobject(x); |
| 61 | } |
| 62 | |
| 63 | static object * |
| 64 | math_2(args, func) |
| 65 | object *args; |
| 66 | double (*func) FPROTO((double, double)); |
| 67 | { |
| 68 | double x, y; |
| 69 | if (!get2doublearg(args, &x, &y)) |
| 70 | return NULL; |
| 71 | errno = 0; |
| 72 | x = (*func)(x, y); |
| 73 | if (errno != 0) |
| 74 | return NULL; |
| 75 | else |
| 76 | return newfloatobject(x); |
| 77 | } |
| 78 | |
| 79 | #define FUNC1(stubname, func) \ |
| 80 | static object * stubname(self, args) object *self, *args; { \ |
| 81 | return math_1(args, func); \ |
| 82 | } |
| 83 | |
| 84 | #define FUNC2(stubname, func) \ |
| 85 | static object * stubname(self, args) object *self, *args; { \ |
| 86 | return math_2(args, func); \ |
| 87 | } |
| 88 | |
| 89 | FUNC1(math_acos, acos) |
| 90 | FUNC1(math_asin, asin) |
| 91 | FUNC1(math_atan, atan) |
| 92 | FUNC2(math_atan2, atan2) |
| 93 | FUNC1(math_ceil, ceil) |
| 94 | FUNC1(math_cos, cos) |
| 95 | FUNC1(math_cosh, cosh) |
| 96 | FUNC1(math_exp, exp) |
| 97 | FUNC1(math_fabs, fabs) |
| 98 | FUNC1(math_floor, floor) |
| 99 | #if 0 |
| 100 | /* XXX This one is not in the Amoeba library yet, so what the heck... */ |
| 101 | FUNC2(math_fmod, fmod) |
| 102 | #endif |
| 103 | FUNC1(math_log, log) |
| 104 | FUNC1(math_log10, log10) |
| 105 | FUNC2(math_pow, pow) |
| 106 | FUNC1(math_sin, sin) |
| 107 | FUNC1(math_sinh, sinh) |
| 108 | FUNC1(math_sqrt, sqrt) |
| 109 | FUNC1(math_tan, tan) |
| 110 | FUNC1(math_tanh, tanh) |
| 111 | |
| 112 | #if 0 |
| 113 | /* What about these? */ |
| 114 | double frexp(double x, int *i); |
| 115 | double ldexp(double x, int n); |
| 116 | double modf(double x, double *i); |
| 117 | #endif |
| 118 | |
| 119 | static struct methodlist math_methods[] = { |
| 120 | {"acos", math_acos}, |
| 121 | {"asin", math_asin}, |
| 122 | {"atan", math_atan}, |
| 123 | {"atan2", math_atan2}, |
| 124 | {"ceil", math_ceil}, |
| 125 | {"cos", math_cos}, |
| 126 | {"cosh", math_cosh}, |
| 127 | {"exp", math_exp}, |
| 128 | {"fabs", math_fabs}, |
| 129 | {"floor", math_floor}, |
| 130 | #if 0 |
| 131 | {"fmod", math_fmod}, |
| 132 | {"frexp", math_freqp}, |
| 133 | {"ldexp", math_ldexp}, |
| 134 | #endif |
| 135 | {"log", math_log}, |
| 136 | {"log10", math_log10}, |
| 137 | #if 0 |
| 138 | {"modf", math_modf}, |
| 139 | #endif |
| 140 | {"pow", math_pow}, |
| 141 | {"sin", math_sin}, |
| 142 | {"sinh", math_sinh}, |
| 143 | {"sqrt", math_sqrt}, |
| 144 | {"tan", math_tan}, |
| 145 | {"tanh", math_tanh}, |
| 146 | {NULL, NULL} /* sentinel */ |
| 147 | }; |
| 148 | |
| 149 | void |
| 150 | initmath() |
| 151 | { |
| 152 | object *m, *d, *v; |
| 153 | struct methodlist *ml; |
| 154 | if ((m = new_module("math")) == NULL) |
| 155 | fatal("can't create math module"); |
| 156 | d = getmoduledict(m); |
| 157 | for (ml = math_methods; ml->ml_name != NULL; ml++) { |
| 158 | v = newmethodobject(ml->ml_name, ml->ml_meth, (object *)NULL); |
| 159 | if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) { |
| 160 | fatal("can't initialize math module"); |
| 161 | } |
| 162 | DECREF(v); |
| 163 | } |
| 164 | dictinsert(d, "pi", newfloatobject(atan(1.0) * 4.0)); |
| 165 | dictinsert(d, "e", newfloatobject(exp(1.0))); |
| 166 | DECREF(m); |
| 167 | } |