Mercurial > cpython-lru_cache-weakref
annotate Objects/floatobject.c @ 47916:bed3dfd99281 legacy-trunk
Issue 3287: Raise correct exception for float inputs.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Thu, 10 Jul 2008 09:31:08 +0000 |
| parents | 0738119c1ef8 |
| children | 97ba936e8df8 |
| 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" |
|
44670
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
8 #include "structseq.h" |
| 6 | 9 |
| 86 | 10 #include <ctype.h> |
| 44085 | 11 #include <float.h> |
| 6 | 12 |
|
46154
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46135
diff
changeset
|
13 #ifdef HAVE_IEEEFP_H |
|
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46135
diff
changeset
|
14 #include <ieeefp.h> |
|
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46135
diff
changeset
|
15 #endif |
|
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46135
diff
changeset
|
16 |
|
44940
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44670
diff
changeset
|
17 #ifdef _OSF_SOURCE |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44670
diff
changeset
|
18 /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44670
diff
changeset
|
19 extern int finite(double); |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44670
diff
changeset
|
20 #endif |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44670
diff
changeset
|
21 |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
22 /* 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
|
23 #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
|
24 #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
|
25 #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
|
26 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 }; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
31 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
32 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
|
33 |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
34 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
|
35 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
|
36 |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
37 static PyFloatObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
38 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
|
39 { |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
40 PyFloatObject *p, *q; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
41 /* 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
|
42 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
|
43 if (p == NULL) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12248
diff
changeset
|
44 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
|
45 ((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
|
46 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
|
47 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
|
48 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
|
49 while (--q > p) |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
50 Py_TYPE(q) = (struct _typeobject *)(q-1); |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
51 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
|
52 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
|
53 } |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
54 |
| 44085 | 55 double |
| 56 PyFloat_GetMax(void) | |
| 57 { | |
| 58 return DBL_MAX; | |
| 59 } | |
| 60 | |
| 61 double | |
| 62 PyFloat_GetMin(void) | |
| 63 { | |
| 64 return DBL_MIN; | |
| 65 } | |
| 66 | |
|
45060
c05ee77b39f2
The previous change was causing a segfault after multiple calls to Py_Initialize() and Py_Finalize().
Christian Heimes <christian@cheimes.de>
parents:
45059
diff
changeset
|
67 static PyTypeObject FloatInfoType = {0, 0, 0, 0, 0, 0}; |
|
44670
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
68 |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
69 PyDoc_STRVAR(floatinfo__doc__, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
70 "sys.floatinfo\n\ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
71 \n\ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
72 A structseq holding information about the float type. It contains low level\n\ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
73 information about the precision and internal representation. Please study\n\ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
74 your system's :file:`float.h` for more information."); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
75 |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
76 static PyStructSequence_Field floatinfo_fields[] = { |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
77 {"max", "DBL_MAX -- maximum representable finite float"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
78 {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) " |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
79 "is representable"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
80 {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e " |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
81 "is representable"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
82 {"min", "DBL_MIN -- Minimum positive normalizer float"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
83 {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) " |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
84 "is a normalized float"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
85 {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is " |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
86 "a normalized"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
87 {"dig", "DBL_DIG -- digits"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
88 {"mant_dig", "DBL_MANT_DIG -- mantissa digits"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
89 {"epsilon", "DBL_EPSILON -- Difference between 1 and the next " |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
90 "representable float"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
91 {"radix", "FLT_RADIX -- radix of exponent"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
92 {"rounds", "FLT_ROUNDS -- addition rounds"}, |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
93 {0} |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
94 }; |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
95 |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
96 static PyStructSequence_Desc floatinfo_desc = { |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
97 "sys.floatinfo", /* name */ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
98 floatinfo__doc__, /* doc */ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
99 floatinfo_fields, /* fields */ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
100 11 |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
101 }; |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
102 |
| 44085 | 103 PyObject * |
| 104 PyFloat_GetInfo(void) | |
| 105 { | |
|
45060
c05ee77b39f2
The previous change was causing a segfault after multiple calls to Py_Initialize() and Py_Finalize().
Christian Heimes <christian@cheimes.de>
parents:
45059
diff
changeset
|
106 PyObject* floatinfo; |
|
44670
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
107 int pos = 0; |
| 44085 | 108 |
|
44670
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
109 floatinfo = PyStructSequence_New(&FloatInfoType); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
110 if (floatinfo == NULL) { |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
111 return NULL; |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
112 } |
| 44085 | 113 |
|
44670
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
114 #define SetIntFlag(flag) \ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
115 PyStructSequence_SET_ITEM(floatinfo, pos++, PyInt_FromLong(flag)) |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
116 #define SetDblFlag(flag) \ |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
117 PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag)) |
| 44085 | 118 |
|
44670
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
119 SetDblFlag(DBL_MAX); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
120 SetIntFlag(DBL_MAX_EXP); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
121 SetIntFlag(DBL_MAX_10_EXP); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
122 SetDblFlag(DBL_MIN); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
123 SetIntFlag(DBL_MIN_EXP); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
124 SetIntFlag(DBL_MIN_10_EXP); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
125 SetIntFlag(DBL_DIG); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
126 SetIntFlag(DBL_MANT_DIG); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
127 SetDblFlag(DBL_EPSILON); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
128 SetIntFlag(FLT_RADIX); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
129 SetIntFlag(FLT_ROUNDS); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
130 #undef SetIntFlag |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
131 #undef SetDblFlag |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
132 |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
133 if (PyErr_Occurred()) { |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
134 Py_CLEAR(floatinfo); |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
135 return NULL; |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
136 } |
|
d66e5837aa57
Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents:
44488
diff
changeset
|
137 return floatinfo; |
| 44085 | 138 } |
| 139 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
140 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
141 PyFloat_FromDouble(double fval) |
| 6 | 142 { |
|
5504
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
143 register PyFloatObject *op; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
144 if (free_list == NULL) { |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
145 if ((free_list = fill_free_list()) == NULL) |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
146 return NULL; |
|
c88b9b868995
Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
147 } |
|
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
|
148 /* 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
|
149 op = free_list; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
150 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
|
151 PyObject_INIT(op, &PyFloat_Type); |
| 27 | 152 op->ob_fval = fval; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
153 return (PyObject *) op; |
| 6 | 154 } |
| 155 | |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
156 /************************************************************************** |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
157 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
|
158 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
|
159 |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
160 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
|
161 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
|
162 help from this function!). |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
163 |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
164 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
|
165 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
|
166 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
|
167 |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
168 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
|
169 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
|
170 *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
|
171 **************************************************************************/ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
172 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
173 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
|
174 { |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
175 const char *s, *last, *end, *sp; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
176 double x; |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
177 char buffer[256]; /* for errors */ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
178 #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
|
179 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
|
180 #endif |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36235
diff
changeset
|
181 Py_ssize_t len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
182 |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
183 if (pend) |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
184 *pend = NULL; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
185 if (PyString_Check(v)) { |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
186 s = PyString_AS_STRING(v); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
187 len = PyString_GET_SIZE(v); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
188 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
189 #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
|
190 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
|
191 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
|
192 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
|
193 "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
|
194 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
|
195 } |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 s = s_buffer; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36235
diff
changeset
|
202 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
|
203 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
204 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
205 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
|
206 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
|
207 "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
|
208 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
209 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
210 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
211 last = s + len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
212 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
|
213 s++; |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
214 if (*s == '\0') { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
215 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
|
216 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
217 } |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
218 sp = s; |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
219 /* 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
|
220 * 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
|
221 * 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
|
222 * 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
|
223 * 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
|
224 * key off errno. |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
225 */ |
|
14851
5449302c4d2e
Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents:
14783
diff
changeset
|
226 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
|
227 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
|
228 PyFPE_END_PROTECT(x) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
229 errno = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
230 /* 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
|
231 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
|
232 if (end > last) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
233 end = last; |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
234 /* Check for inf and nan. This is done late because it rarely happens. */ |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
235 if (end == s) { |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
236 char *p = (char*)sp; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
237 int sign = 1; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
238 |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
239 if (*p == '-') { |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
240 sign = -1; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
241 p++; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
242 } |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
243 if (*p == '+') { |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
244 p++; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
245 } |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
246 if (PyOS_strnicmp(p, "inf", 4) == 0) { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
247 Py_RETURN_INF(sign); |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
248 } |
|
47889
4bf84035cb5f
Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents:
47822
diff
changeset
|
249 if (PyOS_strnicmp(p, "infinity", 9) == 0) { |
|
4bf84035cb5f
Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents:
47822
diff
changeset
|
250 Py_RETURN_INF(sign); |
|
4bf84035cb5f
Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents:
47822
diff
changeset
|
251 } |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
252 #ifdef Py_NAN |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
253 if(PyOS_strnicmp(p, "nan", 4) == 0) { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
254 Py_RETURN_NAN; |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
255 } |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
256 #endif |
|
21079
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
257 PyOS_snprintf(buffer, sizeof(buffer), |
|
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
258 "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
|
259 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
|
260 return NULL; |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
261 } |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
262 /* 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
|
263 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
|
264 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
|
265 end++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
266 if (*end != '\0') { |
|
21079
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
267 PyOS_snprintf(buffer, sizeof(buffer), |
|
8cef75cacb01
PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents:
20829
diff
changeset
|
268 "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
|
269 PyErr_SetString(PyExc_ValueError, buffer); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
270 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
271 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
272 else if (end != last) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
273 PyErr_SetString(PyExc_ValueError, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
274 "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
|
275 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
276 } |
|
14783
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
277 if (x == 0.0) { |
|
77eddddbb7cc
Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
278 /* 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
285 return PyFloat_FromDouble(x); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
286 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10833
diff
changeset
|
287 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
288 static void |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
289 float_dealloc(PyFloatObject *op) |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
290 { |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19976
diff
changeset
|
291 if (PyFloat_CheckExact(op)) { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
292 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
|
293 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
|
294 } |
|
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19976
diff
changeset
|
295 else |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
296 Py_TYPE(op)->tp_free((PyObject *)op); |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
297 } |
|
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
298 |
| 6 | 299 double |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
300 PyFloat_AsDouble(PyObject *op) |
| 6 | 301 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
302 PyNumberMethods *nb; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
303 PyFloatObject *fo; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
304 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
|
305 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
306 if (op && PyFloat_Check(op)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
307 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
|
308 |
|
26272
fbccef8b3153
Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24898
diff
changeset
|
309 if (op == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
310 PyErr_BadArgument(); |
| 6 | 311 return -1; |
| 312 } | |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
313 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
314 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
|
315 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
|
316 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
|
317 } |
|
fbccef8b3153
Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24898
diff
changeset
|
318 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
319 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
|
320 if (fo == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
321 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
322 if (!PyFloat_Check(fo)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
323 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
324 "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
|
325 return -1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
326 } |
|
20823
c7aa332a2ffa
SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents:
20654
diff
changeset
|
327 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
328 val = PyFloat_AS_DOUBLE(fo); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
329 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
|
330 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
331 return val; |
| 6 | 332 } |
| 333 | |
| 334 /* Methods */ | |
| 335 | |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
336 static void |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
337 format_float(char *buf, size_t buflen, PyFloatObject *v, int precision) |
| 6 | 338 { |
| 339 register char *cp; | |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
32150
diff
changeset
|
340 char format[32]; |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
341 int i; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
342 |
| 6 | 343 /* Subroutine for float_repr and float_print. |
| 344 We want float numbers to be recognizable as such, | |
| 345 i.e., they should contain a decimal point or an exponent. | |
| 346 However, %g may print the number as an integer; | |
| 347 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
|
348 |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
349 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
|
350 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
|
351 PyOS_ascii_formatd(buf, buflen, format, v->ob_fval); |
| 6 | 352 cp = buf; |
| 353 if (*cp == '-') | |
| 354 cp++; | |
| 355 for (; *cp != '\0'; cp++) { | |
| 356 /* Any non-digit means it's not an integer; | |
| 357 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
|
358 if (!isdigit(Py_CHARMASK(*cp))) |
| 6 | 359 break; |
| 360 } | |
| 361 if (*cp == '\0') { | |
| 362 *cp++ = '.'; | |
| 363 *cp++ = '0'; | |
| 364 *cp++ = '\0'; | |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
365 return; |
| 6 | 366 } |
|
44344
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
367 /* Checking the next three chars should be more than enough to |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
368 * detect inf or nan, even on Windows. We check for inf or nan |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
369 * at last because they are rare cases. |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
370 */ |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
371 for (i=0; *cp != '\0' && i<3; cp++, i++) { |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
372 if (isdigit(Py_CHARMASK(*cp)) || *cp == '.') |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
373 continue; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
374 /* found something that is neither a digit nor point |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
375 * it might be a NaN or INF |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
376 */ |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
377 #ifdef Py_NAN |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
378 if (Py_IS_NAN(v->ob_fval)) { |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
379 strcpy(buf, "nan"); |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
380 } |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
381 else |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
382 #endif |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
383 if (Py_IS_INFINITY(v->ob_fval)) { |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
384 cp = buf; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
385 if (*cp == '-') |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
386 cp++; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
387 strcpy(cp, "inf"); |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
388 } |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
389 break; |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
390 } |
|
1d69aabf6f7f
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents:
44267
diff
changeset
|
391 |
| 6 | 392 } |
| 393 | |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
394 /* 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
|
395 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
|
396 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
|
397 */ |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
398 void |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
399 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
|
400 { |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
401 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
|
402 } |
|
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
403 |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
404 /* 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
|
405 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
|
406 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
|
407 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
|
408 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
|
409 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
|
410 */ |
|
47822
3ada46f2321d
Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents:
47791
diff
changeset
|
411 #define CONVERT_TO_DOUBLE(obj, dbl) \ |
|
3ada46f2321d
Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents:
47791
diff
changeset
|
412 if (PyFloat_Check(obj)) \ |
|
3ada46f2321d
Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents:
47791
diff
changeset
|
413 dbl = PyFloat_AS_DOUBLE(obj); \ |
|
3ada46f2321d
Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents:
47791
diff
changeset
|
414 else if (convert_to_double(&(obj), &(dbl)) < 0) \ |
|
3ada46f2321d
Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents:
47791
diff
changeset
|
415 return obj; |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
416 |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
417 static int |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
418 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
|
419 { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
420 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
|
421 |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
422 if (PyInt_Check(obj)) { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
423 *dbl = (double)PyInt_AS_LONG(obj); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
424 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
425 else if (PyLong_Check(obj)) { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
426 *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
|
427 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
|
428 *v = NULL; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
429 return -1; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
430 } |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
431 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
432 else { |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
433 Py_INCREF(Py_NotImplemented); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
434 *v = Py_NotImplemented; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
435 return -1; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
436 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
437 return 0; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
438 } |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
439 |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
440 /* 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
|
441 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
442 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
|
443 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
|
444 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
|
445 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
|
446 hardware. |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
447 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
448 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
|
449 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
|
450 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
|
451 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
452 */ |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
453 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
454 #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
|
455 #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
|
456 |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
457 /* 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
|
458 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
|
459 */ |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
460 void |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
461 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
|
462 { |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
463 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
|
464 } |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
465 |
|
17707
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
466 void |
|
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
467 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
|
468 { |
|
21097
9ba3c82946b8
PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21079
diff
changeset
|
469 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
|
470 } |
|
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
471 |
|
745
c7b23cc8cded
answer lint's complaints
Guido van Rossum <guido@python.org>
parents:
632
diff
changeset
|
472 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
473 static int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
474 float_print(PyFloatObject *v, FILE *fp, int flags) |
| 6 | 475 { |
| 476 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
|
477 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
|
478 (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
|
479 Py_BEGIN_ALLOW_THREADS |
| 6 | 480 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
|
481 Py_END_ALLOW_THREADS |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
268
diff
changeset
|
482 return 0; |
| 6 | 483 } |
| 484 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
485 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
486 float_repr(PyFloatObject *v) |
| 6 | 487 { |
|
44267
f909872917b9
The new float repr causes too much trouble and pain. I'm disabling the feature until we have sorted out the issues on all machines. 64bit machines seem to have issues and Guido has reported even worse.
Christian Heimes <christian@cheimes.de>
parents:
44262
diff
changeset
|
488 char buf[100]; |
|
f909872917b9
The new float repr causes too much trouble and pain. I'm disabling the feature until we have sorted out the issues on all machines. 64bit machines seem to have issues and Guido has reported even worse.
Christian Heimes <christian@cheimes.de>
parents:
44262
diff
changeset
|
489 format_float(buf, sizeof(buf), v, PREC_REPR); |
|
f909872917b9
The new float repr causes too much trouble and pain. I'm disabling the feature until we have sorted out the issues on all machines. 64bit machines seem to have issues and Guido has reported even worse.
Christian Heimes <christian@cheimes.de>
parents:
44262
diff
changeset
|
490 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
491 return PyString_FromString(buf); |
|
11631
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
492 } |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
493 |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
494 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
495 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
|
496 { |
|
cf7a0f1b7b48
Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents:
11383
diff
changeset
|
497 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
|
498 format_float(buf, sizeof(buf), v, PREC_STR); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
499 return PyString_FromString(buf); |
| 6 | 500 } |
| 501 | |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
502 /* 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
|
503 * 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
|
504 * 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
|
505 * 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
|
506 * 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
|
507 * 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
|
508 * 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
|
509 * 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
|
510 * 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
|
511 * 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
|
512 * 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
|
513 * 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
|
514 * 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
|
515 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
516 |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
517 static PyObject* |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
518 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
|
519 { |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
520 double i, j; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
521 int r = 0; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
522 |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
523 assert(PyFloat_Check(v)); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
524 i = PyFloat_AS_DOUBLE(v); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
525 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
526 /* 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
|
527 * 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
|
528 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
529 if (PyFloat_Check(w)) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
530 j = PyFloat_AS_DOUBLE(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
531 |
|
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
|
532 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
|
533 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
|
534 /* 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
|
535 * 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
|
536 * 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
|
537 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
538 j = 0.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
539 else |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
540 goto Unimplemented; |
|
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 else if (PyInt_Check(w)) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
544 long jj = PyInt_AS_LONG(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
545 /* 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
|
546 * 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
|
547 * bits. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
548 */ |
|
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
|
549 #if SIZEOF_LONG > 6 |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
550 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
|
551 if (abs >> 48) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
552 /* 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
|
553 * PyLong path. |
|
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 PyObject *result; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
556 PyObject *ww = PyLong_FromLong(jj); |
|
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 if (ww == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
559 return NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
560 result = float_richcompare(v, ww, op); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
561 Py_DECREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
562 return result; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
563 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
564 #endif |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
565 j = (double)jj; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
566 assert((long)j == jj); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
567 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
568 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
569 else if (PyLong_Check(w)) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
570 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
|
571 int wsign = _PyLong_Sign(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
572 size_t nbits; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
573 int exponent; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
574 |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
575 if (vsign != wsign) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
576 /* Magnitudes are irrelevant -- the signs alone |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
577 * determine the outcome. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
578 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
579 i = (double)vsign; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
580 j = (double)wsign; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
581 goto Compare; |
|
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 /* The signs are the same. */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
584 /* 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
|
585 nbits = _PyLong_NumBits(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
586 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
|
587 /* 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
|
588 * 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
|
589 * 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
|
590 * 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
|
591 * finite float. |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
592 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
593 PyErr_Clear(); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
594 i = (double)vsign; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
595 assert(wsign != 0); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
596 j = wsign * 2.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
597 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
598 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
599 if (nbits <= 48) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
600 j = PyLong_AsDouble(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
601 /* 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
|
602 assert(j != -1.0 || ! PyErr_Occurred()); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
603 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
604 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
605 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
|
606 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
|
607 * 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
|
608 * vsign != wsign branch at the start */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
609 /* 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
|
610 if (vsign < 0) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
611 /* "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
|
612 * comparator. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
613 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
614 i = -i; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
615 op = _Py_SwappedOp[op]; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
616 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
617 assert(i > 0.0); |
|
36235
9f66bdd5a061
Fix icc warnings: remove unused variable
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
618 (void) frexp(i, &exponent); |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
619 /* 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
|
620 * 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
|
621 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
622 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
|
623 i = 1.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
624 j = 2.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
625 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
626 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
627 if ((size_t)exponent > nbits) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
628 i = 2.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
629 j = 1.0; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
630 goto Compare; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
631 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
632 /* 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
|
633 * 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
|
634 * outcome. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
635 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
636 { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
637 double fracpart; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
638 double intpart; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
639 PyObject *result = NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
640 PyObject *one = NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
641 PyObject *vv = NULL; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
642 PyObject *ww = w; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
643 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
644 if (wsign < 0) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
645 ww = PyNumber_Negative(w); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
646 if (ww == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
647 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
648 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
649 else |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
650 Py_INCREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
651 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
652 fracpart = modf(i, &intpart); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
653 vv = PyLong_FromDouble(intpart); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
654 if (vv == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
655 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
656 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
657 if (fracpart != 0.0) { |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
658 /* 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
|
659 * to represent the lost fraction. |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
660 */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
661 PyObject *temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
662 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
663 one = PyInt_FromLong(1); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
664 if (one == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
665 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
666 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
667 temp = PyNumber_Lshift(ww, one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
668 if (temp == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
669 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
670 Py_DECREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
671 ww = temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
672 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
673 temp = PyNumber_Lshift(vv, one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
674 if (temp == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
675 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
676 Py_DECREF(vv); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
677 vv = temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
678 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
679 temp = PyNumber_Or(vv, one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
680 if (temp == NULL) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
681 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
682 Py_DECREF(vv); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
683 vv = temp; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
684 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
685 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
686 r = PyObject_RichCompareBool(vv, ww, op); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
687 if (r < 0) |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
688 goto Error; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
689 result = PyBool_FromLong(r); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
690 Error: |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
691 Py_XDECREF(vv); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
692 Py_XDECREF(ww); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
693 Py_XDECREF(one); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
694 return result; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
695 } |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
696 } /* else if (PyLong_Check(w)) */ |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
697 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
698 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
|
699 goto Unimplemented; |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
700 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
701 Compare: |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
702 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
|
703 switch (op) { |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
704 case Py_EQ: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
705 r = i == j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
706 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
707 case Py_NE: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
708 r = i != j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
709 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
710 case Py_LE: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
711 r = i <= j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
712 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
713 case Py_GE: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
714 r = i >= j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
715 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
716 case Py_LT: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
717 r = i < j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
718 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
719 case Py_GT: |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
720 r = i > j; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
721 break; |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
722 } |
|
31589
ca3c998afa55
Pass a variable that actually exists to PyFPE_END_PROTECT in
Michael W. Hudson <mwh@python.net>
parents:
31553
diff
changeset
|
723 PyFPE_END_PROTECT(r) |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
724 return PyBool_FromLong(r); |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
725 |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
726 Unimplemented: |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
727 Py_INCREF(Py_NotImplemented); |
|
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
32533
diff
changeset
|
728 return Py_NotImplemented; |
|
31553
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
729 } |
|
a6d8d5ace6a7
"Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents:
31384
diff
changeset
|
730 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
731 static long |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
732 float_hash(PyFloatObject *v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
733 { |
|
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
|
734 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
|
735 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
736 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
737 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
738 float_add(PyObject *v, PyObject *w) |
| 6 | 739 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
740 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
741 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
742 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
|
743 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
|
744 a = a + b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
745 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
746 return PyFloat_FromDouble(a); |
| 6 | 747 } |
| 748 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
749 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
750 float_sub(PyObject *v, PyObject *w) |
| 6 | 751 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
752 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
753 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
754 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
|
755 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
|
756 a = a - b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
757 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
758 return PyFloat_FromDouble(a); |
| 6 | 759 } |
| 760 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
761 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
762 float_mul(PyObject *v, PyObject *w) |
| 6 | 763 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
764 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
765 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
766 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
|
767 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
|
768 a = a * b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
769 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
770 return PyFloat_FromDouble(a); |
| 6 | 771 } |
| 772 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
773 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
774 float_div(PyObject *v, PyObject *w) |
| 6 | 775 { |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
776 double a,b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
777 CONVERT_TO_DOUBLE(v, a); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
778 CONVERT_TO_DOUBLE(w, b); |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
779 #ifdef Py_NAN |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
780 if (b == 0.0) { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
781 PyErr_SetString(PyExc_ZeroDivisionError, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
782 "float division"); |
| 6 | 783 return NULL; |
| 784 } | |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
785 #endif |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
786 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
|
787 a = a / b; |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
788 PyFPE_END_PROTECT(a) |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
789 return PyFloat_FromDouble(a); |
| 6 | 790 } |
| 791 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
792 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
|
793 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
|
794 { |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
795 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
|
796 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
|
797 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
|
798 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
|
799 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
|
800 return NULL; |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
801 #ifdef Py_NAN |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
802 if (b == 0.0) { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
803 PyErr_SetString(PyExc_ZeroDivisionError, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
804 "float division"); |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
805 return NULL; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
806 } |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
807 #endif |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
808 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
|
809 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
|
810 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
|
811 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
|
812 } |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
813 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19590
diff
changeset
|
814 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
815 float_rem(PyObject *v, PyObject *w) |
| 6 | 816 { |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
817 double vx, wx; |
| 10833 | 818 double mod; |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
819 CONVERT_TO_DOUBLE(v, vx); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
820 CONVERT_TO_DOUBLE(w, wx); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
821 #ifdef Py_NAN |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
822 if (wx == 0.0) { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
823 PyErr_SetString(PyExc_ZeroDivisionError, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
824 "float modulo"); |
| 6 | 825 return NULL; |
| 826 } | |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
827 #endif |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
828 PyFPE_START_PROTECT("modulo", return 0) |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
829 mod = fmod(vx, wx); |
| 10833 | 830 /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 831 0 if wx < sqrt(smallest nonzero double) */ | |
| 832 if (mod && ((wx < 0) != (mod < 0))) { | |
|
632
0c1938f2e0c0
Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents:
496
diff
changeset
|
833 mod += wx; |
| 6 | 834 } |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
835 PyFPE_END_PROTECT(mod) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
836 return PyFloat_FromDouble(mod); |
| 6 | 837 } |
| 838 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
839 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
840 float_divmod(PyObject *v, PyObject *w) |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
841 { |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
842 double vx, wx; |
| 10833 | 843 double div, mod, floordiv; |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
844 CONVERT_TO_DOUBLE(v, vx); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
845 CONVERT_TO_DOUBLE(w, wx); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
846 if (wx == 0.0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
847 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
848 return NULL; |
|
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
849 } |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
850 PyFPE_START_PROTECT("divmod", return 0) |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
851 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
|
852 /* fmod is typically exact, so vx-mod is *mathematically* an |
| 10833 | 853 exact multiple of wx. But this is fp arithmetic, and fp |
| 854 vx - mod is an approximation; the result is that div may | |
| 855 not be an exact integral value after the division, although | |
| 856 it will always be very close to one. | |
| 857 */ | |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
858 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
|
859 if (mod) { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
860 /* 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
|
861 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
|
862 mod += wx; |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
863 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
|
864 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
865 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
866 else { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
867 /* 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
|
868 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
|
869 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
|
870 "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
|
871 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
|
872 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
|
873 mod = -mod; |
|
391
caf8466dcc9c
Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
874 } |
| 10833 | 875 /* 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
|
876 if (div) { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
877 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
|
878 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
|
879 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
|
880 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
881 else { |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
882 /* 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
|
883 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
|
884 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
|
885 } |
|
95201153251c
SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents:
20826
diff
changeset
|
886 PyFPE_END_PROTECT(floordiv) |
| 10833 | 887 return Py_BuildValue("(dd)", floordiv, mod); |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
888 } |
|
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
889 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
890 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
|
891 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
|
892 { |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
893 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
|
894 |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
895 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
|
896 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
|
897 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
|
898 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
|
899 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
|
900 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
|
901 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
|
902 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
|
903 } |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
904 |
|
257c938900a6
float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents:
21097
diff
changeset
|
905 static PyObject * |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
906 float_pow(PyObject *v, PyObject *w, PyObject *z) |
| 6 | 907 { |
| 908 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
|
909 |
|
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
|
910 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
|
911 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
|
912 "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
|
913 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
|
914 } |
|
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
|
915 |
|
15723
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
916 CONVERT_TO_DOUBLE(v, iv); |
|
ac7ae3ead88a
Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
917 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
|
918 |
|
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
|
919 /* 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
|
920 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
|
921 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
|
922 } |
|
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
|
923 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
|
924 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
|
925 PyErr_SetString(PyExc_ZeroDivisionError, |
| 15318 | 926 "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
|
927 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
|
928 } |
|
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
|
929 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
|
930 } |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
931 if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
932 return PyFloat_FromDouble(1.0); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
933 } |
|
29252
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
934 if (iv < 0.0) { |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
935 /* 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
|
936 * 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
|
937 */ |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
938 if (iw != floor(iw)) { |
|
44488
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44432
diff
changeset
|
939 PyErr_SetString(PyExc_ValueError, "negative number " |
|
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44432
diff
changeset
|
940 "cannot be raised to a fractional power"); |
|
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44432
diff
changeset
|
941 return NULL; |
|
29252
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
942 } |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
943 /* 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
|
944 * -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
|
945 * 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
|
946 * 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
|
947 * 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
|
948 * 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
|
949 * 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
|
950 */ |
|
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
|
951 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
|
952 /* 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
|
953 * 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
|
954 * 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
|
955 * indirectly. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
956 */ |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
957 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
|
958 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
|
959 } |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
960 /* 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
|
961 * 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
|
962 * the platform to do this correctly. |
|
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
963 */ |
|
3984
0613f17dcfd8
Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents:
3704
diff
changeset
|
964 } |
|
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
|
965 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
|
966 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
|
967 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
|
968 PyFPE_END_PROTECT(ix) |
|
22228
4b6eda7d0c8f
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21354
diff
changeset
|
969 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
|
970 if (errno != 0) { |
|
29252
0b76679bf3f9
SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents:
28368
diff
changeset
|
971 /* 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
|
972 * 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
|
973 */ |
|
39642
981181d2a4fe
Reverting the patch that tried to fix the issue whereby x**2 raises
Alex Martelli <aleaxit@gmail.com>
parents:
39639
diff
changeset
|
974 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
|
975 PyExc_ValueError); |
| 6 | 976 return NULL; |
| 27 | 977 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
978 return PyFloat_FromDouble(ix); |
| 6 | 979 } |
| 980 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
981 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
982 float_neg(PyFloatObject *v) |
| 6 | 983 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
984 return PyFloat_FromDouble(-v->ob_fval); |
| 6 | 985 } |
| 986 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
987 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
988 float_abs(PyFloatObject *v) |
|
227
7ec6058b539b
Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
989 { |
|
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
|
990 return PyFloat_FromDouble(fabs(v->ob_fval)); |
| 6 | 991 } |
| 992 | |
| 239 | 993 static int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
994 float_nonzero(PyFloatObject *v) |
| 239 | 995 { |
| 996 return v->ob_fval != 0.0; | |
| 997 } | |
| 998 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1271
diff
changeset
|
999 static int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1000 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
|
1001 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1002 if (PyInt_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1003 long x = PyInt_AsLong(*pw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1004 *pw = PyFloat_FromDouble((double)x); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1005 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1006 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1007 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1008 else if (PyLong_Check(*pw)) { |
|
27484
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
1009 double x = PyLong_AsDouble(*pw); |
|
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
1010 if (x == -1.0 && PyErr_Occurred()) |
|
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
1011 return -1; |
|
5ba7934a7ba0
Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents:
26328
diff
changeset
|
1012 *pw = PyFloat_FromDouble(x); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1013 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1014 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1015 } |
|
19976
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
1016 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
|
1017 Py_INCREF(*pv); |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
1018 Py_INCREF(*pw); |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
1019 return 0; |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
1020 } |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1021 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
|
1022 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1023 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1024 static PyObject * |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1025 float_is_integer(PyObject *v) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1026 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1027 double x = PyFloat_AsDouble(v); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1028 PyObject *o; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1029 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1030 if (x == -1.0 && PyErr_Occurred()) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1031 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1032 if (!Py_IS_FINITE(x)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1033 Py_RETURN_FALSE; |
|
46792
8baeba13ae93
Issue #2801: fix bug in float.is_integer where ValueError
Mark Dickinson <dickinsm@gmail.com>
parents:
46473
diff
changeset
|
1034 errno = 0; |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1035 PyFPE_START_PROTECT("is_integer", return NULL) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1036 o = (floor(x) == x) ? Py_True : Py_False; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1037 PyFPE_END_PROTECT(x) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1038 if (errno != 0) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1039 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError : |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1040 PyExc_ValueError); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1041 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1042 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1043 Py_INCREF(o); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1044 return o; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1045 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1046 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1047 #if 0 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1048 static PyObject * |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1049 float_is_inf(PyObject *v) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1050 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1051 double x = PyFloat_AsDouble(v); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1052 if (x == -1.0 && PyErr_Occurred()) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1053 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1054 return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1055 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1056 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1057 static PyObject * |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1058 float_is_nan(PyObject *v) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1059 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1060 double x = PyFloat_AsDouble(v); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1061 if (x == -1.0 && PyErr_Occurred()) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1062 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1063 return PyBool_FromLong((long)Py_IS_NAN(x)); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1064 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1065 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1066 static PyObject * |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1067 float_is_finite(PyObject *v) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1068 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1069 double x = PyFloat_AsDouble(v); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1070 if (x == -1.0 && PyErr_Occurred()) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1071 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1072 return PyBool_FromLong((long)Py_IS_FINITE(x)); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1073 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1074 #endif |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1075 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1076 static PyObject * |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1077 float_trunc(PyObject *v) |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1078 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1079 double x = PyFloat_AsDouble(v); |
|
18838
a8575f358b9f
SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
1080 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
|
1081 |
|
a8575f358b9f
SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
1082 (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
|
1083 /* 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
|
1084 * 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
|
1085 * 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
|
1086 * 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
|
1087 * 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
|
1088 * 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
|
1089 * 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
|
1090 * 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
|
1091 * 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
|
1092 * 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
|
1093 * 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
|
1094 */ |
|
f3a69037fdf0
float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents:
26291
diff
changeset
|
1095 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
|
1096 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
|
1097 return PyInt_FromLong(aslong); |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1098 } |
|
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
|
1099 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
|
1100 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1101 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1102 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1103 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
|
1104 { |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33549
diff
changeset
|
1105 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
|
1106 Py_INCREF(v); |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33549
diff
changeset
|
1107 else |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33549
diff
changeset
|
1108 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
|
1109 return v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1110 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1111 |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1112 static PyObject * |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1113 float_as_integer_ratio(PyObject *v, PyObject *unused) |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1114 { |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1115 double self; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1116 double float_part; |
|
45099
d0b31f24694d
Fix int/long typecase. Add check for non-binary floating point.
Raymond Hettinger <python@rcn.com>
parents:
45096
diff
changeset
|
1117 int exponent; |
|
45100
0b4e8afed663
Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents:
45099
diff
changeset
|
1118 int i; |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1119 |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1120 PyObject *prev; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1121 PyObject *py_exponent = NULL; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1122 PyObject *numerator = NULL; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1123 PyObject *denominator = NULL; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1124 PyObject *result_pair = NULL; |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1125 PyNumberMethods *long_methods = PyLong_Type.tp_as_number; |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1126 |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1127 #define INPLACE_UPDATE(obj, call) \ |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1128 prev = obj; \ |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1129 obj = call; \ |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1130 Py_DECREF(prev); \ |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1131 |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1132 CONVERT_TO_DOUBLE(v, self); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1133 |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1134 if (Py_IS_INFINITY(self)) { |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1135 PyErr_SetString(PyExc_OverflowError, |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1136 "Cannot pass infinity to float.as_integer_ratio."); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1137 return NULL; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1138 } |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1139 #ifdef Py_NAN |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1140 if (Py_IS_NAN(self)) { |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1141 PyErr_SetString(PyExc_ValueError, |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1142 "Cannot pass nan to float.as_integer_ratio."); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1143 return NULL; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1144 } |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1145 #endif |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1146 |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1147 PyFPE_START_PROTECT("as_integer_ratio", goto error); |
|
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1148 float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */ |
|
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1149 PyFPE_END_PROTECT(float_part); |
|
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1150 |
|
45100
0b4e8afed663
Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents:
45099
diff
changeset
|
1151 for (i=0; i<300 && float_part != floor(float_part) ; i++) { |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1152 float_part *= 2.0; |
|
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1153 exponent--; |
|
45100
0b4e8afed663
Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents:
45099
diff
changeset
|
1154 } |
|
45101
f5e18c9bde70
Simpler solution to handling non-IEEE 754 environments.
Raymond Hettinger <python@rcn.com>
parents:
45100
diff
changeset
|
1155 /* self == float_part * 2**exponent exactly and float_part is integral. |
|
f5e18c9bde70
Simpler solution to handling non-IEEE 754 environments.
Raymond Hettinger <python@rcn.com>
parents:
45100
diff
changeset
|
1156 If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part |
|
f5e18c9bde70
Simpler solution to handling non-IEEE 754 environments.
Raymond Hettinger <python@rcn.com>
parents:
45100
diff
changeset
|
1157 to be truncated by PyLong_FromDouble(). */ |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1158 |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1159 numerator = PyLong_FromDouble(float_part); |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1160 if (numerator == NULL) goto error; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1161 |
|
45100
0b4e8afed663
Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents:
45099
diff
changeset
|
1162 /* fold in 2**exponent */ |
|
45095
5cb8288360e7
Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents:
45094
diff
changeset
|
1163 denominator = PyLong_FromLong(1); |
|
45099
d0b31f24694d
Fix int/long typecase. Add check for non-binary floating point.
Raymond Hettinger <python@rcn.com>
parents:
45096
diff
changeset
|
1164 py_exponent = PyLong_FromLong(labs((long)exponent)); |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1165 if (py_exponent == NULL) goto error; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1166 INPLACE_UPDATE(py_exponent, |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1167 long_methods->nb_lshift(denominator, py_exponent)); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1168 if (py_exponent == NULL) goto error; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1169 if (exponent > 0) { |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1170 INPLACE_UPDATE(numerator, |
|
45100
0b4e8afed663
Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents:
45099
diff
changeset
|
1171 long_methods->nb_multiply(numerator, py_exponent)); |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1172 if (numerator == NULL) goto error; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1173 } |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1174 else { |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1175 Py_DECREF(denominator); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1176 denominator = py_exponent; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1177 py_exponent = NULL; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1178 } |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1179 |
|
45095
5cb8288360e7
Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents:
45094
diff
changeset
|
1180 /* Returns ints instead of longs where possible */ |
|
5cb8288360e7
Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents:
45094
diff
changeset
|
1181 INPLACE_UPDATE(numerator, PyNumber_Int(numerator)); |
|
5cb8288360e7
Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents:
45094
diff
changeset
|
1182 if (numerator == NULL) goto error; |
|
5cb8288360e7
Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents:
45094
diff
changeset
|
1183 INPLACE_UPDATE(denominator, PyNumber_Int(denominator)); |
|
5cb8288360e7
Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents:
45094
diff
changeset
|
1184 if (denominator == NULL) goto error; |
|
5cb8288360e7
Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents:
45094
diff
changeset
|
1185 |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1186 result_pair = PyTuple_Pack(2, numerator, denominator); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1187 |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1188 #undef INPLACE_UPDATE |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1189 error: |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1190 Py_XDECREF(py_exponent); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1191 Py_XDECREF(denominator); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1192 Py_XDECREF(numerator); |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1193 return result_pair; |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1194 } |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1195 |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1196 PyDoc_STRVAR(float_as_integer_ratio_doc, |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1197 "float.as_integer_ratio() -> (int, int)\n" |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1198 "\n" |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1199 "Returns a pair of integers, whose ratio is exactly equal to the original\n" |
|
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1200 "float and with a positive denominator.\n" |
|
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1201 "Raises OverflowError on infinities and a ValueError on nans.\n" |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1202 "\n" |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1203 ">>> (10.0).as_integer_ratio()\n" |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1204 "(10, 1)\n" |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1205 ">>> (0.0).as_integer_ratio()\n" |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1206 "(0, 1)\n" |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1207 ">>> (-.25).as_integer_ratio()\n" |
|
45094
beea73ca2b88
Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents:
45060
diff
changeset
|
1208 "(-1, 4)"); |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1209 |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1210 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
1211 static PyObject * |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1212 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
|
1213 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1214 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1215 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
|
1216 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1217 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
|
1218 static char *kwlist[] = {"x", 0}; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1219 |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1220 if (type != &PyFloat_Type) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1221 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
|
1222 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
|
1223 return NULL; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1224 if (PyString_Check(x)) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1225 return PyFloat_FromString(x, NULL); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1226 return PyNumber_Float(x); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1227 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1228 |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1229 /* 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
|
1230 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
|
1231 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
|
1232 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
|
1233 */ |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1234 static PyObject * |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1235 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
|
1236 { |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
1237 PyObject *tmp, *newobj; |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1238 |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1239 assert(PyType_IsSubtype(type, &PyFloat_Type)); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1240 tmp = float_new(&PyFloat_Type, args, kwds); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1241 if (tmp == NULL) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1242 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
|
1243 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
|
1244 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
|
1245 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
|
1246 Py_DECREF(tmp); |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1247 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
|
1248 } |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
1249 ((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
|
1250 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
|
1251 return newobj; |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1252 } |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1253 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1254 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1255 float_getnewargs(PyFloatObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1256 { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1257 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
|
1258 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1259 |
| 34972 | 1260 /* this is for the benefit of the pack/unpack routines below */ |
| 1261 | |
| 1262 typedef enum { | |
| 1263 unknown_format, ieee_big_endian_format, ieee_little_endian_format | |
| 1264 } float_format_type; | |
| 1265 | |
| 1266 static float_format_type double_format, float_format; | |
| 1267 static float_format_type detected_double_format, detected_float_format; | |
| 1268 | |
| 1269 static PyObject * | |
| 1270 float_getformat(PyTypeObject *v, PyObject* arg) | |
| 1271 { | |
| 1272 char* s; | |
| 1273 float_format_type r; | |
| 1274 | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1275 if (!PyString_Check(arg)) { |
| 34972 | 1276 PyErr_Format(PyExc_TypeError, |
| 1277 "__getformat__() argument must be string, not %.500s", | |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
1278 Py_TYPE(arg)->tp_name); |
| 34972 | 1279 return NULL; |
| 1280 } | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1281 s = PyString_AS_STRING(arg); |
| 34972 | 1282 if (strcmp(s, "double") == 0) { |
| 1283 r = double_format; | |
| 1284 } | |
| 1285 else if (strcmp(s, "float") == 0) { | |
| 1286 r = float_format; | |
| 1287 } | |
| 1288 else { | |
| 1289 PyErr_SetString(PyExc_ValueError, | |
| 1290 "__getformat__() argument 1 must be " | |
| 1291 "'double' or 'float'"); | |
| 1292 return NULL; | |
| 1293 } | |
| 1294 | |
| 1295 switch (r) { | |
| 1296 case unknown_format: | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1297 return PyString_FromString("unknown"); |
| 34972 | 1298 case ieee_little_endian_format: |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1299 return PyString_FromString("IEEE, little-endian"); |
| 34972 | 1300 case ieee_big_endian_format: |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1301 return PyString_FromString("IEEE, big-endian"); |
| 34972 | 1302 default: |
| 1303 Py_FatalError("insane float_format or double_format"); | |
| 1304 return NULL; | |
| 1305 } | |
| 1306 } | |
| 1307 | |
| 1308 PyDoc_STRVAR(float_getformat_doc, | |
| 1309 "float.__getformat__(typestr) -> string\n" | |
| 1310 "\n" | |
| 1311 "You probably don't want to use this function. It exists mainly to be\n" | |
| 1312 "used in Python's test suite.\n" | |
| 1313 "\n" | |
| 1314 "typestr must be 'double' or 'float'. This function returns whichever of\n" | |
| 1315 "'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n" | |
| 1316 "format of floating point numbers used by the C type named by typestr."); | |
| 1317 | |
| 1318 static PyObject * | |
| 1319 float_setformat(PyTypeObject *v, PyObject* args) | |
| 1320 { | |
| 1321 char* typestr; | |
| 1322 char* format; | |
| 1323 float_format_type f; | |
| 1324 float_format_type detected; | |
| 1325 float_format_type *p; | |
| 1326 | |
| 1327 if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format)) | |
| 1328 return NULL; | |
| 1329 | |
| 1330 if (strcmp(typestr, "double") == 0) { | |
| 1331 p = &double_format; | |
| 1332 detected = detected_double_format; | |
| 1333 } | |
| 1334 else if (strcmp(typestr, "float") == 0) { | |
| 1335 p = &float_format; | |
| 1336 detected = detected_float_format; | |
| 1337 } | |
| 1338 else { | |
| 1339 PyErr_SetString(PyExc_ValueError, | |
| 1340 "__setformat__() argument 1 must " | |
| 1341 "be 'double' or 'float'"); | |
| 1342 return NULL; | |
| 1343 } | |
| 1344 | |
| 1345 if (strcmp(format, "unknown") == 0) { | |
| 1346 f = unknown_format; | |
| 1347 } | |
| 1348 else if (strcmp(format, "IEEE, little-endian") == 0) { | |
| 1349 f = ieee_little_endian_format; | |
| 1350 } | |
| 1351 else if (strcmp(format, "IEEE, big-endian") == 0) { | |
| 1352 f = ieee_big_endian_format; | |
| 1353 } | |
| 1354 else { | |
| 1355 PyErr_SetString(PyExc_ValueError, | |
| 1356 "__setformat__() argument 2 must be " | |
| 1357 "'unknown', 'IEEE, little-endian' or " | |
| 1358 "'IEEE, big-endian'"); | |
| 1359 return NULL; | |
| 1360 | |
| 1361 } | |
| 1362 | |
| 1363 if (f != unknown_format && f != detected) { | |
| 1364 PyErr_Format(PyExc_ValueError, | |
| 1365 "can only set %s format to 'unknown' or the " | |
| 1366 "detected platform value", typestr); | |
| 1367 return NULL; | |
| 1368 } | |
| 1369 | |
| 1370 *p = f; | |
| 1371 Py_RETURN_NONE; | |
| 1372 } | |
| 1373 | |
| 1374 PyDoc_STRVAR(float_setformat_doc, | |
| 1375 "float.__setformat__(typestr, fmt) -> None\n" | |
| 1376 "\n" | |
| 1377 "You probably don't want to use this function. It exists mainly to be\n" | |
| 1378 "used in Python's test suite.\n" | |
| 1379 "\n" | |
| 1380 "typestr must be 'double' or 'float'. fmt must be one of 'unknown',\n" | |
| 1381 "'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n" | |
| 1382 "one of the latter two if it appears to match the underlying C reality.\n" | |
| 1383 "\n" | |
| 1384 "Overrides the automatic determination of C-level floating point type.\n" | |
| 1385 "This affects how floats are converted to and from binary strings."); | |
| 1386 | |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1387 static PyObject * |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1388 float_getzero(PyObject *v, void *closure) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1389 { |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1390 return PyFloat_FromDouble(0.0); |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1391 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1392 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1393 static PyObject * |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1394 float__format__(PyObject *self, PyObject *args) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1395 { |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1396 PyObject *format_spec; |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1397 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1398 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1399 return NULL; |
|
47294
4fe192d48960
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46792
diff
changeset
|
1400 if (PyBytes_Check(format_spec)) |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1401 return _PyFloat_FormatAdvanced(self, |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1402 PyBytes_AS_STRING(format_spec), |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1403 PyBytes_GET_SIZE(format_spec)); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1404 if (PyUnicode_Check(format_spec)) { |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1405 /* Convert format_spec to a str */ |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1406 PyObject *result; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1407 PyObject *str_spec = PyObject_Str(format_spec); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1408 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1409 if (str_spec == NULL) |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1410 return NULL; |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1411 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1412 result = _PyFloat_FormatAdvanced(self, |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1413 PyBytes_AS_STRING(str_spec), |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1414 PyBytes_GET_SIZE(str_spec)); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1415 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47294
diff
changeset
|
1416 Py_DECREF(str_spec); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1417 return result; |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1418 } |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1419 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode"); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1420 return NULL; |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1421 } |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1422 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1423 PyDoc_STRVAR(float__format__doc, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1424 "float.__format__(format_spec) -> string\n" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1425 "\n" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1426 "Formats the float according to format_spec."); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1427 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1428 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1429 static PyMethodDef float_methods[] = { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1430 {"conjugate", (PyCFunction)float_float, METH_NOARGS, |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1431 "Returns self, the complex conjugate of any float."}, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1432 {"__trunc__", (PyCFunction)float_trunc, METH_NOARGS, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1433 "Returns the Integral closest to x between 0 and x."}, |
|
45024
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1434 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, |
|
f062dda54894
Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44940
diff
changeset
|
1435 float_as_integer_ratio_doc}, |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1436 {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1437 "Returns True if the float is an integer."}, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1438 #if 0 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1439 {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1440 "Returns True if the float is positive or negative infinite."}, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1441 {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1442 "Returns True if the float is finite, neither infinite nor NaN."}, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1443 {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1444 "Returns True if the float is not a number (NaN)."}, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
1445 #endif |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1446 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS}, |
| 34972 | 1447 {"__getformat__", (PyCFunction)float_getformat, |
| 1448 METH_O|METH_CLASS, float_getformat_doc}, | |
| 1449 {"__setformat__", (PyCFunction)float_setformat, | |
| 1450 METH_VARARGS|METH_CLASS, float_setformat_doc}, | |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1451 {"__format__", (PyCFunction)float__format__, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45143
diff
changeset
|
1452 METH_VARARGS, float__format__doc}, |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1453 {NULL, NULL} /* sentinel */ |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1454 }; |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1455 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1456 static PyGetSetDef float_getset[] = { |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1457 {"real", |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1458 (getter)float_float, (setter)NULL, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1459 "the real part of a complex number", |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1460 NULL}, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1461 {"imag", |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1462 (getter)float_getzero, (setter)NULL, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1463 "the imaginary part of a complex number", |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1464 NULL}, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1465 {NULL} /* Sentinel */ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1466 }; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1467 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23344
diff
changeset
|
1468 PyDoc_STRVAR(float_doc, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1469 "float(x) -> floating point number\n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1470 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23344
diff
changeset
|
1471 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
|
1472 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1473 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1474 static PyNumberMethods float_as_number = { |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1475 float_add, /*nb_add*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1476 float_sub, /*nb_subtract*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1477 float_mul, /*nb_multiply*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1478 float_classic_div, /*nb_divide*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1479 float_rem, /*nb_remainder*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1480 float_divmod, /*nb_divmod*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1481 float_pow, /*nb_power*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
1482 (unaryfunc)float_neg, /*nb_negative*/ |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1483 (unaryfunc)float_float, /*nb_positive*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
1484 (unaryfunc)float_abs, /*nb_absolute*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
1485 (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
|
1486 0, /*nb_invert*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1487 0, /*nb_lshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1488 0, /*nb_rshift*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1489 0, /*nb_and*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1490 0, /*nb_xor*/ |
|
d3d328680328
Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
391
diff
changeset
|
1491 0, /*nb_or*/ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1492 float_coerce, /*nb_coerce*/ |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1493 float_trunc, /*nb_int*/ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1494 float_trunc, /*nb_long*/ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1495 float_float, /*nb_float*/ |
|
47822
3ada46f2321d
Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents:
47791
diff
changeset
|
1496 0, /* nb_oct */ |
|
3ada46f2321d
Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents:
47791
diff
changeset
|
1497 0, /* nb_hex */ |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1498 0, /* nb_inplace_add */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1499 0, /* nb_inplace_subtract */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1500 0, /* nb_inplace_multiply */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1501 0, /* nb_inplace_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1502 0, /* nb_inplace_remainder */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1503 0, /* nb_inplace_power */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1504 0, /* nb_inplace_lshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1505 0, /* nb_inplace_rshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1506 0, /* nb_inplace_and */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1507 0, /* nb_inplace_xor */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1508 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
|
1509 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
|
1510 float_div, /* nb_true_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1511 0, /* nb_inplace_floor_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1512 0, /* nb_inplace_true_divide */ |
| 6 | 1513 }; |
| 1514 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1515 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
|
1516 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6 | 1517 "float", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1518 sizeof(PyFloatObject), |
| 6 | 1519 0, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1520 (destructor)float_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1521 (printfunc)float_print, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1522 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1523 0, /* tp_setattr */ |
|
32150
65f9ad9bc829
Remove float_compare as per
Michael W. Hudson <mwh@python.net>
parents:
31590
diff
changeset
|
1524 0, /* tp_compare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1525 (reprfunc)float_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1526 &float_as_number, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1527 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1528 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1529 (hashfunc)float_hash, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1530 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1531 (reprfunc)float_str, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1532 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1533 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1534 0, /* tp_as_buffer */ |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1535 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19457
diff
changeset
|
1536 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1537 float_doc, /* tp_doc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1538 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1539 0, /* tp_clear */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36759
diff
changeset
|
1540 float_richcompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1541 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1542 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1543 0, /* tp_iternext */ |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27484
diff
changeset
|
1544 float_methods, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1545 0, /* tp_members */ |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44350
diff
changeset
|
1546 float_getset, /* tp_getset */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1547 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1548 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1549 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1550 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1551 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1552 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1553 0, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18838
diff
changeset
|
1554 float_new, /* tp_new */ |
| 6 | 1555 }; |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1556 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1557 void |
| 34972 | 1558 _PyFloat_Init(void) |
| 1559 { | |
| 1560 /* We attempt to determine if this machine is using IEEE | |
| 1561 floating point formats by peering at the bits of some | |
| 1562 carefully chosen values. If it looks like we are on an | |
| 1563 IEEE platform, the float packing/unpacking routines can | |
| 1564 just copy bits, if not they resort to arithmetic & shifts | |
| 1565 and masks. The shifts & masks approach works on all finite | |
| 1566 values, but what happens to infinities, NaNs and signed | |
| 1567 zeroes on packing is an accident, and attempting to unpack | |
| 1568 a NaN or an infinity will raise an exception. | |
| 1569 | |
| 1570 Note that if we're on some whacked-out platform which uses | |
| 1571 IEEE formats but isn't strictly little-endian or big- | |
| 1572 endian, we will fall back to the portable shifts & masks | |
| 1573 method. */ | |
| 1574 | |
| 1575 #if SIZEOF_DOUBLE == 8 | |
| 1576 { | |
| 1577 double x = 9006104071832581.0; | |
| 1578 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0) | |
| 1579 detected_double_format = ieee_big_endian_format; | |
| 1580 else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0) | |
| 1581 detected_double_format = ieee_little_endian_format; | |
| 1582 else | |
| 1583 detected_double_format = unknown_format; | |
| 1584 } | |
| 1585 #else | |
| 1586 detected_double_format = unknown_format; | |
| 1587 #endif | |
| 1588 | |
| 1589 #if SIZEOF_FLOAT == 4 | |
| 1590 { | |
| 1591 float y = 16711938.0; | |
| 1592 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0) | |
| 1593 detected_float_format = ieee_big_endian_format; | |
| 1594 else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0) | |
| 1595 detected_float_format = ieee_little_endian_format; | |
| 1596 else | |
| 1597 detected_float_format = unknown_format; | |
| 1598 } | |
| 1599 #else | |
| 1600 detected_float_format = unknown_format; | |
| 1601 #endif | |
| 1602 | |
| 1603 double_format = detected_double_format; | |
| 1604 float_format = detected_float_format; | |
|
44267
f909872917b9
The new float repr causes too much trouble and pain. I'm disabling the feature until we have sorted out the issues on all machines. 64bit machines seem to have issues and Guido has reported even worse.
Christian Heimes <christian@cheimes.de>
parents:
44262
diff
changeset
|
1605 |
|
45060
c05ee77b39f2
The previous change was causing a segfault after multiple calls to Py_Initialize() and Py_Finalize().
Christian Heimes <christian@cheimes.de>
parents:
45059
diff
changeset
|
1606 /* Init float info */ |
|
c05ee77b39f2
The previous change was causing a segfault after multiple calls to Py_Initialize() and Py_Finalize().
Christian Heimes <christian@cheimes.de>
parents:
45059
diff
changeset
|
1607 if (FloatInfoType.tp_name == 0) |
|
c05ee77b39f2
The previous change was causing a segfault after multiple calls to Py_Initialize() and Py_Finalize().
Christian Heimes <christian@cheimes.de>
parents:
45059
diff
changeset
|
1608 PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc); |
| 34972 | 1609 } |
| 1610 | |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1611 int |
|
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1612 PyFloat_ClearFreeList(void) |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1613 { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1614 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
|
1615 PyFloatBlock *list, *next; |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1616 int i; |
|
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1617 int u; /* remaining unfreed ints per block */ |
|
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1618 int freelist_size = 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
|
1619 |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1620 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
|
1621 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
|
1622 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
|
1623 while (list != NULL) { |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1624 u = 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
|
1625 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
|
1626 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
|
1627 i++, p++) { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
1628 if (PyFloat_CheckExact(p) && Py_REFCNT(p) != 0) |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1629 u++; |
|
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
|
1630 } |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1631 next = list->next; |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1632 if (u) { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1633 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
|
1634 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
|
1635 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
|
1636 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
|
1637 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
|
1638 if (!PyFloat_CheckExact(p) || |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
1639 Py_REFCNT(p) == 0) { |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
1640 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
|
1641 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
|
1642 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
|
1643 } |
|
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
|
1644 } |
|
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
|
1645 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1646 else { |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1647 PyMem_FREE(list); |
|
10382
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1648 } |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1649 freelist_size += u; |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1650 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
|
1651 } |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1652 return freelist_size; |
| 45143 | 1653 } |
| 1654 | |
| 1655 void | |
| 1656 PyFloat_Fini(void) | |
| 1657 { | |
| 1658 PyFloatObject *p; | |
| 1659 PyFloatBlock *list; | |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1660 int i; |
|
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1661 int u; /* total unfreed floats per block */ |
| 45143 | 1662 |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1663 u = PyFloat_ClearFreeList(); |
| 45143 | 1664 |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1665 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
|
1666 return; |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1667 fprintf(stderr, "# cleanup floats"); |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1668 if (!u) { |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1669 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
|
1670 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1671 else { |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1672 fprintf(stderr, |
|
47897
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1673 ": %d unfreed float%s\n", |
|
0738119c1ef8
- Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents:
47889
diff
changeset
|
1674 u, u == 1 ? "" : "s"); |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1675 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1676 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
|
1677 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
|
1678 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
|
1679 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
|
1680 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
|
1681 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
|
1682 if (PyFloat_CheckExact(p) && |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
1683 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
|
1684 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
|
1685 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
|
1686 /* 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
|
1687 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
|
1688 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
|
1689 */ |
|
10391
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1690 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
|
1691 "# <float at %p, refcnt=%ld, val=%s>\n", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44344
diff
changeset
|
1692 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
|
1693 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1694 } |
|
7b810aa35b5c
Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents:
10382
diff
changeset
|
1695 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
|
1696 } |
|
e2a08a81e56f
OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents:
10375
diff
changeset
|
1697 } |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5504
diff
changeset
|
1698 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1699 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1700 /*---------------------------------------------------------------------------- |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1701 * _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
|
1702 */ |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1703 int |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1704 _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
|
1705 { |
| 34972 | 1706 if (float_format == unknown_format) { |
| 1707 unsigned char sign; | |
| 1708 int e; | |
| 1709 double f; | |
| 1710 unsigned int fbits; | |
| 1711 int incr = 1; | |
| 1712 | |
| 1713 if (le) { | |
| 1714 p += 3; | |
| 1715 incr = -1; | |
| 1716 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1717 |
| 34972 | 1718 if (x < 0) { |
| 1719 sign = 1; | |
| 1720 x = -x; | |
| 1721 } | |
| 1722 else | |
| 1723 sign = 0; | |
| 1724 | |
| 1725 f = frexp(x, &e); | |
| 1726 | |
| 1727 /* Normalize f to be in the range [1.0, 2.0) */ | |
| 1728 if (0.5 <= f && f < 1.0) { | |
| 1729 f *= 2.0; | |
| 1730 e--; | |
| 1731 } | |
| 1732 else if (f == 0.0) | |
| 1733 e = 0; | |
| 1734 else { | |
| 1735 PyErr_SetString(PyExc_SystemError, | |
| 1736 "frexp() result out of range"); | |
| 1737 return -1; | |
| 1738 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1739 |
| 34972 | 1740 if (e >= 128) |
| 1741 goto Overflow; | |
| 1742 else if (e < -126) { | |
| 1743 /* Gradual underflow */ | |
| 1744 f = ldexp(f, 126 + e); | |
| 1745 e = 0; | |
| 1746 } | |
| 1747 else if (!(e == 0 && f == 0.0)) { | |
| 1748 e += 127; | |
| 1749 f -= 1.0; /* Get rid of leading 1 */ | |
| 1750 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1751 |
| 34972 | 1752 f *= 8388608.0; /* 2**23 */ |
| 1753 fbits = (unsigned int)(f + 0.5); /* Round */ | |
| 1754 assert(fbits <= 8388608); | |
| 1755 if (fbits >> 23) { | |
| 1756 /* The carry propagated out of a string of 23 1 bits. */ | |
| 1757 fbits = 0; | |
| 1758 ++e; | |
| 1759 if (e >= 255) | |
| 1760 goto Overflow; | |
| 1761 } | |
| 1762 | |
| 1763 /* First byte */ | |
| 1764 *p = (sign << 7) | (e >> 1); | |
| 1765 p += incr; | |
| 1766 | |
| 1767 /* Second byte */ | |
| 1768 *p = (char) (((e & 1) << 7) | (fbits >> 16)); | |
| 1769 p += incr; | |
| 1770 | |
| 1771 /* Third byte */ | |
| 1772 *p = (fbits >> 8) & 0xFF; | |
| 1773 p += incr; | |
| 1774 | |
| 1775 /* Fourth byte */ | |
| 1776 *p = fbits & 0xFF; | |
| 1777 | |
| 1778 /* Done */ | |
| 1779 return 0; | |
| 1780 | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1781 } |
| 34972 | 1782 else { |
|
35178
cf7a849ce61f
Apparently some compiler gives a warning on
Michael W. Hudson <mwh@python.net>
parents:
34972
diff
changeset
|
1783 float y = (float)x; |
| 34972 | 1784 const char *s = (char*)&y; |
| 1785 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
|
1786 |
|
45759
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1787 if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x)) |
|
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1788 goto Overflow; |
|
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1789 |
| 34972 | 1790 if ((float_format == ieee_little_endian_format && !le) |
| 1791 || (float_format == ieee_big_endian_format && le)) { | |
| 1792 p += 3; | |
| 1793 incr = -1; | |
| 1794 } | |
|
45759
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1795 |
| 34972 | 1796 for (i = 0; i < 4; i++) { |
| 1797 *p = *s++; | |
| 1798 p += incr; | |
| 1799 } | |
| 1800 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
|
1801 } |
|
45759
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1802 Overflow: |
|
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1803 PyErr_SetString(PyExc_OverflowError, |
|
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1804 "float too large to pack with f format"); |
|
354e377c9590
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents:
45381
diff
changeset
|
1805 return -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
|
1806 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1807 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1808 int |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1809 _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
|
1810 { |
| 34972 | 1811 if (double_format == unknown_format) { |
| 1812 unsigned char sign; | |
| 1813 int e; | |
| 1814 double f; | |
| 1815 unsigned int fhi, flo; | |
| 1816 int incr = 1; | |
| 1817 | |
| 1818 if (le) { | |
| 1819 p += 7; | |
| 1820 incr = -1; | |
| 1821 } | |
| 1822 | |
| 1823 if (x < 0) { | |
| 1824 sign = 1; | |
| 1825 x = -x; | |
| 1826 } | |
| 1827 else | |
| 1828 sign = 0; | |
| 1829 | |
| 1830 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
|
1831 |
| 34972 | 1832 /* Normalize f to be in the range [1.0, 2.0) */ |
| 1833 if (0.5 <= f && f < 1.0) { | |
| 1834 f *= 2.0; | |
| 1835 e--; | |
| 1836 } | |
| 1837 else if (f == 0.0) | |
| 1838 e = 0; | |
| 1839 else { | |
| 1840 PyErr_SetString(PyExc_SystemError, | |
| 1841 "frexp() result out of range"); | |
| 1842 return -1; | |
| 1843 } | |
| 1844 | |
| 1845 if (e >= 1024) | |
| 1846 goto Overflow; | |
| 1847 else if (e < -1022) { | |
| 1848 /* Gradual underflow */ | |
| 1849 f = ldexp(f, 1022 + e); | |
| 1850 e = 0; | |
| 1851 } | |
| 1852 else if (!(e == 0 && f == 0.0)) { | |
| 1853 e += 1023; | |
| 1854 f -= 1.0; /* Get rid of leading 1 */ | |
| 1855 } | |
| 1856 | |
| 1857 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */ | |
| 1858 f *= 268435456.0; /* 2**28 */ | |
| 1859 fhi = (unsigned int)f; /* Truncate */ | |
| 1860 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
|
1861 |
| 34972 | 1862 f -= (double)fhi; |
| 1863 f *= 16777216.0; /* 2**24 */ | |
| 1864 flo = (unsigned int)(f + 0.5); /* Round */ | |
| 1865 assert(flo <= 16777216); | |
| 1866 if (flo >> 24) { | |
| 1867 /* The carry propagated out of a string of 24 1 bits. */ | |
| 1868 flo = 0; | |
| 1869 ++fhi; | |
| 1870 if (fhi >> 28) { | |
| 1871 /* And it also progagated out of the next 28 bits. */ | |
| 1872 fhi = 0; | |
| 1873 ++e; | |
| 1874 if (e >= 2047) | |
| 1875 goto Overflow; | |
| 1876 } | |
| 1877 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1878 |
| 34972 | 1879 /* First byte */ |
| 1880 *p = (sign << 7) | (e >> 4); | |
| 1881 p += incr; | |
| 1882 | |
| 1883 /* Second byte */ | |
| 1884 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24)); | |
| 1885 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
|
1886 |
| 34972 | 1887 /* Third byte */ |
| 1888 *p = (fhi >> 16) & 0xFF; | |
| 1889 p += incr; | |
| 1890 | |
| 1891 /* Fourth byte */ | |
| 1892 *p = (fhi >> 8) & 0xFF; | |
| 1893 p += incr; | |
| 1894 | |
| 1895 /* Fifth byte */ | |
| 1896 *p = fhi & 0xFF; | |
| 1897 p += incr; | |
| 1898 | |
| 1899 /* Sixth byte */ | |
| 1900 *p = (flo >> 16) & 0xFF; | |
| 1901 p += incr; | |
| 1902 | |
| 1903 /* Seventh byte */ | |
| 1904 *p = (flo >> 8) & 0xFF; | |
| 1905 p += incr; | |
| 1906 | |
| 1907 /* Eighth byte */ | |
| 1908 *p = flo & 0xFF; | |
| 1909 p += incr; | |
| 1910 | |
| 1911 /* Done */ | |
| 1912 return 0; | |
| 1913 | |
| 1914 Overflow: | |
| 1915 PyErr_SetString(PyExc_OverflowError, | |
| 1916 "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
|
1917 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
|
1918 } |
| 34972 | 1919 else { |
| 1920 const char *s = (char*)&x; | |
| 1921 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
|
1922 |
| 34972 | 1923 if ((double_format == ieee_little_endian_format && !le) |
| 1924 || (double_format == ieee_big_endian_format && le)) { | |
| 1925 p += 7; | |
| 1926 incr = -1; | |
| 1927 } | |
| 1928 | |
| 1929 for (i = 0; i < 8; i++) { | |
| 1930 *p = *s++; | |
| 1931 p += incr; | |
| 1932 } | |
| 1933 return 0; | |
| 1934 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1935 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1936 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1937 double |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1938 _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
|
1939 { |
| 34972 | 1940 if (float_format == unknown_format) { |
| 1941 unsigned char sign; | |
| 1942 int e; | |
| 1943 unsigned int f; | |
| 1944 double x; | |
| 1945 int incr = 1; | |
| 1946 | |
| 1947 if (le) { | |
| 1948 p += 3; | |
| 1949 incr = -1; | |
| 1950 } | |
| 1951 | |
| 1952 /* First byte */ | |
| 1953 sign = (*p >> 7) & 1; | |
| 1954 e = (*p & 0x7F) << 1; | |
| 1955 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
|
1956 |
| 34972 | 1957 /* Second byte */ |
| 1958 e |= (*p >> 7) & 1; | |
| 1959 f = (*p & 0x7F) << 16; | |
| 1960 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
|
1961 |
| 34972 | 1962 if (e == 255) { |
| 1963 PyErr_SetString( | |
| 1964 PyExc_ValueError, | |
| 1965 "can't unpack IEEE 754 special value " | |
| 1966 "on non-IEEE platform"); | |
| 1967 return -1; | |
| 1968 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
1969 |
| 34972 | 1970 /* Third byte */ |
| 1971 f |= *p << 8; | |
| 1972 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
|
1973 |
| 34972 | 1974 /* Fourth byte */ |
| 1975 f |= *p; | |
| 1976 | |
| 1977 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
|
1978 |
| 34972 | 1979 /* XXX This sadly ignores Inf/NaN issues */ |
| 1980 if (e == 0) | |
| 1981 e = -126; | |
| 1982 else { | |
| 1983 x += 1.0; | |
| 1984 e -= 127; | |
| 1985 } | |
| 1986 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
|
1987 |
| 34972 | 1988 if (sign) |
| 1989 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
|
1990 |
| 34972 | 1991 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
|
1992 } |
| 34972 | 1993 else { |
| 35987 | 1994 float x; |
| 1995 | |
| 34972 | 1996 if ((float_format == ieee_little_endian_format && !le) |
| 1997 || (float_format == ieee_big_endian_format && le)) { | |
| 35987 | 1998 char buf[4]; |
| 34972 | 1999 char *d = &buf[3]; |
| 2000 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
|
2001 |
| 34972 | 2002 for (i = 0; i < 4; i++) { |
| 2003 *d-- = *p++; | |
| 2004 } | |
| 35987 | 2005 memcpy(&x, buf, 4); |
| 34972 | 2006 } |
| 2007 else { | |
| 35987 | 2008 memcpy(&x, p, 4); |
| 34972 | 2009 } |
| 35987 | 2010 |
| 2011 return x; | |
| 34972 | 2012 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
2013 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
2014 |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
2015 double |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
2016 _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
|
2017 { |
| 34972 | 2018 if (double_format == unknown_format) { |
| 2019 unsigned char sign; | |
| 2020 int e; | |
| 2021 unsigned int fhi, flo; | |
| 2022 double x; | |
| 2023 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
|
2024 |
| 34972 | 2025 if (le) { |
| 2026 p += 7; | |
| 2027 incr = -1; | |
| 2028 } | |
| 2029 | |
| 2030 /* First byte */ | |
| 2031 sign = (*p >> 7) & 1; | |
| 2032 e = (*p & 0x7F) << 4; | |
| 2033 | |
| 2034 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
|
2035 |
| 34972 | 2036 /* Second byte */ |
| 2037 e |= (*p >> 4) & 0xF; | |
| 2038 fhi = (*p & 0xF) << 24; | |
| 2039 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
|
2040 |
| 34972 | 2041 if (e == 2047) { |
| 2042 PyErr_SetString( | |
| 2043 PyExc_ValueError, | |
| 2044 "can't unpack IEEE 754 special value " | |
| 2045 "on non-IEEE platform"); | |
| 2046 return -1.0; | |
| 2047 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
2048 |
| 34972 | 2049 /* Third byte */ |
| 2050 fhi |= *p << 16; | |
| 2051 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
|
2052 |
| 34972 | 2053 /* Fourth byte */ |
| 2054 fhi |= *p << 8; | |
| 2055 p += incr; | |
| 2056 | |
| 2057 /* Fifth byte */ | |
| 2058 fhi |= *p; | |
| 2059 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
|
2060 |
| 34972 | 2061 /* Sixth byte */ |
| 2062 flo = *p << 16; | |
| 2063 p += incr; | |
| 2064 | |
| 2065 /* Seventh byte */ | |
| 2066 flo |= *p << 8; | |
| 2067 p += incr; | |
| 2068 | |
| 2069 /* Eighth byte */ | |
| 2070 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
|
2071 |
| 34972 | 2072 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 2073 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
|
2074 |
| 34972 | 2075 if (e == 0) |
| 2076 e = -1022; | |
| 2077 else { | |
| 2078 x += 1.0; | |
| 2079 e -= 1023; | |
| 2080 } | |
| 2081 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
|
2082 |
| 34972 | 2083 if (sign) |
| 2084 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
|
2085 |
| 34972 | 2086 return x; |
| 2087 } | |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
2088 else { |
| 35987 | 2089 double x; |
| 2090 | |
| 34972 | 2091 if ((double_format == ieee_little_endian_format && !le) |
| 2092 || (double_format == ieee_big_endian_format && le)) { | |
| 2093 char buf[8]; | |
| 2094 char *d = &buf[7]; | |
| 2095 int i; | |
| 2096 | |
| 2097 for (i = 0; i < 8; i++) { | |
| 2098 *d-- = *p++; | |
| 2099 } | |
| 35987 | 2100 memcpy(&x, buf, 8); |
| 34972 | 2101 } |
| 2102 else { | |
| 35987 | 2103 memcpy(&x, p, 8); |
| 34972 | 2104 } |
| 35987 | 2105 |
| 2106 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
|
2107 } |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
2108 } |
