Mercurial > cpython-lru_cache-weakref
annotate Objects/floatobject.c @ 1319:cd397266dc1c legacy-trunk
* Makefile: added all: and default: targets.
* many files: made some functions static; removed "extern int errno;".
* frozenmain.c: fixed bugs introduced on 24 June...
* flmodule.c: remove 1.5 bw compat hacks, add new functions in 2.2a
(and some old functions that were omitted).
* timemodule.c: added MSDOS floatsleep version .
* pgenmain.c: changed exit() to goaway() and added defn of goaway().
* intrcheck.c: add hack (to UNIX only) so interrupting 3 times
will exit from a hanging program. The second interrupt prints
a message explaining this to the user.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 05 Jul 1993 10:31:29 +0000 |
| parents | 6ee380349c84 |
| children | a22a350d3a5d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
3 Amsterdam, The Netherlands. |
| 145 | 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 /* Float object implementation */ |
| 26 | |
| 27 | 27 /* XXX There should be overflow checks here, but it's hard to check |
| 28 for any kind of float exception without losing portability. */ | |
| 29 | |
| 86 | 30 #include "allobjects.h" |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
31 #include "modsupport.h" |
| 6 | 32 |
| 91 | 33 #include <errno.h> |
| 86 | 34 #include <ctype.h> |
| 35 #include <math.h> | |
| 6 | 36 |
|
1271
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
37 #ifdef i860 |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
38 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
39 #undef HUGE_VAL |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
40 #endif |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
41 |
|
496
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
42 #ifdef HUGE_VAL |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
43 #define CHECK(x) if (errno != 0) ; \ |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
44 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
45 else errno = ERANGE |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
46 #else |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
47 #define CHECK(x) /* Don't know how to check */ |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
48 #endif |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
49 |
|
60
4f593b38d061
Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents:
46
diff
changeset
|
50 #ifndef THINK_C |
|
4f593b38d061
Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents:
46
diff
changeset
|
51 extern double fmod PROTO((double, double)); |
|
4f593b38d061
Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents:
46
diff
changeset
|
52 extern double pow PROTO((double, double)); |
|
4f593b38d061
Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents:
46
diff
changeset
|
53 #endif |
|
4f593b38d061
Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents:
46
diff
changeset
|
54 |
| 6 | 55 object * |
| 56 newfloatobject(fval) | |
| 57 double fval; | |
| 58 { | |
| 59 /* For efficiency, this code is copied from newobject() */ | |
| 60 register floatobject *op = (floatobject *) malloc(sizeof(floatobject)); | |
| 27 | 61 if (op == NULL) |
| 62 return err_nomem(); | |
| 63 NEWREF(op); | |
| 64 op->ob_type = &Floattype; | |
| 65 op->ob_fval = fval; | |
| 6 | 66 return (object *) op; |
| 67 } | |
| 68 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
69 static void |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
70 float_dealloc(op) |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
71 object *op; |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
72 { |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
73 DEL(op); |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
74 } |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
75 |
| 6 | 76 double |
| 77 getfloatvalue(op) | |
| 78 object *op; | |
| 79 { | |
| 80 if (!is_floatobject(op)) { | |
| 27 | 81 err_badarg(); |
| 6 | 82 return -1; |
| 83 } | |
| 84 else | |
| 85 return ((floatobject *)op) -> ob_fval; | |
| 86 } | |
| 87 | |
| 88 /* Methods */ | |
| 89 | |
|
268
0fe2471166f0
Export float_buf_repr() interface, for marshalling of floats.
Guido van Rossum <guido@python.org>
parents:
249
diff
changeset
|
90 void |
| 6 | 91 float_buf_repr(buf, v) |
| 92 char *buf; | |
| 93 floatobject *v; | |
| 94 { | |
| 95 register char *cp; | |
| 96 /* Subroutine for float_repr and float_print. | |
| 97 We want float numbers to be recognizable as such, | |
| 98 i.e., they should contain a decimal point or an exponent. | |
| 99 However, %g may print the number as an integer; | |
| 100 in such cases, we append ".0" to the string. */ | |
| 101 sprintf(buf, "%.12g", v->ob_fval); | |
| 102 cp = buf; | |
| 103 if (*cp == '-') | |
| 104 cp++; | |
| 105 for (; *cp != '\0'; cp++) { | |
| 106 /* Any non-digit means it's not an integer; | |
| 107 this takes care of NAN and INF as well. */ | |
| 108 if (!isdigit(*cp)) | |
| 109 break; | |
| 110 } | |
| 111 if (*cp == '\0') { | |
| 112 *cp++ = '.'; | |
| 113 *cp++ = '0'; | |
| 114 *cp++ = '\0'; | |
| 115 } | |
| 116 } | |
| 117 | |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
118 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
119 static int |
| 6 | 120 float_print(v, fp, flags) |
| 121 floatobject *v; | |
| 122 FILE *fp; | |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
123 int flags; /* Not used but required by interface */ |
| 6 | 124 { |
| 125 char buf[100]; | |
| 126 float_buf_repr(buf, v); | |
| 127 fputs(buf, fp); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
128 return 0; |
| 6 | 129 } |
| 130 | |
| 131 static object * | |
| 132 float_repr(v) | |
| 133 floatobject *v; | |
| 134 { | |
| 135 char buf[100]; | |
| 136 float_buf_repr(buf, v); | |
| 137 return newstringobject(buf); | |
| 138 } | |
| 139 | |
| 140 static int | |
| 141 float_compare(v, w) | |
| 142 floatobject *v, *w; | |
| 143 { | |
| 144 double i = v->ob_fval; | |
| 145 double j = w->ob_fval; | |
| 146 return (i < j) ? -1 : (i > j) ? 1 : 0; | |
| 147 } | |
| 148 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
149 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
150 float_hash(v) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
151 floatobject *v; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
152 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
153 double intpart, fractpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
154 int expo; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
155 long x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
156 /* This is designed so that Python numbers with the same |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
157 value hash to the same value, otherwise comparisons |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
158 of mapping keys will turn out weird */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
159 fractpart = modf(v->ob_fval, &intpart); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
160 if (fractpart == 0.0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
161 if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
162 /* Convert to long int and use its hash... */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
163 object *w = dnewlongobject(v->ob_fval); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
164 if (w == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
165 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
166 x = hashobject(w); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
167 DECREF(w); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
168 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
169 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
170 x = (long)intpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
171 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
172 else { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
173 fractpart = frexp(fractpart, &expo); |
|
1271
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
174 fractpart = fractpart*4294967296.0; /* 2**32 */ |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
175 x = (long) (intpart + fractpart) ^ expo; /* Rather arbitrary */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
176 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
177 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
178 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
179 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
180 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
181 |
| 6 | 182 static object * |
| 183 float_add(v, w) | |
| 184 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
185 floatobject *w; |
| 6 | 186 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
187 return newfloatobject(v->ob_fval + w->ob_fval); |
| 6 | 188 } |
| 189 | |
| 190 static object * | |
| 191 float_sub(v, w) | |
| 192 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
193 floatobject *w; |
| 6 | 194 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
195 return newfloatobject(v->ob_fval - w->ob_fval); |
| 6 | 196 } |
| 197 | |
| 198 static object * | |
| 199 float_mul(v, w) | |
| 200 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
201 floatobject *w; |
| 6 | 202 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
203 return newfloatobject(v->ob_fval * w->ob_fval); |
| 6 | 204 } |
| 205 | |
| 206 static object * | |
| 207 float_div(v, w) | |
| 208 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
209 floatobject *w; |
| 6 | 210 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
211 if (w->ob_fval == 0) { |
|
470
a1025cbba30d
Formulate better error strings.
Guido van Rossum <guido@python.org>
parents:
405
diff
changeset
|
212 err_setstr(ZeroDivisionError, "float division"); |
| 6 | 213 return NULL; |
| 214 } | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
215 return newfloatobject(v->ob_fval / w->ob_fval); |
| 6 | 216 } |
| 217 | |
| 218 static object * | |
| 219 float_rem(v, w) | |
| 220 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
221 floatobject *w; |
| 6 | 222 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
223 double vx, wx; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
224 double /* div, */ mod; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
225 wx = w->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
226 if (wx == 0.0) { |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
227 err_setstr(ZeroDivisionError, "float modulo"); |
| 6 | 228 return NULL; |
| 229 } | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
230 vx = v->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
231 mod = fmod(vx, wx); |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
232 /* div = (vx - mod) / wx; */ |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
233 if (wx*mod < 0) { |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
234 mod += wx; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
235 /* div -= 1.0; */ |
| 6 | 236 } |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
237 return newfloatobject(mod); |
| 6 | 238 } |
| 239 | |
| 240 static object * | |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
241 float_divmod(v, w) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
242 floatobject *v; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
243 floatobject *w; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
244 { |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
245 double vx, wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
246 double div, mod; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
247 object *t; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
248 wx = w->ob_fval; |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
249 if (wx == 0.0) { |
|
470
a1025cbba30d
Formulate better error strings.
Guido van Rossum <guido@python.org>
parents:
405
diff
changeset
|
250 err_setstr(ZeroDivisionError, "float divmod()"); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
251 return NULL; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
252 } |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
253 vx = v->ob_fval; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
254 mod = fmod(vx, wx); |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
255 div = (vx - mod) / wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
256 if (wx*mod < 0) { |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
257 mod += wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
258 div -= 1.0; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
259 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
260 return mkvalue("(dd)", div, mod); |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
261 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
262 |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
263 static object * |
| 6 | 264 float_pow(v, w) |
| 265 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
266 floatobject *w; |
| 6 | 267 { |
| 268 double iv, iw, ix; | |
| 269 iv = v->ob_fval; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
270 iw = w->ob_fval; |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
271 /* Sort out special cases here instead of relying on pow() */ |
| 27 | 272 if (iw == 0.0) |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
273 return newfloatobject(1.0); /* x**0 is 1, even 0**0 */ |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
274 if (iv == 0.0) { |
|
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
275 if (iw < 0.0) { |
|
470
a1025cbba30d
Formulate better error strings.
Guido van Rossum <guido@python.org>
parents:
405
diff
changeset
|
276 err_setstr(ValueError, "0.0 to the negative power"); |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
277 return NULL; |
|
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
278 } |
|
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
279 return newfloatobject(0.0); |
|
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
280 } |
|
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
281 if (iv < 0.0) { |
|
470
a1025cbba30d
Formulate better error strings.
Guido van Rossum <guido@python.org>
parents:
405
diff
changeset
|
282 err_setstr(ValueError, "negative float to float power"); |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
283 return NULL; |
|
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
284 } |
| 6 | 285 errno = 0; |
| 286 ix = pow(iv, iw); | |
|
496
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
287 CHECK(ix); |
| 27 | 288 if (errno != 0) { |
| 289 /* XXX could it be another type of error? */ | |
| 290 err_errno(OverflowError); | |
| 6 | 291 return NULL; |
| 27 | 292 } |
| 293 return newfloatobject(ix); | |
| 6 | 294 } |
| 295 | |
| 296 static object * | |
| 297 float_neg(v) | |
| 298 floatobject *v; | |
| 299 { | |
| 300 return newfloatobject(-v->ob_fval); | |
| 301 } | |
| 302 | |
| 303 static object * | |
| 304 float_pos(v) | |
| 305 floatobject *v; | |
| 306 { | |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
307 INCREF(v); |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
308 return (object *)v; |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
309 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
310 |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
311 static object * |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
312 float_abs(v) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
313 floatobject *v; |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
314 { |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
315 if (v->ob_fval < 0) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
316 return float_neg(v); |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
317 else |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
318 return float_pos(v); |
| 6 | 319 } |
| 320 | |
| 239 | 321 static int |
| 322 float_nonzero(v) | |
| 323 floatobject *v; | |
| 324 { | |
| 325 return v->ob_fval != 0.0; | |
| 326 } | |
| 327 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
328 static int |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
329 float_coerce(pv, pw) |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
330 object **pv; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
331 object **pw; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
332 { |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
333 if (is_intobject(*pw)) { |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
334 long x = getintvalue(*pw); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
335 *pw = newfloatobject((double)x); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
336 INCREF(*pv); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
337 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
338 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
339 else if (is_longobject(*pw)) { |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
340 *pw = newfloatobject(dgetlongvalue(*pw)); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
341 INCREF(*pv); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
342 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
343 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
344 return 1; /* Can't do it */ |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
345 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
346 |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
347 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
348 float_int(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
349 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
350 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
351 double x = getfloatvalue(v); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
352 /* XXX should check for overflow */ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
353 /* XXX should define how we round */ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
354 return newintobject((long)x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
355 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
356 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
357 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
358 float_long(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
359 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
360 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
361 double x = getfloatvalue(v); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
362 return dnewlongobject(x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
363 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
364 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
365 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
366 float_float(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
367 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
368 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
369 INCREF(v); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
370 return v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
371 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
372 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
373 |
| 6 | 374 static number_methods float_as_number = { |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
375 float_add, /*nb_add*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
376 float_sub, /*nb_subtract*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
377 float_mul, /*nb_multiply*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
378 float_div, /*nb_divide*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
379 float_rem, /*nb_remainder*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
380 float_divmod, /*nb_divmod*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
381 float_pow, /*nb_power*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
382 float_neg, /*nb_negative*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
383 float_pos, /*nb_positive*/ |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
384 float_abs, /*nb_absolute*/ |
| 239 | 385 float_nonzero, /*nb_nonzero*/ |
|
405
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
386 0, /*nb_invert*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
387 0, /*nb_lshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
388 0, /*nb_rshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
389 0, /*nb_and*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
390 0, /*nb_xor*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
391 0, /*nb_or*/ |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
392 float_coerce, /*nb_coerce*/ |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
393 float_int, /*nb_int*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
394 float_long, /*nb_long*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
395 float_float, /*nb_float*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
396 0, /*nb_oct*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
397 0, /*nb_hex*/ |
| 6 | 398 }; |
| 399 | |
| 400 typeobject Floattype = { | |
| 401 OB_HEAD_INIT(&Typetype) | |
| 402 0, | |
| 403 "float", | |
| 404 sizeof(floatobject), | |
| 405 0, | |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
406 float_dealloc, /*tp_dealloc*/ |
| 6 | 407 float_print, /*tp_print*/ |
| 408 0, /*tp_getattr*/ | |
| 409 0, /*tp_setattr*/ | |
| 410 float_compare, /*tp_compare*/ | |
| 411 float_repr, /*tp_repr*/ | |
| 412 &float_as_number, /*tp_as_number*/ | |
| 413 0, /*tp_as_sequence*/ | |
| 414 0, /*tp_as_mapping*/ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
415 float_hash, /*tp_hash */ |
| 6 | 416 }; |
