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