annotate Objects/floatobject.c @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents b540a612f601
children a8f3847619f4
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
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
18 #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
19 /* 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
20 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
21 #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
22
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
23 /* Special free list -- see comments for same code in intobject.c. */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
24 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
25 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
26 #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
27
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
28 struct _floatblock {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
29 struct _floatblock *next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
30 PyFloatObject objects[N_FLOATOBJECTS];
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
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
33 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
34
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
35 static 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
36 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
37
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
38 static PyFloatObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
39 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
40 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
41 PyFloatObject *p, *q;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
42 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
43 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
44 if (p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
45 return (PyFloatObject *) PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
46 ((PyFloatBlock *)p)->next = block_list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
47 block_list = (PyFloatBlock *)p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
48 p = &((PyFloatBlock *)p)->objects[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
49 q = p + N_FLOATOBJECTS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
50 while (--q > p)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
51 Py_TYPE(q) = (struct _typeobject *)(q-1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
52 Py_TYPE(q) = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
53 return p + N_FLOATOBJECTS - 1;
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
54 }
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
55
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
56 double
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
57 PyFloat_GetMax(void)
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
58 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
59 return DBL_MAX;
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
60 }
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
61
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
62 double
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
63 PyFloat_GetMin(void)
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
64 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
65 return DBL_MIN;
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
66 }
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
67
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
68 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
69
d66e5837aa57 Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
70 PyDoc_STRVAR(floatinfo__doc__,
54777
4c6cdba1b0bb add underscores
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
71 "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
72 \n\
d66e5837aa57 Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
73 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
74 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
75 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
76
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 static PyStructSequence_Field floatinfo_fields[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
78 {"max", "DBL_MAX -- maximum representable finite float"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
79 {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
80 "is representable"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
81 {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
82 "is representable"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
83 {"min", "DBL_MIN -- Minimum positive normalizer float"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
84 {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
85 "is a normalized float"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
86 {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
87 "a normalized"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
88 {"dig", "DBL_DIG -- digits"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
89 {"mant_dig", "DBL_MANT_DIG -- mantissa digits"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
90 {"epsilon", "DBL_EPSILON -- Difference between 1 and the next "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
91 "representable float"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
92 {"radix", "FLT_RADIX -- radix of exponent"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
93 {"rounds", "FLT_ROUNDS -- addition rounds"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
94 {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
95 };
d66e5837aa57 Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
96
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 static PyStructSequence_Desc floatinfo_desc = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
98 "sys.float_info", /* name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
99 floatinfo__doc__, /* doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
100 floatinfo_fields, /* fields */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
101 11
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
102 };
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
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
104 PyObject *
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
105 PyFloat_GetInfo(void)
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
106 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
107 PyObject* floatinfo;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
108 int pos = 0;
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
109
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
110 floatinfo = PyStructSequence_New(&FloatInfoType);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
111 if (floatinfo == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
112 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
113 }
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
114
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
115 #define SetIntFlag(flag) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
116 PyStructSequence_SET_ITEM(floatinfo, pos++, PyInt_FromLong(flag))
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
117 #define SetDblFlag(flag) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
118 PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
119
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
120 SetDblFlag(DBL_MAX);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
121 SetIntFlag(DBL_MAX_EXP);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
122 SetIntFlag(DBL_MAX_10_EXP);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
123 SetDblFlag(DBL_MIN);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
124 SetIntFlag(DBL_MIN_EXP);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
125 SetIntFlag(DBL_MIN_10_EXP);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
126 SetIntFlag(DBL_DIG);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
127 SetIntFlag(DBL_MANT_DIG);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
128 SetDblFlag(DBL_EPSILON);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
129 SetIntFlag(FLT_RADIX);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
130 SetIntFlag(FLT_ROUNDS);
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
131 #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
132 #undef SetDblFlag
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
133
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
134 if (PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
135 Py_CLEAR(floatinfo);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
136 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
137 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
138 return floatinfo;
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
139 }
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
140
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
141 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
142 PyFloat_FromDouble(double fval)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
144 register PyFloatObject *op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
145 if (free_list == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
146 if ((free_list = fill_free_list()) == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
147 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
148 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
149 /* Inline PyObject_New */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
150 op = free_list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
151 free_list = (PyFloatObject *)Py_TYPE(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
152 PyObject_INIT(op, &PyFloat_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
153 op->ob_fval = fval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
154 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
157 /**************************************************************************
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
158 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
159 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
160
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
161 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
162 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
163 help from this function!).
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 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
166 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
167 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
168
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
169 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
170 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
171 *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
172 **************************************************************************/
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
173 PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
174 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
175 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
176 const char *s, *last, *end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
177 double x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
178 char buffer[256]; /* for errors */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
179 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
180 char *s_buffer = NULL;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
181 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
182 Py_ssize_t len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
183 PyObject *result = NULL;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
184
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
185 if (pend)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
186 *pend = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
187 if (PyString_Check(v)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
188 s = PyString_AS_STRING(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
189 len = PyString_GET_SIZE(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
190 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
191 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
192 else if (PyUnicode_Check(v)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
193 s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
194 if (s_buffer == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
195 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
196 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
197 PyUnicode_GET_SIZE(v),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
198 s_buffer,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
199 NULL))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
200 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
201 s = s_buffer;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
202 len = strlen(s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
203 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
204 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
205 else if (PyObject_AsCharBuffer(v, &s, &len)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
206 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
207 "float() argument must be a string or a number");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
208 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
209 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
210 last = s + len;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
211
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
212 while (Py_ISSPACE(*s))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
213 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
214 /* We don't care about overflow or underflow. If the platform
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
215 * supports them, infinities and signed zeroes (on underflow) are
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
216 * fine. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
217 x = PyOS_string_to_double(s, (char **)&end, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
218 if (x == -1.0 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
219 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
220 while (Py_ISSPACE(*end))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
221 end++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
222 if (end == last)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
223 result = PyFloat_FromDouble(x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
224 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
225 PyOS_snprintf(buffer, sizeof(buffer),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
226 "invalid literal for float(): %.200s", s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
227 PyErr_SetString(PyExc_ValueError, buffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
228 result = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
229 }
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
230
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
231 error:
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
232 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
233 if (s_buffer)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
234 PyMem_FREE(s_buffer);
56766
c56419cbdd48 Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents: 56753
diff changeset
235 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
236 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
237 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
238
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
239 static void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
240 float_dealloc(PyFloatObject *op)
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
241 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
242 if (PyFloat_CheckExact(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
243 Py_TYPE(op) = (struct _typeobject *)free_list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
244 free_list = op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
245 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
246 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
247 Py_TYPE(op)->tp_free((PyObject *)op);
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
248 }
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
249
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 double
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
251 PyFloat_AsDouble(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
253 PyNumberMethods *nb;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
254 PyFloatObject *fo;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
255 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
256
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
257 if (op && PyFloat_Check(op))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
258 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
259
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
260 if (op == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
261 PyErr_BadArgument();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
262 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
263 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
264
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
265 if ((nb = Py_TYPE(op)->tp_as_number) == NULL || nb->nb_float == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
266 PyErr_SetString(PyExc_TypeError, "a float is required");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
267 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
268 }
26272
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
269
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
270 fo = (PyFloatObject*) (*nb->nb_float) (op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
271 if (fo == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
272 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
273 if (!PyFloat_Check(fo)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
274 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
275 "nb_float should return float object");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
276 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
277 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
278
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
279 val = PyFloat_AS_DOUBLE(fo);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
280 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
281
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
282 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
287 /* 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
288 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
289 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
290 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
291 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
292 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
293 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
294 #define CONVERT_TO_DOUBLE(obj, dbl) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
295 if (PyFloat_Check(obj)) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
296 dbl = PyFloat_AS_DOUBLE(obj); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
297 else if (convert_to_double(&(obj), &(dbl)) < 0) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
298 return obj;
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
299
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
300 static int
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
301 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
302 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
303 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
304
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
305 if (PyInt_Check(obj)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
306 *dbl = (double)PyInt_AS_LONG(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
307 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
308 else if (PyLong_Check(obj)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
309 *dbl = PyLong_AsDouble(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
310 if (*dbl == -1.0 && PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
311 *v = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
312 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
313 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
314 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
315 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
316 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
317 *v = Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
318 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
319 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
320 return 0;
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
321 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
322
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
323 /* 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
324 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
325 */
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
326 void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
327 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
328 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
329 char *tmp = PyOS_double_to_string(v->ob_fval, 'g',
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
330 PyFloat_STR_PRECISION,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
331 Py_DTSF_ADD_DOT_0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
332 strcpy(buf, tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
333 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
334 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
335
17707
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
336 void
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
337 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
338 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
339 char * tmp = PyOS_double_to_string(v->ob_fval, 'r', 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
340 Py_DTSF_ADD_DOT_0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
341 strcpy(buf, tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
342 PyMem_Free(tmp);
17707
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
343 }
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
344
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
345 /* ARGSUSED */
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 268
diff changeset
346 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
347 float_print(PyFloatObject *v, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
349 char *buf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
350 if (flags & Py_PRINT_RAW)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
351 buf = PyOS_double_to_string(v->ob_fval,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
352 'g', PyFloat_STR_PRECISION,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
353 Py_DTSF_ADD_DOT_0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
354 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
355 buf = PyOS_double_to_string(v->ob_fval,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
356 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
357 Py_BEGIN_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
358 fputs(buf, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
359 Py_END_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
360 PyMem_Free(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
361 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
364 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
365 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
366 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
367 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
368 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
369 format_code, precision,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
370 Py_DTSF_ADD_DOT_0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
371 NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
372 if (!buf)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
373 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
374 result = PyString_FromString(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
375 PyMem_Free(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
376 return result;
56753
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
377 }
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
378
095af99e6b64 Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents: 56723
diff changeset
379 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
380 float_repr(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
382 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
383 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
384
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
385 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
386 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
387 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
388 return float_str_or_repr(v, PyFloat_STR_PRECISION, 'g');
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
391 /* 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
392 * 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
393 * 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
394 * 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
395 * 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
396 * 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
397 * 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
398 * 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
399 * 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
400 * 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
401 * 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
402 * 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
403 * 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
404 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
405
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
406 static PyObject*
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
407 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
408 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
409 double i, j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
410 int r = 0;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
411
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
412 assert(PyFloat_Check(v));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
413 i = PyFloat_AS_DOUBLE(v);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
414
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
415 /* Switch on the type of w. Set i and j to doubles to be compared,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
416 * and op to the richcomp to use.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
417 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
418 if (PyFloat_Check(w))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
419 j = PyFloat_AS_DOUBLE(w);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
420
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
421 else if (!Py_IS_FINITE(i)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
422 if (PyInt_Check(w) || PyLong_Check(w))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
423 /* If i is an infinity, its magnitude exceeds any
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
424 * finite integer, so it doesn't matter which int we
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
425 * compare i with. If i is a NaN, similarly.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
426 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
427 j = 0.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
428 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
429 goto Unimplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
430 }
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
431
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
432 else if (PyInt_Check(w)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
433 long jj = PyInt_AS_LONG(w);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
434 /* In the worst realistic case I can imagine, C double is a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
435 * Cray single with 48 bits of precision, and long has 64
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
436 * bits.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
437 */
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
438 #if SIZEOF_LONG > 6
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
439 unsigned long abs = (unsigned long)(jj < 0 ? -jj : jj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
440 if (abs >> 48) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
441 /* Needs more than 48 bits. Make it take the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
442 * PyLong path.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
443 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
444 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
445 PyObject *ww = PyLong_FromLong(jj);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
446
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
447 if (ww == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
448 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
449 result = float_richcompare(v, ww, op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
450 Py_DECREF(ww);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
451 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
452 }
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
453 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
454 j = (double)jj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
455 assert((long)j == jj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
456 }
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
457
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
458 else if (PyLong_Check(w)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
459 int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
460 int wsign = _PyLong_Sign(w);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
461 size_t nbits;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
462 int exponent;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
463
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
464 if (vsign != wsign) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
465 /* Magnitudes are irrelevant -- the signs alone
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
466 * determine the outcome.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
467 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
468 i = (double)vsign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
469 j = (double)wsign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
470 goto Compare;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
471 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
472 /* The signs are the same. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
473 /* Convert w to a double if it fits. In particular, 0 fits. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
474 nbits = _PyLong_NumBits(w);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
475 if (nbits == (size_t)-1 && PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
476 /* This long is so large that size_t isn't big enough
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
477 * to hold the # of bits. Replace with little doubles
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
478 * that give the same outcome -- w is so large that
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
479 * its magnitude must exceed the magnitude of any
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
480 * finite float.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
481 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
482 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
483 i = (double)vsign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
484 assert(wsign != 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
485 j = wsign * 2.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
486 goto Compare;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
487 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
488 if (nbits <= 48) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
489 j = PyLong_AsDouble(w);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
490 /* It's impossible that <= 48 bits overflowed. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
491 assert(j != -1.0 || ! PyErr_Occurred());
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
492 goto Compare;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
493 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
494 assert(wsign != 0); /* else nbits was 0 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
495 assert(vsign != 0); /* if vsign were 0, then since wsign is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
496 * not 0, we would have taken the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
497 * vsign != wsign branch at the start */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
498 /* We want to work with non-negative numbers. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
499 if (vsign < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
500 /* "Multiply both sides" by -1; this also swaps the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
501 * comparator.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
502 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
503 i = -i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
504 op = _Py_SwappedOp[op];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
505 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
506 assert(i > 0.0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
507 (void) frexp(i, &exponent);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
508 /* exponent is the # of bits in v before the radix point;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
509 * we know that nbits (the # of bits in w) > 48 at this point
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
510 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
511 if (exponent < 0 || (size_t)exponent < nbits) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
512 i = 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
513 j = 2.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
514 goto Compare;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
515 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
516 if ((size_t)exponent > nbits) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
517 i = 2.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
518 j = 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
519 goto Compare;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
520 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
521 /* v and w have the same number of bits before the radix
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
522 * point. Construct two longs that have the same comparison
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
523 * outcome.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
524 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
525 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
526 double fracpart;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
527 double intpart;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
528 PyObject *result = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
529 PyObject *one = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
530 PyObject *vv = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
531 PyObject *ww = w;
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
532
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
533 if (wsign < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
534 ww = PyNumber_Negative(w);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
535 if (ww == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
536 goto Error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
537 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
538 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
539 Py_INCREF(ww);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
540
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
541 fracpart = modf(i, &intpart);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
542 vv = PyLong_FromDouble(intpart);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
543 if (vv == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
544 goto Error;
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
545
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
546 if (fracpart != 0.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
547 /* Shift left, and or a 1 bit into vv
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
548 * to represent the lost fraction.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
549 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
550 PyObject *temp;
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
551
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
552 one = PyInt_FromLong(1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
553 if (one == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
554 goto Error;
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
555
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
556 temp = PyNumber_Lshift(ww, one);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
557 if (temp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
558 goto Error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
559 Py_DECREF(ww);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
560 ww = temp;
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
561
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
562 temp = PyNumber_Lshift(vv, one);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
563 if (temp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
564 goto Error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
565 Py_DECREF(vv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
566 vv = temp;
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
567
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
568 temp = PyNumber_Or(vv, one);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
569 if (temp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
570 goto Error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
571 Py_DECREF(vv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
572 vv = temp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
573 }
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
574
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
575 r = PyObject_RichCompareBool(vv, ww, op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
576 if (r < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
577 goto Error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
578 result = PyBool_FromLong(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
579 Error:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
580 Py_XDECREF(vv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
581 Py_XDECREF(ww);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
582 Py_XDECREF(one);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
583 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
584 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
585 } /* else if (PyLong_Check(w)) */
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
586
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
587 else /* w isn't float, int, or long */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
588 goto Unimplemented;
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
589
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
590 Compare:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
591 PyFPE_START_PROTECT("richcompare", return NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
592 switch (op) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
593 case Py_EQ:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
594 r = i == j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
595 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
596 case Py_NE:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
597 r = i != j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
598 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
599 case Py_LE:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
600 r = i <= j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
601 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
602 case Py_GE:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
603 r = i >= j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
604 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
605 case Py_LT:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
606 r = i < j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
607 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
608 case Py_GT:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
609 r = i > j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
610 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
611 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
612 PyFPE_END_PROTECT(r)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
613 return PyBool_FromLong(r);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
614
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
615 Unimplemented:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
616 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
617 return Py_NotImplemented;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
618 }
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
619
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
620 static long
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
621 float_hash(PyFloatObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
622 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
623 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
624 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
625
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
626 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
627 float_add(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
629 double a,b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
630 CONVERT_TO_DOUBLE(v, a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
631 CONVERT_TO_DOUBLE(w, b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
632 PyFPE_START_PROTECT("add", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
633 a = a + b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
634 PyFPE_END_PROTECT(a)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
635 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
637
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
638 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
639 float_sub(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
641 double a,b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
642 CONVERT_TO_DOUBLE(v, a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
643 CONVERT_TO_DOUBLE(w, b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
644 PyFPE_START_PROTECT("subtract", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
645 a = a - b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
646 PyFPE_END_PROTECT(a)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
647 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
649
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
650 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
651 float_mul(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
653 double a,b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
654 CONVERT_TO_DOUBLE(v, a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
655 CONVERT_TO_DOUBLE(w, b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
656 PyFPE_START_PROTECT("multiply", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
657 a = a * b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
658 PyFPE_END_PROTECT(a)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
659 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
661
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
662 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
663 float_div(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
665 double a,b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
666 CONVERT_TO_DOUBLE(v, a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
667 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
668 #ifdef Py_NAN
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
669 if (b == 0.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
670 PyErr_SetString(PyExc_ZeroDivisionError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
671 "float division by zero");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
672 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
673 }
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 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
675 PyFPE_START_PROTECT("divide", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
676 a = a / b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
677 PyFPE_END_PROTECT(a)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
678 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
680
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
681 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
682 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
683 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
684 double a,b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
685 CONVERT_TO_DOUBLE(v, a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
686 CONVERT_TO_DOUBLE(w, b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
687 if (Py_DivisionWarningFlag >= 2 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
688 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
689 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
690 #ifdef Py_NAN
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
691 if (b == 0.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
692 PyErr_SetString(PyExc_ZeroDivisionError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
693 "float division by zero");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
694 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
695 }
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 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
697 PyFPE_START_PROTECT("divide", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
698 a = a / b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
699 PyFPE_END_PROTECT(a)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
700 return PyFloat_FromDouble(a);
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
701 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
702
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
703 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
704 float_rem(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
706 double vx, wx;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
707 double mod;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
708 CONVERT_TO_DOUBLE(v, vx);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
709 CONVERT_TO_DOUBLE(w, wx);
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
710 #ifdef Py_NAN
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
711 if (wx == 0.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
712 PyErr_SetString(PyExc_ZeroDivisionError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
713 "float modulo");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
714 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
715 }
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 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
717 PyFPE_START_PROTECT("modulo", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
718 mod = fmod(vx, wx);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
719 /* note: checking mod*wx < 0 is incorrect -- underflows to
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
720 0 if wx < sqrt(smallest nonzero double) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
721 if (mod && ((wx < 0) != (mod < 0))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
722 mod += wx;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
723 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
724 PyFPE_END_PROTECT(mod)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
725 return PyFloat_FromDouble(mod);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
728 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
729 float_divmod(PyObject *v, PyObject *w)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
730 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
731 double vx, wx;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
732 double div, mod, floordiv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
733 CONVERT_TO_DOUBLE(v, vx);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
734 CONVERT_TO_DOUBLE(w, wx);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
735 if (wx == 0.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
736 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
737 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
738 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
739 PyFPE_START_PROTECT("divmod", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
740 mod = fmod(vx, wx);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
741 /* fmod is typically exact, so vx-mod is *mathematically* an
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
742 exact multiple of wx. But this is fp arithmetic, and fp
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
743 vx - mod is an approximation; the result is that div may
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
744 not be an exact integral value after the division, although
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
745 it will always be very close to one.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
746 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
747 div = (vx - mod) / wx;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
748 if (mod) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
749 /* ensure the remainder has the same sign as the denominator */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
750 if ((wx < 0) != (mod < 0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
751 mod += wx;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
752 div -= 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
753 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
754 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
755 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
756 /* the remainder is zero, and in the presence of signed zeroes
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
757 fmod returns different results across platforms; ensure
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
758 it has the same sign as the denominator; we'd like to do
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
759 "mod = wx * 0.0", but that may get optimized away */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
760 mod *= mod; /* hide "mod = +0" from optimizer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
761 if (wx < 0.0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
762 mod = -mod;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
763 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
764 /* snap quotient to nearest integral value */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
765 if (div) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
766 floordiv = floor(div);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
767 if (div - floordiv > 0.5)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
768 floordiv += 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
769 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
770 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
771 /* div is zero - get the same sign as the true quotient */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
772 div *= div; /* hide "div = +0" from optimizers */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
773 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
774 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
775 PyFPE_END_PROTECT(floordiv)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
776 return Py_BuildValue("(dd)", floordiv, mod);
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
777 }
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
778
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
779 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
780 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
781 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
782 PyObject *t, *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
783
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
784 t = float_divmod(v, w);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
785 if (t == NULL || t == Py_NotImplemented)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
786 return t;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
787 assert(PyTuple_CheckExact(t));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
788 r = PyTuple_GET_ITEM(t, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
789 Py_INCREF(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
790 Py_DECREF(t);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
791 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
792 }
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
793
58001
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
794 /* determine whether x is an odd integer or not; assumes that
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
795 x is not an infinity or nan. */
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
796 #define DOUBLE_IS_ODD_INTEGER(x) (fmod(fabs(x), 2.0) == 1.0)
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
797
21353
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
801 double iv, iw, ix;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
802 int negate_result = 0;
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
803
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
804 if ((PyObject *)z != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
805 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
806 "allowed unless all arguments are integers");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
807 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
808 }
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
809
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
810 CONVERT_TO_DOUBLE(v, iv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
811 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
812
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
813 /* Sort out special cases here instead of relying on pow() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
814 if (iw == 0) { /* v**0 is 1, even 0**0 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
815 return PyFloat_FromDouble(1.0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
816 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
817 if (Py_IS_NAN(iv)) { /* nan**w = nan, unless w == 0 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
818 return PyFloat_FromDouble(iv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
819 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
820 if (Py_IS_NAN(iw)) { /* v**nan = nan, unless v == 1; 1**nan = 1 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
821 return PyFloat_FromDouble(iv == 1.0 ? 1.0 : iw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
822 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
823 if (Py_IS_INFINITY(iw)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
824 /* v**inf is: 0.0 if abs(v) < 1; 1.0 if abs(v) == 1; inf if
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
825 * abs(v) > 1 (including case where v infinite)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
826 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
827 * v**-inf is: inf if abs(v) < 1; 1.0 if abs(v) == 1; 0.0 if
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
828 * abs(v) > 1 (including case where v infinite)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
829 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
830 iv = fabs(iv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
831 if (iv == 1.0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
832 return PyFloat_FromDouble(1.0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
833 else if ((iw > 0.0) == (iv > 1.0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
834 return PyFloat_FromDouble(fabs(iw)); /* return inf */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
835 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
836 return PyFloat_FromDouble(0.0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
837 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
838 if (Py_IS_INFINITY(iv)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
839 /* (+-inf)**w is: inf for w positive, 0 for w negative; in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
840 * both cases, we need to add the appropriate sign if w is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
841 * an odd integer.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
842 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
843 int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
844 if (iw > 0.0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
845 return PyFloat_FromDouble(iw_is_odd ? iv : fabs(iv));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
846 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
847 return PyFloat_FromDouble(iw_is_odd ?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
848 copysign(0.0, iv) : 0.0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
849 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
850 if (iv == 0.0) { /* 0**w is: 0 for w positive, 1 for w zero
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
851 (already dealt with above), and an error
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
852 if w is negative. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
853 int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
854 if (iw < 0.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
855 PyErr_SetString(PyExc_ZeroDivisionError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
856 "0.0 cannot be raised to a "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
857 "negative power");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
858 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
859 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
860 /* use correct sign if iw is odd */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
861 return PyFloat_FromDouble(iw_is_odd ? iv : 0.0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
862 }
58001
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
863
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
864 if (iv < 0.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
865 /* Whether this is an error is a mess, and bumps into libm
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
866 * bugs so we have to figure it out ourselves.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
867 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
868 if (iw != floor(iw)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
869 PyErr_SetString(PyExc_ValueError, "negative number "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
870 "cannot be raised to a fractional power");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
871 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
872 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
873 /* iw is an exact integer, albeit perhaps a very large
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
874 * one. Replace iv by its absolute value and remember
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
875 * to negate the pow result if iw is odd.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
876 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
877 iv = -iv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
878 negate_result = DOUBLE_IS_ODD_INTEGER(iw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
879 }
58001
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
880
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
881 if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
882 /* (-1) ** large_integer also ends up here. Here's an
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
883 * extract from the comments for the previous
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
884 * implementation explaining why this special case is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
885 * necessary:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
886 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
887 * -1 raised to an exact integer should never be exceptional.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
888 * Alas, some libms (chiefly glibc as of early 2003) return
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
889 * NaN and set EDOM on pow(-1, large_int) if the int doesn't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
890 * happen to be representable in a *C* integer. That's a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
891 * bug.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
892 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
893 return PyFloat_FromDouble(negate_result ? -1.0 : 1.0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
894 }
58001
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
895
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
896 /* Now iv and iw are finite, iw is nonzero, and iv is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
897 * positive and not equal to 1.0. We finally allow
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
898 * the platform pow to step in and do the rest.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
899 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
900 errno = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
901 PyFPE_START_PROTECT("pow", return NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
902 ix = pow(iv, iw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
903 PyFPE_END_PROTECT(ix)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
904 Py_ADJUST_ERANGE1(ix);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
905 if (negate_result)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
906 ix = -ix;
58001
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
907
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
908 if (errno != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
909 /* We don't expect any errno value other than ERANGE, but
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
910 * the range of libm bugs appears unbounded.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
911 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
912 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
913 PyExc_ValueError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
914 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
915 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
916 return PyFloat_FromDouble(ix);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
917 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
918
58001
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
919 #undef DOUBLE_IS_ODD_INTEGER
ac5fd8909998 Issue #7534: Fix handling of nans, infinities, and negative zero in **
Mark Dickinson <dickinsm@gmail.com>
parents: 57485
diff changeset
920
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
921 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
922 float_neg(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
923 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
924 return PyFloat_FromDouble(-v->ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
926
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
927 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
928 float_abs(PyFloatObject *v)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
929 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
930 return PyFloat_FromDouble(fabs(v->ob_fval));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
932
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
933 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
934 float_nonzero(PyFloatObject *v)
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
935 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
936 return v->ob_fval != 0.0;
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
937 }
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
938
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
939 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
940 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
941 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
942 if (PyInt_Check(*pw)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
943 long x = PyInt_AsLong(*pw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
944 *pw = PyFloat_FromDouble((double)x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
945 Py_INCREF(*pv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
946 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
947 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
948 else if (PyLong_Check(*pw)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
949 double x = PyLong_AsDouble(*pw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
950 if (x == -1.0 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
951 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
952 *pw = PyFloat_FromDouble(x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
953 Py_INCREF(*pv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
954 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
955 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
956 else if (PyFloat_Check(*pw)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
957 Py_INCREF(*pv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
958 Py_INCREF(*pw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
959 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
960 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
961 return 1; /* Can't do it */
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
962 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
963
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
964 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
965 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
966 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
967 double x = PyFloat_AsDouble(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
968 PyObject *o;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
969
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
970 if (x == -1.0 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
971 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
972 if (!Py_IS_FINITE(x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
973 Py_RETURN_FALSE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
974 errno = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
975 PyFPE_START_PROTECT("is_integer", return NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
976 o = (floor(x) == x) ? Py_True : Py_False;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
977 PyFPE_END_PROTECT(x)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
978 if (errno != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
979 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
980 PyExc_ValueError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
981 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
982 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
983 Py_INCREF(o);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
984 return o;
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
985 }
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
986
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
987 #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
988 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
989 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
990 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
991 double x = PyFloat_AsDouble(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
992 if (x == -1.0 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
993 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
994 return PyBool_FromLong((long)Py_IS_INFINITY(x));
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
995 }
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
996
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
997 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
998 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
999 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1000 double x = PyFloat_AsDouble(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1001 if (x == -1.0 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1002 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1003 return PyBool_FromLong((long)Py_IS_NAN(x));
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
1004 }
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
1005
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
1006 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
1007 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
1008 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1009 double x = PyFloat_AsDouble(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1010 if (x == -1.0 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1011 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1012 return PyBool_FromLong((long)Py_IS_FINITE(x));
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
1013 }
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
1014 #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
1015
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
1016 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
1017 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
1018 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1019 double x = PyFloat_AsDouble(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1020 double wholepart; /* integral portion of x, rounded toward 0 */
18838
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
1021
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1022 (void)modf(x, &wholepart);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1023 /* Try to get out cheap if this fits in a Python int. The attempt
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1024 * to cast to long must be protected, as C doesn't define what
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1025 * happens if the double is too big to fit in a long. Some rare
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1026 * systems raise an exception then (RISCOS was mentioned as one,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1027 * and someone using a non-default option on Sun also bumped into
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1028 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1029 * still be vulnerable: if a long has more bits of precision than
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1030 * a double, casting MIN/MAX to double may yield an approximation,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1031 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1032 * yield true from the C expression wholepart<=LONG_MAX, despite
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1033 * that wholepart is actually greater than LONG_MAX.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1034 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1035 if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1036 const long aslong = (long)wholepart;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1037 return PyInt_FromLong(aslong);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1038 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1039 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
1040 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1041
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1042 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
1043 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
1044 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1045 double x = PyFloat_AsDouble(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1046 return PyLong_FromDouble(x);
49104
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
1047 }
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
1048
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1049 /* _Py_double_round: rounds a finite nonzero double to the closest multiple of
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1050 10**-ndigits; here ndigits is within reasonable bounds (typically, -308 <=
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1051 ndigits <= 323). Returns a Python float, or sets a Python error and
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1052 returns NULL on failure (OverflowError and memory errors are possible). */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1053
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1054 #ifndef PY_NO_SHORT_FLOAT_REPR
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1055 /* version of _Py_double_round that uses the correctly-rounded string<->double
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1056 conversions from Python/dtoa.c */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1057
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1058 /* FIVE_POW_LIMIT is the largest k such that 5**k is exactly representable as
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1059 a double. Since we're using the code in Python/dtoa.c, it should be safe
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1060 to assume that C doubles are IEEE 754 binary64 format. To be on the safe
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1061 side, we check this. */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1062 #if DBL_MANT_DIG == 53
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1063 #define FIVE_POW_LIMIT 22
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1064 #else
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1065 #error "C doubles do not appear to be IEEE 754 binary64 format"
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1066 #endif
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1067
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1068 PyObject *
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1069 _Py_double_round(double x, int ndigits) {
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1070
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1071 double rounded, m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1072 Py_ssize_t buflen, mybuflen=100;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1073 char *buf, *buf_end, shortbuf[100], *mybuf=shortbuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1074 int decpt, sign, val, halfway_case;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1075 PyObject *result = NULL;
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1076
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1077 /* The basic idea is very simple: convert and round the double to a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1078 decimal string using _Py_dg_dtoa, then convert that decimal string
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1079 back to a double with _Py_dg_strtod. There's one minor difficulty:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1080 Python 2.x expects round to do round-half-away-from-zero, while
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1081 _Py_dg_dtoa does round-half-to-even. So we need some way to detect
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1082 and correct the halfway cases.
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1083
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1084 Detection: a halfway value has the form k * 0.5 * 10**-ndigits for
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1085 some odd integer k. Or in other words, a rational number x is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1086 exactly halfway between two multiples of 10**-ndigits if its
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1087 2-valuation is exactly -ndigits-1 and its 5-valuation is at least
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1088 -ndigits. For ndigits >= 0 the latter condition is automatically
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1089 satisfied for a binary float x, since any such float has
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1090 nonnegative 5-valuation. For 0 > ndigits >= -22, x needs to be an
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1091 integral multiple of 5**-ndigits; we can check this using fmod.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1092 For -22 > ndigits, there are no halfway cases: 5**23 takes 54 bits
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1093 to represent exactly, so any odd multiple of 0.5 * 10**n for n >=
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1094 23 takes at least 54 bits of precision to represent exactly.
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1095
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1096 Correction: a simple strategy for dealing with halfway cases is to
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1097 (for the halfway cases only) call _Py_dg_dtoa with an argument of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1098 ndigits+1 instead of ndigits (thus doing an exact conversion to
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1099 decimal), round the resulting string manually, and then convert
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1100 back using _Py_dg_strtod.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1101 */
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1102
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1103 /* nans, infinities and zeros should have already been dealt
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1104 with by the caller (in this case, builtin_round) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1105 assert(Py_IS_FINITE(x) && x != 0.0);
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1106
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1107 /* find 2-valuation val of x */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1108 m = frexp(x, &val);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1109 while (m != floor(m)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1110 m *= 2.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1111 val--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1112 }
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1113
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1114 /* determine whether this is a halfway case */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1115 if (val == -ndigits-1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1116 if (ndigits >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1117 halfway_case = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1118 else if (ndigits >= -FIVE_POW_LIMIT) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1119 double five_pow = 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1120 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1121 for (i=0; i < -ndigits; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1122 five_pow *= 5.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1123 halfway_case = fmod(x, five_pow) == 0.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1124 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1125 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1126 halfway_case = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1127 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1128 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1129 halfway_case = 0;
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1130
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1131 /* round to a decimal string; use an extra place for halfway case */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1132 buf = _Py_dg_dtoa(x, 3, ndigits+halfway_case, &decpt, &sign, &buf_end);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1133 if (buf == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1134 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1135 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1136 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1137 buflen = buf_end - buf;
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1138
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1139 /* in halfway case, do the round-half-away-from-zero manually */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1140 if (halfway_case) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1141 int i, carry;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1142 /* sanity check: _Py_dg_dtoa should not have stripped
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1143 any zeros from the result: there should be exactly
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1144 ndigits+1 places following the decimal point, and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1145 the last digit in the buffer should be a '5'.*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1146 assert(buflen - decpt == ndigits+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1147 assert(buf[buflen-1] == '5');
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1148
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1149 /* increment and shift right at the same time. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1150 decpt += 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1151 carry = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1152 for (i=buflen-1; i-- > 0;) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1153 carry += buf[i] - '0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1154 buf[i+1] = carry % 10 + '0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1155 carry /= 10;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1156 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1157 buf[0] = carry + '0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1158 }
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1159
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1160 /* Get new buffer if shortbuf is too small. Space needed <= buf_end -
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1161 buf + 8: (1 extra for '0', 1 for sign, 5 for exp, 1 for '\0'). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1162 if (buflen + 8 > mybuflen) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1163 mybuflen = buflen+8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1164 mybuf = (char *)PyMem_Malloc(mybuflen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1165 if (mybuf == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1166 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1167 goto exit;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1168 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1169 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1170 /* copy buf to mybuf, adding exponent, sign and leading 0 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1171 PyOS_snprintf(mybuf, mybuflen, "%s0%se%d", (sign ? "-" : ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1172 buf, decpt - (int)buflen);
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1173
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1174 /* and convert the resulting string back to a double */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1175 errno = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1176 rounded = _Py_dg_strtod(mybuf, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1177 if (errno == ERANGE && fabs(rounded) >= 1.)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1178 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1179 "rounded value too large to represent");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1180 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1181 result = PyFloat_FromDouble(rounded);
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1182
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1183 /* done computing value; now clean up */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1184 if (mybuf != shortbuf)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1185 PyMem_Free(mybuf);
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1186 exit:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1187 _Py_dg_freedtoa(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1188 return result;
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1189 }
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1190
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1191 #undef FIVE_POW_LIMIT
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1192
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1193 #else /* PY_NO_SHORT_FLOAT_REPR */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1194
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1195 /* fallback version, to be used when correctly rounded binary<->decimal
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1196 conversions aren't available */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1197
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1198 PyObject *
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1199 _Py_double_round(double x, int ndigits) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1200 double pow1, pow2, y, z;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1201 if (ndigits >= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1202 if (ndigits > 22) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1203 /* pow1 and pow2 are each safe from overflow, but
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1204 pow1*pow2 ~= pow(10.0, ndigits) might overflow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1205 pow1 = pow(10.0, (double)(ndigits-22));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1206 pow2 = 1e22;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1207 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1208 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1209 pow1 = pow(10.0, (double)ndigits);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1210 pow2 = 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1211 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1212 y = (x*pow1)*pow2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1213 /* if y overflows, then rounded value is exactly x */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1214 if (!Py_IS_FINITE(y))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1215 return PyFloat_FromDouble(x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1216 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1217 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1218 pow1 = pow(10.0, (double)-ndigits);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1219 pow2 = 1.0; /* unused; silences a gcc compiler warning */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1220 y = x / pow1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1221 }
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1222
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1223 z = round(y);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1224 if (fabs(y-z) == 0.5)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1225 /* halfway between two integers; use round-away-from-zero */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1226 z = y + copysign(0.5, y);
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1227
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1228 if (ndigits >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1229 z = (z / pow2) / pow1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1230 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1231 z *= pow1;
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1232
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1233 /* if computation resulted in overflow, raise OverflowError */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1234 if (!Py_IS_FINITE(z)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1235 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1236 "overflow occurred during round");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1237 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1238 }
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1239
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1240 return PyFloat_FromDouble(z);
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1241 }
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1242
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1243 #endif /* PY_NO_SHORT_FLOAT_REPR */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
1244
49104
974040c8f369 #3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48818
diff changeset
1245 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1246 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
1247 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1248 if (PyFloat_CheckExact(v))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1249 Py_INCREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1250 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1251 v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1252 return v;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1253 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1254
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1255 /* 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
1256
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1257 static char
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1258 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
1259 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1260 assert(0 <= x && x < 16);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1261 return "0123456789abcdef"[x];
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1262 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1263
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1264 static int
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1265 hex_from_char(char c) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1266 int x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1267 switch(c) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1268 case '0':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1269 x = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1270 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1271 case '1':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1272 x = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1273 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1274 case '2':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1275 x = 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1276 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1277 case '3':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1278 x = 3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1279 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1280 case '4':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1281 x = 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1282 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1283 case '5':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1284 x = 5;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1285 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1286 case '6':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1287 x = 6;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1288 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1289 case '7':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1290 x = 7;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1291 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1292 case '8':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1293 x = 8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1294 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1295 case '9':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1296 x = 9;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1297 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1298 case 'a':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1299 case 'A':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1300 x = 10;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1301 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1302 case 'b':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1303 case 'B':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1304 x = 11;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1305 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1306 case 'c':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1307 case 'C':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1308 x = 12;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1309 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1310 case 'd':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1311 case 'D':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1312 x = 13;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1313 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1314 case 'e':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1315 case 'E':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1316 x = 14;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1317 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1318 case 'f':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1319 case 'F':
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1320 x = 15;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1321 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1322 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1323 x = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1324 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1325 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1326 return x;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1327 }
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 /* 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
1330
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1331 /* 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
1332 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
1333 #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
1334
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1335 static PyObject *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1336 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
1337 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1338 double x, m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1339 int e, shift, i, si, esign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1340 /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1341 trailing NUL byte. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1342 char s[(TOHEX_NBITS-1)/4+3];
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1343
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1344 CONVERT_TO_DOUBLE(v, x);
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1345
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1346 if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1347 return float_str((PyFloatObject *)v);
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1348
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1349 if (x == 0.0) {
62689
b540a612f601 add space
Benjamin Peterson <benjamin@python.org>
parents: 61463
diff changeset
1350 if (copysign(1.0, x) == -1.0)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1351 return PyString_FromString("-0x0.0p+0");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1352 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1353 return PyString_FromString("0x0.0p+0");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1354 }
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1355
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1356 m = frexp(fabs(x), &e);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1357 shift = 1 - MAX(DBL_MIN_EXP - e, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1358 m = ldexp(m, shift);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1359 e -= shift;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1360
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1361 si = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1362 s[si] = char_from_hex((int)m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1363 si++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1364 m -= (int)m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1365 s[si] = '.';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1366 si++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1367 for (i=0; i < (TOHEX_NBITS-1)/4; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1368 m *= 16.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1369 s[si] = char_from_hex((int)m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1370 si++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1371 m -= (int)m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1372 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1373 s[si] = '\0';
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1374
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1375 if (e < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1376 esign = (int)'-';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1377 e = -e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1378 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1379 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1380 esign = (int)'+';
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1381
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1382 if (x < 0.0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1383 return PyString_FromFormat("-0x%sp%c%d", s, esign, e);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1384 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1385 return PyString_FromFormat("0x%sp%c%d", s, esign, e);
47966
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
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1388 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
1389 "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
1390 \n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1391 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
1392 >>> (-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
1393 '-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
1394 >>> 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
1395 '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
1396
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1397 /* 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
1398 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
1399 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
1400
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1401 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
1402 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
1403 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1404 while(*t && Py_TOLOWER(*s) == *t) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1405 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1406 t++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1407 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1408 return *t ? 0 : 1;
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1409 }
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
1410
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1411 /* 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
1412
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1413 static PyObject *
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1414 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
1415 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1416 PyObject *result_as_float, *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1417 double x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1418 long exp, top_exp, lsb, key_digit;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1419 char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1420 int half_eps, digit, round_up, sign=1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1421 Py_ssize_t length, ndigits, fdigits, i;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1422
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1423 /*
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1424 * For the sake of simplicity and correctness, we impose an artificial
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1425 * limit on ndigits, the total number of hex digits in the coefficient
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1426 * The limit is chosen to ensure that, writing exp for the exponent,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1427 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1428 * (1) if exp > LONG_MAX/2 then the value of the hex string is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1429 * guaranteed to overflow (provided it's nonzero)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1430 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1431 * (2) if exp < LONG_MIN/2 then the value of the hex string is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1432 * guaranteed to underflow to 0.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1433 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1434 * (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1435 * overflow in the calculation of exp and top_exp below.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1436 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1437 * More specifically, ndigits is assumed to satisfy the following
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1438 * inequalities:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1439 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1440 * 4*ndigits <= DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1441 * 4*ndigits <= LONG_MAX/2 + 1 - DBL_MAX_EXP
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1442 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1443 * If either of these inequalities is not satisfied, a ValueError is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1444 * raised. Otherwise, write x for the value of the hex string, and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1445 * assume x is nonzero. Then
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1446 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1447 * 2**(exp-4*ndigits) <= |x| < 2**(exp+4*ndigits).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1448 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1449 * Now if exp > LONG_MAX/2 then:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1450 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1451 * exp - 4*ndigits >= LONG_MAX/2 + 1 - (LONG_MAX/2 + 1 - DBL_MAX_EXP)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1452 * = DBL_MAX_EXP
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1453 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1454 * so |x| >= 2**DBL_MAX_EXP, which is too large to be stored in C
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1455 * double, so overflows. If exp < LONG_MIN/2, then
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1456 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1457 * exp + 4*ndigits <= LONG_MIN/2 - 1 + (
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1458 * DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1459 * = DBL_MIN_EXP - DBL_MANT_DIG - 1
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1460 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1461 * and so |x| < 2**(DBL_MIN_EXP-DBL_MANT_DIG-1), hence underflows to 0
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1462 * when converted to a C double.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1463 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1464 * It's easy to show that if LONG_MIN/2 <= exp <= LONG_MAX/2 then both
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1465 * exp+4*ndigits and exp-4*ndigits are within the range of a long.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1466 */
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1467
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1468 if (PyString_AsStringAndSize(arg, &s, &length))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1469 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1470 s_end = s + length;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1471
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1472 /********************
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1473 * Parse the string *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1474 ********************/
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1475
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1476 /* leading whitespace and optional sign */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1477 while (Py_ISSPACE(*s))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1478 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1479 if (*s == '-') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1480 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1481 sign = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1482 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1483 else if (*s == '+')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1484 s++;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1485
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1486 /* infinities and nans */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1487 if (*s == 'i' || *s == 'I') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1488 if (!case_insensitive_match(s+1, "nf"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1489 goto parse_error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1490 s += 3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1491 x = Py_HUGE_VAL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1492 if (case_insensitive_match(s, "inity"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1493 s += 5;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1494 goto finished;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1495 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1496 if (*s == 'n' || *s == 'N') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1497 if (!case_insensitive_match(s+1, "an"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1498 goto parse_error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1499 s += 3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1500 x = Py_NAN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1501 goto finished;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1502 }
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1503
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1504 /* [0x] */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1505 s_store = s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1506 if (*s == '0') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1507 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1508 if (*s == 'x' || *s == 'X')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1509 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1510 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1511 s = s_store;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1512 }
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1513
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1514 /* coefficient: <integer> [. <fraction>] */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1515 coeff_start = s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1516 while (hex_from_char(*s) >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1517 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1518 s_store = s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1519 if (*s == '.') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1520 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1521 while (hex_from_char(*s) >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1522 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1523 coeff_end = s-1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1524 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1525 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1526 coeff_end = s;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1527
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1528 /* ndigits = total # of hex digits; fdigits = # after point */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1529 ndigits = coeff_end - coeff_start;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1530 fdigits = coeff_end - s_store;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1531 if (ndigits == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1532 goto parse_error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1533 if (ndigits > MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1534 LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1535 goto insane_length_error;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1536
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1537 /* [p <exponent>] */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1538 if (*s == 'p' || *s == 'P') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1539 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1540 exp_start = s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1541 if (*s == '-' || *s == '+')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1542 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1543 if (!('0' <= *s && *s <= '9'))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1544 goto parse_error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1545 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1546 while ('0' <= *s && *s <= '9')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1547 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1548 exp = strtol(exp_start, NULL, 10);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1549 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1550 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1551 exp = 0;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1552
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1553 /* for 0 <= j < ndigits, HEX_DIGIT(j) gives the jth most significant digit */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1554 #define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ? \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1555 coeff_end-(j) : \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1556 coeff_end-1-(j)))
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1557
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1558 /*******************************************
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1559 * Compute rounded value of the hex string *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1560 *******************************************/
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1561
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1562 /* Discard leading zeros, and catch extreme overflow and underflow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1563 while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1564 ndigits--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1565 if (ndigits == 0 || exp < LONG_MIN/2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1566 x = 0.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1567 goto finished;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1568 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1569 if (exp > LONG_MAX/2)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1570 goto overflow_error;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1571
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1572 /* Adjust exponent for fractional part. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1573 exp = exp - 4*((long)fdigits);
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1574
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1575 /* top_exp = 1 more than exponent of most sig. bit of coefficient */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1576 top_exp = exp + 4*((long)ndigits - 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1577 for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1578 top_exp++;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1579
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1580 /* catch almost all nonextreme cases of overflow and underflow here */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1581 if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1582 x = 0.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1583 goto finished;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1584 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1585 if (top_exp > DBL_MAX_EXP)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1586 goto overflow_error;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1587
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1588 /* lsb = exponent of least significant bit of the *rounded* value.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1589 This is top_exp - DBL_MANT_DIG unless result is subnormal. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1590 lsb = MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1591
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1592 x = 0.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1593 if (exp >= lsb) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1594 /* no rounding required */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1595 for (i = ndigits-1; i >= 0; i--)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1596 x = 16.0*x + HEX_DIGIT(i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1597 x = ldexp(x, (int)(exp));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1598 goto finished;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1599 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1600 /* rounding required. key_digit is the index of the hex digit
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1601 containing the first bit to be rounded away. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1602 half_eps = 1 << (int)((lsb - exp - 1) % 4);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1603 key_digit = (lsb - exp - 1) / 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1604 for (i = ndigits-1; i > key_digit; i--)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1605 x = 16.0*x + HEX_DIGIT(i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1606 digit = HEX_DIGIT(key_digit);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1607 x = 16.0*x + (double)(digit & (16-2*half_eps));
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1608
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1609 /* round-half-even: round up if bit lsb-1 is 1 and at least one of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1610 bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1611 if ((digit & half_eps) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1612 round_up = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1613 if ((digit & (3*half_eps-1)) != 0 ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1614 (half_eps == 8 && (HEX_DIGIT(key_digit+1) & 1) != 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1615 round_up = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1616 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1617 for (i = key_digit-1; i >= 0; i--)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1618 if (HEX_DIGIT(i) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1619 round_up = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1620 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1621 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1622 if (round_up == 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1623 x += 2*half_eps;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1624 if (top_exp == DBL_MAX_EXP &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1625 x == ldexp((double)(2*half_eps), DBL_MANT_DIG))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1626 /* overflow corner case: pre-rounded value <
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1627 2**DBL_MAX_EXP; rounded=2**DBL_MAX_EXP. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1628 goto overflow_error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1629 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1630 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1631 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
1632
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1633 finished:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1634 /* optional trailing whitespace leading to the end of the string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1635 while (Py_ISSPACE(*s))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1636 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1637 if (s != s_end)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1638 goto parse_error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1639 result_as_float = Py_BuildValue("(d)", sign * x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1640 if (result_as_float == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1641 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1642 result = PyObject_CallObject(cls, result_as_float);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1643 Py_DECREF(result_as_float);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1644 return result;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1645
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1646 overflow_error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1647 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1648 "hexadecimal value too large to represent as a float");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1649 return NULL;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1650
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1651 parse_error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1652 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1653 "invalid hexadecimal floating-point string");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1654 return NULL;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1655
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1656 insane_length_error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1657 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1658 "hexadecimal string too long to convert");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1659 return NULL;
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1660 }
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1661
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1662 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
1663 "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
1664 \n\
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1665 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
1666 >>> 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
1667 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
1668 >>> 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
1669 -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
1670
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1671
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1672 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
1673 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
1674 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1675 double self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1676 double float_part;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1677 int exponent;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1678 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
1679
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1680 PyObject *prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1681 PyObject *py_exponent = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1682 PyObject *numerator = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1683 PyObject *denominator = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1684 PyObject *result_pair = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1685 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
1686
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1687 #define INPLACE_UPDATE(obj, call) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1688 prev = obj; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1689 obj = call; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1690 Py_DECREF(prev); \
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1691
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1692 CONVERT_TO_DOUBLE(v, self);
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1693
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1694 if (Py_IS_INFINITY(self)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1695 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1696 "Cannot pass infinity to float.as_integer_ratio.");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1697 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1698 }
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1699 #ifdef Py_NAN
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1700 if (Py_IS_NAN(self)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1701 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1702 "Cannot pass NaN to float.as_integer_ratio.");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1703 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1704 }
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1705 #endif
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1706
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1707 PyFPE_START_PROTECT("as_integer_ratio", goto error);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1708 float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1709 PyFPE_END_PROTECT(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
1710
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1711 for (i=0; i<300 && float_part != floor(float_part) ; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1712 float_part *= 2.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1713 exponent--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1714 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1715 /* self == float_part * 2**exponent exactly and float_part is integral.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1716 If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1717 to be truncated by PyLong_FromDouble(). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1718
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1719 numerator = PyLong_FromDouble(float_part);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1720 if (numerator == NULL) goto error;
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1721
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1722 /* fold in 2**exponent */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1723 denominator = PyLong_FromLong(1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1724 py_exponent = PyLong_FromLong(labs((long)exponent));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1725 if (py_exponent == NULL) goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1726 INPLACE_UPDATE(py_exponent,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1727 long_methods->nb_lshift(denominator, py_exponent));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1728 if (py_exponent == NULL) goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1729 if (exponent > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1730 INPLACE_UPDATE(numerator,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1731 long_methods->nb_multiply(numerator, py_exponent));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1732 if (numerator == NULL) goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1733 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1734 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1735 Py_DECREF(denominator);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1736 denominator = py_exponent;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1737 py_exponent = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1738 }
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1739
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1740 /* Returns ints instead of longs where possible */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1741 INPLACE_UPDATE(numerator, PyNumber_Int(numerator));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1742 if (numerator == NULL) goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1743 INPLACE_UPDATE(denominator, PyNumber_Int(denominator));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1744 if (denominator == NULL) goto error;
45095
5cb8288360e7 Integer ratio should return ints instead of longs whereever possible.
Raymond Hettinger <python@rcn.com>
parents: 45094
diff changeset
1745
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1746 result_pair = PyTuple_Pack(2, numerator, denominator);
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1747
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1748 #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
1749 error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1750 Py_XDECREF(py_exponent);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1751 Py_XDECREF(denominator);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1752 Py_XDECREF(numerator);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1753 return result_pair;
45024
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1754 }
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1755
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1756 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
1757 "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
1758 "\n"
45094
beea73ca2b88 Issue #1996: float.as_integer_ratio() should return fraction in lowest terms.
Raymond Hettinger <python@rcn.com>
parents: 45060
diff changeset
1759 "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
1760 "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
1761 "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
1762 "\n"
f062dda54894 Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44940
diff changeset
1763 ">>> (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
1764 "(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
1765 ">>> (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
1766 "(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
1767 ">>> (-.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
1768 "(-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
1769
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1770
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
1771 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1772 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
1773
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1774 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1775 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
1776 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1777 PyObject *x = Py_False; /* Integer zero */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1778 static char *kwlist[] = {"x", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1779
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1780 if (type != &PyFloat_Type)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1781 return float_subtype_new(type, args, kwds); /* Wimp out */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1782 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1783 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1784 /* If it's a string, but not a string subclass, use
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1785 PyFloat_FromString. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1786 if (PyString_CheckExact(x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1787 return PyFloat_FromString(x, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1788 return PyNumber_Float(x);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1789 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1790
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1791 /* 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
1792 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
1793 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
1794 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
1795 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1796 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1797 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
1798 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1799 PyObject *tmp, *newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1800
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1801 assert(PyType_IsSubtype(type, &PyFloat_Type));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1802 tmp = float_new(&PyFloat_Type, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1803 if (tmp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1804 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1805 assert(PyFloat_CheckExact(tmp));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1806 newobj = type->tp_alloc(type, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1807 if (newobj == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1808 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1809 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1810 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1811 ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1812 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1813 return newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1814 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1815
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1816 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1817 float_getnewargs(PyFloatObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1818 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1819 return Py_BuildValue("(d)", v->ob_fval);
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1820 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1821
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1822 /* 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
1823
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1824 typedef enum {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1825 unknown_format, ieee_big_endian_format, ieee_little_endian_format
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1826 } float_format_type;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1827
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1828 static float_format_type double_format, float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1829 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
1830
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1831 static PyObject *
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1832 float_getformat(PyTypeObject *v, PyObject* arg)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1833 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1834 char* s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1835 float_format_type r;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1836
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1837 if (!PyString_Check(arg)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1838 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1839 "__getformat__() argument must be string, not %.500s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1840 Py_TYPE(arg)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1841 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1842 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1843 s = PyString_AS_STRING(arg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1844 if (strcmp(s, "double") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1845 r = double_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1846 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1847 else if (strcmp(s, "float") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1848 r = float_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1849 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1850 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1851 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1852 "__getformat__() argument 1 must be "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1853 "'double' or 'float'");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1854 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1855 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1856
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1857 switch (r) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1858 case unknown_format:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1859 return PyString_FromString("unknown");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1860 case ieee_little_endian_format:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1861 return PyString_FromString("IEEE, little-endian");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1862 case ieee_big_endian_format:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1863 return PyString_FromString("IEEE, big-endian");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1864 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1865 Py_FatalError("insane float_format or double_format");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1866 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1867 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1868 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1869
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1870 PyDoc_STRVAR(float_getformat_doc,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1871 "float.__getformat__(typestr) -> string\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1872 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1873 "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
1874 "used in Python's test suite.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1875 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1876 "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
1877 "'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
1878 "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
1879
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1880 static PyObject *
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1881 float_setformat(PyTypeObject *v, PyObject* args)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1882 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1883 char* typestr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1884 char* format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1885 float_format_type f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1886 float_format_type detected;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1887 float_format_type *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1888
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1889 if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1890 return NULL;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1891
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1892 if (strcmp(typestr, "double") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1893 p = &double_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1894 detected = detected_double_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1895 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1896 else if (strcmp(typestr, "float") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1897 p = &float_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1898 detected = detected_float_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1899 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1900 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1901 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1902 "__setformat__() argument 1 must "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1903 "be 'double' or 'float'");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1904 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1905 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1906
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1907 if (strcmp(format, "unknown") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1908 f = unknown_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1909 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1910 else if (strcmp(format, "IEEE, little-endian") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1911 f = ieee_little_endian_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1912 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1913 else if (strcmp(format, "IEEE, big-endian") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1914 f = ieee_big_endian_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1915 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1916 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1917 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1918 "__setformat__() argument 2 must be "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1919 "'unknown', 'IEEE, little-endian' or "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1920 "'IEEE, big-endian'");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1921 return NULL;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1922
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1923 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1924
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1925 if (f != unknown_format && f != detected) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1926 PyErr_Format(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1927 "can only set %s format to 'unknown' or the "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1928 "detected platform value", typestr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1929 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1930 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1931
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1932 *p = f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1933 Py_RETURN_NONE;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1934 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1935
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1936 PyDoc_STRVAR(float_setformat_doc,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1937 "float.__setformat__(typestr, fmt) -> None\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1938 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1939 "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
1940 "used in Python's test suite.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1941 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1942 "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
1943 "'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
1944 "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
1945 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1946 "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
1947 "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
1948
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
1949 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
1950 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
1951 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1952 return PyFloat_FromDouble(0.0);
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
1953 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1954
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1955 static PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1956 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
1957 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1958 PyObject *format_spec;
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1959
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1960 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1961 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1962 if (PyBytes_Check(format_spec))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1963 return _PyFloat_FormatAdvanced(self,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1964 PyBytes_AS_STRING(format_spec),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1965 PyBytes_GET_SIZE(format_spec));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1966 if (PyUnicode_Check(format_spec)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1967 /* Convert format_spec to a str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1968 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1969 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
1970
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1971 if (str_spec == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1972 return NULL;
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1973
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1974 result = _PyFloat_FormatAdvanced(self,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1975 PyBytes_AS_STRING(str_spec),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1976 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
1977
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1978 Py_DECREF(str_spec);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1979 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1980 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1981 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1982 return NULL;
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1983 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1984
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1985 PyDoc_STRVAR(float__format__doc,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1986 "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
1987 "\n"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1988 "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
1989
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1990
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1991 static PyMethodDef float_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1992 {"conjugate", (PyCFunction)float_float, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1993 "Returns self, the complex conjugate of any float."},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1994 {"__trunc__", (PyCFunction)float_trunc, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1995 "Returns the Integral closest to x between 0 and x."},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1996 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1997 float_as_integer_ratio_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1998 {"fromhex", (PyCFunction)float_fromhex,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
1999 METH_O|METH_CLASS, float_fromhex_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2000 {"hex", (PyCFunction)float_hex,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2001 METH_NOARGS, float_hex_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2002 {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2003 "Returns True if the float is an integer."},
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
2004 #if 0
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2005 {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2006 "Returns True if the float is positive or negative infinite."},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2007 {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2008 "Returns True if the float is finite, neither infinite nor NaN."},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2009 {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2010 "Returns True if the float is not a number (NaN)."},
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
2011 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2012 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2013 {"__getformat__", (PyCFunction)float_getformat,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2014 METH_O|METH_CLASS, float_getformat_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2015 {"__setformat__", (PyCFunction)float_setformat,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2016 METH_VARARGS|METH_CLASS, float_setformat_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2017 {"__format__", (PyCFunction)float__format__,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2018 METH_VARARGS, float__format__doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2019 {NULL, NULL} /* sentinel */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
2020 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
2021
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
2022 static PyGetSetDef float_getset[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2023 {"real",
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
2024 (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
2025 "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
2026 NULL},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2027 {"imag",
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
2028 (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
2029 "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
2030 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
2031 {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
2032 };
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
2033
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
2034 PyDoc_STRVAR(float_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
2035 "float(x) -> floating point number\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
2036 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
2037 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
2038
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
2039
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2040 static PyNumberMethods float_as_number = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2041 float_add, /*nb_add*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2042 float_sub, /*nb_subtract*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2043 float_mul, /*nb_multiply*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2044 float_classic_div, /*nb_divide*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2045 float_rem, /*nb_remainder*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2046 float_divmod, /*nb_divmod*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2047 float_pow, /*nb_power*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2048 (unaryfunc)float_neg, /*nb_negative*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2049 (unaryfunc)float_float, /*nb_positive*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2050 (unaryfunc)float_abs, /*nb_absolute*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2051 (inquiry)float_nonzero, /*nb_nonzero*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2052 0, /*nb_invert*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2053 0, /*nb_lshift*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2054 0, /*nb_rshift*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2055 0, /*nb_and*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2056 0, /*nb_xor*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2057 0, /*nb_or*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2058 float_coerce, /*nb_coerce*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2059 float_trunc, /*nb_int*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2060 float_long, /*nb_long*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2061 float_float, /*nb_float*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2062 0, /* nb_oct */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2063 0, /* nb_hex */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2064 0, /* nb_inplace_add */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2065 0, /* nb_inplace_subtract */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2066 0, /* nb_inplace_multiply */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2067 0, /* nb_inplace_divide */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2068 0, /* nb_inplace_remainder */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2069 0, /* nb_inplace_power */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2070 0, /* nb_inplace_lshift */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2071 0, /* nb_inplace_rshift */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2072 0, /* nb_inplace_and */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2073 0, /* nb_inplace_xor */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2074 0, /* nb_inplace_or */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2075 float_floor_div, /* nb_floor_divide */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2076 float_div, /* nb_true_divide */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2077 0, /* nb_inplace_floor_divide */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2078 0, /* nb_inplace_true_divide */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2079 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2080
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2081 PyTypeObject PyFloat_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2082 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2083 "float",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2084 sizeof(PyFloatObject),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2085 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2086 (destructor)float_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2087 (printfunc)float_print, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2088 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2089 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2090 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2091 (reprfunc)float_repr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2092 &float_as_number, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2093 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2094 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2095 (hashfunc)float_hash, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2096 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2097 (reprfunc)float_str, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2098 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2099 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2100 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2101 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2102 Py_TPFLAGS_BASETYPE, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2103 float_doc, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2104 0, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2105 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2106 float_richcompare, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2107 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2108 0, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2109 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2110 float_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2111 0, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2112 float_getset, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2113 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2114 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2115 0, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2116 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2117 0, /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2118 0, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2119 0, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2120 float_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2121 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
2122
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
2123 void
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2124 _PyFloat_Init(void)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2125 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2126 /* We attempt to determine if this machine is using IEEE
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2127 floating point formats by peering at the bits of some
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2128 carefully chosen values. If it looks like we are on an
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2129 IEEE platform, the float packing/unpacking routines can
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2130 just copy bits, if not they resort to arithmetic & shifts
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2131 and masks. The shifts & masks approach works on all finite
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2132 values, but what happens to infinities, NaNs and signed
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2133 zeroes on packing is an accident, and attempting to unpack
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2134 a NaN or an infinity will raise an exception.
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2135
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2136 Note that if we're on some whacked-out platform which uses
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2137 IEEE formats but isn't strictly little-endian or big-
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2138 endian, we will fall back to the portable shifts & masks
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2139 method. */
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2140
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2141 #if SIZEOF_DOUBLE == 8
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2142 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2143 double x = 9006104071832581.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2144 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2145 detected_double_format = ieee_big_endian_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2146 else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2147 detected_double_format = ieee_little_endian_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2148 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2149 detected_double_format = unknown_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2150 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2151 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2152 detected_double_format = unknown_format;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2153 #endif
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2154
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2155 #if SIZEOF_FLOAT == 4
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2156 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2157 float y = 16711938.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2158 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2159 detected_float_format = ieee_big_endian_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2160 else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2161 detected_float_format = ieee_little_endian_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2162 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2163 detected_float_format = unknown_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2164 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2165 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2166 detected_float_format = unknown_format;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2167 #endif
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2168
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2169 double_format = detected_double_format;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2170 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
2171
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2172 /* Init float info */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2173 if (FloatInfoType.tp_name == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2174 PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2175 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2176
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47889
diff changeset
2177 int
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47889
diff changeset
2178 PyFloat_ClearFreeList(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
2179 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2180 PyFloatObject *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2181 PyFloatBlock *list, *next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2182 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2183 int u; /* remaining unfreed ints per block */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2184 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
2185
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2186 list = block_list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2187 block_list = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2188 free_list = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2189 while (list != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2190 u = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2191 for (i = 0, p = &list->objects[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2192 i < N_FLOATOBJECTS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2193 i++, p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2194 if (PyFloat_CheckExact(p) && Py_REFCNT(p) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2195 u++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2196 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2197 next = list->next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2198 if (u) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2199 list->next = block_list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2200 block_list = list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2201 for (i = 0, p = &list->objects[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2202 i < N_FLOATOBJECTS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2203 i++, p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2204 if (!PyFloat_CheckExact(p) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2205 Py_REFCNT(p) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2206 Py_TYPE(p) = (struct _typeobject *)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2207 free_list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2208 free_list = p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2209 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2210 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2211 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2212 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2213 PyMem_FREE(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2214 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2215 freelist_size += u;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2216 list = next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2217 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2218 return freelist_size;
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
2219 }
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
2220
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
2221 void
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
2222 PyFloat_Fini(void)
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
2223 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2224 PyFloatObject *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2225 PyFloatBlock *list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2226 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2227 int u; /* total unfreed floats per block */
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
2228
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2229 u = PyFloat_ClearFreeList();
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 45101
diff changeset
2230
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2231 if (!Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2232 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2233 fprintf(stderr, "# cleanup floats");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2234 if (!u) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2235 fprintf(stderr, "\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2236 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2237 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2238 fprintf(stderr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2239 ": %d unfreed float%s\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2240 u, u == 1 ? "" : "s");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2241 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2242 if (Py_VerboseFlag > 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2243 list = block_list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2244 while (list != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2245 for (i = 0, p = &list->objects[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2246 i < N_FLOATOBJECTS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2247 i++, p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2248 if (PyFloat_CheckExact(p) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2249 Py_REFCNT(p) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2250 char *buf = PyOS_double_to_string(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2251 PyFloat_AS_DOUBLE(p), 'r',
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2252 0, 0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2253 if (buf) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2254 /* XXX(twouters) cast
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2255 refcount to long
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2256 until %zd is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2257 universally
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2258 available
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2259 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2260 fprintf(stderr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2261 "# <float at %p, refcnt=%ld, val=%s>\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2262 p, (long)Py_REFCNT(p), buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2263 PyMem_Free(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2264 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2265 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2266 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2267 list = list->next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2268 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2269 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
2270 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2271
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2272 /*----------------------------------------------------------------------------
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2273 * _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
2274 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2275 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2276 _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
2277 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2278 if (float_format == unknown_format) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2279 unsigned char sign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2280 int e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2281 double f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2282 unsigned int fbits;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2283 int incr = 1;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2284
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2285 if (le) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2286 p += 3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2287 incr = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2288 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2289
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2290 if (x < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2291 sign = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2292 x = -x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2293 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2294 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2295 sign = 0;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2296
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2297 f = frexp(x, &e);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2298
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2299 /* Normalize f to be in the range [1.0, 2.0) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2300 if (0.5 <= f && f < 1.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2301 f *= 2.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2302 e--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2303 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2304 else if (f == 0.0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2305 e = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2306 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2307 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2308 "frexp() result out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2309 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2310 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2311
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2312 if (e >= 128)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2313 goto Overflow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2314 else if (e < -126) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2315 /* Gradual underflow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2316 f = ldexp(f, 126 + e);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2317 e = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2318 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2319 else if (!(e == 0 && f == 0.0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2320 e += 127;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2321 f -= 1.0; /* Get rid of leading 1 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2322 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2323
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2324 f *= 8388608.0; /* 2**23 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2325 fbits = (unsigned int)(f + 0.5); /* Round */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2326 assert(fbits <= 8388608);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2327 if (fbits >> 23) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2328 /* The carry propagated out of a string of 23 1 bits. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2329 fbits = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2330 ++e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2331 if (e >= 255)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2332 goto Overflow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2333 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2334
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2335 /* First byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2336 *p = (sign << 7) | (e >> 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2337 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2338
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2339 /* Second byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2340 *p = (char) (((e & 1) << 7) | (fbits >> 16));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2341 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2342
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2343 /* Third byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2344 *p = (fbits >> 8) & 0xFF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2345 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2346
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2347 /* Fourth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2348 *p = fbits & 0xFF;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2349
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2350 /* Done */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2351 return 0;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2352
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2353 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2354 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2355 float y = (float)x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2356 const char *s = (char*)&y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2357 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
2358
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2359 if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2360 goto Overflow;
45759
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2361
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2362 if ((float_format == ieee_little_endian_format && !le)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2363 || (float_format == ieee_big_endian_format && le)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2364 p += 3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2365 incr = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2366 }
45759
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2367
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2368 for (i = 0; i < 4; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2369 *p = *s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2370 p += incr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2371 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2372 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2373 }
45759
354e377c9590 Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
Mark Dickinson <dickinsm@gmail.com>
parents: 45381
diff changeset
2374 Overflow:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2375 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2376 "float too large to pack with f format");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2377 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
2378 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2379
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2380 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2381 _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
2382 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2383 if (double_format == unknown_format) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2384 unsigned char sign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2385 int e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2386 double f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2387 unsigned int fhi, flo;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2388 int incr = 1;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2389
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2390 if (le) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2391 p += 7;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2392 incr = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2393 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2394
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2395 if (x < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2396 sign = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2397 x = -x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2398 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2399 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2400 sign = 0;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2401
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2402 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
2403
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2404 /* Normalize f to be in the range [1.0, 2.0) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2405 if (0.5 <= f && f < 1.0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2406 f *= 2.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2407 e--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2408 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2409 else if (f == 0.0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2410 e = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2411 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2412 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2413 "frexp() result out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2414 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2415 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2416
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2417 if (e >= 1024)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2418 goto Overflow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2419 else if (e < -1022) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2420 /* Gradual underflow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2421 f = ldexp(f, 1022 + e);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2422 e = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2423 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2424 else if (!(e == 0 && f == 0.0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2425 e += 1023;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2426 f -= 1.0; /* Get rid of leading 1 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2427 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2428
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2429 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2430 f *= 268435456.0; /* 2**28 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2431 fhi = (unsigned int)f; /* Truncate */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2432 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
2433
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2434 f -= (double)fhi;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2435 f *= 16777216.0; /* 2**24 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2436 flo = (unsigned int)(f + 0.5); /* Round */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2437 assert(flo <= 16777216);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2438 if (flo >> 24) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2439 /* The carry propagated out of a string of 24 1 bits. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2440 flo = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2441 ++fhi;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2442 if (fhi >> 28) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2443 /* And it also progagated out of the next 28 bits. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2444 fhi = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2445 ++e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2446 if (e >= 2047)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2447 goto Overflow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2448 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2449 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2450
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2451 /* First byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2452 *p = (sign << 7) | (e >> 4);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2453 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2454
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2455 /* Second byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2456 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2457 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
2458
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2459 /* Third byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2460 *p = (fhi >> 16) & 0xFF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2461 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2462
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2463 /* Fourth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2464 *p = (fhi >> 8) & 0xFF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2465 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2466
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2467 /* Fifth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2468 *p = fhi & 0xFF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2469 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2470
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2471 /* Sixth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2472 *p = (flo >> 16) & 0xFF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2473 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2474
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2475 /* Seventh byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2476 *p = (flo >> 8) & 0xFF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2477 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2478
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2479 /* Eighth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2480 *p = flo & 0xFF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2481 /* p += incr; Unneeded (for now) */
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2482
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2483 /* Done */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2484 return 0;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2485
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2486 Overflow:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2487 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2488 "float too large to pack with d format");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2489 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2490 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2491 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2492 const char *s = (char*)&x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2493 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
2494
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2495 if ((double_format == ieee_little_endian_format && !le)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2496 || (double_format == ieee_big_endian_format && le)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2497 p += 7;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2498 incr = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2499 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2500
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2501 for (i = 0; i < 8; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2502 *p = *s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2503 p += incr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2504 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2505 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2506 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2507 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2508
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2509 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2510 _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
2511 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2512 if (float_format == unknown_format) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2513 unsigned char sign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2514 int e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2515 unsigned int f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2516 double x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2517 int incr = 1;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2518
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2519 if (le) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2520 p += 3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2521 incr = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2522 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2523
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2524 /* First byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2525 sign = (*p >> 7) & 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2526 e = (*p & 0x7F) << 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2527 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
2528
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2529 /* Second byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2530 e |= (*p >> 7) & 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2531 f = (*p & 0x7F) << 16;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2532 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
2533
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2534 if (e == 255) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2535 PyErr_SetString(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2536 PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2537 "can't unpack IEEE 754 special value "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2538 "on non-IEEE platform");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2539 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2540 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2541
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2542 /* Third byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2543 f |= *p << 8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2544 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
2545
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2546 /* Fourth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2547 f |= *p;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2548
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2549 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
2550
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2551 /* XXX This sadly ignores Inf/NaN issues */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2552 if (e == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2553 e = -126;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2554 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2555 x += 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2556 e -= 127;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2557 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2558 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
2559
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2560 if (sign)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2561 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
2562
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2563 return x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2564 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2565 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2566 float x;
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2567
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2568 if ((float_format == ieee_little_endian_format && !le)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2569 || (float_format == ieee_big_endian_format && le)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2570 char buf[4];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2571 char *d = &buf[3];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2572 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
2573
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2574 for (i = 0; i < 4; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2575 *d-- = *p++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2576 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2577 memcpy(&x, buf, 4);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2578 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2579 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2580 memcpy(&x, p, 4);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2581 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2582
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2583 return x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2584 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2585 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2586
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2587 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2588 _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
2589 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2590 if (double_format == unknown_format) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2591 unsigned char sign;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2592 int e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2593 unsigned int fhi, flo;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2594 double x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2595 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
2596
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2597 if (le) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2598 p += 7;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2599 incr = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2600 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2601
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2602 /* First byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2603 sign = (*p >> 7) & 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2604 e = (*p & 0x7F) << 4;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2605
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2606 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
2607
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2608 /* Second byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2609 e |= (*p >> 4) & 0xF;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2610 fhi = (*p & 0xF) << 24;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2611 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
2612
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2613 if (e == 2047) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2614 PyErr_SetString(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2615 PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2616 "can't unpack IEEE 754 special value "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2617 "on non-IEEE platform");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2618 return -1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2619 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2620
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2621 /* Third byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2622 fhi |= *p << 16;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2623 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
2624
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2625 /* Fourth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2626 fhi |= *p << 8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2627 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2628
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2629 /* Fifth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2630 fhi |= *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2631 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
2632
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2633 /* Sixth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2634 flo = *p << 16;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2635 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2636
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2637 /* Seventh byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2638 flo |= *p << 8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2639 p += incr;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2640
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2641 /* Eighth byte */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2642 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
2643
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2644 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2645 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
2646
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2647 if (e == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2648 e = -1022;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2649 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2650 x += 1.0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2651 e -= 1023;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2652 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2653 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
2654
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2655 if (sign)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2656 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
2657
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2658 return x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2659 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2660 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2661 double x;
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2662
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2663 if ((double_format == ieee_little_endian_format && !le)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2664 || (double_format == ieee_big_endian_format && le)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2665 char buf[8];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2666 char *d = &buf[7];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2667 int i;
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2668
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2669 for (i = 0; i < 8; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2670 *d-- = *p++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2671 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2672 memcpy(&x, buf, 8);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2673 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2674 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2675 memcpy(&x, p, 8);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2676 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2677
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2678 return x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61323
diff changeset
2679 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2680 }