Mercurial > cpython-lru_cache-weakref
annotate Objects/floatobject.c @ 43405:c19ec9515a33 legacy-trunk
Annotated the correction to urllib.py, issue #1177
| author | Facundo Batista <facundobatista@gmail.com> |
|---|---|
| date | Wed, 19 Sep 2007 14:02:03 +0000 |
| parents | 7630248b593c |
| children | c218d1ac916e |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Float object implementation */ |
| 3 | |
| 27 | 4 /* XXX There should be overflow checks here, but it's hard to check |
| 5 for any kind of float exception without losing portability. */ | |
| 6 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
7 #include "Python.h" |
| 6 | 8 |
| 86 | 9 #include <ctype.h> |
| 6 | 10 |
|
30979
86fdf8fe7f17
Getting rid of all the code inside #ifdef macintosh too.
Jack Jansen <jack.jansen@cwi.nl>
parents:
29641
diff
changeset
|
11 #if !defined(__STDC__) |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
12 extern double fmod(double, double); |
|
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
13 extern double pow(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
|
14 #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
|
15 |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
16 /* 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
|
17 #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
|
18 #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
|
19 #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
|
20 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 }; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
25 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
26 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
|
27 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
28 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
|
29 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
|
30 |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
31 static PyFloatObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
32 fill_free_list(void) |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
33 { |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
34 PyFloatObject *p, *q; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
35 /* 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
|
36 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
|
37 if (p == NULL) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
38 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
|
39 ((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
|
40 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
|
41 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
|
42 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
|
43 while (--q > p) |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
44 Py_Type(q) = (struct _typeobject *)(q-1); |
|
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
45 Py_Type(q) = NULL; |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
46 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
|
47 } |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
48 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
49 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
50 PyFloat_FromDouble(double fval) |
| 6 | 51 { |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
52 register PyFloatObject *op; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
53 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
|
54 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
|
55 return NULL; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
56 } |
|
24898
a869d2de536f
Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents:
24280
diff
changeset
|
57 /* Inline PyObject_New */ |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
58 op = free_list; |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
59 free_list = (PyFloatObject *)Py_Type(op); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
60 PyObject_INIT(op, &PyFloat_Type); |
| 27 | 61 op->ob_fval = fval; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
62 return (PyObject *) op; |
| 6 | 63 } |
| 64 | |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
65 /************************************************************************** |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
66 RED_FLAG 22-Sep-2000 tim |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
67 PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG, |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
68 |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
69 1. If v was a regular string, *pend was set to point to its terminating |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
70 null byte. That's useless (the caller can find that without any |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
71 help from this function!). |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
72 |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
73 2. If v was a Unicode string, or an object convertible to a character |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
74 buffer, *pend was set to point into stack trash (the auto temp |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
75 vector holding the character buffer). That was downright dangerous. |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
76 |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
77 Since we can't change the interface of a public API function, pend is |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
78 still supported but now *officially* useless: if pend is not NULL, |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
79 *pend is set to NULL. |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
80 **************************************************************************/ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
81 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
82 PyFloat_FromString(PyObject *v, char **pend) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
83 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
84 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
|
85 double x; |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
86 char buffer[256]; /* for errors */ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
87 #ifdef Py_USING_UNICODE |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
88 char s_buffer[256]; /* for objects convertible to a char buffer */ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
89 #endif |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36235
diff
changeset
|
90 Py_ssize_t len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
91 |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
92 if (pend) |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
93 *pend = NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
94 if (PyString_Check(v)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
95 s = PyString_AS_STRING(v); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
96 len = PyString_GET_SIZE(v); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
97 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
98 #ifdef Py_USING_UNICODE |
|
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
|
99 else if (PyUnicode_Check(v)) { |
|
37815
20cb13bdf1f0
C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents:
37589
diff
changeset
|
100 if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) { |
|
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
|
101 PyErr_SetString(PyExc_ValueError, |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
102 "Unicode float() literal too long to convert"); |
|
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
|
103 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
|
104 } |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
105 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
|
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
|
106 PyUnicode_GET_SIZE(v), |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
107 s_buffer, |
|
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
|
108 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
|
109 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
|
110 s = s_buffer; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36235
diff
changeset
|
111 len = strlen(s); |
|
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
|
112 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
113 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
114 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
|
115 PyErr_SetString(PyExc_TypeError, |
|
23344
bffc7236c4ba
clarify message when raising TypeError to indicate that float() accepts
Skip Montanaro <skip@pobox.com>
parents:
22228
diff
changeset
|
116 "float() argument must be a string or a number"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
117 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
118 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
119 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
120 last = s + len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
121 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
|
122 s++; |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
123 if (*s == '\0') { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
124 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
|
125 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
126 } |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
127 /* We don't care about overflow or underflow. If the platform supports |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
128 * them, infinities and signed zeroes (on underflow) are fine. |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
129 * However, strtod can return 0 for denormalized numbers, where atof |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
130 * does not. So (alas!) we special-case a zero result. Note that |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
131 * whether strtod sets errno on underflow is not defined, so we can't |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
132 * key off errno. |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
133 */ |
|
14851
5449302c4d2e
Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents:
14783
diff
changeset
|
134 PyFPE_START_PROTECT("strtod", return NULL) |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
32150
diff
changeset
|
135 x = PyOS_ascii_strtod(s, (char **)&end); |
|
14851
5449302c4d2e
Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents:
14783
diff
changeset
|
136 PyFPE_END_PROTECT(x) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
137 errno = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
138 /* Believe it or not, Solaris 2.6 can move end *beyond* the null |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
139 byte at the end of the string, when the input is inf(inity). */ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
140 if (end > last) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
141 end = last; |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
142 if (end == s) { |
|
21079
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
143 PyOS_snprintf(buffer, sizeof(buffer), |
|
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
144 "invalid literal for float(): %.200s", s); |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
145 PyErr_SetString(PyExc_ValueError, buffer); |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
146 return NULL; |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
147 } |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
148 /* Since end != s, the platform made *some* kind of sense out |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
149 of the input. Trust it. */ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
150 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
|
151 end++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
152 if (*end != '\0') { |
|
21079
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
153 PyOS_snprintf(buffer, sizeof(buffer), |
|
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
154 "invalid literal for float(): %.200s", s); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
155 PyErr_SetString(PyExc_ValueError, buffer); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
156 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
157 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
158 else if (end != last) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
159 PyErr_SetString(PyExc_ValueError, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
160 "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
|
161 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
162 } |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
163 if (x == 0.0) { |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
164 /* See above -- may have been strtod being anal |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
165 about denorms. */ |
|
14851
5449302c4d2e
Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents:
14783
diff
changeset
|
166 PyFPE_START_PROTECT("atof", return NULL) |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
32150
diff
changeset
|
167 x = PyOS_ascii_atof(s); |
|
14851
5449302c4d2e
Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents:
14783
diff
changeset
|
168 PyFPE_END_PROTECT(x) |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
169 errno = 0; /* whether atof ever set errno is undefined */ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
170 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
171 return PyFloat_FromDouble(x); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
172 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
173 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
174 static void |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
175 float_dealloc(PyFloatObject *op) |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
176 { |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19976
diff
changeset
|
177 if (PyFloat_CheckExact(op)) { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
178 Py_Type(op) = (struct _typeobject *)free_list; |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19976
diff
changeset
|
179 free_list = op; |
|
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19976
diff
changeset
|
180 } |
|
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19976
diff
changeset
|
181 else |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
182 Py_Type(op)->tp_free((PyObject *)op); |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
183 } |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
184 |
| 6 | 185 double |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
186 PyFloat_AsDouble(PyObject *op) |
| 6 | 187 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
188 PyNumberMethods *nb; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
189 PyFloatObject *fo; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
190 double val; |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
191 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
192 if (op && PyFloat_Check(op)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
193 return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
194 |
|
26272
fbccef8b3153
Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24898
diff
changeset
|
195 if (op == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
196 PyErr_BadArgument(); |
| 6 | 197 return -1; |
| 198 } | |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
199 |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
200 if ((nb = Py_Type(op)->tp_as_number) == NULL || nb->nb_float == NULL) { |
|
26272
fbccef8b3153
Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24898
diff
changeset
|
201 PyErr_SetString(PyExc_TypeError, "a float is required"); |
|
fbccef8b3153
Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24898
diff
changeset
|
202 return -1; |
|
fbccef8b3153
Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24898
diff
changeset
|
203 } |
|
fbccef8b3153
Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24898
diff
changeset
|
204 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
205 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
|
206 if (fo == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
207 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
208 if (!PyFloat_Check(fo)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
209 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
210 "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
|
211 return -1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
212 } |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
213 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
214 val = PyFloat_AS_DOUBLE(fo); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
215 Py_DECREF(fo); |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
216 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
217 return val; |
| 6 | 218 } |
| 219 | |
| 220 /* Methods */ | |
| 221 | |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
222 static void |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
223 format_float(char *buf, size_t buflen, PyFloatObject *v, int precision) |
| 6 | 224 { |
| 225 register char *cp; | |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
32150
diff
changeset
|
226 char format[32]; |
| 6 | 227 /* Subroutine for float_repr and float_print. |
| 228 We want float numbers to be recognizable as such, | |
| 229 i.e., they should contain a decimal point or an exponent. | |
| 230 However, %g may print the number as an integer; | |
| 231 in such cases, we append ".0" to the string. */ | |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
232 |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
233 assert(PyFloat_Check(v)); |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
32150
diff
changeset
|
234 PyOS_snprintf(format, 32, "%%.%ig", precision); |
|
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
32150
diff
changeset
|
235 PyOS_ascii_formatd(buf, buflen, format, v->ob_fval); |
| 6 | 236 cp = buf; |
| 237 if (*cp == '-') | |
| 238 cp++; | |
| 239 for (; *cp != '\0'; cp++) { | |
| 240 /* Any non-digit means it's not an integer; | |
| 241 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
|
242 if (!isdigit(Py_CHARMASK(*cp))) |
| 6 | 243 break; |
| 244 } | |
| 245 if (*cp == '\0') { | |
| 246 *cp++ = '.'; | |
| 247 *cp++ = '0'; | |
| 248 *cp++ = '\0'; | |
| 249 } | |
| 250 } | |
| 251 | |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
252 /* XXX PyFloat_AsStringEx should not be a public API function (for one |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
253 XXX thing, its signature passes a buffer without a length; for another, |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
254 XXX it isn't useful outside this file). |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
255 */ |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
256 void |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
257 PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision) |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
258 { |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
259 format_float(buf, 100, v, precision); |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
260 } |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
261 |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
262 /* Macro and helper that convert PyObject obj to a C double and store |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
263 the value in dbl; this replaces the functionality of the coercion |
|
21354
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
264 slot function. If conversion to double raises an exception, obj is |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
265 set to NULL, and the function invoking this macro returns NULL. If |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
266 obj is not of float, int or long type, Py_NotImplemented is incref'ed, |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
267 stored in obj, and returned from the function invoking this macro. |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
268 */ |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
269 #define CONVERT_TO_DOUBLE(obj, dbl) \ |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
270 if (PyFloat_Check(obj)) \ |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
271 dbl = PyFloat_AS_DOUBLE(obj); \ |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
272 else if (convert_to_double(&(obj), &(dbl)) < 0) \ |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
273 return obj; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
274 |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
275 static int |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
276 convert_to_double(PyObject **v, double *dbl) |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
277 { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
278 register PyObject *obj = *v; |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
279 |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
280 if (PyInt_Check(obj)) { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
281 *dbl = (double)PyInt_AS_LONG(obj); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
282 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
283 else if (PyLong_Check(obj)) { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
284 *dbl = PyLong_AsDouble(obj); |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
285 if (*dbl == -1.0 && PyErr_Occurred()) { |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
286 *v = NULL; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
287 return -1; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
288 } |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
289 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
290 else { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
291 Py_INCREF(Py_NotImplemented); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
292 *v = Py_NotImplemented; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
293 return -1; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
294 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
295 return 0; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
296 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
297 |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
298 /* 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
|
299 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
300 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
|
301 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
|
302 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
|
303 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
|
304 hardware. |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
305 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
306 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
|
307 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
|
308 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
|
309 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
310 */ |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
311 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
312 #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
|
313 #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
|
314 |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
315 /* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated: |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
316 XXX they pass a char buffer without passing a length. |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
317 */ |
|
11631
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 |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
319 PyFloat_AsString(char *buf, 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
|
320 { |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
321 format_float(buf, 100, v, PREC_STR); |
|
11631
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 |
|
17707
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
324 void |
|
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
325 PyFloat_AsReprString(char *buf, PyFloatObject *v) |
|
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
326 { |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
327 format_float(buf, 100, v, PREC_REPR); |
|
17707
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
328 } |
|
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
329 |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
330 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
331 static int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
332 float_print(PyFloatObject *v, FILE *fp, int flags) |
| 6 | 333 { |
| 334 char buf[100]; | |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
335 format_float(buf, sizeof(buf), v, |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
336 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
337 Py_BEGIN_ALLOW_THREADS |
| 6 | 338 fputs(buf, fp); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
339 Py_END_ALLOW_THREADS |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
340 return 0; |
| 6 | 341 } |
| 342 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
343 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
344 float_repr(PyFloatObject *v) |
| 6 | 345 { |
| 346 char buf[100]; | |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
347 format_float(buf, sizeof(buf), v, PREC_REPR); |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
348 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
|
349 } |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
350 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
351 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
352 float_str(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
|
353 { |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
354 char buf[100]; |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
355 format_float(buf, sizeof(buf), v, PREC_STR); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
356 return PyString_FromString(buf); |
| 6 | 357 } |
| 358 | |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
359 /* Comparison is pretty much a nightmare. When comparing float to float, |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
360 * we do it as straightforwardly (and long-windedly) as conceivable, so |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
361 * that, e.g., Python x == y delivers the same result as the platform |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
362 * C x == y when x and/or y is a NaN. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
363 * When mixing float with an integer type, there's no good *uniform* approach. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
364 * Converting the double to an integer obviously doesn't work, since we |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
365 * may lose info from fractional bits. Converting the integer to a double |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
366 * also has two failure modes: (1) a long int may trigger overflow (too |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
367 * large to fit in the dynamic range of a C double); (2) even a C long may have |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
368 * more bits than fit in a C double (e.g., on a a 64-bit box long may have |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
369 * 63 bits of precision, but a C double probably has only 53), and then |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
370 * we can falsely claim equality when low-order integer bits are lost by |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
371 * coercion to double. So this part is painful too. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
372 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
373 |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
374 static PyObject* |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
375 float_richcompare(PyObject *v, PyObject *w, int op) |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
376 { |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
377 double i, j; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
378 int r = 0; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
379 |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
380 assert(PyFloat_Check(v)); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
381 i = PyFloat_AS_DOUBLE(v); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
382 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
383 /* Switch on the type of w. Set i and j to doubles to be compared, |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
384 * and op to the richcomp to use. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
385 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
386 if (PyFloat_Check(w)) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
387 j = PyFloat_AS_DOUBLE(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
388 |
|
38293
688f25757104
Added a new macro, Py_IS_FINITE(X). On windows there is an intrinsic for this and it is more efficient than to use !Py_IS_INFINITE(X) && !Py_IS_NAN(X). No change on other platforms
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
37815
diff
changeset
|
389 else if (!Py_IS_FINITE(i)) { |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
390 if (PyInt_Check(w) || PyLong_Check(w)) |
|
33549
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
391 /* If i is an infinity, its magnitude exceeds any |
|
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
392 * finite integer, so it doesn't matter which int we |
|
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
393 * compare i with. If i is a NaN, similarly. |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
394 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
395 j = 0.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
396 else |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
397 goto Unimplemented; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
398 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
399 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
400 else if (PyInt_Check(w)) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
401 long jj = PyInt_AS_LONG(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
402 /* In the worst realistic case I can imagine, C double is a |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
403 * Cray single with 48 bits of precision, and long has 64 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
404 * bits. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
405 */ |
|
33549
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
406 #if SIZEOF_LONG > 6 |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
407 unsigned long abs = (unsigned long)(jj < 0 ? -jj : jj); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
408 if (abs >> 48) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
409 /* Needs more than 48 bits. Make it take the |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
410 * PyLong path. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
411 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
412 PyObject *result; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
413 PyObject *ww = PyLong_FromLong(jj); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
414 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
415 if (ww == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
416 return NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
417 result = float_richcompare(v, ww, op); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
418 Py_DECREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
419 return result; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
420 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
421 #endif |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
422 j = (double)jj; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
423 assert((long)j == jj); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
424 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
425 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
426 else if (PyLong_Check(w)) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
427 int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
428 int wsign = _PyLong_Sign(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
429 size_t nbits; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
430 int exponent; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
431 |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
432 if (vsign != wsign) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
433 /* Magnitudes are irrelevant -- the signs alone |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
434 * determine the outcome. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
435 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
436 i = (double)vsign; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
437 j = (double)wsign; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
438 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
439 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
440 /* The signs are the same. */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
441 /* Convert w to a double if it fits. In particular, 0 fits. */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
442 nbits = _PyLong_NumBits(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
443 if (nbits == (size_t)-1 && PyErr_Occurred()) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
444 /* This long is so large that size_t isn't big enough |
|
33549
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
445 * to hold the # of bits. Replace with little doubles |
|
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
446 * that give the same outcome -- w is so large that |
|
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
447 * its magnitude must exceed the magnitude of any |
|
ecaf0e8fa737
float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents:
33547
diff
changeset
|
448 * finite float. |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
449 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
450 PyErr_Clear(); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
451 i = (double)vsign; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
452 assert(wsign != 0); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
453 j = wsign * 2.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
454 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
455 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
456 if (nbits <= 48) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
457 j = PyLong_AsDouble(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
458 /* It's impossible that <= 48 bits overflowed. */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
459 assert(j != -1.0 || ! PyErr_Occurred()); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
460 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
461 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
462 assert(wsign != 0); /* else nbits was 0 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
463 assert(vsign != 0); /* if vsign were 0, then since wsign is |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
464 * not 0, we would have taken the |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
465 * vsign != wsign branch at the start */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
466 /* We want to work with non-negative numbers. */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
467 if (vsign < 0) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
468 /* "Multiply both sides" by -1; this also swaps the |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
469 * comparator. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
470 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
471 i = -i; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
472 op = _Py_SwappedOp[op]; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
473 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
474 assert(i > 0.0); |
|
36235
9f66bdd5a061
Fix icc warnings: remove unused variable
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
475 (void) frexp(i, &exponent); |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
476 /* exponent is the # of bits in v before the radix point; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
477 * we know that nbits (the # of bits in w) > 48 at this point |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
478 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
479 if (exponent < 0 || (size_t)exponent < nbits) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
480 i = 1.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
481 j = 2.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
482 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
483 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
484 if ((size_t)exponent > nbits) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
485 i = 2.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
486 j = 1.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
487 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
488 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
489 /* v and w have the same number of bits before the radix |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
490 * point. Construct two longs that have the same comparison |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
491 * outcome. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
492 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
493 { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
494 double fracpart; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
495 double intpart; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
496 PyObject *result = NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
497 PyObject *one = NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
498 PyObject *vv = NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
499 PyObject *ww = w; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
500 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
501 if (wsign < 0) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
502 ww = PyNumber_Negative(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
503 if (ww == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
504 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
505 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
506 else |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
507 Py_INCREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
508 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
509 fracpart = modf(i, &intpart); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
510 vv = PyLong_FromDouble(intpart); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
511 if (vv == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
512 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
513 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
514 if (fracpart != 0.0) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
515 /* Shift left, and or a 1 bit into vv |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
516 * to represent the lost fraction. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
517 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
518 PyObject *temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
519 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
520 one = PyInt_FromLong(1); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
521 if (one == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
522 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
523 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
524 temp = PyNumber_Lshift(ww, one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
525 if (temp == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
526 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
527 Py_DECREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
528 ww = temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
529 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
530 temp = PyNumber_Lshift(vv, one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
531 if (temp == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
532 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
533 Py_DECREF(vv); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
534 vv = temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
535 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
536 temp = PyNumber_Or(vv, one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
537 if (temp == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
538 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
539 Py_DECREF(vv); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
540 vv = temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
541 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
542 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
543 r = PyObject_RichCompareBool(vv, ww, op); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
544 if (r < 0) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
545 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
546 result = PyBool_FromLong(r); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
547 Error: |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
548 Py_XDECREF(vv); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
549 Py_XDECREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
550 Py_XDECREF(one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
551 return result; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
552 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
553 } /* else if (PyLong_Check(w)) */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
554 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
555 else /* w isn't float, int, or long */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
556 goto Unimplemented; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
557 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
558 Compare: |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
559 PyFPE_START_PROTECT("richcompare", return NULL) |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
560 switch (op) { |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
561 case Py_EQ: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
562 r = i == j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
563 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
564 case Py_NE: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
565 r = i != j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
566 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
567 case Py_LE: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
568 r = i <= j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
569 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
570 case Py_GE: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
571 r = i >= j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
572 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
573 case Py_LT: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
574 r = i < j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
575 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
576 case Py_GT: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
577 r = i > j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
578 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
579 } |
|
31589
ca3c998afa55
Pass a variable that actually exists to PyFPE_END_PROTECT in
Michael W. Hudson <mwh@python.net>
parents:
31553
diff
changeset
|
580 PyFPE_END_PROTECT(r) |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
581 return PyBool_FromLong(r); |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
582 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
583 Unimplemented: |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
584 Py_INCREF(Py_NotImplemented); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
585 return Py_NotImplemented; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
586 } |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
587 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
588 static long |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
589 float_hash(PyFloatObject *v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
590 { |
|
13976
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13944
diff
changeset
|
591 return _Py_HashDouble(v->ob_fval); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
592 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
593 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
594 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
595 float_add(PyObject *v, PyObject *w) |
| 6 | 596 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
597 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
598 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
599 CONVERT_TO_DOUBLE(w, b); |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
600 PyFPE_START_PROTECT("add", return 0) |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
601 a = a + b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
602 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
603 return PyFloat_FromDouble(a); |
| 6 | 604 } |
| 605 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
606 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
607 float_sub(PyObject *v, PyObject *w) |
| 6 | 608 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
609 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
610 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
611 CONVERT_TO_DOUBLE(w, b); |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
612 PyFPE_START_PROTECT("subtract", return 0) |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
613 a = a - b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
614 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
615 return PyFloat_FromDouble(a); |
| 6 | 616 } |
| 617 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
618 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
619 float_mul(PyObject *v, PyObject *w) |
| 6 | 620 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
621 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
622 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
623 CONVERT_TO_DOUBLE(w, b); |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
624 PyFPE_START_PROTECT("multiply", return 0) |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
625 a = a * b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
626 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
627 return PyFloat_FromDouble(a); |
| 6 | 628 } |
| 629 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
630 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
631 float_div(PyObject *v, PyObject *w) |
| 6 | 632 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
633 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
634 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
635 CONVERT_TO_DOUBLE(w, b); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
636 if (b == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
637 PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
| 6 | 638 return NULL; |
| 639 } | |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
640 PyFPE_START_PROTECT("divide", return 0) |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
641 a = a / b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
642 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
643 return PyFloat_FromDouble(a); |
| 6 | 644 } |
| 645 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
646 static PyObject * |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
647 float_classic_div(PyObject *v, PyObject *w) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
648 { |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
649 double a,b; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
650 CONVERT_TO_DOUBLE(v, a); |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
651 CONVERT_TO_DOUBLE(w, b); |
|
19673
b3271297b984
PEP 238 documented -Qwarn as warning only for classic int or long
Guido van Rossum <guido@python.org>
parents:
19666
diff
changeset
|
652 if (Py_DivisionWarningFlag >= 2 && |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
653 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
654 return NULL; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
655 if (b == 0.0) { |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
656 PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
657 return NULL; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
658 } |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
659 PyFPE_START_PROTECT("divide", return 0) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
660 a = a / b; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
661 PyFPE_END_PROTECT(a) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
662 return PyFloat_FromDouble(a); |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
663 } |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
664 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
665 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
666 float_rem(PyObject *v, PyObject *w) |
| 6 | 667 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
668 double vx, wx; |
| 10833 | 669 double mod; |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
670 CONVERT_TO_DOUBLE(v, vx); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
671 CONVERT_TO_DOUBLE(w, wx); |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
672 if (wx == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
673 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
| 6 | 674 return NULL; |
| 675 } | |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
676 PyFPE_START_PROTECT("modulo", return 0) |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
677 mod = fmod(vx, wx); |
| 10833 | 678 /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 679 0 if wx < sqrt(smallest nonzero double) */ | |
| 680 if (mod && ((wx < 0) != (mod < 0))) { | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
681 mod += wx; |
| 6 | 682 } |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
683 PyFPE_END_PROTECT(mod) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
684 return PyFloat_FromDouble(mod); |
| 6 | 685 } |
| 686 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
687 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
688 float_divmod(PyObject *v, PyObject *w) |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
689 { |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
690 double vx, wx; |
| 10833 | 691 double div, mod, floordiv; |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
692 CONVERT_TO_DOUBLE(v, vx); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
693 CONVERT_TO_DOUBLE(w, wx); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
694 if (wx == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
695 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
696 return NULL; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
697 } |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
698 PyFPE_START_PROTECT("divmod", return 0) |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
699 mod = fmod(vx, wx); |
|
14616
284508c653c7
Fixed legit gripe from c.l.py that math.fmod docs aren't confusing enough.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
700 /* fmod is typically exact, so vx-mod is *mathematically* an |
| 10833 | 701 exact multiple of wx. But this is fp arithmetic, and fp |
| 702 vx - mod is an approximation; the result is that div may | |
| 703 not be an exact integral value after the division, although | |
| 704 it will always be very close to one. | |
| 705 */ | |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
706 div = (vx - mod) / wx; |
|
20827
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
707 if (mod) { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
708 /* ensure the remainder has the same sign as the denominator */ |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
709 if ((wx < 0) != (mod < 0)) { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
710 mod += wx; |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
711 div -= 1.0; |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
712 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
713 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
714 else { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
715 /* the remainder is zero, and in the presence of signed zeroes |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
716 fmod returns different results across platforms; ensure |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
717 it has the same sign as the denominator; we'd like to do |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
718 "mod = wx * 0.0", but that may get optimized away */ |
|
20829
14dc693af532
float_divmod(): the code wasn't sick enough to stop the MS optimizer
Tim Peters <tim.peters@gmail.com>
parents:
20827
diff
changeset
|
719 mod *= mod; /* hide "mod = +0" from optimizer */ |
|
20827
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
720 if (wx < 0.0) |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
721 mod = -mod; |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
722 } |
| 10833 | 723 /* snap quotient to nearest integral value */ |
|
20827
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
724 if (div) { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
725 floordiv = floor(div); |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
726 if (div - floordiv > 0.5) |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
727 floordiv += 1.0; |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
728 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
729 else { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
730 /* div is zero - get the same sign as the true quotient */ |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
731 div *= div; /* hide "div = +0" from optimizers */ |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
732 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */ |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
733 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
734 PyFPE_END_PROTECT(floordiv) |
| 10833 | 735 return Py_BuildValue("(dd)", floordiv, mod); |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
736 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
737 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
738 static PyObject * |
|
21353
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
739 float_floor_div(PyObject *v, PyObject *w) |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
740 { |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
741 PyObject *t, *r; |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
742 |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
743 t = float_divmod(v, w); |
|
21354
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
744 if (t == NULL || t == Py_NotImplemented) |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
745 return t; |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
746 assert(PyTuple_CheckExact(t)); |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
747 r = PyTuple_GET_ITEM(t, 0); |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
748 Py_INCREF(r); |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
749 Py_DECREF(t); |
|
fbddb64d21c2
float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents:
21353
diff
changeset
|
750 return r; |
|
21353
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
751 } |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
752 |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
753 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
754 float_pow(PyObject *v, PyObject *w, PyObject *z) |
| 6 | 755 { |
| 756 double iv, iw, ix; | |
|
19666
cf65ebf87560
New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
757 |
|
cf65ebf87560
New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
758 if ((PyObject *)z != Py_None) { |
|
19719
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19674
diff
changeset
|
759 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not " |
|
19753
c0580d8c193e
Better error msg for 3-arg pow with a float argument.
Tim Peters <tim.peters@gmail.com>
parents:
19750
diff
changeset
|
760 "allowed unless all arguments are integers"); |
|
19666
cf65ebf87560
New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
761 return NULL; |
|
cf65ebf87560
New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
762 } |
|
cf65ebf87560
New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
763 |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
764 CONVERT_TO_DOUBLE(v, iv); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
765 CONVERT_TO_DOUBLE(w, iw); |
|
15041
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
766 |
|
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
767 /* Sort out special cases here instead of relying on pow() */ |
|
19457
6c2b7b624ef2
float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
768 if (iw == 0) { /* v**0 is 1, even 0**0 */ |
|
41580
a68467cc06d3
Remove dead code. This code couldn't be reached because earlier in
Neal Norwitz <nnorwitz@gmail.com>
parents:
39642
diff
changeset
|
769 return PyFloat_FromDouble(1.0); |
|
15041
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
770 } |
|
19457
6c2b7b624ef2
float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
771 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */ |
|
15041
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
772 if (iw < 0.0) { |
|
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
773 PyErr_SetString(PyExc_ZeroDivisionError, |
| 15318 | 774 "0.0 cannot be raised to a negative power"); |
|
15041
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
775 return NULL; |
|
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
776 } |
|
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
777 return PyFloat_FromDouble(0.0); |
|
7eefde90ec0e
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
778 } |
|
29252
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
779 if (iv < 0.0) { |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
780 /* Whether this is an error is a mess, and bumps into libm |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
781 * bugs so we have to figure it out ourselves. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
782 */ |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
783 if (iw != floor(iw)) { |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
784 PyErr_SetString(PyExc_ValueError, "negative number " |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
785 "cannot be raised to a fractional power"); |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
786 return NULL; |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
787 } |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
788 /* iw is an exact integer, albeit perhaps a very large one. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
789 * -1 raised to an exact integer should never be exceptional. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
790 * Alas, some libms (chiefly glibc as of early 2003) return |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
791 * NaN and set EDOM on pow(-1, large_int) if the int doesn't |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
792 * happen to be representable in a *C* integer. That's a |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
793 * bug; we let that slide in math.pow() (which currently |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
794 * reflects all platform accidents), but not for Python's **. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
795 */ |
|
38293
688f25757104
Added a new macro, Py_IS_FINITE(X). On windows there is an intrinsic for this and it is more efficient than to use !Py_IS_INFINITE(X) && !Py_IS_NAN(X). No change on other platforms
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
37815
diff
changeset
|
796 if (iv == -1.0 && Py_IS_FINITE(iw)) { |
|
29252
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
797 /* Return 1 if iw is even, -1 if iw is odd; there's |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
798 * no guarantee that any C integral type is big |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
799 * enough to hold iw, so we have to check this |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
800 * indirectly. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
801 */ |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
802 ix = floor(iw * 0.5) * 2.0; |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
803 return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0); |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
804 } |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
805 /* Else iv != -1.0, and overflow or underflow are possible. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
806 * Unless we're to write pow() ourselves, we have to trust |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
807 * the platform to do this correctly. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
808 */ |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
809 } |
|
19457
6c2b7b624ef2
float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
810 errno = 0; |
|
6c2b7b624ef2
float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
811 PyFPE_START_PROTECT("pow", return NULL) |
|
6c2b7b624ef2
float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
812 ix = pow(iv, iw); |
|
6c2b7b624ef2
float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
813 PyFPE_END_PROTECT(ix) |
|
22228
4b6eda7d0c8f
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21354
diff
changeset
|
814 Py_ADJUST_ERANGE1(ix); |
|
39642
981181d2a4fe
Reverting the patch that tried to fix the issue whereby x**2 raises
Alex Martelli <aleaxit@gmail.com>
parents:
39639
diff
changeset
|
815 if (errno != 0) { |
|
29252
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
816 /* We don't expect any errno value other than ERANGE, but |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
817 * the range of libm bugs appears unbounded. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
818 */ |
|
39642
981181d2a4fe
Reverting the patch that tried to fix the issue whereby x**2 raises
Alex Martelli <aleaxit@gmail.com>
parents:
39639
diff
changeset
|
819 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError : |
|
981181d2a4fe
Reverting the patch that tried to fix the issue whereby x**2 raises
Alex Martelli <aleaxit@gmail.com>
parents:
39639
diff
changeset
|
820 PyExc_ValueError); |
| 6 | 821 return NULL; |
| 27 | 822 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
823 return PyFloat_FromDouble(ix); |
| 6 | 824 } |
| 825 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
826 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
827 float_neg(PyFloatObject *v) |
| 6 | 828 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
829 return PyFloat_FromDouble(-v->ob_fval); |
| 6 | 830 } |
| 831 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
832 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
833 float_pos(PyFloatObject *v) |
| 6 | 834 { |
|
19879
bc50581b3d7c
More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents:
19865
diff
changeset
|
835 if (PyFloat_CheckExact(v)) { |
|
bc50581b3d7c
More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents:
19865
diff
changeset
|
836 Py_INCREF(v); |
|
bc50581b3d7c
More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents:
19865
diff
changeset
|
837 return (PyObject *)v; |
|
bc50581b3d7c
More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents:
19865
diff
changeset
|
838 } |
|
bc50581b3d7c
More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents:
19865
diff
changeset
|
839 else |
|
bc50581b3d7c
More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents:
19865
diff
changeset
|
840 return PyFloat_FromDouble(v->ob_fval); |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
841 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
842 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
843 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
844 float_abs(PyFloatObject *v) |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
845 { |
|
20826
3751366c2f39
float_abs() again: Guido pointed out that this could screw up in the
Tim Peters <tim.peters@gmail.com>
parents:
20823
diff
changeset
|
846 return PyFloat_FromDouble(fabs(v->ob_fval)); |
| 6 | 847 } |
| 848 | |
| 239 | 849 static int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
850 float_nonzero(PyFloatObject *v) |
| 239 | 851 { |
| 852 return v->ob_fval != 0.0; | |
| 853 } | |
| 854 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
855 static int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
856 float_coerce(PyObject **pv, PyObject **pw) |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
857 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
858 if (PyInt_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
859 long x = PyInt_AsLong(*pw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
860 *pw = PyFloat_FromDouble((double)x); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
861 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
862 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
863 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
864 else if (PyLong_Check(*pw)) { |
|
27484
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
865 double x = PyLong_AsDouble(*pw); |
|
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
866 if (x == -1.0 && PyErr_Occurred()) |
|
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
867 return -1; |
|
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
868 *pw = PyFloat_FromDouble(x); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
869 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
870 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
871 } |
|
19976
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
872 else if (PyFloat_Check(*pw)) { |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
873 Py_INCREF(*pv); |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
874 Py_INCREF(*pw); |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
875 return 0; |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
876 } |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
877 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
|
878 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
879 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
880 static PyObject * |
|
26291
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26272
diff
changeset
|
881 float_long(PyObject *v) |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26272
diff
changeset
|
882 { |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26272
diff
changeset
|
883 double x = PyFloat_AsDouble(v); |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26272
diff
changeset
|
884 return PyLong_FromDouble(x); |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26272
diff
changeset
|
885 } |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26272
diff
changeset
|
886 |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26272
diff
changeset
|
887 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
888 float_int(PyObject *v) |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
889 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
890 double x = PyFloat_AsDouble(v); |
|
18838
a8575f358b9f
SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
891 double wholepart; /* integral portion of x, rounded toward 0 */ |
|
a8575f358b9f
SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
892 |
|
a8575f358b9f
SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
893 (void)modf(x, &wholepart); |
|
26328
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
894 /* Try to get out cheap if this fits in a Python int. The attempt |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
895 * to cast to long must be protected, as C doesn't define what |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
896 * happens if the double is too big to fit in a long. Some rare |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
897 * systems raise an exception then (RISCOS was mentioned as one, |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
898 * and someone using a non-default option on Sun also bumped into |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
899 * that). Note that checking for >= and <= LONG_{MIN,MAX} would |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
900 * still be vulnerable: if a long has more bits of precision than |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
901 * a double, casting MIN/MAX to double may yield an approximation, |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
902 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
903 * yield true from the C expression wholepart<=LONG_MAX, despite |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
904 * that wholepart is actually greater than LONG_MAX. |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
905 */ |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
906 if (LONG_MIN < wholepart && wholepart < LONG_MAX) { |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
907 const long aslong = (long)wholepart; |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
908 return PyInt_FromLong(aslong); |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
909 } |
|
26328
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
910 return PyLong_FromDouble(wholepart); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
911 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
912 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
913 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
914 float_float(PyObject *v) |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
915 { |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33549
diff
changeset
|
916 if (PyFloat_CheckExact(v)) |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33549
diff
changeset
|
917 Py_INCREF(v); |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33549
diff
changeset
|
918 else |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33549
diff
changeset
|
919 v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
920 return v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
921 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
922 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
923 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
924 static PyObject * |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
925 float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
926 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
927 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
928 float_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
929 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
930 PyObject *x = Py_False; /* Integer zero */ |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
931 static char *kwlist[] = {"x", 0}; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
932 |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
933 if (type != &PyFloat_Type) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
934 return float_subtype_new(type, args, kwds); /* Wimp out */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
935 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
936 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
937 if (PyString_Check(x)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
938 return PyFloat_FromString(x, NULL); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
939 return PyNumber_Float(x); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
940 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
941 |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
942 /* Wimpy, slow approach to tp_new calls for subtypes of float: |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
943 first create a regular float from whatever arguments we got, |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
944 then allocate a subtype instance and initialize its ob_fval |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
945 from the regular float. The regular float is then thrown away. |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
946 */ |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
947 static PyObject * |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
948 float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
949 { |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
950 PyObject *tmp, *newobj; |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
951 |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
952 assert(PyType_IsSubtype(type, &PyFloat_Type)); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
953 tmp = float_new(&PyFloat_Type, args, kwds); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
954 if (tmp == NULL) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
955 return NULL; |
|
19889
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19879
diff
changeset
|
956 assert(PyFloat_CheckExact(tmp)); |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
957 newobj = type->tp_alloc(type, 0); |
|
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
958 if (newobj == NULL) { |
|
29641
4909aa64405f
SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents:
29252
diff
changeset
|
959 Py_DECREF(tmp); |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
960 return NULL; |
|
29641
4909aa64405f
SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents:
29252
diff
changeset
|
961 } |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
962 ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval; |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
963 Py_DECREF(tmp); |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
964 return newobj; |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
965 } |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
966 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
967 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
968 float_getnewargs(PyFloatObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
969 { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
970 return Py_BuildValue("(d)", v->ob_fval); |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
971 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
972 |
| 34972 | 973 /* this is for the benefit of the pack/unpack routines below */ |
| 974 | |
| 975 typedef enum { | |
| 976 unknown_format, ieee_big_endian_format, ieee_little_endian_format | |
| 977 } float_format_type; | |
| 978 | |
| 979 static float_format_type double_format, float_format; | |
| 980 static float_format_type detected_double_format, detected_float_format; | |
| 981 | |
| 982 static PyObject * | |
| 983 float_getformat(PyTypeObject *v, PyObject* arg) | |
| 984 { | |
| 985 char* s; | |
| 986 float_format_type r; | |
| 987 | |
| 988 if (!PyString_Check(arg)) { | |
| 989 PyErr_Format(PyExc_TypeError, | |
| 990 "__getformat__() argument must be string, not %.500s", | |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
991 Py_Type(arg)->tp_name); |
| 34972 | 992 return NULL; |
| 993 } | |
| 994 s = PyString_AS_STRING(arg); | |
| 995 if (strcmp(s, "double") == 0) { | |
| 996 r = double_format; | |
| 997 } | |
| 998 else if (strcmp(s, "float") == 0) { | |
| 999 r = float_format; | |
| 1000 } | |
| 1001 else { | |
| 1002 PyErr_SetString(PyExc_ValueError, | |
| 1003 "__getformat__() argument 1 must be " | |
| 1004 "'double' or 'float'"); | |
| 1005 return NULL; | |
| 1006 } | |
| 1007 | |
| 1008 switch (r) { | |
| 1009 case unknown_format: | |
| 1010 return PyString_FromString("unknown"); | |
| 1011 case ieee_little_endian_format: | |
| 1012 return PyString_FromString("IEEE, little-endian"); | |
| 1013 case ieee_big_endian_format: | |
| 1014 return PyString_FromString("IEEE, big-endian"); | |
| 1015 default: | |
| 1016 Py_FatalError("insane float_format or double_format"); | |
| 1017 return NULL; | |
| 1018 } | |
| 1019 } | |
| 1020 | |
| 1021 PyDoc_STRVAR(float_getformat_doc, | |
| 1022 "float.__getformat__(typestr) -> string\n" | |
| 1023 "\n" | |
| 1024 "You probably don't want to use this function. It exists mainly to be\n" | |
| 1025 "used in Python's test suite.\n" | |
| 1026 "\n" | |
| 1027 "typestr must be 'double' or 'float'. This function returns whichever of\n" | |
| 1028 "'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n" | |
| 1029 "format of floating point numbers used by the C type named by typestr."); | |
| 1030 | |
| 1031 static PyObject * | |
| 1032 float_setformat(PyTypeObject *v, PyObject* args) | |
| 1033 { | |
| 1034 char* typestr; | |
| 1035 char* format; | |
| 1036 float_format_type f; | |
| 1037 float_format_type detected; | |
| 1038 float_format_type *p; | |
| 1039 | |
| 1040 if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format)) | |
| 1041 return NULL; | |
| 1042 | |
| 1043 if (strcmp(typestr, "double") == 0) { | |
| 1044 p = &double_format; | |
| 1045 detected = detected_double_format; | |
| 1046 } | |
| 1047 else if (strcmp(typestr, "float") == 0) { | |
| 1048 p = &float_format; | |
| 1049 detected = detected_float_format; | |
| 1050 } | |
| 1051 else { | |
| 1052 PyErr_SetString(PyExc_ValueError, | |
| 1053 "__setformat__() argument 1 must " | |
| 1054 "be 'double' or 'float'"); | |
| 1055 return NULL; | |
| 1056 } | |
| 1057 | |
| 1058 if (strcmp(format, "unknown") == 0) { | |
| 1059 f = unknown_format; | |
| 1060 } | |
| 1061 else if (strcmp(format, "IEEE, little-endian") == 0) { | |
| 1062 f = ieee_little_endian_format; | |
| 1063 } | |
| 1064 else if (strcmp(format, "IEEE, big-endian") == 0) { | |
| 1065 f = ieee_big_endian_format; | |
| 1066 } | |
| 1067 else { | |
| 1068 PyErr_SetString(PyExc_ValueError, | |
| 1069 "__setformat__() argument 2 must be " | |
| 1070 "'unknown', 'IEEE, little-endian' or " | |
| 1071 "'IEEE, big-endian'"); | |
| 1072 return NULL; | |
| 1073 | |
| 1074 } | |
| 1075 | |
| 1076 if (f != unknown_format && f != detected) { | |
| 1077 PyErr_Format(PyExc_ValueError, | |
| 1078 "can only set %s format to 'unknown' or the " | |
| 1079 "detected platform value", typestr); | |
| 1080 return NULL; | |
| 1081 } | |
| 1082 | |
| 1083 *p = f; | |
| 1084 Py_RETURN_NONE; | |
| 1085 } | |
| 1086 | |
| 1087 PyDoc_STRVAR(float_setformat_doc, | |
| 1088 "float.__setformat__(typestr, fmt) -> None\n" | |
| 1089 "\n" | |
| 1090 "You probably don't want to use this function. It exists mainly to be\n" | |
| 1091 "used in Python's test suite.\n" | |
| 1092 "\n" | |
| 1093 "typestr must be 'double' or 'float'. fmt must be one of 'unknown',\n" | |
| 1094 "'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n" | |
| 1095 "one of the latter two if it appears to match the underlying C reality.\n" | |
| 1096 "\n" | |
| 1097 "Overrides the automatic determination of C-level floating point type.\n" | |
| 1098 "This affects how floats are converted to and from binary strings."); | |
| 1099 | |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1100 static PyMethodDef float_methods[] = { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1101 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS}, |
| 34972 | 1102 {"__getformat__", (PyCFunction)float_getformat, |
| 1103 METH_O|METH_CLASS, float_getformat_doc}, | |
| 1104 {"__setformat__", (PyCFunction)float_setformat, | |
| 1105 METH_VARARGS|METH_CLASS, float_setformat_doc}, | |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1106 {NULL, NULL} /* sentinel */ |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1107 }; |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1108 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23344
diff
changeset
|
1109 PyDoc_STRVAR(float_doc, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1110 "float(x) -> floating point number\n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1111 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23344
diff
changeset
|
1112 Convert a string or number to a floating point number, if possible."); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1113 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1114 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1115 static PyNumberMethods float_as_number = { |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1116 float_add, /*nb_add*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1117 float_sub, /*nb_subtract*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1118 float_mul, /*nb_multiply*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1119 float_classic_div, /*nb_divide*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1120 float_rem, /*nb_remainder*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1121 float_divmod, /*nb_divmod*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1122 float_pow, /*nb_power*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
1123 (unaryfunc)float_neg, /*nb_negative*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
1124 (unaryfunc)float_pos, /*nb_positive*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
1125 (unaryfunc)float_abs, /*nb_absolute*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
1126 (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
|
1127 0, /*nb_invert*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1128 0, /*nb_lshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1129 0, /*nb_rshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1130 0, /*nb_and*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1131 0, /*nb_xor*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1132 0, /*nb_or*/ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1133 float_coerce, /*nb_coerce*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1134 float_int, /*nb_int*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1135 float_long, /*nb_long*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1136 float_float, /*nb_float*/ |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1137 0, /* nb_oct */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1138 0, /* nb_hex */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1139 0, /* nb_inplace_add */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1140 0, /* nb_inplace_subtract */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1141 0, /* nb_inplace_multiply */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1142 0, /* nb_inplace_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1143 0, /* nb_inplace_remainder */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1144 0, /* nb_inplace_power */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1145 0, /* nb_inplace_lshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1146 0, /* nb_inplace_rshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1147 0, /* nb_inplace_and */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1148 0, /* nb_inplace_xor */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1149 0, /* nb_inplace_or */ |
|
21353
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
1150 float_floor_div, /* nb_floor_divide */ |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1151 float_div, /* nb_true_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1152 0, /* nb_inplace_floor_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1153 0, /* nb_inplace_true_divide */ |
| 6 | 1154 }; |
| 1155 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1156 PyTypeObject PyFloat_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
1157 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6 | 1158 "float", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1159 sizeof(PyFloatObject), |
| 6 | 1160 0, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1161 (destructor)float_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1162 (printfunc)float_print, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1163 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1164 0, /* tp_setattr */ |
|
32150
65f9ad9bc829
Remove float_compare as per
Michael W. Hudson <mwh@python.net>
parents:
31590
diff
changeset
|
1165 0, /* tp_compare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1166 (reprfunc)float_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1167 &float_as_number, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1168 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1169 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1170 (hashfunc)float_hash, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1171 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1172 (reprfunc)float_str, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1173 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1174 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1175 0, /* tp_as_buffer */ |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1176 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1177 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1178 float_doc, /* tp_doc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1179 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1180 0, /* tp_clear */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1181 float_richcompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1182 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1183 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1184 0, /* tp_iternext */ |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1185 float_methods, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1186 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1187 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1188 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1189 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1190 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1191 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1192 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1193 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1194 0, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1195 float_new, /* tp_new */ |
| 6 | 1196 }; |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1197 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1198 void |
| 34972 | 1199 _PyFloat_Init(void) |
| 1200 { | |
| 1201 /* We attempt to determine if this machine is using IEEE | |
| 1202 floating point formats by peering at the bits of some | |
| 1203 carefully chosen values. If it looks like we are on an | |
| 1204 IEEE platform, the float packing/unpacking routines can | |
| 1205 just copy bits, if not they resort to arithmetic & shifts | |
| 1206 and masks. The shifts & masks approach works on all finite | |
| 1207 values, but what happens to infinities, NaNs and signed | |
| 1208 zeroes on packing is an accident, and attempting to unpack | |
| 1209 a NaN or an infinity will raise an exception. | |
| 1210 | |
| 1211 Note that if we're on some whacked-out platform which uses | |
| 1212 IEEE formats but isn't strictly little-endian or big- | |
| 1213 endian, we will fall back to the portable shifts & masks | |
| 1214 method. */ | |
| 1215 | |
| 1216 #if SIZEOF_DOUBLE == 8 | |
| 1217 { | |
| 1218 double x = 9006104071832581.0; | |
| 1219 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0) | |
| 1220 detected_double_format = ieee_big_endian_format; | |
| 1221 else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0) | |
| 1222 detected_double_format = ieee_little_endian_format; | |
| 1223 else | |
| 1224 detected_double_format = unknown_format; | |
| 1225 } | |
| 1226 #else | |
| 1227 detected_double_format = unknown_format; | |
| 1228 #endif | |
| 1229 | |
| 1230 #if SIZEOF_FLOAT == 4 | |
| 1231 { | |
| 1232 float y = 16711938.0; | |
| 1233 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0) | |
| 1234 detected_float_format = ieee_big_endian_format; | |
| 1235 else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0) | |
| 1236 detected_float_format = ieee_little_endian_format; | |
| 1237 else | |
| 1238 detected_float_format = unknown_format; | |
| 1239 } | |
| 1240 #else | |
| 1241 detected_float_format = unknown_format; | |
| 1242 #endif | |
| 1243 | |
| 1244 double_format = detected_double_format; | |
| 1245 float_format = detected_float_format; | |
| 1246 } | |
| 1247 | |
| 1248 void | |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1249 PyFloat_Fini(void) |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1250 { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1251 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
|
1252 PyFloatBlock *list, *next; |
|
32533
201986d6d9d6
Fix a couple of signed/unsigned comparison warnings
Neal Norwitz <nnorwitz@gmail.com>
parents:
32305
diff
changeset
|
1253 unsigned i; |
|
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
|
1254 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
|
1255 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
|
1256 |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1257 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
|
1258 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
|
1259 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
|
1260 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
|
1261 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
|
1262 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
|
1263 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
|
1264 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
|
1265 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
|
1266 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
|
1267 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
|
1268 i++, p++) { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
1269 if (PyFloat_CheckExact(p) && Py_Refcnt(p) != 0) |
|
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
|
1270 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
|
1271 } |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1272 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
|
1273 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
|
1274 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
|
1275 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
|
1276 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
|
1277 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
|
1278 i++, p++) { |
|
19865
f2a3009b528e
Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents:
19753
diff
changeset
|
1279 if (!PyFloat_CheckExact(p) || |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
1280 Py_Refcnt(p) == 0) { |
|
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
1281 Py_Type(p) = (struct _typeobject *) |
|
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
|
1282 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
|
1283 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
|
1284 } |
|
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
|
1285 } |
|
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
|
1286 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1287 else { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
1288 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
|
1289 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
|
1290 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1291 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
|
1292 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
|
1293 } |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1294 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
|
1295 return; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1296 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
|
1297 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
|
1298 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
|
1299 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1300 else { |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1301 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
|
1302 ": %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
|
1303 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
|
1304 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
|
1305 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1306 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
|
1307 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
|
1308 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
|
1309 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
|
1310 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
|
1311 i++, p++) { |
|
19865
f2a3009b528e
Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents:
19753
diff
changeset
|
1312 if (PyFloat_CheckExact(p) && |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
1313 Py_Refcnt(p) != 0) { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1314 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
|
1315 PyFloat_AsString(buf, p); |
|
36759
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
1316 /* XXX(twouters) cast refcount to |
|
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
1317 long until %zd is universally |
|
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
1318 available |
|
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
1319 */ |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1320 fprintf(stderr, |
|
36759
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
1321 "# <float at %p, refcnt=%ld, val=%s>\n", |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41580
diff
changeset
|
1322 p, (long)Py_Refcnt(p), 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
|
1323 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1324 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1325 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
|
1326 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1327 } |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1328 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1329 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1330 /*---------------------------------------------------------------------------- |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1331 * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h. |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1332 * |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1333 * TODO: On platforms that use the standard IEEE-754 single and double |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1334 * formats natively, these routines could simply copy the bytes. |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1335 */ |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1336 int |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1337 _PyFloat_Pack4(double x, unsigned char *p, int le) |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1338 { |
| 34972 | 1339 if (float_format == unknown_format) { |
| 1340 unsigned char sign; | |
| 1341 int e; | |
| 1342 double f; | |
| 1343 unsigned int fbits; | |
| 1344 int incr = 1; | |
| 1345 | |
| 1346 if (le) { | |
| 1347 p += 3; | |
| 1348 incr = -1; | |
| 1349 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1350 |
| 34972 | 1351 if (x < 0) { |
| 1352 sign = 1; | |
| 1353 x = -x; | |
| 1354 } | |
| 1355 else | |
| 1356 sign = 0; | |
| 1357 | |
| 1358 f = frexp(x, &e); | |
| 1359 | |
| 1360 /* Normalize f to be in the range [1.0, 2.0) */ | |
| 1361 if (0.5 <= f && f < 1.0) { | |
| 1362 f *= 2.0; | |
| 1363 e--; | |
| 1364 } | |
| 1365 else if (f == 0.0) | |
| 1366 e = 0; | |
| 1367 else { | |
| 1368 PyErr_SetString(PyExc_SystemError, | |
| 1369 "frexp() result out of range"); | |
| 1370 return -1; | |
| 1371 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1372 |
| 34972 | 1373 if (e >= 128) |
| 1374 goto Overflow; | |
| 1375 else if (e < -126) { | |
| 1376 /* Gradual underflow */ | |
| 1377 f = ldexp(f, 126 + e); | |
| 1378 e = 0; | |
| 1379 } | |
| 1380 else if (!(e == 0 && f == 0.0)) { | |
| 1381 e += 127; | |
| 1382 f -= 1.0; /* Get rid of leading 1 */ | |
| 1383 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1384 |
| 34972 | 1385 f *= 8388608.0; /* 2**23 */ |
| 1386 fbits = (unsigned int)(f + 0.5); /* Round */ | |
| 1387 assert(fbits <= 8388608); | |
| 1388 if (fbits >> 23) { | |
| 1389 /* The carry propagated out of a string of 23 1 bits. */ | |
| 1390 fbits = 0; | |
| 1391 ++e; | |
| 1392 if (e >= 255) | |
| 1393 goto Overflow; | |
| 1394 } | |
| 1395 | |
| 1396 /* First byte */ | |
| 1397 *p = (sign << 7) | (e >> 1); | |
| 1398 p += incr; | |
| 1399 | |
| 1400 /* Second byte */ | |
| 1401 *p = (char) (((e & 1) << 7) | (fbits >> 16)); | |
| 1402 p += incr; | |
| 1403 | |
| 1404 /* Third byte */ | |
| 1405 *p = (fbits >> 8) & 0xFF; | |
| 1406 p += incr; | |
| 1407 | |
| 1408 /* Fourth byte */ | |
| 1409 *p = fbits & 0xFF; | |
| 1410 | |
| 1411 /* Done */ | |
| 1412 return 0; | |
| 1413 | |
| 1414 Overflow: | |
| 1415 PyErr_SetString(PyExc_OverflowError, | |
| 1416 "float too large to pack with f format"); | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1417 return -1; |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1418 } |
| 34972 | 1419 else { |
|
35178
cf7a849ce61f
Apparently some compiler gives a warning on
Michael W. Hudson <mwh@python.net>
parents:
34972
diff
changeset
|
1420 float y = (float)x; |
| 34972 | 1421 const char *s = (char*)&y; |
| 1422 int i, incr = 1; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1423 |
| 34972 | 1424 if ((float_format == ieee_little_endian_format && !le) |
| 1425 || (float_format == ieee_big_endian_format && le)) { | |
| 1426 p += 3; | |
| 1427 incr = -1; | |
| 1428 } | |
| 1429 | |
| 1430 for (i = 0; i < 4; i++) { | |
| 1431 *p = *s++; | |
| 1432 p += incr; | |
| 1433 } | |
| 1434 return 0; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1435 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1436 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1437 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1438 int |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1439 _PyFloat_Pack8(double x, unsigned char *p, int le) |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1440 { |
| 34972 | 1441 if (double_format == unknown_format) { |
| 1442 unsigned char sign; | |
| 1443 int e; | |
| 1444 double f; | |
| 1445 unsigned int fhi, flo; | |
| 1446 int incr = 1; | |
| 1447 | |
| 1448 if (le) { | |
| 1449 p += 7; | |
| 1450 incr = -1; | |
| 1451 } | |
| 1452 | |
| 1453 if (x < 0) { | |
| 1454 sign = 1; | |
| 1455 x = -x; | |
| 1456 } | |
| 1457 else | |
| 1458 sign = 0; | |
| 1459 | |
| 1460 f = frexp(x, &e); | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1461 |
| 34972 | 1462 /* Normalize f to be in the range [1.0, 2.0) */ |
| 1463 if (0.5 <= f && f < 1.0) { | |
| 1464 f *= 2.0; | |
| 1465 e--; | |
| 1466 } | |
| 1467 else if (f == 0.0) | |
| 1468 e = 0; | |
| 1469 else { | |
| 1470 PyErr_SetString(PyExc_SystemError, | |
| 1471 "frexp() result out of range"); | |
| 1472 return -1; | |
| 1473 } | |
| 1474 | |
| 1475 if (e >= 1024) | |
| 1476 goto Overflow; | |
| 1477 else if (e < -1022) { | |
| 1478 /* Gradual underflow */ | |
| 1479 f = ldexp(f, 1022 + e); | |
| 1480 e = 0; | |
| 1481 } | |
| 1482 else if (!(e == 0 && f == 0.0)) { | |
| 1483 e += 1023; | |
| 1484 f -= 1.0; /* Get rid of leading 1 */ | |
| 1485 } | |
| 1486 | |
| 1487 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */ | |
| 1488 f *= 268435456.0; /* 2**28 */ | |
| 1489 fhi = (unsigned int)f; /* Truncate */ | |
| 1490 assert(fhi < 268435456); | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1491 |
| 34972 | 1492 f -= (double)fhi; |
| 1493 f *= 16777216.0; /* 2**24 */ | |
| 1494 flo = (unsigned int)(f + 0.5); /* Round */ | |
| 1495 assert(flo <= 16777216); | |
| 1496 if (flo >> 24) { | |
| 1497 /* The carry propagated out of a string of 24 1 bits. */ | |
| 1498 flo = 0; | |
| 1499 ++fhi; | |
| 1500 if (fhi >> 28) { | |
| 1501 /* And it also progagated out of the next 28 bits. */ | |
| 1502 fhi = 0; | |
| 1503 ++e; | |
| 1504 if (e >= 2047) | |
| 1505 goto Overflow; | |
| 1506 } | |
| 1507 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1508 |
| 34972 | 1509 /* First byte */ |
| 1510 *p = (sign << 7) | (e >> 4); | |
| 1511 p += incr; | |
| 1512 | |
| 1513 /* Second byte */ | |
| 1514 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24)); | |
| 1515 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1516 |
| 34972 | 1517 /* Third byte */ |
| 1518 *p = (fhi >> 16) & 0xFF; | |
| 1519 p += incr; | |
| 1520 | |
| 1521 /* Fourth byte */ | |
| 1522 *p = (fhi >> 8) & 0xFF; | |
| 1523 p += incr; | |
| 1524 | |
| 1525 /* Fifth byte */ | |
| 1526 *p = fhi & 0xFF; | |
| 1527 p += incr; | |
| 1528 | |
| 1529 /* Sixth byte */ | |
| 1530 *p = (flo >> 16) & 0xFF; | |
| 1531 p += incr; | |
| 1532 | |
| 1533 /* Seventh byte */ | |
| 1534 *p = (flo >> 8) & 0xFF; | |
| 1535 p += incr; | |
| 1536 | |
| 1537 /* Eighth byte */ | |
| 1538 *p = flo & 0xFF; | |
| 1539 p += incr; | |
| 1540 | |
| 1541 /* Done */ | |
| 1542 return 0; | |
| 1543 | |
| 1544 Overflow: | |
| 1545 PyErr_SetString(PyExc_OverflowError, | |
| 1546 "float too large to pack with d format"); | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1547 return -1; |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1548 } |
| 34972 | 1549 else { |
| 1550 const char *s = (char*)&x; | |
| 1551 int i, incr = 1; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1552 |
| 34972 | 1553 if ((double_format == ieee_little_endian_format && !le) |
| 1554 || (double_format == ieee_big_endian_format && le)) { | |
| 1555 p += 7; | |
| 1556 incr = -1; | |
| 1557 } | |
| 1558 | |
| 1559 for (i = 0; i < 8; i++) { | |
| 1560 *p = *s++; | |
| 1561 p += incr; | |
| 1562 } | |
| 1563 return 0; | |
| 1564 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1565 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1566 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1567 double |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1568 _PyFloat_Unpack4(const unsigned char *p, int le) |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1569 { |
| 34972 | 1570 if (float_format == unknown_format) { |
| 1571 unsigned char sign; | |
| 1572 int e; | |
| 1573 unsigned int f; | |
| 1574 double x; | |
| 1575 int incr = 1; | |
| 1576 | |
| 1577 if (le) { | |
| 1578 p += 3; | |
| 1579 incr = -1; | |
| 1580 } | |
| 1581 | |
| 1582 /* First byte */ | |
| 1583 sign = (*p >> 7) & 1; | |
| 1584 e = (*p & 0x7F) << 1; | |
| 1585 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1586 |
| 34972 | 1587 /* Second byte */ |
| 1588 e |= (*p >> 7) & 1; | |
| 1589 f = (*p & 0x7F) << 16; | |
| 1590 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1591 |
| 34972 | 1592 if (e == 255) { |
| 1593 PyErr_SetString( | |
| 1594 PyExc_ValueError, | |
| 1595 "can't unpack IEEE 754 special value " | |
| 1596 "on non-IEEE platform"); | |
| 1597 return -1; | |
| 1598 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1599 |
| 34972 | 1600 /* Third byte */ |
| 1601 f |= *p << 8; | |
| 1602 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1603 |
| 34972 | 1604 /* Fourth byte */ |
| 1605 f |= *p; | |
| 1606 | |
| 1607 x = (double)f / 8388608.0; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1608 |
| 34972 | 1609 /* XXX This sadly ignores Inf/NaN issues */ |
| 1610 if (e == 0) | |
| 1611 e = -126; | |
| 1612 else { | |
| 1613 x += 1.0; | |
| 1614 e -= 127; | |
| 1615 } | |
| 1616 x = ldexp(x, e); | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1617 |
| 34972 | 1618 if (sign) |
| 1619 x = -x; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1620 |
| 34972 | 1621 return x; |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1622 } |
| 34972 | 1623 else { |
| 35987 | 1624 float x; |
| 1625 | |
| 34972 | 1626 if ((float_format == ieee_little_endian_format && !le) |
| 1627 || (float_format == ieee_big_endian_format && le)) { | |
| 35987 | 1628 char buf[4]; |
| 34972 | 1629 char *d = &buf[3]; |
| 1630 int i; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1631 |
| 34972 | 1632 for (i = 0; i < 4; i++) { |
| 1633 *d-- = *p++; | |
| 1634 } | |
| 35987 | 1635 memcpy(&x, buf, 4); |
| 34972 | 1636 } |
| 1637 else { | |
| 35987 | 1638 memcpy(&x, p, 4); |
| 34972 | 1639 } |
| 35987 | 1640 |
| 1641 return x; | |
| 34972 | 1642 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1643 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1644 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1645 double |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1646 _PyFloat_Unpack8(const unsigned char *p, int le) |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1647 { |
| 34972 | 1648 if (double_format == unknown_format) { |
| 1649 unsigned char sign; | |
| 1650 int e; | |
| 1651 unsigned int fhi, flo; | |
| 1652 double x; | |
| 1653 int incr = 1; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1654 |
| 34972 | 1655 if (le) { |
| 1656 p += 7; | |
| 1657 incr = -1; | |
| 1658 } | |
| 1659 | |
| 1660 /* First byte */ | |
| 1661 sign = (*p >> 7) & 1; | |
| 1662 e = (*p & 0x7F) << 4; | |
| 1663 | |
| 1664 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1665 |
| 34972 | 1666 /* Second byte */ |
| 1667 e |= (*p >> 4) & 0xF; | |
| 1668 fhi = (*p & 0xF) << 24; | |
| 1669 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1670 |
| 34972 | 1671 if (e == 2047) { |
| 1672 PyErr_SetString( | |
| 1673 PyExc_ValueError, | |
| 1674 "can't unpack IEEE 754 special value " | |
| 1675 "on non-IEEE platform"); | |
| 1676 return -1.0; | |
| 1677 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1678 |
| 34972 | 1679 /* Third byte */ |
| 1680 fhi |= *p << 16; | |
| 1681 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1682 |
| 34972 | 1683 /* Fourth byte */ |
| 1684 fhi |= *p << 8; | |
| 1685 p += incr; | |
| 1686 | |
| 1687 /* Fifth byte */ | |
| 1688 fhi |= *p; | |
| 1689 p += incr; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1690 |
| 34972 | 1691 /* Sixth byte */ |
| 1692 flo = *p << 16; | |
| 1693 p += incr; | |
| 1694 | |
| 1695 /* Seventh byte */ | |
| 1696 flo |= *p << 8; | |
| 1697 p += incr; | |
| 1698 | |
| 1699 /* Eighth byte */ | |
| 1700 flo |= *p; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1701 |
| 34972 | 1702 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 1703 x /= 268435456.0; /* 2**28 */ | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1704 |
| 34972 | 1705 if (e == 0) |
| 1706 e = -1022; | |
| 1707 else { | |
| 1708 x += 1.0; | |
| 1709 e -= 1023; | |
| 1710 } | |
| 1711 x = ldexp(x, e); | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1712 |
| 34972 | 1713 if (sign) |
| 1714 x = -x; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1715 |
| 34972 | 1716 return x; |
| 1717 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1718 else { |
| 35987 | 1719 double x; |
| 1720 | |
| 34972 | 1721 if ((double_format == ieee_little_endian_format && !le) |
| 1722 || (double_format == ieee_big_endian_format && le)) { | |
| 1723 char buf[8]; | |
| 1724 char *d = &buf[7]; | |
| 1725 int i; | |
| 1726 | |
| 1727 for (i = 0; i < 8; i++) { | |
| 1728 *d-- = *p++; | |
| 1729 } | |
| 35987 | 1730 memcpy(&x, buf, 8); |
| 34972 | 1731 } |
| 1732 else { | |
| 35987 | 1733 memcpy(&x, p, 8); |
| 34972 | 1734 } |
| 35987 | 1735 |
| 1736 return x; | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1737 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1738 } |
