Mercurial > cpython-lru_cache-weakref
annotate Objects/floatobject.c @ 13163:a09e8635534d legacy-trunk
Don't try to guess the name of a .def file -- if one is supplied, use it,
otherwise just generate an '/export:' option.
| author | Greg Ward <gward@python.net> |
|---|---|
| date | Thu, 29 Jun 2000 23:09:20 +0000 |
| parents | cca84745da3e |
| children | ff243b0f16ee |
| 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 |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
62 #if SIZEOF_LONG == 4 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
63 #define LONG_MAX 0X7FFFFFFFL |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
64 #elif SIZEOF_LONG == 8 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
65 #define LONG_MAX 0X7FFFFFFFFFFFFFFFL |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
66 #else |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
67 #error "could not set LONG_MAX" |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
68 #endif |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
69 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
70 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
71 #ifndef LONG_MIN |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
72 #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
|
73 #endif |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
74 |
| 2327 | 75 #ifdef __NeXT__ |
| 76 #ifdef __sparc__ | |
| 77 /* | |
| 78 * This works around a bug in the NS/Sparc 3.3 pre-release | |
| 79 * limits.h header file. | |
| 80 * 10-Feb-1995 bwarsaw@cnri.reston.va.us | |
| 81 */ | |
| 82 #undef LONG_MIN | |
| 83 #define LONG_MIN (-LONG_MAX-1) | |
| 84 #endif | |
| 85 #endif | |
| 86 | |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
87 #if !defined(__STDC__) && !defined(macintosh) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
88 extern double fmod Py_PROTO((double, double)); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
89 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
|
90 #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
|
91 |
|
6537
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
92 #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
|
93 /* 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 }; |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
100 #endif |
|
25505e563b37
Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
101 |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
102 /* 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
|
103 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
104 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
105 #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject)) |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
106 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
107 struct _floatblock { |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
108 struct _floatblock *next; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
109 PyFloatObject objects[N_FLOATOBJECTS]; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
110 }; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
111 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
112 typedef struct _floatblock PyFloatBlock; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
113 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
114 static PyFloatBlock *block_list = NULL; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
115 static PyFloatObject *free_list = NULL; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
116 |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
117 static PyFloatObject * |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
118 fill_free_list() |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
119 { |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
120 PyFloatObject *p, *q; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
121 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
122 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock)); |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
123 if (p == NULL) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
124 return (PyFloatObject *) PyErr_NoMemory(); |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
125 ((PyFloatBlock *)p)->next = block_list; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
126 block_list = (PyFloatBlock *)p; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
127 p = &((PyFloatBlock *)p)->objects[0]; |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
128 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
|
129 while (--q > p) |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
130 q->ob_type = (struct _typeobject *)(q-1); |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
131 q->ob_type = NULL; |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
132 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
|
133 } |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
134 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
135 PyObject * |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
136 #ifdef __SC__ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
137 PyFloat_FromDouble(double fval) |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
138 #else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
139 PyFloat_FromDouble(fval) |
| 6 | 140 double fval; |
|
2433
6cbd2830b5a2
for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents:
2327
diff
changeset
|
141 #endif |
| 6 | 142 { |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
143 register PyFloatObject *op; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
144 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
|
145 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
|
146 return NULL; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
147 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
148 /* PyObject_New is inlined */ |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
149 op = free_list; |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
150 free_list = (PyFloatObject *)op->ob_type; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
151 PyObject_INIT(op, &PyFloat_Type); |
| 27 | 152 op->ob_fval = fval; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
153 return (PyObject *) op; |
| 6 | 154 } |
| 155 | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
156 PyObject * |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
157 PyFloat_FromString(v, pend) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
158 PyObject *v; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
159 char **pend; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
160 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
161 extern double strtod Py_PROTO((const char *, char **)); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
162 const char *s, *last, *end; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
163 double x; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
164 char buffer[256]; /* For errors */ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
165 int len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
166 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
167 if (PyString_Check(v)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
168 s = PyString_AS_STRING(v); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
169 len = PyString_GET_SIZE(v); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
170 } |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
171 else if (PyUnicode_Check(v)) { |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
172 char s_buffer[256]; |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
173 |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
174 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) { |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
175 PyErr_SetString(PyExc_ValueError, |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
176 "float() literal too large to convert"); |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
177 return NULL; |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
178 } |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
179 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
180 PyUnicode_GET_SIZE(v), |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
181 s_buffer, |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
182 NULL)) |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
183 return NULL; |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
184 s = s_buffer; |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
185 len = strlen(s); |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
186 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
187 else if (PyObject_AsCharBuffer(v, &s, &len)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
188 PyErr_SetString(PyExc_TypeError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
189 "float() needs a string argument"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
190 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
191 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
192 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
193 last = s + len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
194 while (*s && isspace(Py_CHARMASK(*s))) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
195 s++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
196 if (s[0] == '\0') { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
197 PyErr_SetString(PyExc_ValueError, "empty string for float()"); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
198 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
199 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
200 errno = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
201 PyFPE_START_PROTECT("PyFloat_FromString", return 0) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
202 x = strtod((char *)s, (char **)&end); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
203 PyFPE_END_PROTECT(x) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
204 /* Believe it or not, Solaris 2.6 can move end *beyond* the null |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
205 byte at the end of the string, when the input is inf(inity) */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
206 if (end > last) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
207 end = last; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
208 while (*end && isspace(Py_CHARMASK(*end))) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
209 end++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
210 if (*end != '\0') { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
211 sprintf(buffer, "invalid literal for float(): %.200s", s); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
212 PyErr_SetString(PyExc_ValueError, buffer); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
213 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
214 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
215 else if (end != last) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
216 PyErr_SetString(PyExc_ValueError, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
217 "null byte in argument for float()"); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
218 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
219 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
220 else if (errno != 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
221 sprintf(buffer, "float() literal too large: %.200s", s); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
222 PyErr_SetString(PyExc_ValueError, buffer); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
223 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
224 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
225 if (pend) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
226 *pend = (char *)end; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
227 return PyFloat_FromDouble(x); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
228 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
229 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
230 static void |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
231 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
|
232 PyFloatObject *op; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
233 { |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
234 op->ob_type = (struct _typeobject *)free_list; |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
235 free_list = op; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
236 } |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
237 |
| 6 | 238 double |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
239 PyFloat_AsDouble(op) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
240 PyObject *op; |
| 6 | 241 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
242 PyNumberMethods *nb; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
243 PyFloatObject *fo; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
244 double val; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
245 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
246 if (op && PyFloat_Check(op)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
247 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
|
248 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
249 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
|
250 nb->nb_float == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
251 PyErr_BadArgument(); |
| 6 | 252 return -1; |
| 253 } | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
254 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
255 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
|
256 if (fo == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
257 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
258 if (!PyFloat_Check(fo)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
259 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
260 "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
|
261 return -1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
262 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
263 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
264 val = PyFloat_AS_DOUBLE(fo); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
265 Py_DECREF(fo); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
266 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
267 return val; |
| 6 | 268 } |
| 269 | |
| 270 /* Methods */ | |
| 271 | |
|
268
0fe2471166f0
Export float_buf_repr() interface, for marshalling of floats.
Guido van Rossum <guido@python.org>
parents:
249
diff
changeset
|
272 void |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
273 PyFloat_AsStringEx(buf, v, precision) |
| 6 | 274 char *buf; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
275 PyFloatObject *v; |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
276 int precision; |
| 6 | 277 { |
| 278 register char *cp; | |
| 279 /* Subroutine for float_repr and float_print. | |
| 280 We want float numbers to be recognizable as such, | |
| 281 i.e., they should contain a decimal point or an exponent. | |
| 282 However, %g may print the number as an integer; | |
| 283 in such cases, we append ".0" to the string. */ | |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
284 sprintf(buf, "%.*g", precision, v->ob_fval); |
| 6 | 285 cp = buf; |
| 286 if (*cp == '-') | |
| 287 cp++; | |
| 288 for (; *cp != '\0'; cp++) { | |
| 289 /* Any non-digit means it's not an integer; | |
| 290 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
|
291 if (!isdigit(Py_CHARMASK(*cp))) |
| 6 | 292 break; |
| 293 } | |
| 294 if (*cp == '\0') { | |
| 295 *cp++ = '.'; | |
| 296 *cp++ = '0'; | |
| 297 *cp++ = '\0'; | |
| 298 } | |
| 299 } | |
| 300 | |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
301 /* Precisions used by repr() and str(), respectively. |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
302 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
303 The repr() precision (17 significant decimal digits) is the minimal number |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
304 that is guaranteed to have enough precision so that if the number is read |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
305 back in the exact same binary value is recreated. This is true for IEEE |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
306 floating point by design, and also happens to work for all other modern |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
307 hardware. |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
308 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
309 The str() precision is chosen so that in most cases, the rounding noise |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
310 created by various operations is suppressed, while giving plenty of |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
311 precision for practical use. |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
312 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
313 */ |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
314 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
315 #define PREC_REPR 17 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
316 #define PREC_STR 12 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
317 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
318 void |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
319 PyFloat_AsString(buf, v) |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
320 char *buf; |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
321 PyFloatObject *v; |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
322 { |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
323 PyFloat_AsStringEx(buf, v, PREC_STR); |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
324 } |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
325 |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
326 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
327 static int |
| 6 | 328 float_print(v, fp, flags) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
329 PyFloatObject *v; |
| 6 | 330 FILE *fp; |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
331 int flags; /* Not used but required by interface */ |
| 6 | 332 { |
| 333 char buf[100]; | |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
334 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR); |
| 6 | 335 fputs(buf, fp); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
336 return 0; |
| 6 | 337 } |
| 338 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
339 static PyObject * |
| 6 | 340 float_repr(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
341 PyFloatObject *v; |
| 6 | 342 { |
| 343 char buf[100]; | |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
344 PyFloat_AsStringEx(buf, v, PREC_REPR); |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
345 return PyString_FromString(buf); |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
346 } |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
347 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
348 static PyObject * |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
349 float_str(v) |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
350 PyFloatObject *v; |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
351 { |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
352 char buf[100]; |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
353 PyFloat_AsStringEx(buf, v, PREC_STR); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
354 return PyString_FromString(buf); |
| 6 | 355 } |
| 356 | |
| 357 static int | |
| 358 float_compare(v, w) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
359 PyFloatObject *v, *w; |
| 6 | 360 { |
| 361 double i = v->ob_fval; | |
| 362 double j = w->ob_fval; | |
| 363 return (i < j) ? -1 : (i > j) ? 1 : 0; | |
| 364 } | |
| 365 | |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
366 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
367 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
368 float_hash(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
369 PyFloatObject *v; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
370 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
371 double intpart, fractpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
372 long x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
373 /* 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
|
374 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
|
375 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
|
376 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
377 #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
|
378 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
379 extended e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
380 fractpart = modf(v->ob_fval, &e); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
381 intpart = e; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
382 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
383 #else |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
384 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
|
385 #endif |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
386 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
387 if (fractpart == 0.0) { |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
388 if (intpart > LONG_MAX || -intpart > LONG_MAX) { |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
389 /* 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
|
390 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
|
391 if (w == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
392 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
393 x = PyObject_Hash(w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
394 Py_DECREF(w); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
395 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
396 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
397 x = (long)intpart; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
398 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
399 else { |
|
4933
b56bc25aec85
New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
400 /* 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
|
401 in complexobject.c */ |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
402 x = _Py_HashDouble(v->ob_fval); |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
403 if (x == -1) |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
404 return -1; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
405 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
406 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
407 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
408 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
409 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
410 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
411 static PyObject * |
| 6 | 412 float_add(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
413 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
414 PyFloatObject *w; |
| 6 | 415 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
416 double result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
417 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
|
418 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
|
419 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
420 return PyFloat_FromDouble(result); |
| 6 | 421 } |
| 422 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
423 static PyObject * |
| 6 | 424 float_sub(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
425 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
426 PyFloatObject *w; |
| 6 | 427 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
428 double result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
429 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
|
430 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
|
431 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
432 return PyFloat_FromDouble(result); |
| 6 | 433 } |
| 434 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
435 static PyObject * |
| 6 | 436 float_mul(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
437 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
438 PyFloatObject *w; |
| 6 | 439 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
440 double result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
441 |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
442 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
|
443 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
|
444 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
445 return PyFloat_FromDouble(result); |
| 6 | 446 } |
| 447 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
448 static PyObject * |
| 6 | 449 float_div(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
450 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
451 PyFloatObject *w; |
| 6 | 452 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
453 double result; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
454 if (w->ob_fval == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
455 PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
| 6 | 456 return NULL; |
| 457 } | |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
458 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
|
459 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
|
460 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
461 return PyFloat_FromDouble(result); |
| 6 | 462 } |
| 463 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
464 static PyObject * |
| 6 | 465 float_rem(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
466 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
467 PyFloatObject *w; |
| 6 | 468 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
469 double vx, wx; |
| 10833 | 470 double mod; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
471 wx = w->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
472 if (wx == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
473 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
| 6 | 474 return NULL; |
| 475 } | |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
476 PyFPE_START_PROTECT("modulo", return 0) |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
477 vx = v->ob_fval; |
|
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
478 mod = fmod(vx, wx); |
| 10833 | 479 /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 480 0 if wx < sqrt(smallest nonzero double) */ | |
| 481 if (mod && ((wx < 0) != (mod < 0))) { | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
482 mod += wx; |
| 6 | 483 } |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
484 PyFPE_END_PROTECT(mod) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
485 return PyFloat_FromDouble(mod); |
| 6 | 486 } |
| 487 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
488 static PyObject * |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
489 float_divmod(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
490 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
491 PyFloatObject *w; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
492 { |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
493 double vx, wx; |
| 10833 | 494 double div, mod, floordiv; |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
495 wx = w->ob_fval; |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
496 if (wx == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
497 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
498 return NULL; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
499 } |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
500 PyFPE_START_PROTECT("divmod", return 0) |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
501 vx = v->ob_fval; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
502 mod = fmod(vx, wx); |
| 10833 | 503 /* fmod is typically exact, so vx-mod is *mathemtically* an |
| 504 exact multiple of wx. But this is fp arithmetic, and fp | |
| 505 vx - mod is an approximation; the result is that div may | |
| 506 not be an exact integral value after the division, although | |
| 507 it will always be very close to one. | |
| 508 */ | |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
509 div = (vx - mod) / wx; |
| 10833 | 510 /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 511 0 if wx < sqrt(smallest nonzero double) */ | |
| 512 if (mod && ((wx < 0) != (mod < 0))) { | |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
513 mod += wx; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
514 div -= 1.0; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
515 } |
| 10833 | 516 /* snap quotient to nearest integral value */ |
| 517 floordiv = floor(div); | |
| 518 if (div - floordiv > 0.5) | |
| 519 floordiv += 1.0; | |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
520 PyFPE_END_PROTECT(div) |
| 10833 | 521 return Py_BuildValue("(dd)", floordiv, mod); |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
522 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
523 |
|
3608
8573e65fa948
make some things static
Guido van Rossum <guido@python.org>
parents:
3329
diff
changeset
|
524 static double powu(x, n) |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
525 double x; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
526 long n; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
527 { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
528 double r = 1.; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
529 double p = x; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
530 long mask = 1; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
531 while (mask > 0 && n >= mask) { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
532 if (n & mask) |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
533 r *= p; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
534 mask <<= 1; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
535 p *= p; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
536 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
537 return r; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
538 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
539 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
540 static PyObject * |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
541 float_pow(v, w, z) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
542 PyFloatObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
543 PyObject *w; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
544 PyFloatObject *z; |
| 6 | 545 { |
| 546 double iv, iw, ix; | |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
547 long intw; |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
548 /* 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
|
549 * 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
|
550 * 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
|
551 * [AMK] |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
552 */ |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
553 iv = v->ob_fval; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
554 iw = ((PyFloatObject *)w)->ob_fval; |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
555 intw = (long)iw; |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
556 if (iw == intw && -10000 < intw && intw < 10000) { |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
557 /* 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
|
558 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
|
559 PyFPE_START_PROTECT("pow", return 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
560 if ((PyObject *)z!=Py_None) { |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
561 ix=fmod(1.0, z->ob_fval); |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
562 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
|
563 } |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
564 else ix=1.0; |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
565 PyFPE_END_PROTECT(ix) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
566 return PyFloat_FromDouble(ix); |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
567 } |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
568 errno = 0; |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
569 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
|
570 if (intw > 0) |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
571 ix = powu(iv, intw); |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
572 else |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
573 ix = 1./powu(iv, -intw); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
574 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
|
575 } |
|
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
576 else { |
|
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
577 /* 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
|
578 if (iv == 0.0) { |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
579 if (iw < 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
580 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
|
581 "0.0 to a negative power"); |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
582 return NULL; |
|
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
583 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
584 return PyFloat_FromDouble(0.0); |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
585 } |
|
4007
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
586 if (iv < 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
587 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
|
588 "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
|
589 return NULL; |
|
1a0aa986f6fa
Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents:
3984
diff
changeset
|
590 } |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
591 errno = 0; |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
592 PyFPE_START_PROTECT("pow", return 0) |
|
3329
67d347c32fff
better power implementation
Guido van Rossum <guido@python.org>
parents:
2433
diff
changeset
|
593 ix = pow(iv, iw); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
594 PyFPE_END_PROTECT(ix) |
|
249
bab1f5494b8e
Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents:
239
diff
changeset
|
595 } |
|
496
0ac392b7f76e
Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents:
470
diff
changeset
|
596 CHECK(ix); |
| 27 | 597 if (errno != 0) { |
| 598 /* 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
|
599 PyErr_SetFromErrno(PyExc_OverflowError); |
| 6 | 600 return NULL; |
| 27 | 601 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
602 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
|
603 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
|
604 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
|
605 if ( ix!=0 && |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
606 ((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
|
607 ix+=z->ob_fval; |
|
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
608 } |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
609 PyFPE_END_PROTECT(ix) |
|
1775
17c80a952914
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
610 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
611 return PyFloat_FromDouble(ix); |
| 6 | 612 } |
| 613 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
614 static PyObject * |
| 6 | 615 float_neg(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
616 PyFloatObject *v; |
| 6 | 617 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
618 return PyFloat_FromDouble(-v->ob_fval); |
| 6 | 619 } |
| 620 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
621 static PyObject * |
| 6 | 622 float_pos(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
623 PyFloatObject *v; |
| 6 | 624 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
625 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
626 return (PyObject *)v; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
627 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
628 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
629 static PyObject * |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
630 float_abs(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
631 PyFloatObject *v; |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
632 { |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
633 if (v->ob_fval < 0) |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
634 return float_neg(v); |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
635 else |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
636 return float_pos(v); |
| 6 | 637 } |
| 638 | |
| 239 | 639 static int |
| 640 float_nonzero(v) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
641 PyFloatObject *v; |
| 239 | 642 { |
| 643 return v->ob_fval != 0.0; | |
| 644 } | |
| 645 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
646 static int |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
647 float_coerce(pv, pw) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
648 PyObject **pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
649 PyObject **pw; |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
650 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
651 if (PyInt_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
652 long x = PyInt_AsLong(*pw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
653 *pw = PyFloat_FromDouble((double)x); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
654 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
655 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
656 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
657 else if (PyLong_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
658 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw)); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
659 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
660 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
661 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
662 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
|
663 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
664 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
665 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
666 float_int(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
667 PyObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
668 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
669 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
|
670 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
|
671 : (x = floor(x)) > (double)LONG_MAX) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
672 PyErr_SetString(PyExc_OverflowError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
673 "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
|
674 return NULL; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1775
diff
changeset
|
675 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
676 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
|
677 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
678 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
679 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
680 float_long(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
681 PyObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
682 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
683 double x = PyFloat_AsDouble(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
684 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
|
685 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
686 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
687 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
688 float_float(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
689 PyObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
690 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
691 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
|
692 return v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
693 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
694 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
695 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
696 static PyNumberMethods float_as_number = { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
697 (binaryfunc)float_add, /*nb_add*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
698 (binaryfunc)float_sub, /*nb_subtract*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
699 (binaryfunc)float_mul, /*nb_multiply*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
700 (binaryfunc)float_div, /*nb_divide*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
701 (binaryfunc)float_rem, /*nb_remainder*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
702 (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
|
703 (ternaryfunc)float_pow, /*nb_power*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
704 (unaryfunc)float_neg, /*nb_negative*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
705 (unaryfunc)float_pos, /*nb_positive*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
706 (unaryfunc)float_abs, /*nb_absolute*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
707 (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
|
708 0, /*nb_invert*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
709 0, /*nb_lshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
710 0, /*nb_rshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
711 0, /*nb_and*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
712 0, /*nb_xor*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
713 0, /*nb_or*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
714 (coercion)float_coerce, /*nb_coerce*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
715 (unaryfunc)float_int, /*nb_int*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
716 (unaryfunc)float_long, /*nb_long*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
717 (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
|
718 0, /*nb_oct*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
719 0, /*nb_hex*/ |
| 6 | 720 }; |
| 721 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
722 PyTypeObject PyFloat_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
723 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 724 0, |
| 725 "float", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
726 sizeof(PyFloatObject), |
| 6 | 727 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
728 (destructor)float_dealloc, /*tp_dealloc*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
729 (printfunc)float_print, /*tp_print*/ |
| 6 | 730 0, /*tp_getattr*/ |
| 731 0, /*tp_setattr*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
732 (cmpfunc)float_compare, /*tp_compare*/ |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
733 (reprfunc)float_repr, /*tp_repr*/ |
| 6 | 734 &float_as_number, /*tp_as_number*/ |
| 735 0, /*tp_as_sequence*/ | |
| 736 0, /*tp_as_mapping*/ | |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
737 (hashfunc)float_hash, /*tp_hash*/ |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
738 0, /*tp_call*/ |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
739 (reprfunc)float_str, /*tp_str*/ |
| 6 | 740 }; |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
741 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
742 void |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
743 PyFloat_Fini() |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
744 { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
745 PyFloatObject *p; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
746 PyFloatBlock *list, *next; |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
747 int i; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
748 int bc, bf; /* block count, number of freed blocks */ |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
749 int frem, fsum; /* remaining unfreed floats per block, total */ |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
750 |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
751 bc = 0; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
752 bf = 0; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
753 fsum = 0; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
754 list = block_list; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
755 block_list = NULL; |
|
10432
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
756 free_list = NULL; |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
757 while (list != NULL) { |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
758 bc++; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
759 frem = 0; |
|
10432
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
760 for (i = 0, p = &list->objects[0]; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
761 i < N_FLOATOBJECTS; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
762 i++, p++) { |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
763 if (PyFloat_Check(p) && p->ob_refcnt != 0) |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
764 frem++; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
765 } |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
766 next = list->next; |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
767 if (frem) { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
768 list->next = block_list; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
769 block_list = list; |
|
10432
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
770 for (i = 0, p = &list->objects[0]; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
771 i < N_FLOATOBJECTS; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
772 i++, p++) { |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
773 if (!PyFloat_Check(p) || p->ob_refcnt == 0) { |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
774 p->ob_type = (struct _typeobject *) |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
775 free_list; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
776 free_list = p; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
777 } |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
778 } |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
779 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
780 else { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
781 PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
782 bf++; |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
783 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
784 fsum += frem; |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
785 list = next; |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
786 } |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
787 if (!Py_VerboseFlag) |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
788 return; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
789 fprintf(stderr, "# cleanup floats"); |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
790 if (!fsum) { |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
791 fprintf(stderr, "\n"); |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
792 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
793 else { |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
794 fprintf(stderr, |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
795 ": %d unfreed float%s in %d out of %d block%s\n", |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
796 fsum, fsum == 1 ? "" : "s", |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
797 bc - bf, bc, bc == 1 ? "" : "s"); |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
798 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
799 if (Py_VerboseFlag > 1) { |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
800 list = block_list; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
801 while (list != NULL) { |
|
10432
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
802 for (i = 0, p = &list->objects[0]; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
803 i < N_FLOATOBJECTS; |
|
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
804 i++, p++) { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
805 if (PyFloat_Check(p) && p->ob_refcnt != 0) { |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
806 char buf[100]; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
807 PyFloat_AsString(buf, p); |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
808 fprintf(stderr, |
|
10432
1cc5d20d2ed8
Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents:
10391
diff
changeset
|
809 "# <float at %lx, refcnt=%d, val=%s>\n", |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
810 (long)p, p->ob_refcnt, buf); |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
811 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
812 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
813 list = list->next; |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
814 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
815 } |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
816 } |
