Mercurial > cpython-lru_cache-weakref
annotate Objects/floatobject.c @ 7278:59d2761ba1cf legacy-trunk
Remove obsolete pagenumbering / pagestyle cruft.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 13 Feb 1998 07:08:56 +0000 |
| parents | 25505e563b37 |
| children | 48141da518df |
| 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 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4282
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Float object implementation */ |
| 33 | |
| 27 | 34 /* XXX There should be overflow checks here, but it's hard to check |
| 35 for any kind of float exception without losing portability. */ | |
| 36 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
37 #include "Python.h" |
| 6 | 38 |
| 86 | 39 #include <ctype.h> |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
40 #include "mymath.h" |
| 6 | 41 |
|
1271
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
42 #ifdef i860 |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
43 /* 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
|
44 #undef HUGE_VAL |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
45 #endif |
|
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
46 |
|
3704
17030515f623
Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents:
3608
diff
changeset
|
47 #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
|
48 #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
|
49 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
|
50 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
|
51 #endif |
|
17030515f623
Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents:
3608
diff
changeset
|
52 |
|
17030515f623
Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents:
3608
diff
changeset
|
53 #ifndef CHECK |
|
496
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
54 #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
|
55 #endif |
|
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
56 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
57 #ifdef HAVE_LIMITS_H |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
58 #include <limits.h> |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
59 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
60 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
61 #ifndef LONG_MAX |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
62 #define LONG_MAX 0X7FFFFFFFL |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
63 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
64 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
65 #ifndef LONG_MIN |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
66 #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
|
67 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
68 |
| 2327 | 69 #ifdef __NeXT__ |
| 70 #ifdef __sparc__ | |
| 71 /* | |
| 72 * This works around a bug in the NS/Sparc 3.3 pre-release | |
| 73 * limits.h header file. | |
| 74 * 10-Feb-1995 bwarsaw@cnri.reston.va.us | |
| 75 */ | |
| 76 #undef LONG_MIN | |
| 77 #define LONG_MIN (-LONG_MAX-1) | |
| 78 #endif | |
| 79 #endif | |
| 80 | |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
81 #if !defined(__STDC__) && !defined(macintosh) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
82 extern double fmod Py_PROTO((double, double)); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
83 extern double pow Py_PROTO((double, double)); |
|
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
|
84 #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
|
85 |
|
6537
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
86 #ifdef sun |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
87 /* On SunOS4.1 only libm.a exists. Make sure that references to all |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
88 needed math functions exist in the executable, so that dynamic |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
89 loading of mathmodule does not fail. */ |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
90 double (*_Py_math_funcs_hack[])() = { |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
91 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
92 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
93 }; |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
94 #endif |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
95 |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
96 /* Special free list -- see comments for same code in intobject.c. */ |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
97 static PyFloatObject *free_list = NULL; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
98 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
99 #define N_FLOATOBJECTS (BLOCK_SIZE / sizeof(PyFloatObject)) |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
100 |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
101 static PyFloatObject * |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
102 fill_free_list() |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
103 { |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
104 PyFloatObject *p, *q; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
105 p = PyMem_NEW(PyFloatObject, N_FLOATOBJECTS); |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
106 if (p == NULL) |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
107 return (PyFloatObject *)PyErr_NoMemory(); |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
108 q = p + N_FLOATOBJECTS; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
109 while (--q > p) |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
110 *(PyFloatObject **)q = q-1; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
111 *(PyFloatObject **)q = NULL; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
112 return p + N_FLOATOBJECTS - 1; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
113 } |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
114 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
115 PyObject * |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
116 #ifdef __SC__ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
117 PyFloat_FromDouble(double fval) |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
118 #else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
119 PyFloat_FromDouble(fval) |
| 6 | 120 double fval; |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
121 #endif |
| 6 | 122 { |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
123 register PyFloatObject *op; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
124 if (free_list == NULL) { |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
125 if ((free_list = fill_free_list()) == NULL) |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
126 return NULL; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
127 } |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
128 op = free_list; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
129 free_list = *(PyFloatObject **)free_list; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
130 op->ob_type = &PyFloat_Type; |
| 27 | 131 op->ob_fval = fval; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
132 _Py_NewReference(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
133 return (PyObject *) op; |
| 6 | 134 } |
| 135 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
136 static void |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
137 float_dealloc(op) |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
138 PyFloatObject *op; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
139 { |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
140 *(PyFloatObject **)op = free_list; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
141 free_list = op; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
142 } |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
143 |
| 6 | 144 double |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
145 PyFloat_AsDouble(op) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
146 PyObject *op; |
| 6 | 147 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
148 PyNumberMethods *nb; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
149 PyFloatObject *fo; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
150 double val; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
151 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
152 if (op && PyFloat_Check(op)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
153 return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
154 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
155 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
|
156 nb->nb_float == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
157 PyErr_BadArgument(); |
| 6 | 158 return -1; |
| 159 } | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
160 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
161 fo = (PyFloatObject*) (*nb->nb_float) (op); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
162 if (fo == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
163 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
164 if (!PyFloat_Check(fo)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
165 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
166 "nb_float should return float object"); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
167 return -1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
168 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
169 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
170 val = PyFloat_AS_DOUBLE(fo); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
171 Py_DECREF(fo); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
172 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
173 return val; |
| 6 | 174 } |
| 175 | |
| 176 /* Methods */ | |
| 177 | |
|
268
0fe2471166f0
Export float_buf_repr() interface, for marshalling of floats.
Guido van Rossum <guido@python.org>
parents:
249
diff
changeset
|
178 void |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
179 PyFloat_AsString(buf, v) |
| 6 | 180 char *buf; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
181 PyFloatObject *v; |
| 6 | 182 { |
| 183 register char *cp; | |
| 184 /* Subroutine for float_repr and float_print. | |
| 185 We want float numbers to be recognizable as such, | |
| 186 i.e., they should contain a decimal point or an exponent. | |
| 187 However, %g may print the number as an integer; | |
| 188 in such cases, we append ".0" to the string. */ | |
| 189 sprintf(buf, "%.12g", v->ob_fval); | |
| 190 cp = buf; | |
| 191 if (*cp == '-') | |
| 192 cp++; | |
| 193 for (; *cp != '\0'; cp++) { | |
| 194 /* Any non-digit means it's not an integer; | |
| 195 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
|
196 if (!isdigit(Py_CHARMASK(*cp))) |
| 6 | 197 break; |
| 198 } | |
| 199 if (*cp == '\0') { | |
| 200 *cp++ = '.'; | |
| 201 *cp++ = '0'; | |
| 202 *cp++ = '\0'; | |
| 203 } | |
| 204 } | |
| 205 | |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
206 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
207 static int |
| 6 | 208 float_print(v, fp, flags) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
209 PyFloatObject *v; |
| 6 | 210 FILE *fp; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
211 int flags; /* Not used but required by interface */ |
| 6 | 212 { |
| 213 char buf[100]; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
214 PyFloat_AsString(buf, v); |
| 6 | 215 fputs(buf, fp); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
216 return 0; |
| 6 | 217 } |
| 218 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
219 static PyObject * |
| 6 | 220 float_repr(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
221 PyFloatObject *v; |
| 6 | 222 { |
| 223 char buf[100]; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
224 PyFloat_AsString(buf, v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
225 return PyString_FromString(buf); |
| 6 | 226 } |
| 227 | |
| 228 static int | |
| 229 float_compare(v, w) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
230 PyFloatObject *v, *w; |
| 6 | 231 { |
| 232 double i = v->ob_fval; | |
| 233 double j = w->ob_fval; | |
| 234 return (i < j) ? -1 : (i > j) ? 1 : 0; | |
| 235 } | |
| 236 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
237 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
238 float_hash(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
239 PyFloatObject *v; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
240 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
241 double intpart, fractpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
242 int expo; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
243 long x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
244 /* 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
|
245 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
|
246 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
|
247 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
248 #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
|
249 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
250 extended e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
251 fractpart = modf(v->ob_fval, &e); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
252 intpart = e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
253 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
254 #else |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
255 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
|
256 #endif |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
257 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
258 if (fractpart == 0.0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
259 if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
260 /* Convert to long int and use its hash... */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
261 PyObject *w = PyLong_FromDouble(v->ob_fval); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
262 if (w == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
263 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
264 x = PyObject_Hash(w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
265 Py_DECREF(w); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
266 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
267 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
268 x = (long)intpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
269 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
270 else { |
|
4933
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
271 /* Note -- if you change this code, also change the copy |
|
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
272 in complexobject.c */ |
|
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
273 long hipart; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
274 fractpart = frexp(fractpart, &expo); |
|
4933
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
275 fractpart = fractpart * 2147483648.0; /* 2**31 */ |
|
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
276 hipart = (long)fractpart; /* Take the top 32 bits */ |
|
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
277 fractpart = (fractpart - (double)hipart) * 2147483648.0; |
|
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
278 /* Get the next 32 bits */ |
|
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
279 x = hipart + (long)fractpart + (long)intpart + (expo << 15); |
|
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
280 /* Combine everything */ |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
281 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
282 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
283 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
284 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
285 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
286 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
287 static PyObject * |
| 6 | 288 float_add(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
289 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
290 PyFloatObject *w; |
| 6 | 291 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
292 double result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
293 PyFPE_START_PROTECT("add", return 0) |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
294 result = v->ob_fval + w->ob_fval; |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
295 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
296 return PyFloat_FromDouble(result); |
| 6 | 297 } |
| 298 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
299 static PyObject * |
| 6 | 300 float_sub(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
301 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
302 PyFloatObject *w; |
| 6 | 303 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
304 double result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
305 PyFPE_START_PROTECT("subtract", return 0) |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
306 result = v->ob_fval - w->ob_fval; |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
307 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
308 return PyFloat_FromDouble(result); |
| 6 | 309 } |
| 310 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
311 static PyObject * |
| 6 | 312 float_mul(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
313 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
314 PyFloatObject *w; |
| 6 | 315 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
316 double result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
317 |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
318 PyFPE_START_PROTECT("multiply", return 0) |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
319 result = v->ob_fval * w->ob_fval; |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
320 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
321 return PyFloat_FromDouble(result); |
| 6 | 322 } |
| 323 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
324 static PyObject * |
| 6 | 325 float_div(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
326 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
327 PyFloatObject *w; |
| 6 | 328 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
329 double result; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
330 if (w->ob_fval == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
331 PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
| 6 | 332 return NULL; |
| 333 } | |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
334 PyFPE_START_PROTECT("divide", return 0) |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
335 result = v->ob_fval / w->ob_fval; |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
336 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
337 return PyFloat_FromDouble(result); |
| 6 | 338 } |
| 339 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
340 static PyObject * |
| 6 | 341 float_rem(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
342 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
343 PyFloatObject *w; |
| 6 | 344 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
345 double vx, wx; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
346 double /* div, */ mod; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
347 wx = w->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
348 if (wx == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
349 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
| 6 | 350 return NULL; |
| 351 } | |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
352 PyFPE_START_PROTECT("modulo", return 0) |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
353 vx = v->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
354 mod = fmod(vx, wx); |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
355 /* div = (vx - mod) / wx; */ |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
356 if (wx*mod < 0) { |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
357 mod += wx; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
358 /* div -= 1.0; */ |
| 6 | 359 } |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
360 PyFPE_END_PROTECT(mod) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
361 return PyFloat_FromDouble(mod); |
| 6 | 362 } |
| 363 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
364 static PyObject * |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
365 float_divmod(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
366 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
367 PyFloatObject *w; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
368 { |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
369 double vx, wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
370 double div, mod; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
371 wx = w->ob_fval; |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
372 if (wx == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
373 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
374 return NULL; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
375 } |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
376 PyFPE_START_PROTECT("divmod", return 0) |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
377 vx = v->ob_fval; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
378 mod = fmod(vx, wx); |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
379 div = (vx - mod) / wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
380 if (wx*mod < 0) { |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
381 mod += wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
382 div -= 1.0; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
383 } |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
384 PyFPE_END_PROTECT(div) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
385 return Py_BuildValue("(dd)", div, mod); |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
386 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
387 |
|
3608
8573e65fa948
make some things static
Guido van Rossum <guido@python.org>
parents:
3329
diff
changeset
|
388 static double powu(x, n) |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
389 double x; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
390 long n; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
391 { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
392 double r = 1.; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
393 double p = x; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
394 long mask = 1; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
395 while (mask > 0 && n >= mask) { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
396 if (n & mask) |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
397 r *= p; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
398 mask <<= 1; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
399 p *= p; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
400 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
401 return r; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
402 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
403 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
404 static PyObject * |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
405 float_pow(v, w, z) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
406 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
407 PyObject *w; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
408 PyFloatObject *z; |
| 6 | 409 { |
| 410 double iv, iw, ix; | |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
411 long intw; |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
412 /* 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
|
413 * 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
|
414 * 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
|
415 * [AMK] |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
416 */ |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
417 iv = v->ob_fval; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
418 iw = ((PyFloatObject *)w)->ob_fval; |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
419 intw = (long)iw; |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
420 if (iw == intw && -10000 < intw && intw < 10000) { |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
421 /* 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
|
422 if (intw == 0) { /* x**0 is 1, even 0**0 */ |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
423 PyFPE_START_PROTECT("pow", return 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
424 if ((PyObject *)z!=Py_None) { |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
425 ix=fmod(1.0, z->ob_fval); |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
426 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
|
427 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
428 else ix=1.0; |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
429 PyFPE_END_PROTECT(ix) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
430 return PyFloat_FromDouble(ix); |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
431 } |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
432 errno = 0; |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
433 PyFPE_START_PROTECT("pow", return 0) |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
434 if (intw > 0) |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
435 ix = powu(iv, intw); |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
436 else |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
437 ix = 1./powu(iv, -intw); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
438 PyFPE_END_PROTECT(ix) |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
439 } |
|
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
440 else { |
|
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
441 /* 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
|
442 if (iv == 0.0) { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
443 if (iw < 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
444 PyErr_SetString(PyExc_ValueError, |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
445 "0.0 to a negative power"); |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
446 return NULL; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
447 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
448 return PyFloat_FromDouble(0.0); |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
449 } |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
450 if (iv < 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
451 PyErr_SetString(PyExc_ValueError, |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
452 "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
|
453 return NULL; |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
454 } |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
455 errno = 0; |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
456 PyFPE_START_PROTECT("pow", return 0) |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
457 ix = pow(iv, iw); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
458 PyFPE_END_PROTECT(ix) |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
459 } |
|
496
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
460 CHECK(ix); |
| 27 | 461 if (errno != 0) { |
| 462 /* XXX could it be another type of error? */ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
463 PyErr_SetFromErrno(PyExc_OverflowError); |
| 6 | 464 return NULL; |
| 27 | 465 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
466 if ((PyObject *)z!=Py_None) { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
467 PyFPE_START_PROTECT("pow", return 0) |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
468 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
|
469 if ( ix!=0 && |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
470 ((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
|
471 ix+=z->ob_fval; |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
472 } |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
473 PyFPE_END_PROTECT(ix) |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
474 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
475 return PyFloat_FromDouble(ix); |
| 6 | 476 } |
| 477 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
478 static PyObject * |
| 6 | 479 float_neg(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
480 PyFloatObject *v; |
| 6 | 481 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
482 return PyFloat_FromDouble(-v->ob_fval); |
| 6 | 483 } |
| 484 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
485 static PyObject * |
| 6 | 486 float_pos(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
487 PyFloatObject *v; |
| 6 | 488 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
489 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
490 return (PyObject *)v; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
491 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
492 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
493 static PyObject * |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
494 float_abs(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
495 PyFloatObject *v; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
496 { |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
497 if (v->ob_fval < 0) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
498 return float_neg(v); |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
499 else |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
500 return float_pos(v); |
| 6 | 501 } |
| 502 | |
| 239 | 503 static int |
| 504 float_nonzero(v) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
505 PyFloatObject *v; |
| 239 | 506 { |
| 507 return v->ob_fval != 0.0; | |
| 508 } | |
| 509 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
510 static int |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
511 float_coerce(pv, pw) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
512 PyObject **pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
513 PyObject **pw; |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
514 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
515 if (PyInt_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
516 long x = PyInt_AsLong(*pw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
517 *pw = PyFloat_FromDouble((double)x); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
518 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
519 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
520 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
521 else if (PyLong_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
522 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw)); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
523 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
524 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
525 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
526 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
|
527 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
528 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
529 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
530 float_int(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
531 PyObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
532 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
533 double x = PyFloat_AsDouble(v); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
534 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
|
535 : (x = floor(x)) > (double)LONG_MAX) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
536 PyErr_SetString(PyExc_OverflowError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
537 "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
|
538 return NULL; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
539 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
540 return PyInt_FromLong((long)x); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
541 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
542 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
543 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
544 float_long(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
545 PyObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
546 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
547 double x = PyFloat_AsDouble(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
548 return PyLong_FromDouble(x); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
549 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
550 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
551 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
552 float_float(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
553 PyObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
554 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
555 Py_INCREF(v); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
556 return v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
557 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
558 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
559 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
560 static PyNumberMethods float_as_number = { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
561 (binaryfunc)float_add, /*nb_add*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
562 (binaryfunc)float_sub, /*nb_subtract*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
563 (binaryfunc)float_mul, /*nb_multiply*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
564 (binaryfunc)float_div, /*nb_divide*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
565 (binaryfunc)float_rem, /*nb_remainder*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
566 (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
|
567 (ternaryfunc)float_pow, /*nb_power*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
568 (unaryfunc)float_neg, /*nb_negative*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
569 (unaryfunc)float_pos, /*nb_positive*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
570 (unaryfunc)float_abs, /*nb_absolute*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
571 (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
|
572 0, /*nb_invert*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
573 0, /*nb_lshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
574 0, /*nb_rshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
575 0, /*nb_and*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
576 0, /*nb_xor*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
577 0, /*nb_or*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
578 (coercion)float_coerce, /*nb_coerce*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
579 (unaryfunc)float_int, /*nb_int*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
580 (unaryfunc)float_long, /*nb_long*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
581 (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
|
582 0, /*nb_oct*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
583 0, /*nb_hex*/ |
| 6 | 584 }; |
| 585 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
586 PyTypeObject PyFloat_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
587 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 588 0, |
| 589 "float", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
590 sizeof(PyFloatObject), |
| 6 | 591 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
592 (destructor)float_dealloc, /*tp_dealloc*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
593 (printfunc)float_print, /*tp_print*/ |
| 6 | 594 0, /*tp_getattr*/ |
| 595 0, /*tp_setattr*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
596 (cmpfunc)float_compare, /*tp_compare*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
597 (reprfunc)float_repr, /*tp_repr*/ |
| 6 | 598 &float_as_number, /*tp_as_number*/ |
| 599 0, /*tp_as_sequence*/ | |
| 600 0, /*tp_as_mapping*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
601 (hashfunc)float_hash, /*tp_hash*/ |
| 6 | 602 }; |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
603 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
604 void |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
605 PyFloat_Fini() |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
606 { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
607 /* XXX Alas, the free list is not easily and safely freeable */ |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
608 } |
