Mercurial > cpython-lru_cache-weakref
annotate Objects/floatobject.c @ 4282:daf12f64d5aa legacy-trunk
Get rid of bogus MSC_VER undef of CHECK()
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 11 Sep 1996 20:21:41 +0000 |
| parents | 1a0aa986f6fa |
| children | 65d2d8e67da3 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
3 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> |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
35 #include "mymath.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 |
|
3704
17030515f623
Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents:
3608
diff
changeset
|
42 #if defined(HUGE_VAL) && !defined(CHECK) |
|
496
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 |
|
3704
17030515f623
Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents:
3608
diff
changeset
|
46 #endif |
|
17030515f623
Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents:
3608
diff
changeset
|
47 |
|
17030515f623
Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents:
3608
diff
changeset
|
48 #ifndef CHECK |
|
496
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
49 #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
|
50 #endif |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
51 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
52 #ifdef HAVE_LIMITS_H |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
53 #include <limits.h> |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
54 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
55 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
56 #ifndef LONG_MAX |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
57 #define LONG_MAX 0X7FFFFFFFL |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
58 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
59 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
60 #ifndef LONG_MIN |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
61 #define LONG_MIN (-LONG_MAX-1) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
62 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
63 |
| 2327 | 64 #ifdef __NeXT__ |
| 65 #ifdef __sparc__ | |
| 66 /* | |
| 67 * This works around a bug in the NS/Sparc 3.3 pre-release | |
| 68 * limits.h header file. | |
| 69 * 10-Feb-1995 bwarsaw@cnri.reston.va.us | |
| 70 */ | |
| 71 #undef LONG_MIN | |
| 72 #define LONG_MIN (-LONG_MAX-1) | |
| 73 #endif | |
| 74 #endif | |
| 75 | |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
76 #if !defined(__STDC__) && !defined(macintosh) |
|
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
|
77 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
|
78 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
|
79 #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
|
80 |
| 6 | 81 object * |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
82 #ifdef __SC__ |
|
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
83 newfloatobject(double fval) |
|
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
84 #else |
| 6 | 85 newfloatobject(fval) |
| 86 double fval; | |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
87 #endif |
| 6 | 88 { |
| 89 /* For efficiency, this code is copied from newobject() */ | |
| 90 register floatobject *op = (floatobject *) malloc(sizeof(floatobject)); | |
| 27 | 91 if (op == NULL) |
| 92 return err_nomem(); | |
| 93 op->ob_type = &Floattype; | |
| 94 op->ob_fval = fval; | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1319
diff
changeset
|
95 NEWREF(op); |
| 6 | 96 return (object *) op; |
| 97 } | |
| 98 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
99 static void |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
100 float_dealloc(op) |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
101 object *op; |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
102 { |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
103 DEL(op); |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
104 } |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
105 |
| 6 | 106 double |
| 107 getfloatvalue(op) | |
| 108 object *op; | |
| 109 { | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
110 number_methods *nb; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
111 floatobject *fo; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
112 double val; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
113 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
114 if (op && is_floatobject(op)) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
115 return GETFLOATVALUE((floatobject*) op); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
116 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
117 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
118 nb->nb_float == NULL) { |
| 27 | 119 err_badarg(); |
| 6 | 120 return -1; |
| 121 } | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
122 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
123 fo = (floatobject*) (*nb->nb_float) (op); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
124 if (fo == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
125 return -1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
126 if (!is_floatobject(fo)) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
127 err_setstr(TypeError, "nb_float should return float object"); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
128 return -1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
129 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
130 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
131 val = GETFLOATVALUE(fo); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
132 DECREF(fo); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
133 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
134 return val; |
| 6 | 135 } |
| 136 | |
| 137 /* Methods */ | |
| 138 | |
|
268
0fe2471166f0
Export float_buf_repr() interface, for marshalling of floats.
Guido van Rossum <guido@python.org>
parents:
249
diff
changeset
|
139 void |
| 6 | 140 float_buf_repr(buf, v) |
| 141 char *buf; | |
| 142 floatobject *v; | |
| 143 { | |
| 144 register char *cp; | |
| 145 /* Subroutine for float_repr and float_print. | |
| 146 We want float numbers to be recognizable as such, | |
| 147 i.e., they should contain a decimal point or an exponent. | |
| 148 However, %g may print the number as an integer; | |
| 149 in such cases, we append ".0" to the string. */ | |
| 150 sprintf(buf, "%.12g", v->ob_fval); | |
| 151 cp = buf; | |
| 152 if (*cp == '-') | |
| 153 cp++; | |
| 154 for (; *cp != '\0'; cp++) { | |
| 155 /* Any non-digit means it's not an integer; | |
| 156 this takes care of NAN and INF as well. */ | |
|
2308
14b126d0b5be
use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents:
2156
diff
changeset
|
157 if (!isdigit(Py_CHARMASK(*cp))) |
| 6 | 158 break; |
| 159 } | |
| 160 if (*cp == '\0') { | |
| 161 *cp++ = '.'; | |
| 162 *cp++ = '0'; | |
| 163 *cp++ = '\0'; | |
| 164 } | |
| 165 } | |
| 166 | |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
167 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
168 static int |
| 6 | 169 float_print(v, fp, flags) |
| 170 floatobject *v; | |
| 171 FILE *fp; | |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
172 int flags; /* Not used but required by interface */ |
| 6 | 173 { |
| 174 char buf[100]; | |
| 175 float_buf_repr(buf, v); | |
| 176 fputs(buf, fp); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
177 return 0; |
| 6 | 178 } |
| 179 | |
| 180 static object * | |
| 181 float_repr(v) | |
| 182 floatobject *v; | |
| 183 { | |
| 184 char buf[100]; | |
| 185 float_buf_repr(buf, v); | |
| 186 return newstringobject(buf); | |
| 187 } | |
| 188 | |
| 189 static int | |
| 190 float_compare(v, w) | |
| 191 floatobject *v, *w; | |
| 192 { | |
| 193 double i = v->ob_fval; | |
| 194 double j = w->ob_fval; | |
| 195 return (i < j) ? -1 : (i > j) ? 1 : 0; | |
| 196 } | |
| 197 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
198 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
199 float_hash(v) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
200 floatobject *v; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
201 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
202 double intpart, fractpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
203 int expo; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
204 long x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
205 /* 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
|
206 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
|
207 of mapping keys will turn out weird */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
208 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
209 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
210 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
211 extended e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
212 fractpart = modf(v->ob_fval, &e); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
213 intpart = e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
214 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
215 #else |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
216 fractpart = modf(v->ob_fval, &intpart); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
217 #endif |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
218 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
219 if (fractpart == 0.0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
220 if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
221 /* 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
|
222 object *w = dnewlongobject(v->ob_fval); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
223 if (w == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
224 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
225 x = hashobject(w); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
226 DECREF(w); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
227 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
228 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
229 x = (long)intpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
230 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
231 else { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
232 fractpart = frexp(fractpart, &expo); |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
233 fractpart = fractpart*2147483648.0; /* 2**31 */ |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
234 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
|
235 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
236 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
237 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
238 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
239 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
240 |
| 6 | 241 static object * |
| 242 float_add(v, w) | |
| 243 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
244 floatobject *w; |
| 6 | 245 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
246 return newfloatobject(v->ob_fval + w->ob_fval); |
| 6 | 247 } |
| 248 | |
| 249 static object * | |
| 250 float_sub(v, w) | |
| 251 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
252 floatobject *w; |
| 6 | 253 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
254 return newfloatobject(v->ob_fval - w->ob_fval); |
| 6 | 255 } |
| 256 | |
| 257 static object * | |
| 258 float_mul(v, w) | |
| 259 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
260 floatobject *w; |
| 6 | 261 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
262 return newfloatobject(v->ob_fval * w->ob_fval); |
| 6 | 263 } |
| 264 | |
| 265 static object * | |
| 266 float_div(v, w) | |
| 267 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
268 floatobject *w; |
| 6 | 269 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
270 if (w->ob_fval == 0) { |
|
470
a1025cbba30d
Formulate better error strings.
Guido van Rossum <guido@python.org>
parents:
405
diff
changeset
|
271 err_setstr(ZeroDivisionError, "float division"); |
| 6 | 272 return NULL; |
| 273 } | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
274 return newfloatobject(v->ob_fval / w->ob_fval); |
| 6 | 275 } |
| 276 | |
| 277 static object * | |
| 278 float_rem(v, w) | |
| 279 floatobject *v; | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
280 floatobject *w; |
| 6 | 281 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
282 double vx, wx; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
283 double /* div, */ mod; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
284 wx = w->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
285 if (wx == 0.0) { |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
286 err_setstr(ZeroDivisionError, "float modulo"); |
| 6 | 287 return NULL; |
| 288 } | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
289 vx = v->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
290 mod = fmod(vx, wx); |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
291 /* div = (vx - mod) / wx; */ |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
292 if (wx*mod < 0) { |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
293 mod += wx; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
294 /* div -= 1.0; */ |
| 6 | 295 } |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
296 return newfloatobject(mod); |
| 6 | 297 } |
| 298 | |
| 299 static object * | |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
300 float_divmod(v, w) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
301 floatobject *v; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
302 floatobject *w; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
303 { |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
304 double vx, wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
305 double div, mod; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
306 wx = w->ob_fval; |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
307 if (wx == 0.0) { |
|
470
a1025cbba30d
Formulate better error strings.
Guido van Rossum <guido@python.org>
parents:
405
diff
changeset
|
308 err_setstr(ZeroDivisionError, "float divmod()"); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
309 return NULL; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
310 } |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
311 vx = v->ob_fval; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
312 mod = fmod(vx, wx); |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
313 div = (vx - mod) / wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
314 if (wx*mod < 0) { |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
315 mod += wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
316 div -= 1.0; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
317 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
318 return mkvalue("(dd)", div, mod); |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
319 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
320 |
|
3608
8573e65fa948
make some things static
Guido van Rossum <guido@python.org>
parents:
3329
diff
changeset
|
321 static double powu(x, n) |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
322 double x; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
323 long n; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
324 { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
325 double r = 1.; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
326 double p = x; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
327 long mask = 1; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
328 while (mask > 0 && n >= mask) { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
329 if (n & mask) |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
330 r *= p; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
331 mask <<= 1; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
332 p *= p; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
333 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
334 return r; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
335 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
336 |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
337 static object * |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
338 float_pow(v, w, z) |
| 6 | 339 floatobject *v; |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
340 object *w; |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
341 floatobject *z; |
| 6 | 342 { |
| 343 double iv, iw, ix; | |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
344 long intw; |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
345 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :( |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
346 * The z parameter is really only going to be useful for integers and |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
347 * long integers. Maybe something clever with logarithms could be done. |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
348 * [AMK] |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
349 */ |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
350 iv = v->ob_fval; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
351 iw = ((floatobject *)w)->ob_fval; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
352 intw = (long)iw; |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
353 if (iw == intw && -10000 < intw && intw < 10000) { |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
354 /* Sort out special cases here instead of relying on pow() */ |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
355 if (intw == 0) { /* x**0 is 1, even 0**0 */ |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
356 if ((object *)z!=None) { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
357 ix=fmod(1.0, z->ob_fval); |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
358 if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
359 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
360 else ix=1.0; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
361 return newfloatobject(ix); |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
362 } |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
363 errno = 0; |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
364 if (intw > 0) |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
365 ix = powu(iv, intw); |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
366 else |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
367 ix = 1./powu(iv, -intw); |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
368 } |
|
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
369 else { |
|
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
370 /* Sort out special cases here instead of relying on pow() */ |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
371 if (iv == 0.0) { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
372 if (iw < 0.0) { |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
373 err_setstr(ValueError, |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
374 "0.0 to a negative power"); |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
375 return NULL; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
376 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
377 return newfloatobject(0.0); |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
378 } |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
379 if (iv < 0.0) { |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
380 err_setstr(ValueError, |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
381 "negative number to a float power"); |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
382 return NULL; |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
383 } |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
384 errno = 0; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
385 ix = pow(iv, iw); |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
386 } |
|
496
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
387 CHECK(ix); |
| 27 | 388 if (errno != 0) { |
| 389 /* XXX could it be another type of error? */ | |
| 390 err_errno(OverflowError); | |
| 6 | 391 return NULL; |
| 27 | 392 } |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
393 if ((object *)z!=None) { |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
394 ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */ |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
395 if ( ix!=0 && |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
396 ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) { |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
397 ix+=z->ob_fval; |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
398 } |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
399 } |
| 27 | 400 return newfloatobject(ix); |
| 6 | 401 } |
| 402 | |
| 403 static object * | |
| 404 float_neg(v) | |
| 405 floatobject *v; | |
| 406 { | |
| 407 return newfloatobject(-v->ob_fval); | |
| 408 } | |
| 409 | |
| 410 static object * | |
| 411 float_pos(v) | |
| 412 floatobject *v; | |
| 413 { | |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
414 INCREF(v); |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
415 return (object *)v; |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
416 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
417 |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
418 static object * |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
419 float_abs(v) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
420 floatobject *v; |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
421 { |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
422 if (v->ob_fval < 0) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
423 return float_neg(v); |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
424 else |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
425 return float_pos(v); |
| 6 | 426 } |
| 427 | |
| 239 | 428 static int |
| 429 float_nonzero(v) | |
| 430 floatobject *v; | |
| 431 { | |
| 432 return v->ob_fval != 0.0; | |
| 433 } | |
| 434 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
435 static int |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
436 float_coerce(pv, pw) |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
437 object **pv; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
438 object **pw; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
439 { |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
440 if (is_intobject(*pw)) { |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
441 long x = getintvalue(*pw); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
442 *pw = newfloatobject((double)x); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
443 INCREF(*pv); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
444 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
445 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
446 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
|
447 *pw = newfloatobject(dgetlongvalue(*pw)); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
448 INCREF(*pv); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
449 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
450 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
451 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
|
452 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
453 |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
454 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
455 float_int(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
456 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
457 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
458 double x = getfloatvalue(v); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
459 if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
460 : (x = floor(x)) > (double)LONG_MAX) { |
| 2327 | 461 err_setstr(OverflowError, "float too large to convert"); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
462 return NULL; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
463 } |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
464 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
|
465 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
466 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
467 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
468 float_long(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
469 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
470 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
471 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
|
472 return dnewlongobject(x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
473 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
474 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
475 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
476 float_float(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
477 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
478 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
479 INCREF(v); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
480 return v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
481 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
482 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
483 |
| 6 | 484 static number_methods float_as_number = { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
485 (binaryfunc)float_add, /*nb_add*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
486 (binaryfunc)float_sub, /*nb_subtract*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
487 (binaryfunc)float_mul, /*nb_multiply*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
488 (binaryfunc)float_div, /*nb_divide*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
489 (binaryfunc)float_rem, /*nb_remainder*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
490 (binaryfunc)float_divmod, /*nb_divmod*/ |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
491 (ternaryfunc)float_pow, /*nb_power*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
492 (unaryfunc)float_neg, /*nb_negative*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
493 (unaryfunc)float_pos, /*nb_positive*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
494 (unaryfunc)float_abs, /*nb_absolute*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
495 (inquiry)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
|
496 0, /*nb_invert*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
497 0, /*nb_lshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
498 0, /*nb_rshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
499 0, /*nb_and*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
500 0, /*nb_xor*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
501 0, /*nb_or*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
502 (coercion)float_coerce, /*nb_coerce*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
503 (unaryfunc)float_int, /*nb_int*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
504 (unaryfunc)float_long, /*nb_long*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
505 (unaryfunc)float_float, /*nb_float*/ |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
506 0, /*nb_oct*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
507 0, /*nb_hex*/ |
| 6 | 508 }; |
| 509 | |
| 510 typeobject Floattype = { | |
| 511 OB_HEAD_INIT(&Typetype) | |
| 512 0, | |
| 513 "float", | |
| 514 sizeof(floatobject), | |
| 515 0, | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
516 (destructor)float_dealloc, /*tp_dealloc*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
517 (printfunc)float_print, /*tp_print*/ |
| 6 | 518 0, /*tp_getattr*/ |
| 519 0, /*tp_setattr*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
520 (cmpfunc)float_compare, /*tp_compare*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
521 (reprfunc)float_repr, /*tp_repr*/ |
| 6 | 522 &float_as_number, /*tp_as_number*/ |
| 523 0, /*tp_as_sequence*/ | |
| 524 0, /*tp_as_mapping*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
525 (hashfunc)float_hash, /*tp_hash*/ |
| 6 | 526 }; |
