annotate Objects/floatobject.c @ 57303:4aa4c19dfaed legacy-trunk

#1757126: fix typo with the cyrillic_asian alias
author Philip Jenvey <pjenvey@underboss.org>
date Tue, 17 Nov 2009 02:42:26 +0000
parents c56419cbdd48
children 92e06cdbeab9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Float object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
4 /* XXX There should be overflow checks here, but it's hard to check
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
5 for any kind of float exception without losing portability. */
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 60
diff changeset
10 #include <ctype.h>
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
11 #include <float.h>
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
13 #undef MAX
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
14 #undef MIN
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
15 #define MAX(x, y) ((x) < (y) ? (y) : (x))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
16 #define MIN(x, y) ((x) < (y) ? (x) : (y))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
17
46154
1e10b30f7498 Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46135
diff changeset
18 #ifdef HAVE_IEEEFP_H
1e10b30f7498 Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46135
diff changeset
19 #include <ieeefp.h>
1e10b30f7498 Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46135
diff changeset
20 #endif
1e10b30f7498 Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46135
diff changeset
21
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
22 #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
23 /* 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
24 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
25 #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
26
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
27 /* 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
28 #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
29 #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
30 #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
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 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
33 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
34 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
35 };
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
36
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
37 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
38
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
39 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
40 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
41
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
42 static PyFloatObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
43 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
44 {
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
45 PyFloatObject *p, *q;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
46 /* 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
47 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
48 if (p == NULL)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
49 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
50 ((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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
58 }
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
59
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
60 double
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
61 PyFloat_GetMax(void)
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
62 {
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
63 return DBL_MAX;
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
64 }
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
65
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
66 double
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
67 PyFloat_GetMin(void)
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
68 {
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
69 return DBL_MIN;
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
70 }
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
71
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
72 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
73
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 PyDoc_STRVAR(floatinfo__doc__,
54777
4c6cdba1b0bb add underscores
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
75 "sys.float_info\n\
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
76 \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
77 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
78 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
79 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
80
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 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
82 {"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
83 {"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
84 "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
85 {"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
86 "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
87 {"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
88 {"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
89 "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
90 {"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
91 "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
92 {"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
93 {"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
94 {"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
95 "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
96 {"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
97 {"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
98 {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
99 };
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
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 static PyStructSequence_Desc floatinfo_desc = {
54777
4c6cdba1b0bb add underscores
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
102 "sys.float_info", /* name */
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
103 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
104 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
105 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
106 };
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
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
108 PyObject *
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
109 PyFloat_GetInfo(void)
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
110 {
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
111 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
112 int pos = 0;
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
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 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
115 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
116 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
117 }
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
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 #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
120 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
121 #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
122 PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
123
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
124 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
125 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
126 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
127 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
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 #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
136 #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
137
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
138 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
139 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
140 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
141 }
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
142 return floatinfo;
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
143 }
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
144
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
145 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
146 PyFloat_FromDouble(double fval)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 {
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
148 register PyFloatObject *op;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
149 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
150 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
151 return NULL;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
152 }
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
153 /* 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
154 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
155 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
156 PyObject_INIT(op, &PyFloat_Type);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
157 op->ob_fval = fval;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
158 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
161 /**************************************************************************
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
162 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
163 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
164
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
165 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
166 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
167 help from this function!).
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
168
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
169 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
170 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
171 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
172
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
173 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
174 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
175 *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
176 **************************************************************************/
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
177 PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
178 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
179 {
53470
01c3363a357f Backport r71967 changes from py3k to trunk.
Mark Dickinson <dickinsm@gmail.com>
parents: 53442
diff changeset
180 const char *s, *last, *end;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
181 double x;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
182 char buffer[256]; /* for errors */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
183 #ifdef Py_USING_UNICODE
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
184 char *s_buffer = NULL;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
185 #endif
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36235
diff changeset
186 Py_ssize_t len;
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
187 PyObject *result = NULL;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
188
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
189 if (pend)
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
190 *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
191 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
192 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
193 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
194 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
195 #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
196 else if (PyUnicode_Check(v)) {
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
197 s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
198 if (s_buffer == NULL)
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
199 return PyErr_NoMemory();
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
200 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
201 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
202 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
203 NULL))
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
204 goto error;
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
205 s = s_buffer;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36235
diff changeset
206 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
207 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
208 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
209 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
210 PyErr_SetString(PyExc_TypeError,
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
211 "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
212 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
213 }
53470
01c3363a357f Backport r71967 changes from py3k to trunk.
Mark Dickinson <dickinsm@gmail.com>
parents: 53442
diff changeset
214 last = s + len;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
215
53712
1f00bc1d115a Eliminate some locale-dependent calls to isspace and tolower.
Mark Dickinson <dickinsm@gmail.com>
parents: 53618
diff changeset
216 while (Py_ISSPACE(*s))
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
217 s++;
53470
01c3363a357f Backport r71967 changes from py3k to trunk.
Mark Dickinson <dickinsm@gmail.com>
parents: 53442
diff changeset
218 /* We don't care about overflow or underflow. If the platform
01c3363a357f Backport r71967 changes from py3k to trunk.
Mark Dickinson <dickinsm@gmail.com>
parents: 53442
diff changeset
219 * supports them, infinities and signed zeroes (on underflow) are
01c3363a357f Backport r71967 changes from py3k to trunk.
Mark Dickinson <dickinsm@gmail.com>
parents: 53442
diff changeset
220 * fine. */
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
221 x = PyOS_string_to_double(s, (char **)&end, NULL);
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
222 if (x == -1.0 && PyErr_Occurred())
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
223 goto error;
53712
1f00bc1d115a Eliminate some locale-dependent calls to isspace and tolower.
Mark Dickinson <dickinsm@gmail.com>
parents: 53618
diff changeset
224 while (Py_ISSPACE(*end))
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
225 end++;
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
226 if (end == last)
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
227 result = PyFloat_FromDouble(x);
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
228 else {
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
229 PyOS_snprintf(buffer, sizeof(buffer),
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
230 "invalid literal for float(): %.200s", s);
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
231 PyErr_SetString(PyExc_ValueError, buffer);
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
232 result = NULL;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
233 }
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
234
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
235 error:
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
236 #ifdef Py_USING_UNICODE
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
237 if (s_buffer)
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
238 PyMem_FREE(s_buffer);
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
239 #endif
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
240 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
241 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
242
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
243 static void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
244 float_dealloc(PyFloatObject *op)
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
245 {
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
246 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
247 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
248 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
249 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
250 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
251 Py_TYPE(op)->tp_free((PyObject *)op);
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
252 }
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
253
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 double
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
255 PyFloat_AsDouble(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
257 PyNumberMethods *nb;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
258 PyFloatObject *fo;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
259 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
260
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
261 if (op && PyFloat_Check(op))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
262 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
263
26272
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
264 if (op == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
265 PyErr_BadArgument();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
268
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
269 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
270 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
271 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
272 }
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
273
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
274 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
275 if (fo == NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
276 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
277 if (!PyFloat_Check(fo)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
278 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
279 "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
280 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
281 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
282
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
283 val = PyFloat_AS_DOUBLE(fo);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
284 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
285
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
286 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
291 /* 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
292 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
293 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
294 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
295 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
296 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
297 */
47822
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47791
diff changeset
298 #define CONVERT_TO_DOUBLE(obj, dbl) \
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47791
diff changeset
299 if (PyFloat_Check(obj)) \
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47791
diff changeset
300 dbl = PyFloat_AS_DOUBLE(obj); \
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47791
diff changeset
301 else if (convert_to_double(&(obj), &(dbl)) < 0) \
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47791
diff changeset
302 return obj;
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
303
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
304 static int
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
305 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
306 {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
307 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
308
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
309 if (PyInt_Check(obj)) {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
310 *dbl = (double)PyInt_AS_LONG(obj);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
311 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
312 else if (PyLong_Check(obj)) {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
313 *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
314 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
315 *v = NULL;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
316 return -1;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
317 }
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
318 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
319 else {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
320 Py_INCREF(Py_NotImplemented);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
321 *v = Py_NotImplemented;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
322 return -1;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
323 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
324 return 0;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
325 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
326
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
327 /* XXX PyFloat_AsString and PyFloat_AsReprString are deprecated:
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
328 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
329 */
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
330 void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
331 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
332 {
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
333 char *tmp = PyOS_double_to_string(v->ob_fval, 'g',
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
334 PyFloat_STR_PRECISION,
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
335 Py_DTSF_ADD_DOT_0, NULL);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
336 strcpy(buf, tmp);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
337 PyMem_Free(tmp);
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
338 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
339
17707
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
340 void
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
341 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
342 {
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
343 char * tmp = PyOS_double_to_string(v->ob_fval, 'r', 0,
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
344 Py_DTSF_ADD_DOT_0, NULL);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
345 strcpy(buf, tmp);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
346 PyMem_Free(tmp);
17707
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
347 }
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
348
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
349 /* ARGSUSED */
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 268
diff changeset
350 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
351 float_print(PyFloatObject *v, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 {
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
353 char *buf;
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
354 if (flags & Py_PRINT_RAW)
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
355 buf = PyOS_double_to_string(v->ob_fval,
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
356 'g', PyFloat_STR_PRECISION,
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
357 Py_DTSF_ADD_DOT_0, NULL);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
358 else
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
359 buf = PyOS_double_to_string(v->ob_fval,
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
360 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
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
361 Py_BEGIN_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 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
363 Py_END_ALLOW_THREADS
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
364 PyMem_Free(buf);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 268
diff changeset
365 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
368 static PyObject *
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
369 float_str_or_repr(PyFloatObject *v, int precision, char format_code)
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
370 {
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
371 PyObject *result;
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
372 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
373 format_code, precision,
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
374 Py_DTSF_ADD_DOT_0,
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
375 NULL);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
376 if (!buf)
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
377 return PyErr_NoMemory();
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
378 result = PyString_FromString(buf);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
379 PyMem_Free(buf);
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
380 return result;
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
381 }
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
382
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
383 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
384 float_repr(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 {
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
386 return float_str_or_repr(v, 0, 'r');
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
387 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
388
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
389 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
390 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
391 {
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
392 return float_str_or_repr(v, PyFloat_STR_PRECISION, 'g');
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
395 /* 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
396 * 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
397 * 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
398 * 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
399 * 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
400 * 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
401 * 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
402 * 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
403 * 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
404 * 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
405 * 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
406 * 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
407 * 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
408 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
409
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
410 static PyObject*
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
411 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
412 {
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
413 double i, j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
414 int r = 0;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
415
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
416 assert(PyFloat_Check(v));
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
417 i = PyFloat_AS_DOUBLE(v);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
418
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
419 /* 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
420 * 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
421 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
422 if (PyFloat_Check(w))
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
423 j = PyFloat_AS_DOUBLE(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
424
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
425 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
426 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
427 /* 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
428 * 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
429 * 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
430 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
431 j = 0.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
432 else
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
433 goto Unimplemented;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
434 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
435
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
436 else if (PyInt_Check(w)) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
437 long jj = PyInt_AS_LONG(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
438 /* 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
439 * 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
440 * bits.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
441 */
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
442 #if SIZEOF_LONG > 6
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
443 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
444 if (abs >> 48) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
445 /* 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
446 * PyLong path.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
447 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
448 PyObject *result;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
449 PyObject *ww = PyLong_FromLong(jj);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
450
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
451 if (ww == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
452 return NULL;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
453 result = float_richcompare(v, ww, op);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
454 Py_DECREF(ww);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
455 return result;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
456 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
457 #endif
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
458 j = (double)jj;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
459 assert((long)j == jj);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
460 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
461
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
462 else if (PyLong_Check(w)) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
463 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
464 int wsign = _PyLong_Sign(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
465 size_t nbits;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
466 int exponent;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
467
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
468 if (vsign != wsign) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
469 /* Magnitudes are irrelevant -- the signs alone
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
470 * determine the outcome.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
471 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
472 i = (double)vsign;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
473 j = (double)wsign;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
474 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
475 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
476 /* The signs are the same. */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
477 /* 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
478 nbits = _PyLong_NumBits(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
479 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
480 /* 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
481 * 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
482 * 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
483 * 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
484 * finite float.
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
485 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
486 PyErr_Clear();
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
487 i = (double)vsign;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
488 assert(wsign != 0);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
489 j = wsign * 2.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
490 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
491 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
492 if (nbits <= 48) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
493 j = PyLong_AsDouble(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
494 /* 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
495 assert(j != -1.0 || ! PyErr_Occurred());
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
496 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
497 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
498 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
499 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
500 * 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
501 * vsign != wsign branch at the start */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
502 /* 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
503 if (vsign < 0) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
504 /* "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
505 * comparator.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
506 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
507 i = -i;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
508 op = _Py_SwappedOp[op];
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
509 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
510 assert(i > 0.0);
36235
9f66bdd5a061 Fix icc warnings: remove unused variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
511 (void) frexp(i, &exponent);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
512 /* 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
513 * 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
514 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
515 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
516 i = 1.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
517 j = 2.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
518 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
519 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
520 if ((size_t)exponent > nbits) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
521 i = 2.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
522 j = 1.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
523 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
524 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
525 /* 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
526 * 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
527 * outcome.
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 {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
530 double fracpart;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
531 double intpart;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
532 PyObject *result = NULL;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
533 PyObject *one = NULL;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
534 PyObject *vv = NULL;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
535 PyObject *ww = w;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
536
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
537 if (wsign < 0) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
538 ww = PyNumber_Negative(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
539 if (ww == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
540 goto Error;
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 else
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
543 Py_INCREF(ww);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
544
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
545 fracpart = modf(i, &intpart);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
546 vv = PyLong_FromDouble(intpart);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
547 if (vv == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
548 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
549
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
550 if (fracpart != 0.0) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
551 /* 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
552 * to represent the lost fraction.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
553 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
554 PyObject *temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
555
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
556 one = PyInt_FromLong(1);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
557 if (one == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
558 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
559
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
560 temp = PyNumber_Lshift(ww, one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
561 if (temp == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
562 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
563 Py_DECREF(ww);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
564 ww = temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
565
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
566 temp = PyNumber_Lshift(vv, one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
567 if (temp == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
568 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
569 Py_DECREF(vv);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
570 vv = temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
571
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
572 temp = PyNumber_Or(vv, one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
573 if (temp == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
574 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
575 Py_DECREF(vv);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
576 vv = temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
577 }
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 r = PyObject_RichCompareBool(vv, ww, op);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
580 if (r < 0)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
581 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
582 result = PyBool_FromLong(r);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
583 Error:
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
584 Py_XDECREF(vv);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
585 Py_XDECREF(ww);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
586 Py_XDECREF(one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
587 return result;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
588 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
589 } /* else if (PyLong_Check(w)) */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
590
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
591 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
592 goto Unimplemented;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
593
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
594 Compare:
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
595 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
596 switch (op) {
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
597 case Py_EQ:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
598 r = i == j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
599 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
600 case Py_NE:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
601 r = i != j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
602 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
603 case Py_LE:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
604 r = i <= j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
605 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
606 case Py_GE:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
607 r = i >= j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
608 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
609 case Py_LT:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
610 r = i < j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
611 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
612 case Py_GT:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
613 r = i > j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
614 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
615 }
31589
ca3c998afa55 Pass a variable that actually exists to PyFPE_END_PROTECT in
Michael W. Hudson <mwh@python.net>
parents: 31553
diff changeset
616 PyFPE_END_PROTECT(r)
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
617 return PyBool_FromLong(r);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
618
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
619 Unimplemented:
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
620 Py_INCREF(Py_NotImplemented);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
621 return Py_NotImplemented;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
622 }
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
623
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
624 static long
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
625 float_hash(PyFloatObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
626 {
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
627 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
628 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
629
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
630 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
631 float_add(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
633 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
634 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
635 CONVERT_TO_DOUBLE(w, b);
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
636 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
637 a = a + b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
638 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
639 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
642 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
643 float_sub(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
645 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
646 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
647 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
648 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
649 a = a - b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
650 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
651 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
653
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
654 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
655 float_mul(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
657 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
658 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
659 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
660 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
661 a = a * b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
662 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
663 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
665
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
666 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
667 float_div(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
669 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
670 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
671 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
672 #ifdef Py_NAN
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
673 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
674 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
675 "float division");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 }
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
678 #endif
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
679 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
680 a = a / b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
681 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
682 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
684
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
685 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
686 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
687 {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
688 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
689 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
690 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
691 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
692 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
693 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
694 #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
695 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
696 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
697 "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
698 return NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
699 }
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
700 #endif
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
701 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
702 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
703 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
704 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
705 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
706
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
707 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
708 float_rem(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
710 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
711 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
712 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
713 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
714 #ifdef Py_NAN
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
715 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
716 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
717 "float modulo");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 }
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
720 #endif
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
721 PyFPE_START_PROTECT("modulo", return 0)
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
722 mod = fmod(vx, wx);
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
723 /* note: checking mod*wx < 0 is incorrect -- underflows to
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
724 0 if wx < sqrt(smallest nonzero double) */
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
725 if (mod && ((wx < 0) != (mod < 0))) {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
726 mod += wx;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 }
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
728 PyFPE_END_PROTECT(mod)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
729 return PyFloat_FromDouble(mod);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
731
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
732 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
733 float_divmod(PyObject *v, PyObject *w)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
734 {
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
735 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
736 double div, mod, floordiv;
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
737 CONVERT_TO_DOUBLE(v, vx);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
738 CONVERT_TO_DOUBLE(w, wx);
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
739 if (wx == 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
740 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
741 return NULL;
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
742 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
743 PyFPE_START_PROTECT("divmod", return 0)
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
744 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
745 /* fmod is typically exact, so vx-mod is *mathematically* an
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
746 exact multiple of wx. But this is fp arithmetic, and fp
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
747 vx - mod is an approximation; the result is that div may
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
748 not be an exact integral value after the division, although
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
749 it will always be very close to one.
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
750 */
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
751 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
752 if (mod) {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
753 /* 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
754 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
755 mod += wx;
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
756 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
757 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
758 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
759 else {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
760 /* 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
761 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
762 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
763 "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
764 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
765 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
766 mod = -mod;
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
767 }
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
768 /* 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
769 if (div) {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
770 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
771 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
772 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
773 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
774 else {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
775 /* 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
776 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
777 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
778 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
779 PyFPE_END_PROTECT(floordiv)
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
780 return Py_BuildValue("(dd)", floordiv, mod);
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
781 }
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
782
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
783 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
784 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
785 {
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
786 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
787
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
788 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
789 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
790 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
791 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
792 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
793 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
794 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
795 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
796 }
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
797
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
798 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
799 float_pow(PyObject *v, PyObject *w, PyObject *z)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 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
802
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
803 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
804 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
805 "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
806 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
807 }
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
808
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
809 CONVERT_TO_DOUBLE(v, iv);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
810 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
811
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
812 /* 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
813 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
814 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
815 }
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
816 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
817 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
818 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
819 "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
820 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
821 }
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
822 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
823 }
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
824 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
825 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
826 }
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
827 if (iv < 0.0) {
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
828 /* 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
829 * 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
830 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
831 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
832 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
833 "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
834 return NULL;
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
835 }
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
836 /* 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
837 * -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
838 * 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
839 * 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
840 * 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
841 * 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
842 * 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
843 */
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
844 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
845 /* 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
846 * 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
847 * 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
848 * indirectly.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
849 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
850 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
851 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
852 }
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
853 /* 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
854 * 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
855 * the platform to do this correctly.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
856 */
3984
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
857 }
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
858 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
859 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
860 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
861 PyFPE_END_PROTECT(ix)
22228
4b6eda7d0c8f backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21354
diff changeset
862 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
863 if (errno != 0) {
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
864 /* 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
865 * 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
866 */
39642
981181d2a4fe Reverting the patch that tried to fix the issue whereby x**2 raises
Alex Martelli <aleaxit@gmail.com>
parents: 39639
diff changeset
867 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
868 PyExc_ValueError);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 return NULL;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
870 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
871 return PyFloat_FromDouble(ix);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
873
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
874 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
875 float_neg(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
877 return PyFloat_FromDouble(-v->ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
879
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
880 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
881 float_abs(PyFloatObject *v)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
882 {
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
883 return PyFloat_FromDouble(fabs(v->ob_fval));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
885
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
886 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
887 float_nonzero(PyFloatObject *v)
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
888 {
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
889 return v->ob_fval != 0.0;
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
890 }
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
891
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
892 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
893 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
894 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
895 if (PyInt_Check(*pw)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
896 long x = PyInt_AsLong(*pw);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
897 *pw = PyFloat_FromDouble((double)x);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
898 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
899 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
900 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
901 else if (PyLong_Check(*pw)) {
27484
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
902 double x = PyLong_AsDouble(*pw);
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
903 if (x == -1.0 && PyErr_Occurred())
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
904 return -1;
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
905 *pw = PyFloat_FromDouble(x);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
906 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
907 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
908 }
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
909 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
910 Py_INCREF(*pv);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
911 Py_INCREF(*pw);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
912 return 0;
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
913 }
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
914 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
915 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
916
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
917 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
918 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
919 {
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
920 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
921 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
922
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
923 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
924 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
925 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
926 Py_RETURN_FALSE;
46792
8baeba13ae93 Issue #2801: fix bug in float.is_integer where ValueError
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
927 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
928 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
929 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
930 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
931 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
932 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
933 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
934 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
935 }
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
936 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
937 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
938 }
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
939
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
940 #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
941 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
942 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
943 {
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
944 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
945 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
946 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
947 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
948 }
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
949
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
950 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
951 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
952 {
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
953 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
954 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
955 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
956 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
957 }
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
958
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
959 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
960 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
961 {
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
962 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
963 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
964 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
965 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
966 }
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
967 #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
968
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
969 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
970 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
971 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
972 double x = PyFloat_AsDouble(v);
18838
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
973 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
974
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
975 (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
976 /* 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
977 * 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
978 * 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
979 * 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
980 * 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
981 * 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
982 * 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
983 * 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
984 * 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
985 * 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
986 * 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
987 */
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
988 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
989 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
990 return PyInt_FromLong(aslong);
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
991 }
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
992 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
993 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
994
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
995 static PyObject *
49104
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
996 float_long(PyObject *v)
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
997 {
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
998 double x = PyFloat_AsDouble(v);
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
999 return PyLong_FromDouble(x);
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
1000 }
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
1001
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
1002 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1003 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
1004 {
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33549
diff changeset
1005 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
1006 Py_INCREF(v);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33549
diff changeset
1007 else
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33549
diff changeset
1008 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
1009 return v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1010 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1011
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1012 /* turn ASCII hex characters into integer values and vice versa */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1013
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1014 static char
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1015 char_from_hex(int x)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1016 {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1017 assert(0 <= x && x < 16);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1018 return "0123456789abcdef"[x];
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1019 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1020
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1021 static int
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1022 hex_from_char(char c) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1023 int x;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1024 switch(c) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1025 case '0':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1026 x = 0;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1027 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1028 case '1':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1029 x = 1;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1030 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1031 case '2':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1032 x = 2;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1033 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1034 case '3':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1035 x = 3;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1036 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1037 case '4':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1038 x = 4;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1039 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1040 case '5':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1041 x = 5;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1042 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1043 case '6':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1044 x = 6;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1045 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1046 case '7':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1047 x = 7;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1048 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1049 case '8':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1050 x = 8;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1051 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1052 case '9':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1053 x = 9;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1054 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1055 case 'a':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1056 case 'A':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1057 x = 10;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1058 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1059 case 'b':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1060 case 'B':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1061 x = 11;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1062 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1063 case 'c':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1064 case 'C':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1065 x = 12;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1066 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1067 case 'd':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1068 case 'D':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1069 x = 13;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1070 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1071 case 'e':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1072 case 'E':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1073 x = 14;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1074 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1075 case 'f':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1076 case 'F':
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1077 x = 15;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1078 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1079 default:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1080 x = -1;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1081 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1082 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1083 return x;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1084 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1085
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1086 /* convert a float to a hexadecimal string */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1087
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1088 /* TOHEX_NBITS is DBL_MANT_DIG rounded up to the next integer
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1089 of the form 4k+1. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1090 #define TOHEX_NBITS DBL_MANT_DIG + 3 - (DBL_MANT_DIG+2)%4
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1091
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1092 static PyObject *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1093 float_hex(PyObject *v)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1094 {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1095 double x, m;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1096 int e, shift, i, si, esign;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1097 /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1098 trailing NUL byte. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1099 char s[(TOHEX_NBITS-1)/4+3];
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1100
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1101 CONVERT_TO_DOUBLE(v, x);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1102
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1103 if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1104 return float_str((PyFloatObject *)v);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1105
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1106 if (x == 0.0) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1107 if(copysign(1.0, x) == -1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1108 return PyString_FromString("-0x0.0p+0");
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1109 else
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1110 return PyString_FromString("0x0.0p+0");
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1111 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1112
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1113 m = frexp(fabs(x), &e);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1114 shift = 1 - MAX(DBL_MIN_EXP - e, 0);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1115 m = ldexp(m, shift);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1116 e -= shift;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1117
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1118 si = 0;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1119 s[si] = char_from_hex((int)m);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1120 si++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1121 m -= (int)m;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1122 s[si] = '.';
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1123 si++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1124 for (i=0; i < (TOHEX_NBITS-1)/4; i++) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1125 m *= 16.0;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1126 s[si] = char_from_hex((int)m);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1127 si++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1128 m -= (int)m;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1129 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1130 s[si] = '\0';
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1131
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1132 if (e < 0) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1133 esign = (int)'-';
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1134 e = -e;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1135 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1136 else
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1137 esign = (int)'+';
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1138
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1139 if (x < 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1140 return PyString_FromFormat("-0x%sp%c%d", s, esign, e);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1141 else
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1142 return PyString_FromFormat("0x%sp%c%d", s, esign, e);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1143 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1144
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1145 PyDoc_STRVAR(float_hex_doc,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1146 "float.hex() -> string\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1147 \n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1148 Return a hexadecimal representation of a floating-point number.\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1149 >>> (-0.1).hex()\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1150 '-0x1.999999999999ap-4'\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1151 >>> 3.14159.hex()\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1152 '0x1.921f9f01b866ep+1'");
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1153
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1154 /* Case-insensitive locale-independent string match used for nan and inf
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1155 detection. t should be lower-case and null-terminated. Return a nonzero
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1156 result if the first strlen(t) characters of s match t and 0 otherwise. */
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1157
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1158 static int
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1159 case_insensitive_match(const char *s, const char *t)
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1160 {
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1161 while(*t && Py_TOLOWER(*s) == *t) {
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1162 s++;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1163 t++;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1164 }
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1165 return *t ? 0 : 1;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1166 }
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1167
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1168 /* Convert a hexadecimal string to a float. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1169
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1170 static PyObject *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1171 float_fromhex(PyObject *cls, PyObject *arg)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1172 {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1173 PyObject *result_as_float, *result;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1174 double x;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1175 long exp, top_exp, lsb, key_digit;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1176 char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1177 int half_eps, digit, round_up, sign=1;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1178 Py_ssize_t length, ndigits, fdigits, i;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1179
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1180 /*
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1181 * For the sake of simplicity and correctness, we impose an artificial
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1182 * limit on ndigits, the total number of hex digits in the coefficient
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1183 * The limit is chosen to ensure that, writing exp for the exponent,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1184 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1185 * (1) if exp > LONG_MAX/2 then the value of the hex string is
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1186 * guaranteed to overflow (provided it's nonzero)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1187 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1188 * (2) if exp < LONG_MIN/2 then the value of the hex string is
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1189 * guaranteed to underflow to 0.
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1190 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1191 * (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1192 * overflow in the calculation of exp and top_exp below.
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1193 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1194 * More specifically, ndigits is assumed to satisfy the following
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1195 * inequalities:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1196 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1197 * 4*ndigits <= DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1198 * 4*ndigits <= LONG_MAX/2 + 1 - DBL_MAX_EXP
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1199 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1200 * If either of these inequalities is not satisfied, a ValueError is
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1201 * raised. Otherwise, write x for the value of the hex string, and
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1202 * assume x is nonzero. Then
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1203 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1204 * 2**(exp-4*ndigits) <= |x| < 2**(exp+4*ndigits).
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1205 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1206 * Now if exp > LONG_MAX/2 then:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1207 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1208 * exp - 4*ndigits >= LONG_MAX/2 + 1 - (LONG_MAX/2 + 1 - DBL_MAX_EXP)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1209 * = DBL_MAX_EXP
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1210 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1211 * so |x| >= 2**DBL_MAX_EXP, which is too large to be stored in C
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1212 * double, so overflows. If exp < LONG_MIN/2, then
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1213 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1214 * exp + 4*ndigits <= LONG_MIN/2 - 1 + (
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1215 * DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1216 * = DBL_MIN_EXP - DBL_MANT_DIG - 1
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1217 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1218 * and so |x| < 2**(DBL_MIN_EXP-DBL_MANT_DIG-1), hence underflows to 0
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1219 * when converted to a C double.
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1220 *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1221 * It's easy to show that if LONG_MIN/2 <= exp <= LONG_MAX/2 then both
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1222 * exp+4*ndigits and exp-4*ndigits are within the range of a long.
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1223 */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1224
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1225 if (PyString_AsStringAndSize(arg, &s, &length))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1226 return NULL;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1227 s_end = s + length;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1228
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1229 /********************
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1230 * Parse the string *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1231 ********************/
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1232
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1233 /* leading whitespace and optional sign */
53712
1f00bc1d115a Eliminate some locale-dependent calls to isspace and tolower.
Mark Dickinson <dickinsm@gmail.com>
parents: 53618
diff changeset
1234 while (Py_ISSPACE(*s))
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1235 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1236 if (*s == '-') {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1237 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1238 sign = -1;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1239 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1240 else if (*s == '+')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1241 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1242
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1243 /* infinities and nans */
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1244 if (*s == 'i' || *s == 'I') {
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1245 if (!case_insensitive_match(s+1, "nf"))
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1246 goto parse_error;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1247 s += 3;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1248 x = Py_HUGE_VAL;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1249 if (case_insensitive_match(s, "inity"))
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1250 s += 5;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1251 goto finished;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1252 }
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1253 if (*s == 'n' || *s == 'N') {
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1254 if (!case_insensitive_match(s+1, "an"))
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1255 goto parse_error;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1256 s += 3;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1257 x = Py_NAN;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1258 goto finished;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1259 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1260
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1261 /* [0x] */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1262 s_store = s;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1263 if (*s == '0') {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1264 s++;
53712
1f00bc1d115a Eliminate some locale-dependent calls to isspace and tolower.
Mark Dickinson <dickinsm@gmail.com>
parents: 53618
diff changeset
1265 if (*s == 'x' || *s == 'X')
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1266 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1267 else
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1268 s = s_store;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1269 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1270
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1271 /* coefficient: <integer> [. <fraction>] */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1272 coeff_start = s;
48818
e39d44adb618 issue 3633: Solaris allows fullwidth Unicode digits in isxdigit, so
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
1273 while (hex_from_char(*s) >= 0)
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1274 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1275 s_store = s;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1276 if (*s == '.') {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1277 s++;
48818
e39d44adb618 issue 3633: Solaris allows fullwidth Unicode digits in isxdigit, so
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
1278 while (hex_from_char(*s) >= 0)
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1279 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1280 coeff_end = s-1;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1281 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1282 else
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1283 coeff_end = s;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1284
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1285 /* ndigits = total # of hex digits; fdigits = # after point */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1286 ndigits = coeff_end - coeff_start;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1287 fdigits = coeff_end - s_store;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1288 if (ndigits == 0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1289 goto parse_error;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1290 if (ndigits > MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1291 LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1292 goto insane_length_error;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1293
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1294 /* [p <exponent>] */
53712
1f00bc1d115a Eliminate some locale-dependent calls to isspace and tolower.
Mark Dickinson <dickinsm@gmail.com>
parents: 53618
diff changeset
1295 if (*s == 'p' || *s == 'P') {
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1296 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1297 exp_start = s;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1298 if (*s == '-' || *s == '+')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1299 s++;
48818
e39d44adb618 issue 3633: Solaris allows fullwidth Unicode digits in isxdigit, so
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
1300 if (!('0' <= *s && *s <= '9'))
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1301 goto parse_error;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1302 s++;
48818
e39d44adb618 issue 3633: Solaris allows fullwidth Unicode digits in isxdigit, so
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
1303 while ('0' <= *s && *s <= '9')
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1304 s++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1305 exp = strtol(exp_start, NULL, 10);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1306 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1307 else
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1308 exp = 0;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1309
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1310 /* for 0 <= j < ndigits, HEX_DIGIT(j) gives the jth most significant digit */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1311 #define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ? \
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1312 coeff_end-(j) : \
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1313 coeff_end-1-(j)))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1314
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1315 /*******************************************
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1316 * Compute rounded value of the hex string *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1317 *******************************************/
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1318
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1319 /* Discard leading zeros, and catch extreme overflow and underflow */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1320 while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1321 ndigits--;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1322 if (ndigits == 0 || exp < LONG_MIN/2) {
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1323 x = 0.0;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1324 goto finished;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1325 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1326 if (exp > LONG_MAX/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1327 goto overflow_error;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1328
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1329 /* Adjust exponent for fractional part. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1330 exp = exp - 4*((long)fdigits);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1331
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1332 /* top_exp = 1 more than exponent of most sig. bit of coefficient */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1333 top_exp = exp + 4*((long)ndigits - 1);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1334 for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1335 top_exp++;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1336
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1337 /* catch almost all nonextreme cases of overflow and underflow here */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1338 if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) {
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1339 x = 0.0;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1340 goto finished;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1341 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1342 if (top_exp > DBL_MAX_EXP)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1343 goto overflow_error;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1344
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1345 /* lsb = exponent of least significant bit of the *rounded* value.
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1346 This is top_exp - DBL_MANT_DIG unless result is subnormal. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1347 lsb = MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1348
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1349 x = 0.0;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1350 if (exp >= lsb) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1351 /* no rounding required */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1352 for (i = ndigits-1; i >= 0; i--)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1353 x = 16.0*x + HEX_DIGIT(i);
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1354 x = ldexp(x, (int)(exp));
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1355 goto finished;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1356 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1357 /* rounding required. key_digit is the index of the hex digit
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1358 containing the first bit to be rounded away. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1359 half_eps = 1 << (int)((lsb - exp - 1) % 4);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1360 key_digit = (lsb - exp - 1) / 4;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1361 for (i = ndigits-1; i > key_digit; i--)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1362 x = 16.0*x + HEX_DIGIT(i);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1363 digit = HEX_DIGIT(key_digit);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1364 x = 16.0*x + (double)(digit & (16-2*half_eps));
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1365
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1366 /* round-half-even: round up if bit lsb-1 is 1 and at least one of
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1367 bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1368 if ((digit & half_eps) != 0) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1369 round_up = 0;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1370 if ((digit & (3*half_eps-1)) != 0 ||
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1371 (half_eps == 8 && (HEX_DIGIT(key_digit+1) & 1) != 0))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1372 round_up = 1;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1373 else
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1374 for (i = key_digit-1; i >= 0; i--)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1375 if (HEX_DIGIT(i) != 0) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1376 round_up = 1;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1377 break;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1378 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1379 if (round_up == 1) {
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1380 x += 2*half_eps;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1381 if (top_exp == DBL_MAX_EXP &&
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1382 x == ldexp((double)(2*half_eps), DBL_MANT_DIG))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1383 /* overflow corner case: pre-rounded value <
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1384 2**DBL_MAX_EXP; rounded=2**DBL_MAX_EXP. */
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1385 goto overflow_error;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1386 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1387 }
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1388 x = ldexp(x, (int)(exp+4*key_digit));
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1389
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1390 finished:
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1391 /* optional trailing whitespace leading to the end of the string */
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1392 while (Py_ISSPACE(*s))
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1393 s++;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1394 if (s != s_end)
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1395 goto parse_error;
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1396 result_as_float = Py_BuildValue("(d)", sign * x);
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1397 if (result_as_float == NULL)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1398 return NULL;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1399 result = PyObject_CallObject(cls, result_as_float);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1400 Py_DECREF(result_as_float);
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1401 return result;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1402
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1403 overflow_error:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1404 PyErr_SetString(PyExc_OverflowError,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1405 "hexadecimal value too large to represent as a float");
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1406 return NULL;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1407
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1408 parse_error:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1409 PyErr_SetString(PyExc_ValueError,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1410 "invalid hexadecimal floating-point string");
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1411 return NULL;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1412
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1413 insane_length_error:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1414 PyErr_SetString(PyExc_ValueError,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1415 "hexadecimal string too long to convert");
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1416 return NULL;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1417 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1418
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1419 PyDoc_STRVAR(float_fromhex_doc,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1420 "float.fromhex(string) -> float\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1421 \n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1422 Create a floating-point number from a hexadecimal string.\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1423 >>> float.fromhex('0x1.ffffp10')\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1424 2047.984375\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1425 >>> float.fromhex('-0x1p-1074')\n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1426 -4.9406564584124654e-324");
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1427
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1428
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1429 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
1430 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
1431 {
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1432 double self;
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1433 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
1434 int exponent;
45100
0b4e8afed663 Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents: 45099
diff changeset
1435 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
1436
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1437 PyObject *prev;
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1438 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
1439 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
1440 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
1441 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
1442 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
1443
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1444 #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
1445 prev = obj; \
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1446 obj = call; \
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1447 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
1448
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1449 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
1450
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1451 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
1452 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
1453 "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
1454 return NULL;
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1455 }
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1456 #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
1457 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
1458 PyErr_SetString(PyExc_ValueError,
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49266
diff changeset
1459 "Cannot pass NaN to float.as_integer_ratio.");
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1460 return NULL;
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1461 }
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1462 #endif
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1463
45094
beea73ca2b88 Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents: 45060
diff changeset
1464 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
1465 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
1466 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
1467
45100
0b4e8afed663 Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents: 45099
diff changeset
1468 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
1469 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
1470 exponent--;
45100
0b4e8afed663 Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents: 45099
diff changeset
1471 }
45101
f5e18c9bde70 Simpler solution to handling non-IEEE 754 environments.
Raymond Hettinger <python@rcn.com>
parents: 45100
diff changeset
1472 /* 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
1473 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
1474 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
1475
45094
beea73ca2b88 Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents: 45060
diff changeset
1476 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
1477 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
1478
45100
0b4e8afed663 Add protection from weirdness while scaling the mantissa to an integer.
Raymond Hettinger <python@rcn.com>
parents: 45099
diff changeset
1479 /* 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
1480 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
1481 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
1482 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
1483 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
1484 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
1485 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
1486 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
1487 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
1488 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
1489 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
1490 }
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1491 else {
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1492 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
1493 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
1494 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
1495 }
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1496
45095
5cb8288360e7 Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents: 45094
diff changeset
1497 /* 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
1498 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
1499 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
1500 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
1501 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
1502
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1503 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
1504
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1505 #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
1506 error:
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1507 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
1508 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
1509 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
1510 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
1511 }
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1512
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1513 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
1514 "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
1515 "\n"
45094
beea73ca2b88 Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents: 45060
diff changeset
1516 "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
1517 "float and with a positive denominator.\n"
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49266
diff changeset
1518 "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
1519 "\n"
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1520 ">>> (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
1521 "(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
1522 ">>> (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
1523 "(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
1524 ">>> (-.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
1525 "(-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
1526
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1527
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
1528 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1529 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
1530
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1531 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1532 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
1533 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1534 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
1535 static char *kwlist[] = {"x", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1536
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1537 if (type != &PyFloat_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1538 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
1539 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
1540 return NULL;
53162
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49431
diff changeset
1541 /* If it's a string, but not a string subclass, use
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49431
diff changeset
1542 PyFloat_FromString. */
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49431
diff changeset
1543 if (PyString_CheckExact(x))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1544 return PyFloat_FromString(x, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1545 return PyNumber_Float(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1546 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1547
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1548 /* 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
1549 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
1550 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
1551 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
1552 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1553 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1554 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
1555 {
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
1556 PyObject *tmp, *newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1557
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1558 assert(PyType_IsSubtype(type, &PyFloat_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1559 tmp = float_new(&PyFloat_Type, args, kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1560 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1561 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
1562 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
1563 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
1564 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
1565 Py_DECREF(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1566 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
1567 }
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
1568 ((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
1569 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
1570 return newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1571 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1572
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1573 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1574 float_getnewargs(PyFloatObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1575 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1576 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
1577 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1578
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1579 /* this is for the benefit of the pack/unpack routines below */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1580
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1581 typedef enum {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1582 unknown_format, ieee_big_endian_format, ieee_little_endian_format
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1583 } float_format_type;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1584
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1585 static float_format_type double_format, float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1586 static float_format_type detected_double_format, detected_float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1587
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1588 static PyObject *
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1589 float_getformat(PyTypeObject *v, PyObject* arg)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1590 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1591 char* s;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1592 float_format_type r;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1593
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1594 if (!PyString_Check(arg)) {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1595 PyErr_Format(PyExc_TypeError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1596 "__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
1597 Py_TYPE(arg)->tp_name);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1598 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1599 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1600 s = PyString_AS_STRING(arg);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1601 if (strcmp(s, "double") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1602 r = double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1603 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1604 else if (strcmp(s, "float") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1605 r = float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1606 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1607 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1608 PyErr_SetString(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1609 "__getformat__() argument 1 must be "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1610 "'double' or 'float'");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1611 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1612 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1613
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1614 switch (r) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1615 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
1616 return PyString_FromString("unknown");
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1617 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
1618 return PyString_FromString("IEEE, little-endian");
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1619 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
1620 return PyString_FromString("IEEE, big-endian");
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1621 default:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1622 Py_FatalError("insane float_format or double_format");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1623 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1624 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1625 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1626
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1627 PyDoc_STRVAR(float_getformat_doc,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1628 "float.__getformat__(typestr) -> string\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1629 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1630 "You probably don't want to use this function. It exists mainly to be\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1631 "used in Python's test suite.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1632 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1633 "typestr must be 'double' or 'float'. This function returns whichever of\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1634 "'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1635 "format of floating point numbers used by the C type named by typestr.");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1636
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1637 static PyObject *
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1638 float_setformat(PyTypeObject *v, PyObject* args)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1639 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1640 char* typestr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1641 char* format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1642 float_format_type f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1643 float_format_type detected;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1644 float_format_type *p;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1645
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1646 if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format))
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1647 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1648
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1649 if (strcmp(typestr, "double") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1650 p = &double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1651 detected = detected_double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1652 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1653 else if (strcmp(typestr, "float") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1654 p = &float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1655 detected = detected_float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1656 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1657 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1658 PyErr_SetString(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1659 "__setformat__() argument 1 must "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1660 "be 'double' or 'float'");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1661 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1662 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1663
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1664 if (strcmp(format, "unknown") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1665 f = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1666 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1667 else if (strcmp(format, "IEEE, little-endian") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1668 f = ieee_little_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1669 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1670 else if (strcmp(format, "IEEE, big-endian") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1671 f = ieee_big_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1672 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1673 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1674 PyErr_SetString(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1675 "__setformat__() argument 2 must be "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1676 "'unknown', 'IEEE, little-endian' or "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1677 "'IEEE, big-endian'");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1678 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1679
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1680 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1681
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1682 if (f != unknown_format && f != detected) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1683 PyErr_Format(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1684 "can only set %s format to 'unknown' or the "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1685 "detected platform value", typestr);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1686 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1687 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1688
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1689 *p = f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1690 Py_RETURN_NONE;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1691 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1692
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1693 PyDoc_STRVAR(float_setformat_doc,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1694 "float.__setformat__(typestr, fmt) -> None\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1695 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1696 "You probably don't want to use this function. It exists mainly to be\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1697 "used in Python's test suite.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1698 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1699 "typestr must be 'double' or 'float'. fmt must be one of 'unknown',\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1700 "'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1701 "one of the latter two if it appears to match the underlying C reality.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1702 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1703 "Overrides the automatic determination of C-level floating point type.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1704 "This affects how floats are converted to and from binary strings.");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1705
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
1706 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
1707 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
1708 {
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1709 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
1710 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1711
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1712 static PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1713 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
1714 {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1715 PyObject *format_spec;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1716
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1717 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
1718 return NULL;
47294
4fe192d48960 Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46792
diff changeset
1719 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
1720 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
1721 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
1722 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
1723 if (PyUnicode_Check(format_spec)) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1724 /* 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
1725 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
1726 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
1727
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1728 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
1729 return NULL;
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1730
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1731 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
1732 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
1733 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
1734
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1735 Py_DECREF(str_spec);
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1736 return result;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1737 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1738 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
1739 return NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1740 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1741
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1742 PyDoc_STRVAR(float__format__doc,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1743 "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
1744 "\n"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1745 "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
1746
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1747
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1748 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
1749 {"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
1750 "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
1751 {"__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
1752 "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
1753 {"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
1754 float_as_integer_ratio_doc},
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1755 {"fromhex", (PyCFunction)float_fromhex,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1756 METH_O|METH_CLASS, float_fromhex_doc},
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1757 {"hex", (PyCFunction)float_hex,
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1758 METH_NOARGS, float_hex_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
1759 {"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
1760 "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
1761 #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
1762 {"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
1763 "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
1764 {"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
1765 "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
1766 {"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
1767 "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
1768 #endif
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1769 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1770 {"__getformat__", (PyCFunction)float_getformat,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1771 METH_O|METH_CLASS, float_getformat_doc},
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1772 {"__setformat__", (PyCFunction)float_setformat,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1773 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
1774 {"__format__", (PyCFunction)float__format__,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1775 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
1776 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1777 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1778
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
1779 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
1780 {"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
1781 (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
1782 "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
1783 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
1784 {"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
1785 (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
1786 "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
1787 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
1788 {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
1789 };
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1790
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
1791 PyDoc_STRVAR(float_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1792 "float(x) -> floating point number\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1793 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
1794 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
1795
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1796
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1797 static PyNumberMethods float_as_number = {
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1798 float_add, /*nb_add*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1799 float_sub, /*nb_subtract*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1800 float_mul, /*nb_multiply*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1801 float_classic_div, /*nb_divide*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1802 float_rem, /*nb_remainder*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1803 float_divmod, /*nb_divmod*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1804 float_pow, /*nb_power*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
1805 (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
1806 (unaryfunc)float_float, /*nb_positive*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
1807 (unaryfunc)float_abs, /*nb_absolute*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
1808 (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
1809 0, /*nb_invert*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1810 0, /*nb_lshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1811 0, /*nb_rshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1812 0, /*nb_and*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1813 0, /*nb_xor*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1814 0, /*nb_or*/
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1815 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
1816 float_trunc, /*nb_int*/
49104
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
1817 float_long, /*nb_long*/
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1818 float_float, /*nb_float*/
47822
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47791
diff changeset
1819 0, /* nb_oct */
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47791
diff changeset
1820 0, /* nb_hex */
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1821 0, /* nb_inplace_add */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1822 0, /* nb_inplace_subtract */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1823 0, /* nb_inplace_multiply */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1824 0, /* nb_inplace_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1825 0, /* nb_inplace_remainder */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1826 0, /* nb_inplace_power */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1827 0, /* nb_inplace_lshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1828 0, /* nb_inplace_rshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1829 0, /* nb_inplace_and */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1830 0, /* nb_inplace_xor */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1831 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
1832 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
1833 float_div, /* nb_true_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1834 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1835 0, /* nb_inplace_true_divide */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1836 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1837
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1838 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
1839 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1840 "float",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1841 sizeof(PyFloatObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1842 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1843 (destructor)float_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1844 (printfunc)float_print, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1845 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1846 0, /* tp_setattr */
32150
65f9ad9bc829 Remove float_compare as per
Michael W. Hudson <mwh@python.net>
parents: 31590
diff changeset
1847 0, /* tp_compare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1848 (reprfunc)float_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1849 &float_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1850 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1851 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1852 (hashfunc)float_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1853 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1854 (reprfunc)float_str, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1855 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1856 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1857 0, /* tp_as_buffer */
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1858 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1859 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1860 float_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1861 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1862 0, /* tp_clear */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36759
diff changeset
1863 float_richcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1864 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1865 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1866 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1867 float_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1868 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
1869 float_getset, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1870 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1871 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1872 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1873 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1874 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1875 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1876 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1877 float_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1878 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
1879
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
1880 void
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1881 _PyFloat_Init(void)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1882 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1883 /* We attempt to determine if this machine is using IEEE
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1884 floating point formats by peering at the bits of some
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1885 carefully chosen values. If it looks like we are on an
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1886 IEEE platform, the float packing/unpacking routines can
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1887 just copy bits, if not they resort to arithmetic & shifts
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1888 and masks. The shifts & masks approach works on all finite
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1889 values, but what happens to infinities, NaNs and signed
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1890 zeroes on packing is an accident, and attempting to unpack
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1891 a NaN or an infinity will raise an exception.
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1892
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1893 Note that if we're on some whacked-out platform which uses
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1894 IEEE formats but isn't strictly little-endian or big-
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1895 endian, we will fall back to the portable shifts & masks
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1896 method. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1897
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1898 #if SIZEOF_DOUBLE == 8
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1899 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1900 double x = 9006104071832581.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1901 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1902 detected_double_format = ieee_big_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1903 else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1904 detected_double_format = ieee_little_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1905 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1906 detected_double_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1907 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1908 #else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1909 detected_double_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1910 #endif
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1911
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1912 #if SIZEOF_FLOAT == 4
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1913 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1914 float y = 16711938.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1915 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1916 detected_float_format = ieee_big_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1917 else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1918 detected_float_format = ieee_little_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1919 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1920 detected_float_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1921 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1922 #else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1923 detected_float_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1924 #endif
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1925
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1926 double_format = detected_double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1927 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
1928
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
1929 /* 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
1930 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
1931 PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1932 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1933
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47889
diff changeset
1934 int
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47889
diff changeset
1935 PyFloat_ClearFreeList(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
1936 {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1937 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
1938 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
1939 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
1940 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
1941 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
1942
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1943 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
1944 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
1945 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
1946 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
1947 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
1948 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
1949 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
1950 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
1951 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
1952 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
1953 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1954 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
1955 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
1956 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
1957 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
1958 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
1959 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
1960 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
1961 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
1962 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
1963 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
1964 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
1965 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
1966 }
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
1967 }
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
1968 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1969 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
1970 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
1971 }
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47889
diff changeset
1972 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
1973 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
1974 }
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47889
diff changeset
1975 return freelist_size;
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1976 }
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1977
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1978 void
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1979 PyFloat_Fini(void)
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1980 {
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1981 PyFloatObject *p;
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1982 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
1983 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
1984 int u; /* total unfreed floats per block */
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1985
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47889
diff changeset
1986 u = PyFloat_ClearFreeList();
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
1987
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1988 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
1989 return;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1990 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
1991 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
1992 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
1993 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1994 else {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1995 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
1996 ": %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
1997 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
1998 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1999 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
2000 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
2001 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
2002 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
2003 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
2004 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
2005 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
2006 Py_REFCNT(p) != 0) {
56549
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2007 char *buf = PyOS_double_to_string(
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2008 PyFloat_AS_DOUBLE(p), 'r',
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2009 0, 0, NULL);
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2010 if (buf) {
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2011 /* XXX(twouters) cast
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2012 refcount to long
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2013 until %zd is
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2014 universally
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2015 available
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2016 */
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2017 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
2018 "# <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
2019 p, (long)Py_REFCNT(p), buf);
56549
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2020 PyMem_Free(buf);
7fa14be8ad73 Removed usage of unsafe PyFloat_AsString.
Eric Smith <eric@trueblade.com>
parents: 54777
diff changeset
2021 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
2022 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
2023 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
2024 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
2025 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
2026 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
2027 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2028
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2029 /*----------------------------------------------------------------------------
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2030 * _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
2031 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2032 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2033 _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
2034 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2035 if (float_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2036 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2037 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2038 double f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2039 unsigned int fbits;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2040 int incr = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2041
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2042 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2043 p += 3;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2044 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2045 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2046
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2047 if (x < 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2048 sign = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2049 x = -x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2050 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2051 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2052 sign = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2053
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2054 f = frexp(x, &e);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2055
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2056 /* Normalize f to be in the range [1.0, 2.0) */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2057 if (0.5 <= f && f < 1.0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2058 f *= 2.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2059 e--;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2060 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2061 else if (f == 0.0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2062 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2063 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2064 PyErr_SetString(PyExc_SystemError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2065 "frexp() result out of range");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2066 return -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2067 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2068
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2069 if (e >= 128)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2070 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2071 else if (e < -126) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2072 /* Gradual underflow */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2073 f = ldexp(f, 126 + e);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2074 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2075 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2076 else if (!(e == 0 && f == 0.0)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2077 e += 127;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2078 f -= 1.0; /* Get rid of leading 1 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2079 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2080
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2081 f *= 8388608.0; /* 2**23 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2082 fbits = (unsigned int)(f + 0.5); /* Round */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2083 assert(fbits <= 8388608);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2084 if (fbits >> 23) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2085 /* The carry propagated out of a string of 23 1 bits. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2086 fbits = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2087 ++e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2088 if (e >= 255)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2089 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2090 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2091
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2092 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2093 *p = (sign << 7) | (e >> 1);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2094 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2095
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2096 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2097 *p = (char) (((e & 1) << 7) | (fbits >> 16));
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2098 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2099
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2100 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2101 *p = (fbits >> 8) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2102 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2103
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2104 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2105 *p = fbits & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2106
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2107 /* Done */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2108 return 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2109
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2110 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2111 else {
35178
cf7a849ce61f Apparently some compiler gives a warning on
Michael W. Hudson <mwh@python.net>
parents: 34972
diff changeset
2112 float y = (float)x;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2113 const char *s = (char*)&y;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2114 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
2115
45759
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2116 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
2117 goto Overflow;
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2118
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2119 if ((float_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2120 || (float_format == ieee_big_endian_format && le)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2121 p += 3;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2122 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2123 }
45759
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2124
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2125 for (i = 0; i < 4; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2126 *p = *s++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2127 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2128 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2129 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
2130 }
45759
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2131 Overflow:
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2132 PyErr_SetString(PyExc_OverflowError,
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2133 "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
2134 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
2135 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2136
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2137 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2138 _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
2139 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2140 if (double_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2141 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2142 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2143 double f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2144 unsigned int fhi, flo;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2145 int incr = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2146
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2147 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2148 p += 7;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2149 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2150 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2151
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2152 if (x < 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2153 sign = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2154 x = -x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2155 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2156 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2157 sign = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2158
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2159 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
2160
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2161 /* Normalize f to be in the range [1.0, 2.0) */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2162 if (0.5 <= f && f < 1.0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2163 f *= 2.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2164 e--;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2165 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2166 else if (f == 0.0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2167 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2168 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2169 PyErr_SetString(PyExc_SystemError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2170 "frexp() result out of range");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2171 return -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2172 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2173
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2174 if (e >= 1024)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2175 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2176 else if (e < -1022) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2177 /* Gradual underflow */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2178 f = ldexp(f, 1022 + e);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2179 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2180 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2181 else if (!(e == 0 && f == 0.0)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2182 e += 1023;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2183 f -= 1.0; /* Get rid of leading 1 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2184 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2185
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2186 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2187 f *= 268435456.0; /* 2**28 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2188 fhi = (unsigned int)f; /* Truncate */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2189 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
2190
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2191 f -= (double)fhi;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2192 f *= 16777216.0; /* 2**24 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2193 flo = (unsigned int)(f + 0.5); /* Round */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2194 assert(flo <= 16777216);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2195 if (flo >> 24) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2196 /* The carry propagated out of a string of 24 1 bits. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2197 flo = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2198 ++fhi;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2199 if (fhi >> 28) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2200 /* And it also progagated out of the next 28 bits. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2201 fhi = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2202 ++e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2203 if (e >= 2047)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2204 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2205 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2206 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2207
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2208 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2209 *p = (sign << 7) | (e >> 4);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2210 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2211
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2212 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2213 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2214 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
2215
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2216 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2217 *p = (fhi >> 16) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2218 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2219
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2220 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2221 *p = (fhi >> 8) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2222 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2223
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2224 /* Fifth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2225 *p = fhi & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2226 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2227
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2228 /* Sixth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2229 *p = (flo >> 16) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2230 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2231
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2232 /* Seventh byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2233 *p = (flo >> 8) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2234 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2235
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2236 /* Eighth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2237 *p = flo & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2238 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2239
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2240 /* Done */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2241 return 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2242
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2243 Overflow:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2244 PyErr_SetString(PyExc_OverflowError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2245 "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
2246 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
2247 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2248 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2249 const char *s = (char*)&x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2250 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
2251
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2252 if ((double_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2253 || (double_format == ieee_big_endian_format && le)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2254 p += 7;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2255 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2256 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2257
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2258 for (i = 0; i < 8; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2259 *p = *s++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2260 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2261 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2262 return 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2263 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2264 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2265
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2266 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2267 _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
2268 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2269 if (float_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2270 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2271 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2272 unsigned int f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2273 double x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2274 int incr = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2275
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2276 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2277 p += 3;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2278 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2279 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2280
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2281 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2282 sign = (*p >> 7) & 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2283 e = (*p & 0x7F) << 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2284 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
2285
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2286 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2287 e |= (*p >> 7) & 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2288 f = (*p & 0x7F) << 16;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2289 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
2290
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2291 if (e == 255) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2292 PyErr_SetString(
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2293 PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2294 "can't unpack IEEE 754 special value "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2295 "on non-IEEE platform");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2296 return -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2297 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2298
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2299 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2300 f |= *p << 8;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2301 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
2302
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2303 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2304 f |= *p;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2305
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2306 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
2307
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2308 /* XXX This sadly ignores Inf/NaN issues */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2309 if (e == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2310 e = -126;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2311 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2312 x += 1.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2313 e -= 127;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2314 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2315 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
2316
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2317 if (sign)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2318 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
2319
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2320 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
2321 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2322 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2323 float x;
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2324
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2325 if ((float_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2326 || (float_format == ieee_big_endian_format && le)) {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2327 char buf[4];
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2328 char *d = &buf[3];
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2329 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
2330
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2331 for (i = 0; i < 4; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2332 *d-- = *p++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2333 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2334 memcpy(&x, buf, 4);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2335 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2336 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2337 memcpy(&x, p, 4);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2338 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2339
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2340 return x;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2341 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2342 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2343
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2344 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2345 _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
2346 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2347 if (double_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2348 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2349 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2350 unsigned int fhi, flo;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2351 double x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2352 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
2353
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2354 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2355 p += 7;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2356 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2357 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2358
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2359 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2360 sign = (*p >> 7) & 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2361 e = (*p & 0x7F) << 4;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2362
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2363 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
2364
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2365 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2366 e |= (*p >> 4) & 0xF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2367 fhi = (*p & 0xF) << 24;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2368 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
2369
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2370 if (e == 2047) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2371 PyErr_SetString(
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2372 PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2373 "can't unpack IEEE 754 special value "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2374 "on non-IEEE platform");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2375 return -1.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2376 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2377
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2378 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2379 fhi |= *p << 16;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2380 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
2381
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2382 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2383 fhi |= *p << 8;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2384 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2385
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2386 /* Fifth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2387 fhi |= *p;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2388 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
2389
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2390 /* Sixth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2391 flo = *p << 16;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2392 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2393
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2394 /* Seventh byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2395 flo |= *p << 8;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2396 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2397
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2398 /* Eighth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2399 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
2400
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2401 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2402 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
2403
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2404 if (e == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2405 e = -1022;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2406 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2407 x += 1.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2408 e -= 1023;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2409 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2410 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
2411
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2412 if (sign)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2413 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
2414
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2415 return x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2416 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2417 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2418 double x;
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2419
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2420 if ((double_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2421 || (double_format == ieee_big_endian_format && le)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2422 char buf[8];
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2423 char *d = &buf[7];
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2424 int i;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2425
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2426 for (i = 0; i < 8; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2427 *d-- = *p++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2428 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2429 memcpy(&x, buf, 8);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2430 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2431 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2432 memcpy(&x, p, 8);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2433 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2434
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2435 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
2436 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2437 }