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