Mercurial > cpython-lru_cache-weakref
annotate Modules/mathmodule.c @ 438:64bc8a284fb3 legacy-trunk
Added PROTO macro where needed.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 12 Nov 1991 15:44:14 +0000 |
| parents | 9b3d557665a9 |
| children | 56bb1067a880 |
| rev | line source |
|---|---|
| 145 | 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 | |
| 6 | 25 /* Math module -- standard C math library functions, pi and e */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 |
| 92 | 29 #include <errno.h> |
| 30 #ifndef errno | |
| 31 extern int errno; | |
| 32 #endif | |
| 33 | |
| 6 | 34 #include "modsupport.h" |
| 35 | |
| 86 | 36 #include <math.h> |
| 37 | |
| 6 | 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) | |
|
320
26426c42a162
Add some err_errno calls that were missing
Guido van Rossum <guido@python.org>
parents:
289
diff
changeset
|
49 return err_errno(RuntimeError); |
| 6 | 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) | |
|
320
26426c42a162
Add some err_errno calls that were missing
Guido van Rossum <guido@python.org>
parents:
289
diff
changeset
|
65 return err_errno(RuntimeError); |
| 6 | 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) | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
90 #ifndef AMOEBA |
| 6 | 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) | |
|
320
26426c42a162
Add some err_errno calls that were missing
Guido van Rossum <guido@python.org>
parents:
289
diff
changeset
|
96 #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */ |
|
289
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
97 FUNC2(math_pow, power) |
|
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
98 #else |
| 6 | 99 FUNC2(math_pow, pow) |
|
289
c4c278c56e90
Apparently MPW has power() instead of pow().
Guido van Rossum <guido@python.org>
parents:
168
diff
changeset
|
100 #endif |
| 6 | 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 | |
|
438
64bc8a284fb3
Added PROTO macro where needed.
Guido van Rossum <guido@python.org>
parents:
407
diff
changeset
|
107 double frexp PROTO((double, int *)); |
|
64bc8a284fb3
Added PROTO macro where needed.
Guido van Rossum <guido@python.org>
parents:
407
diff
changeset
|
108 double ldexp PROTO((double, int)); |
|
64bc8a284fb3
Added PROTO macro where needed.
Guido van Rossum <guido@python.org>
parents:
407
diff
changeset
|
109 double modf PROTO((double, double *)); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
110 |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
111 static object * |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
112 math_frexp(self, args) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
113 object *self; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
114 object *args; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
115 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
116 object *v; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
117 double x; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
118 int i; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
119 if (!getdoublearg(args, &x)) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
120 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
121 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
122 x = frexp(x, &i); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
123 if (errno != 0) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
124 return err_errno(RuntimeError); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
125 v = newtupleobject(2); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
126 if (v != NULL) { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
127 settupleitem(v, 0, newfloatobject(x)); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
128 settupleitem(v, 1, newintobject((long)i)); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
129 if (err_occurred()) { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
130 DECREF(v); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
131 v = NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
132 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
133 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
134 return v; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
135 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
136 |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
137 static object * |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
138 math_ldexp(self, args) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
139 object *self; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
140 object *args; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
141 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
142 double x, y; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
143 /* Cheat -- allow float as second argument */ |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
144 if (!get2doublearg(args, &x, &y)) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
145 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
146 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
147 x = ldexp(x, (int)y); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
148 if (errno != 0) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
149 return err_errno(RuntimeError); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
150 else |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
151 return newfloatobject(x); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
152 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
153 |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
154 static object * |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
155 math_modf(self, args) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
156 object *self; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
157 object *args; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
158 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
159 object *v; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
160 double x, y; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
161 if (!getdoublearg(args, &x)) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
162 return NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
163 errno = 0; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
164 x = modf(x, &y); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
165 if (errno != 0) |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
166 return err_errno(RuntimeError); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
167 v = newtupleobject(2); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
168 if (v != NULL) { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
169 settupleitem(v, 0, newfloatobject(x)); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
170 settupleitem(v, 1, newfloatobject(y)); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
171 if (err_occurred()) { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
172 DECREF(v); |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
173 v = NULL; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
174 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
175 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
176 return v; |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
177 } |
| 6 | 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}, | |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
190 #ifndef AMOEBA |
| 6 | 191 {"fmod", math_fmod}, |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
192 #endif |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
193 {"frexp", math_frexp}, |
| 6 | 194 {"ldexp", math_ldexp}, |
| 195 {"log", math_log}, | |
| 196 {"log10", math_log10}, | |
| 197 {"modf", math_modf}, | |
| 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; | |
|
42
4835c2d38b84
Take out duplication of code in initmodule().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
211 |
|
4835c2d38b84
Take out duplication of code in initmodule().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
212 m = initmodule("math", math_methods); |
| 6 | 213 d = getmoduledict(m); |
|
75
85eed060a46c
DECREF pi and e after inserting in symbol table.
Guido van Rossum <guido@python.org>
parents:
42
diff
changeset
|
214 dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0)); |
|
85eed060a46c
DECREF pi and e after inserting in symbol table.
Guido van Rossum <guido@python.org>
parents:
42
diff
changeset
|
215 DECREF(v); |
|
85eed060a46c
DECREF pi and e after inserting in symbol table.
Guido van Rossum <guido@python.org>
parents:
42
diff
changeset
|
216 dictinsert(d, "e", v = newfloatobject(exp(1.0))); |
|
85eed060a46c
DECREF pi and e after inserting in symbol table.
Guido van Rossum <guido@python.org>
parents:
42
diff
changeset
|
217 DECREF(v); |
| 6 | 218 } |
