annotate Objects/floatobject.c @ 48345:cb871f736be0

merge error: test.test_support is now test.support
author Amaury Forgeot d'Arc <amauryfa@gmail.com>
date Thu, 19 Jun 2008 22:03:50 +0000
parents 6a7eb0b70198
children c57d1e954d93
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"
44674
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
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>
44086
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
11 #include <float.h>
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
13 #ifdef HAVE_IEEEFP_H
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
14 #include <ieeefp.h>
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
15 #endif
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
16
60
4f593b38d061 Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents: 46
diff changeset
17
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44674
diff changeset
18 #ifdef _OSF_SOURCE
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44674
diff changeset
19 /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44674
diff changeset
20 extern int finite(double);
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44674
diff changeset
21 #endif
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44674
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. */
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
24 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
25 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
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 {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
29 struct _floatblock *next;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
30 PyFloatObject objects[N_FLOATOBJECTS];
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
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 {
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
41 PyFloatObject *p, *q;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
42 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
43 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
44 if (p == NULL)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
45 return (PyFloatObject *) PyErr_NoMemory();
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
46 ((PyFloatBlock *)p)->next = block_list;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
47 block_list = (PyFloatBlock *)p;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
48 p = &((PyFloatBlock *)p)->objects[0];
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
49 q = p + N_FLOATOBJECTS;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
50 while (--q > p)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
51 Py_TYPE(q) = (struct _typeobject *)(q-1);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
52 Py_TYPE(q) = NULL;
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
53 return p + N_FLOATOBJECTS - 1;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
54 }
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
55
44086
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
56 double
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
57 PyFloat_GetMax(void)
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
58 {
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
59 return DBL_MAX;
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
60 }
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
61
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
62 double
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
63 PyFloat_GetMin(void)
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
64 {
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
65 return DBL_MIN;
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
66 }
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
67
44674
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
68 static PyTypeObject FloatInfoType;
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
69
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
70 PyDoc_STRVAR(floatinfo__doc__,
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
71 "sys.floatinfo\n\
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
72 \n\
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
73 A structseq holding information about the float type. It contains low level\n\
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
74 information about the precision and internal representation. Please study\n\
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
75 your system's :file:`float.h` for more information.");
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
76
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
77 static PyStructSequence_Field floatinfo_fields[] = {
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
78 {"max", "DBL_MAX -- maximum representable finite float"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
79 {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
80 "is representable"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
81 {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e "
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
82 "is representable"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
83 {"min", "DBL_MIN -- Minimum positive normalizer float"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
84 {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
85 "is a normalized float"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
86 {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
87 "a normalized"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
88 {"dig", "DBL_DIG -- digits"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
89 {"mant_dig", "DBL_MANT_DIG -- mantissa digits"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
90 {"epsilon", "DBL_EPSILON -- Difference between 1 and the next "
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
91 "representable float"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
92 {"radix", "FLT_RADIX -- radix of exponent"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
93 {"rounds", "FLT_ROUNDS -- addition rounds"},
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
94 {0}
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
95 };
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
96
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
97 static PyStructSequence_Desc floatinfo_desc = {
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
98 "sys.floatinfo", /* name */
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
99 floatinfo__doc__, /* doc */
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
100 floatinfo_fields, /* fields */
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
101 11
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
102 };
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
103
44086
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
104 PyObject *
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
105 PyFloat_GetInfo(void)
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
106 {
45071
f380d991390e Merged revisions 60441-60474 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45026
diff changeset
107 PyObject* floatinfo;
44674
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
108 int pos = 0;
44086
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
109
44674
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
110 floatinfo = PyStructSequence_New(&FloatInfoType);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
111 if (floatinfo == NULL) {
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
112 return NULL;
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
113 }
44086
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
114
44674
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
115 #define SetIntFlag(flag) \
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
116 PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag))
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
117 #define SetDblFlag(flag) \
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
118 PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
44086
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
119
44674
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
120 SetDblFlag(DBL_MAX);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
121 SetIntFlag(DBL_MAX_EXP);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
122 SetIntFlag(DBL_MAX_10_EXP);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
123 SetDblFlag(DBL_MIN);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
124 SetIntFlag(DBL_MIN_EXP);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
125 SetIntFlag(DBL_MIN_10_EXP);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
126 SetIntFlag(DBL_DIG);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
127 SetIntFlag(DBL_MANT_DIG);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
128 SetDblFlag(DBL_EPSILON);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
129 SetIntFlag(FLT_RADIX);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
130 SetIntFlag(FLT_ROUNDS);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
131 #undef SetIntFlag
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
132 #undef SetDblFlag
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
133
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
134 if (PyErr_Occurred()) {
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
135 Py_CLEAR(floatinfo);
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
136 return NULL;
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
137 }
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
138 return floatinfo;
44086
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
139 }
110ff614ec28 Merged revisions 59245-59254 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43420
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 {
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
144 register PyFloatObject *op;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
145 if (free_list == NULL) {
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
146 if ((free_list = fill_free_list()) == NULL)
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
147 return NULL;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
148 }
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
149 /* Inline PyObject_New */
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
150 op = free_list;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
151 free_list = (PyFloatObject *)Py_TYPE(op);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
152 PyObject_INIT(op, &PyFloat_Type);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
153 op->ob_fval = fval;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
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
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
157 PyObject *
41229
1f843890a4e6 Remove the deprecated and useless "pend" argument from
Georg Brandl <georg@python.org>
parents: 40626
diff changeset
158 PyFloat_FromString(PyObject *v)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
159 {
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
160 const char *s, *last, *end, *sp;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
161 double x;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
162 char buffer[256]; /* for errors */
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
163 char *s_buffer = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36235
diff changeset
164 Py_ssize_t len;
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
165 PyObject *result = NULL;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
166
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
167 if (PyUnicode_Check(v)) {
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
168 s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
169 if (s_buffer == NULL)
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
170 return PyErr_NoMemory();
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
171 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
172 PyUnicode_GET_SIZE(v),
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
173 s_buffer,
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
174 NULL))
42559
e781085ac989 Fix memory leak
Neal Norwitz <nnorwitz@gmail.com>
parents: 42477
diff changeset
175 goto error;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
176 s = s_buffer;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36235
diff changeset
177 len = strlen(s);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
178 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
179 else if (PyObject_AsCharBuffer(v, &s, &len)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
180 PyErr_SetString(PyExc_TypeError,
23344
bffc7236c4ba clarify message when raising TypeError to indicate that float() accepts
Skip Montanaro <skip@pobox.com>
parents: 22228
diff changeset
181 "float() argument must be a string or a number");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
182 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
183 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
184
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
185 last = s + len;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
186 while (*s && isspace(Py_CHARMASK(*s)))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
187 s++;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
188 if (*s == '\0') {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
189 PyErr_SetString(PyExc_ValueError, "empty string for float()");
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
190 goto error;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
191 }
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
192 sp = s;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
193 /* We don't care about overflow or underflow. If the platform supports
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
194 * them, infinities and signed zeroes (on underflow) are fine.
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
195 * However, strtod can return 0 for denormalized numbers, where atof
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
196 * does not. So (alas!) we special-case a zero result. Note that
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
197 * whether strtod sets errno on underflow is not defined, so we can't
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
198 * key off errno.
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
199 */
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
200 PyFPE_START_PROTECT("strtod", goto error)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
201 x = PyOS_ascii_strtod(s, (char **)&end);
14851
5449302c4d2e Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents: 14783
diff changeset
202 PyFPE_END_PROTECT(x)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
203 errno = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
204 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
205 byte at the end of the string, when the input is inf(inity). */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
206 if (end > last)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
207 end = last;
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
208 /* Check for inf and nan. This is done late because it rarely happens. */
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
209 if (end == s) {
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
210 char *p = (char*)sp;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
211 int sign = 1;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
212
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
213 if (*p == '-') {
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
214 sign = -1;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
215 p++;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
216 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
217 if (*p == '+') {
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
218 p++;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
219 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
220 if (PyOS_strnicmp(p, "inf", 4) == 0) {
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
221 Py_RETURN_INF(sign);
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
222 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
223 #ifdef Py_NAN
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
224 if(PyOS_strnicmp(p, "nan", 4) == 0) {
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
225 Py_RETURN_NAN;
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
226 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
227 #endif
21079
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
228 PyOS_snprintf(buffer, sizeof(buffer),
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
229 "invalid literal for float(): %.200s", s);
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
230 PyErr_SetString(PyExc_ValueError, buffer);
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
231 goto error;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
232 }
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
233 /* Since end != s, the platform made *some* kind of sense out
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
234 of the input. Trust it. */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
235 while (*end && isspace(Py_CHARMASK(*end)))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
236 end++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
237 if (*end != '\0') {
21079
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
238 PyOS_snprintf(buffer, sizeof(buffer),
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
239 "invalid literal for float(): %.200s", s);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
240 PyErr_SetString(PyExc_ValueError, buffer);
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
241 goto error;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
242 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
243 else if (end != last) {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
244 PyErr_SetString(PyExc_ValueError,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
245 "null byte in argument for float()");
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
246 goto error;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
247 }
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
248 if (x == 0.0) {
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
249 /* See above -- may have been strtod being anal
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
250 about denorms. */
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
251 PyFPE_START_PROTECT("atof", goto error)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
252 x = PyOS_ascii_atof(s);
14851
5449302c4d2e Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents: 14783
diff changeset
253 PyFPE_END_PROTECT(x)
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
254 errno = 0; /* whether atof ever set errno is undefined */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
255 }
41725
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
256 result = PyFloat_FromDouble(x);
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
257 error:
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
258 if (s_buffer)
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
259 PyMem_FREE(s_buffer);
d1146d4db549 Make tset_float pass. float(<unicode>) was never very good -- it used
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
260 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
261 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
262
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
263 static void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
264 float_dealloc(PyFloatObject *op)
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
265 {
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
266 if (PyFloat_CheckExact(op)) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
267 Py_TYPE(op) = (struct _typeobject *)free_list;
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
268 free_list = op;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
269 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
270 else
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
271 Py_TYPE(op)->tp_free((PyObject *)op);
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
272 }
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
273
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 double
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
275 PyFloat_AsDouble(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
277 PyNumberMethods *nb;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
278 PyFloatObject *fo;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
279 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
280
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
281 if (op && PyFloat_Check(op))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
282 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
283
26272
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
284 if (op == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
285 PyErr_BadArgument();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
288
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
289 if ((nb = Py_TYPE(op)->tp_as_number) == NULL || nb->nb_float == NULL) {
26272
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
290 PyErr_SetString(PyExc_TypeError, "a float is required");
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
291 return -1;
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
292 }
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
293
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
294 fo = (PyFloatObject*) (*nb->nb_float) (op);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
295 if (fo == NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
296 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
297 if (!PyFloat_Check(fo)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
298 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
299 "nb_float should return float object");
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
300 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
301 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
302
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
303 val = PyFloat_AS_DOUBLE(fo);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
304 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
305
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
306 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
311 static void
40527
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
312 format_double(char *buf, size_t buflen, double ob_fval, int precision)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 register char *cp;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
315 char format[32];
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
316 int i;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
317
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
318 /* Subroutine for float_repr, float_str and float_print.
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 We want float numbers to be recognizable as such,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 i.e., they should contain a decimal point or an exponent.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 However, %g may print the number as an integer;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 in such cases, we append ".0" to the string. */
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
323
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
324 PyOS_snprintf(format, 32, "%%.%ig", precision);
40527
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
325 PyOS_ascii_formatd(buf, buflen, format, ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 cp = buf;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 if (*cp == '-')
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 cp++;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 for (; *cp != '\0'; cp++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 /* Any non-digit means it's not an integer;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 this takes care of NAN and INF as well. */
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2156
diff changeset
332 if (!isdigit(Py_CHARMASK(*cp)))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 break;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 if (*cp == '\0') {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 *cp++ = '.';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 *cp++ = '0';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 *cp++ = '\0';
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
339 return;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 }
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
341 /* Checking the next three chars should be more than enough to
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
342 * detect inf or nan, even on Windows. We check for inf or nan
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
343 * at last because they are rare cases.
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
344 */
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
345 for (i=0; *cp != '\0' && i<3; cp++, i++) {
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
346 if (isdigit(Py_CHARMASK(*cp)) || *cp == '.')
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
347 continue;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
348 /* found something that is neither a digit nor point
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
349 * it might be a NaN or INF
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
350 */
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
351 #ifdef Py_NAN
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
352 if (Py_IS_NAN(ob_fval)) {
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
353 strcpy(buf, "nan");
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
354 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
355 else
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
356 #endif
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
357 if (Py_IS_INFINITY(ob_fval)) {
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
358 cp = buf;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
359 if (*cp == '-')
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
360 cp++;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
361 strcpy(cp, "inf");
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
362 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
363 break;
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
364 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44268
diff changeset
365
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
40527
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
368 static void
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
369 format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
370 {
40527
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
371 assert(PyFloat_Check(v));
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
372 format_double(buf, buflen, PyFloat_AS_DOUBLE(v), precision);
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
373 }
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
374
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
375 /* Macro and helper that convert PyObject obj to a C double and store
43420
0ff41b163f5b Remove more cruft leftover from nb_coerce. Rename nb_coerce to
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 43284
diff changeset
376 the value in dbl. If conversion to double raises an exception, obj is
21354
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
377 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
378 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
379 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
380 */
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
381 #define CONVERT_TO_DOUBLE(obj, dbl) \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
382 if (PyFloat_Check(obj)) \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
383 dbl = PyFloat_AS_DOUBLE(obj); \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
384 else if (convert_to_double(&(obj), &(dbl)) < 0) \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
385 return obj;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
386
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
387 static int
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
388 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
389 {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
390 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
391
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40527
diff changeset
392 if (PyLong_Check(obj)) {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
393 *dbl = PyLong_AsDouble(obj);
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
394 if (*dbl == -1.0 && PyErr_Occurred()) {
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
395 *v = NULL;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
396 return -1;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
397 }
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
398 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
399 else {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
400 Py_INCREF(Py_NotImplemented);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
401 *v = Py_NotImplemented;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
402 return -1;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
403 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
404 return 0;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
405 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
406
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
407 /* Precisions used by repr() and str(), respectively.
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
408
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
409 The repr() precision (17 significant decimal digits) is the minimal number
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
410 that is guaranteed to have enough precision so that if the number is read
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
411 back in the exact same binary value is recreated. This is true for IEEE
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
412 floating point by design, and also happens to work for all other modern
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
413 hardware.
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
414
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
415 The str() precision is chosen so that in most cases, the rounding noise
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
416 created by various operations is suppressed, while giving plenty of
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
417 precision for practical use.
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
418
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
419 */
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
420
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
421 #define PREC_REPR 17
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
422 #define PREC_STR 12
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
423
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
424 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
425 float_repr(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 {
44268
7ae604b3533b Merged revisions 59450-59464 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44260
diff changeset
427 char buf[100];
7ae604b3533b Merged revisions 59450-59464 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44260
diff changeset
428 format_float(buf, sizeof(buf), v, PREC_REPR);
7ae604b3533b Merged revisions 59450-59464 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44260
diff changeset
429
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41725
diff changeset
430 return PyUnicode_FromString(buf);
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
431 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
432
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
433 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
434 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
435 {
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
436 char buf[100];
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
437 format_float(buf, sizeof(buf), v, PREC_STR);
41920
71d861ee9ff0 Change float.__str__() and complex.__str__() to return
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
438 return PyUnicode_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
441 /* 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
442 * 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
443 * 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
444 * 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
445 * 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
446 * 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
447 * 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
448 * 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
449 * 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
450 * 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
451 * 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
452 * 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
453 * 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
454 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
455
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
456 static PyObject*
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
457 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
458 {
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
459 double i, j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
460 int r = 0;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
461
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
462 assert(PyFloat_Check(v));
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
463 i = PyFloat_AS_DOUBLE(v);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
464
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
465 /* Switch on the type of w. Set i and j to doubles to be compared,
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
466 * and op to the richcomp to use.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
467 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
468 if (PyFloat_Check(w))
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
469 j = PyFloat_AS_DOUBLE(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
470
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
471 else if (!Py_IS_FINITE(i)) {
43124
727539865f04 Remove checking redundantly for checks of PyInt and PyLong.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43104
diff changeset
472 if (PyLong_Check(w))
33549
ecaf0e8fa737 float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents: 33547
diff changeset
473 /* If i is an infinity, its magnitude exceeds any
ecaf0e8fa737 float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents: 33547
diff changeset
474 * finite integer, so it doesn't matter which int we
ecaf0e8fa737 float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents: 33547
diff changeset
475 * compare i with. If i is a NaN, similarly.
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
476 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
477 j = 0.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
478 else
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
479 goto Unimplemented;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
480 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
481
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
482 else if (PyLong_Check(w)) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
483 int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
484 int wsign = _PyLong_Sign(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
485 size_t nbits;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
486 int exponent;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
487
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
488 if (vsign != wsign) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
489 /* Magnitudes are irrelevant -- the signs alone
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
490 * determine the outcome.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
491 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
492 i = (double)vsign;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
493 j = (double)wsign;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
494 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
495 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
496 /* The signs are the same. */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
497 /* Convert w to a double if it fits. In particular, 0 fits. */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
498 nbits = _PyLong_NumBits(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
499 if (nbits == (size_t)-1 && PyErr_Occurred()) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
500 /* This long is so large that size_t isn't big enough
33549
ecaf0e8fa737 float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents: 33547
diff changeset
501 * to hold the # of bits. Replace with little doubles
ecaf0e8fa737 float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents: 33547
diff changeset
502 * that give the same outcome -- w is so large that
ecaf0e8fa737 float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents: 33547
diff changeset
503 * its magnitude must exceed the magnitude of any
ecaf0e8fa737 float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
Tim Peters <tim.peters@gmail.com>
parents: 33547
diff changeset
504 * finite float.
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
505 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
506 PyErr_Clear();
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
507 i = (double)vsign;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
508 assert(wsign != 0);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
509 j = wsign * 2.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
510 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
511 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
512 if (nbits <= 48) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
513 j = PyLong_AsDouble(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
514 /* It's impossible that <= 48 bits overflowed. */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
515 assert(j != -1.0 || ! PyErr_Occurred());
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
516 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
517 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
518 assert(wsign != 0); /* else nbits was 0 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
519 assert(vsign != 0); /* if vsign were 0, then since wsign is
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
520 * not 0, we would have taken the
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
521 * vsign != wsign branch at the start */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
522 /* We want to work with non-negative numbers. */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
523 if (vsign < 0) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
524 /* "Multiply both sides" by -1; this also swaps the
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
525 * comparator.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
526 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
527 i = -i;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
528 op = _Py_SwappedOp[op];
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
529 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
530 assert(i > 0.0);
36235
9f66bdd5a061 Fix icc warnings: remove unused variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
531 (void) frexp(i, &exponent);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
532 /* exponent is the # of bits in v before the radix point;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
533 * we know that nbits (the # of bits in w) > 48 at this point
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
534 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
535 if (exponent < 0 || (size_t)exponent < nbits) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
536 i = 1.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
537 j = 2.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
538 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
539 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
540 if ((size_t)exponent > nbits) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
541 i = 2.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
542 j = 1.0;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
543 goto Compare;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
544 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
545 /* v and w have the same number of bits before the radix
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
546 * point. Construct two longs that have the same comparison
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
547 * outcome.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
548 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
549 {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
550 double fracpart;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
551 double intpart;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
552 PyObject *result = NULL;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
553 PyObject *one = NULL;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
554 PyObject *vv = NULL;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
555 PyObject *ww = w;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
556
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
557 if (wsign < 0) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
558 ww = PyNumber_Negative(w);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
559 if (ww == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
560 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
561 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
562 else
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
563 Py_INCREF(ww);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
564
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
565 fracpart = modf(i, &intpart);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
566 vv = PyLong_FromDouble(intpart);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
567 if (vv == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
568 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
569
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
570 if (fracpart != 0.0) {
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
571 /* Shift left, and or a 1 bit into vv
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
572 * to represent the lost fraction.
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
573 */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
574 PyObject *temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
575
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44086
diff changeset
576 one = PyLong_FromLong(1);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
577 if (one == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
578 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
579
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
580 temp = PyNumber_Lshift(ww, one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
581 if (temp == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
582 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
583 Py_DECREF(ww);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
584 ww = temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
585
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
586 temp = PyNumber_Lshift(vv, one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
587 if (temp == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
588 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
589 Py_DECREF(vv);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
590 vv = temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
591
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
592 temp = PyNumber_Or(vv, one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
593 if (temp == NULL)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
594 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
595 Py_DECREF(vv);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
596 vv = temp;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
597 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
598
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
599 r = PyObject_RichCompareBool(vv, ww, op);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
600 if (r < 0)
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
601 goto Error;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
602 result = PyBool_FromLong(r);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
603 Error:
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
604 Py_XDECREF(vv);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
605 Py_XDECREF(ww);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
606 Py_XDECREF(one);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
607 return result;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
608 }
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
609 } /* else if (PyLong_Check(w)) */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
610
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
611 else /* w isn't float, int, or long */
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
612 goto Unimplemented;
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
613
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
614 Compare:
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
615 PyFPE_START_PROTECT("richcompare", return NULL)
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
616 switch (op) {
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
617 case Py_EQ:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
618 r = i == j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
619 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
620 case Py_NE:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
621 r = i != j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
622 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
623 case Py_LE:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
624 r = i <= j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
625 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
626 case Py_GE:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
627 r = i >= j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
628 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
629 case Py_LT:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
630 r = i < j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
631 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
632 case Py_GT:
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
633 r = i > j;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
634 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
635 }
31589
ca3c998afa55 Pass a variable that actually exists to PyFPE_END_PROTECT in
Michael W. Hudson <mwh@python.net>
parents: 31553
diff changeset
636 PyFPE_END_PROTECT(r)
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
637 return PyBool_FromLong(r);
33547
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
638
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
639 Unimplemented:
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
640 Py_INCREF(Py_NotImplemented);
702367da2c72 SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents: 32533
diff changeset
641 return Py_NotImplemented;
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
642 }
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
643
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
644 static long
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
645 float_hash(PyFloatObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
646 {
13976
5ada9fa54801 Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents: 13944
diff changeset
647 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
648 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
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_add(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
653 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
654 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
655 CONVERT_TO_DOUBLE(w, b);
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
656 PyFPE_START_PROTECT("add", return 0)
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
657 a = a + b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
658 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
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_sub(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
665 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
666 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
667 CONVERT_TO_DOUBLE(w, b);
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
668 PyFPE_START_PROTECT("subtract", return 0)
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
669 a = a - b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
670 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
671 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
673
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
674 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
675 float_mul(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
677 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
678 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
679 CONVERT_TO_DOUBLE(w, b);
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
680 PyFPE_START_PROTECT("multiply", return 0)
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
681 a = a * b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
682 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
683 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
685
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
686 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
687 float_div(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
689 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
690 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
691 CONVERT_TO_DOUBLE(w, b);
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
692 #ifdef Py_NAN
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
693 if (b == 0.0) {
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
694 PyErr_SetString(PyExc_ZeroDivisionError,
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
695 "float division");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 }
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
698 #endif
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
699 PyFPE_START_PROTECT("divide", return 0)
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
700 a = a / b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
701 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
702 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
704
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
705 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
706 float_rem(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
708 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
709 double mod;
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
710 CONVERT_TO_DOUBLE(v, vx);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
711 CONVERT_TO_DOUBLE(w, wx);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
712 #ifdef Py_NAN
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
713 if (wx == 0.0) {
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
714 PyErr_SetString(PyExc_ZeroDivisionError,
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
715 "float modulo");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 }
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
718 #endif
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
719 PyFPE_START_PROTECT("modulo", return 0)
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
720 mod = fmod(vx, wx);
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
721 /* note: checking mod*wx < 0 is incorrect -- underflows to
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
722 0 if wx < sqrt(smallest nonzero double) */
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
723 if (mod && ((wx < 0) != (mod < 0))) {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
724 mod += wx;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 }
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
726 PyFPE_END_PROTECT(mod)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
727 return PyFloat_FromDouble(mod);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
729
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
730 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
731 float_divmod(PyObject *v, PyObject *w)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
732 {
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
733 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
734 double div, mod, floordiv;
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
735 CONVERT_TO_DOUBLE(v, vx);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
736 CONVERT_TO_DOUBLE(w, wx);
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
737 if (wx == 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
738 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
739 return NULL;
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
740 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
741 PyFPE_START_PROTECT("divmod", return 0)
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
742 mod = fmod(vx, wx);
14616
284508c653c7 Fixed legit gripe from c.l.py that math.fmod docs aren't confusing enough.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
743 /* fmod is typically exact, so vx-mod is *mathematically* an
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
744 exact multiple of wx. But this is fp arithmetic, and fp
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
745 vx - mod is an approximation; the result is that div may
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
746 not be an exact integral value after the division, although
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
747 it will always be very close to one.
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
748 */
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
749 div = (vx - mod) / wx;
20827
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
750 if (mod) {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
751 /* ensure the remainder has the same sign as the denominator */
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
752 if ((wx < 0) != (mod < 0)) {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
753 mod += wx;
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
754 div -= 1.0;
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
755 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
756 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
757 else {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
758 /* the remainder is zero, and in the presence of signed zeroes
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
759 fmod returns different results across platforms; ensure
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
760 it has the same sign as the denominator; we'd like to do
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
761 "mod = wx * 0.0", but that may get optimized away */
20829
14dc693af532 float_divmod(): the code wasn't sick enough to stop the MS optimizer
Tim Peters <tim.peters@gmail.com>
parents: 20827
diff changeset
762 mod *= mod; /* hide "mod = +0" from optimizer */
20827
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
763 if (wx < 0.0)
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
764 mod = -mod;
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
765 }
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
766 /* snap quotient to nearest integral value */
20827
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
767 if (div) {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
768 floordiv = floor(div);
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
769 if (div - floordiv > 0.5)
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
770 floordiv += 1.0;
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
771 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
772 else {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
773 /* div is zero - get the same sign as the true quotient */
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
774 div *= div; /* hide "div = +0" from optimizers */
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
775 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
776 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
777 PyFPE_END_PROTECT(floordiv)
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
778 return Py_BuildValue("(dd)", floordiv, mod);
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
779 }
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
780
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
781 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
782 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
783 {
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
784 PyObject *t, *r;
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
785
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
786 t = float_divmod(v, w);
21354
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
787 if (t == NULL || t == Py_NotImplemented)
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
788 return t;
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
789 assert(PyTuple_CheckExact(t));
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
790 r = PyTuple_GET_ITEM(t, 0);
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
791 Py_INCREF(r);
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
792 Py_DECREF(t);
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
793 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
794 }
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
795
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
796 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
797 float_pow(PyObject *v, PyObject *w, PyObject *z)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 double iv, iw, ix;
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
800
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
801 if ((PyObject *)z != Py_None) {
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
802 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
19753
c0580d8c193e Better error msg for 3-arg pow with a float argument.
Tim Peters <tim.peters@gmail.com>
parents: 19750
diff changeset
803 "allowed unless all arguments are integers");
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
804 return NULL;
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
805 }
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
806
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
807 CONVERT_TO_DOUBLE(v, iv);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
808 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
809
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
810 /* Sort out special cases here instead of relying on pow() */
19457
6c2b7b624ef2 float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
811 if (iw == 0) { /* v**0 is 1, even 0**0 */
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
812 return PyFloat_FromDouble(1.0);
15041
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
813 }
19457
6c2b7b624ef2 float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
814 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
15041
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
815 if (iw < 0.0) {
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
816 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
817 "0.0 cannot be raised to a negative power");
15041
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
818 return NULL;
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
819 }
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
820 return PyFloat_FromDouble(0.0);
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
821 }
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
822 if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
823 return PyFloat_FromDouble(1.0);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
824 }
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
825 if (iv < 0.0) {
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
826 /* Whether this is an error is a mess, and bumps into libm
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
827 * bugs so we have to figure it out ourselves.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
828 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
829 if (iw != floor(iw)) {
43284
1f9eee08ab35 Check in some documentation tweaks for PEP 3141, add some tests, and implement
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43124
diff changeset
830 /* Negative numbers raised to fractional powers
1f9eee08ab35 Check in some documentation tweaks for PEP 3141, add some tests, and implement
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43124
diff changeset
831 * become complex.
1f9eee08ab35 Check in some documentation tweaks for PEP 3141, add some tests, and implement
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43124
diff changeset
832 */
1f9eee08ab35 Check in some documentation tweaks for PEP 3141, add some tests, and implement
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43124
diff changeset
833 return PyComplex_Type.tp_as_number->nb_power(v, w, z);
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
834 }
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
835 /* iw is an exact integer, albeit perhaps a very large one.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
836 * -1 raised to an exact integer should never be exceptional.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
837 * Alas, some libms (chiefly glibc as of early 2003) return
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
838 * NaN and set EDOM on pow(-1, large_int) if the int doesn't
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
839 * happen to be representable in a *C* integer. That's a
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
840 * bug; we let that slide in math.pow() (which currently
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
841 * reflects all platform accidents), but not for Python's **.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
842 */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
843 if (iv == -1.0 && Py_IS_FINITE(iw)) {
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
844 /* Return 1 if iw is even, -1 if iw is odd; there's
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
845 * no guarantee that any C integral type is big
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
846 * enough to hold iw, so we have to check this
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
847 * indirectly.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
848 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
849 ix = floor(iw * 0.5) * 2.0;
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
850 return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
851 }
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
852 /* Else iv != -1.0, and overflow or underflow are possible.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
853 * Unless we're to write pow() ourselves, we have to trust
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
854 * the platform to do this correctly.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
855 */
3984
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
856 }
19457
6c2b7b624ef2 float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
857 errno = 0;
6c2b7b624ef2 float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
858 PyFPE_START_PROTECT("pow", return NULL)
6c2b7b624ef2 float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
859 ix = pow(iv, iw);
6c2b7b624ef2 float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
860 PyFPE_END_PROTECT(ix)
22228
4b6eda7d0c8f backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21354
diff changeset
861 Py_ADJUST_ERANGE1(ix);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
862 if (errno != 0) {
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
863 /* We don't expect any errno value other than ERANGE, but
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
864 * the range of libm bugs appears unbounded.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
865 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
866 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
867 PyExc_ValueError);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 return NULL;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
869 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
870 return PyFloat_FromDouble(ix);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
871 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
872
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
873 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
874 float_neg(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
875 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
876 return PyFloat_FromDouble(-v->ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
877 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
878
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
879 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
880 float_abs(PyFloatObject *v)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
881 {
20826
3751366c2f39 float_abs() again: Guido pointed out that this could screw up in the
Tim Peters <tim.peters@gmail.com>
parents: 20823
diff changeset
882 return PyFloat_FromDouble(fabs(v->ob_fval));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
883 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
884
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
885 static int
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39603
diff changeset
886 float_bool(PyFloatObject *v)
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
887 {
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
888 return v->ob_fval != 0.0;
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
889 }
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
890
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
891 static PyObject *
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
892 float_is_integer(PyObject *v)
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
893 {
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
894 double x = PyFloat_AsDouble(v);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
895 PyObject *o;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
896
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
897 if (x == -1.0 && PyErr_Occurred())
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
898 return NULL;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
899 if (!Py_IS_FINITE(x))
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
900 Py_RETURN_FALSE;
46790
17cd612b06da Issue 2801: fix bug in float.is_integer where ValueError was incorrectly
Mark Dickinson <dickinsm@gmail.com>
parents: 46477
diff changeset
901 errno = 0;
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
902 PyFPE_START_PROTECT("is_integer", return NULL)
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
903 o = (floor(x) == x) ? Py_True : Py_False;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
904 PyFPE_END_PROTECT(x)
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
905 if (errno != 0) {
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
906 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
907 PyExc_ValueError);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
908 return NULL;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
909 }
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
910 Py_INCREF(o);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
911 return o;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
912 }
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
913
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
914 #if 0
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
915 static PyObject *
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
916 float_is_inf(PyObject *v)
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
917 {
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
918 double x = PyFloat_AsDouble(v);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
919 if (x == -1.0 && PyErr_Occurred())
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
920 return NULL;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
921 return PyBool_FromLong((long)Py_IS_INFINITY(x));
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
922 }
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
923
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
924 static PyObject *
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
925 float_is_nan(PyObject *v)
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
926 {
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
927 double x = PyFloat_AsDouble(v);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
928 if (x == -1.0 && PyErr_Occurred())
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
929 return NULL;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
930 return PyBool_FromLong((long)Py_IS_NAN(x));
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
931 }
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
932
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
933 static PyObject *
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
934 float_is_finite(PyObject *v)
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
935 {
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
936 double x = PyFloat_AsDouble(v);
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
937 if (x == -1.0 && PyErr_Occurred())
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
938 return NULL;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
939 return PyBool_FromLong((long)Py_IS_FINITE(x));
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
940 }
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
941 #endif
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
942
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
943 static PyObject *
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
944 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
945 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
946 double x = PyFloat_AsDouble(v);
18838
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
947 double wholepart; /* integral portion of x, rounded toward 0 */
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
948
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
949 (void)modf(x, &wholepart);
26328
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
950 /* Try to get out cheap if this fits in a Python int. The attempt
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
951 * to cast to long must be protected, as C doesn't define what
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
952 * happens if the double is too big to fit in a long. Some rare
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
953 * systems raise an exception then (RISCOS was mentioned as one,
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
954 * and someone using a non-default option on Sun also bumped into
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
955 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
956 * still be vulnerable: if a long has more bits of precision than
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
957 * a double, casting MIN/MAX to double may yield an approximation,
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
958 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
959 * yield true from the C expression wholepart<=LONG_MAX, despite
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
960 * that wholepart is actually greater than LONG_MAX.
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
961 */
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
962 if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
963 const long aslong = (long)wholepart;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44086
diff changeset
964 return PyLong_FromLong(aslong);
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
965 }
26328
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
966 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
967 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
968
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
969 static PyObject *
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
970 float_round(PyObject *v, PyObject *args)
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
971 {
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
972 #define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
973 double x;
43104
dd6cf00c1794 Use pow() instead of repeated multiplication by 10 in round(x, n).
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
974 double f = 1.0;
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
975 double flr, cil;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
976 double rounded;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
977 int ndigits = UNDEF_NDIGITS;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
978
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
979 if (!PyArg_ParseTuple(args, "|i", &ndigits))
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
980 return NULL;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
981
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
982 x = PyFloat_AsDouble(v);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
983
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
984 if (ndigits != UNDEF_NDIGITS) {
43104
dd6cf00c1794 Use pow() instead of repeated multiplication by 10 in round(x, n).
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
985 f = pow(10.0, ndigits);
dd6cf00c1794 Use pow() instead of repeated multiplication by 10 in round(x, n).
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
986 x *= f;
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
987 }
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
988
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
989 flr = floor(x);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
990 cil = ceil(x);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
991
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
992 if (x-flr > 0.5)
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
993 rounded = cil;
43104
dd6cf00c1794 Use pow() instead of repeated multiplication by 10 in round(x, n).
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
994 else if (x-flr == 0.5)
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
995 rounded = fmod(flr, 2) == 0 ? flr : cil;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
996 else
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
997 rounded = flr;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
998
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
999 if (ndigits != UNDEF_NDIGITS) {
43104
dd6cf00c1794 Use pow() instead of repeated multiplication by 10 in round(x, n).
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
1000 rounded /= f;
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1001 return PyFloat_FromDouble(rounded);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1002 }
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1003
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1004 return PyLong_FromDouble(rounded);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1005 #undef UNDEF_NDIGITS
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1006 }
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1007
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1008 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1009 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
1010 {
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33549
diff changeset
1011 if (PyFloat_CheckExact(v))
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33549
diff changeset
1012 Py_INCREF(v);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33549
diff changeset
1013 else
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33549
diff changeset
1014 v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1015 return v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1016 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1017
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1018 static PyObject *
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1019 float_as_integer_ratio(PyObject *v, PyObject *unused)
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1020 {
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1021 double self;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1022 double float_part;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1023 int exponent;
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1024 int i;
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1025
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1026 PyObject *prev;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1027 PyObject *py_exponent = NULL;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1028 PyObject *numerator = NULL;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1029 PyObject *denominator = NULL;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1030 PyObject *result_pair = NULL;
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1031 PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1032
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1033 #define INPLACE_UPDATE(obj, call) \
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1034 prev = obj; \
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1035 obj = call; \
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1036 Py_DECREF(prev); \
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1037
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1038 CONVERT_TO_DOUBLE(v, self);
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1039
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1040 if (Py_IS_INFINITY(self)) {
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1041 PyErr_SetString(PyExc_OverflowError,
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1042 "Cannot pass infinity to float.as_integer_ratio.");
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1043 return NULL;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1044 }
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1045 #ifdef Py_NAN
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1046 if (Py_IS_NAN(self)) {
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1047 PyErr_SetString(PyExc_ValueError,
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1048 "Cannot pass nan to float.as_integer_ratio.");
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1049 return NULL;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1050 }
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1051 #endif
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1052
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1053 PyFPE_START_PROTECT("as_integer_ratio", goto error);
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1054 float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1055 PyFPE_END_PROTECT(float_part);
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1056
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1057 for (i=0; i<300 && float_part != floor(float_part) ; i++) {
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1058 float_part *= 2.0;
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1059 exponent--;
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1060 }
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1061 /* self == float_part * 2**exponent exactly and float_part is integral.
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1062 If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1063 to be truncated by PyLong_FromDouble(). */
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1064
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1065 numerator = PyLong_FromDouble(float_part);
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1066 if (numerator == NULL) goto error;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1067
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1068 /* fold in 2**exponent */
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1069 denominator = PyLong_FromLong(1);
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1070 py_exponent = PyLong_FromLong(labs((long)exponent));
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1071 if (py_exponent == NULL) goto error;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1072 INPLACE_UPDATE(py_exponent,
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1073 long_methods->nb_lshift(denominator, py_exponent));
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1074 if (py_exponent == NULL) goto error;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1075 if (exponent > 0) {
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1076 INPLACE_UPDATE(numerator,
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1077 long_methods->nb_multiply(numerator, py_exponent));
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1078 if (numerator == NULL) goto error;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1079 }
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1080 else {
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1081 Py_DECREF(denominator);
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1082 denominator = py_exponent;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1083 py_exponent = NULL;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1084 }
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1085
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1086 /* Returns ints instead of longs where possible */
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1087 INPLACE_UPDATE(numerator, PyNumber_Int(numerator));
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1088 if (numerator == NULL) goto error;
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1089 INPLACE_UPDATE(denominator, PyNumber_Int(denominator));
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1090 if (denominator == NULL) goto error;
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1091
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1092 result_pair = PyTuple_Pack(2, numerator, denominator);
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1093
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1094 #undef INPLACE_UPDATE
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1095 error:
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1096 Py_XDECREF(py_exponent);
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1097 Py_XDECREF(denominator);
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1098 Py_XDECREF(numerator);
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1099 return result_pair;
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1100 }
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1101
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1102 PyDoc_STRVAR(float_as_integer_ratio_doc,
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1103 "float.as_integer_ratio() -> (int, int)\n"
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1104 "\n"
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1105 "Returns a pair of integers, whose ratio is exactly equal to the original\n"
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1106 "float and with a positive denominator.\n"
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1107 "Raises OverflowError on infinities and a ValueError on nans.\n"
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1108 "\n"
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1109 ">>> (10.0).as_integer_ratio()\n"
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1110 "(10, 1)\n"
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1111 ">>> (0.0).as_integer_ratio()\n"
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1112 "(0, 1)\n"
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1113 ">>> (-.25).as_integer_ratio()\n"
45134
bdcf40d1b38e Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1114 "(-1, 4)");
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1115
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1116
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
1117 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1118 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
1119
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1120 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1121 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
1122 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1123 PyObject *x = Py_False; /* Integer zero */
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1124 static char *kwlist[] = {"x", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1125
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1126 if (type != &PyFloat_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1127 return float_subtype_new(type, args, kwds); /* Wimp out */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1128 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1129 return NULL;
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1130 if (PyUnicode_Check(x))
41229
1f843890a4e6 Remove the deprecated and useless "pend" argument from
Georg Brandl <georg@python.org>
parents: 40626
diff changeset
1131 return PyFloat_FromString(x);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1132 return PyNumber_Float(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1133 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1134
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1135 /* 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
1136 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
1137 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
1138 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
1139 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1140 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1141 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
1142 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1143 PyObject *tmp, *newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1144
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1145 assert(PyType_IsSubtype(type, &PyFloat_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1146 tmp = float_new(&PyFloat_Type, args, kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1147 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1148 return NULL;
19889
733314fcfdaa Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19879
diff changeset
1149 assert(PyFloat_CheckExact(tmp));
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1150 newobj = type->tp_alloc(type, 0);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1151 if (newobj == NULL) {
29641
4909aa64405f SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents: 29252
diff changeset
1152 Py_DECREF(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1153 return NULL;
29641
4909aa64405f SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents: 29252
diff changeset
1154 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1155 ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1156 Py_DECREF(tmp);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1157 return newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1158 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
1159
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1160 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1161 float_getnewargs(PyFloatObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1162 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1163 return Py_BuildValue("(d)", v->ob_fval);
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1164 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1165
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1166 /* 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
1167
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1168 typedef enum {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1169 unknown_format, ieee_big_endian_format, ieee_little_endian_format
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1170 } float_format_type;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1171
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1172 static float_format_type double_format, float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1173 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
1174
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1175 static PyObject *
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1176 float_getformat(PyTypeObject *v, PyObject* arg)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1177 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1178 char* s;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1179 float_format_type r;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1180
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1181 if (!PyUnicode_Check(arg)) {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1182 PyErr_Format(PyExc_TypeError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1183 "__getformat__() argument must be string, not %.500s",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
1184 Py_TYPE(arg)->tp_name);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1185 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1186 }
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1187 s = PyUnicode_AsString(arg);
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1188 if (s == NULL)
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1189 return NULL;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1190 if (strcmp(s, "double") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1191 r = double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1192 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1193 else if (strcmp(s, "float") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1194 r = float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1195 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1196 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1197 PyErr_SetString(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1198 "__getformat__() argument 1 must be "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1199 "'double' or 'float'");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1200 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1201 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1202
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1203 switch (r) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1204 case unknown_format:
42126
8fc93215c0cc Change float.__getformat__() to return a unicode string.
Walter Dörwald <walter@livinglogic.de>
parents: 41920
diff changeset
1205 return PyUnicode_FromString("unknown");
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1206 case ieee_little_endian_format:
42126
8fc93215c0cc Change float.__getformat__() to return a unicode string.
Walter Dörwald <walter@livinglogic.de>
parents: 41920
diff changeset
1207 return PyUnicode_FromString("IEEE, little-endian");
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1208 case ieee_big_endian_format:
42126
8fc93215c0cc Change float.__getformat__() to return a unicode string.
Walter Dörwald <walter@livinglogic.de>
parents: 41920
diff changeset
1209 return PyUnicode_FromString("IEEE, big-endian");
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1210 default:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1211 Py_FatalError("insane float_format or double_format");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1212 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1213 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1214 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1215
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1216 PyDoc_STRVAR(float_getformat_doc,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1217 "float.__getformat__(typestr) -> string\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1218 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1219 "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
1220 "used in Python's test suite.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1221 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1222 "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
1223 "'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
1224 "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
1225
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1226 static PyObject *
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1227 float_setformat(PyTypeObject *v, PyObject* args)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1228 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1229 char* typestr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1230 char* format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1231 float_format_type f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1232 float_format_type detected;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1233 float_format_type *p;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1234
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1235 if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format))
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1236 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1237
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1238 if (strcmp(typestr, "double") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1239 p = &double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1240 detected = detected_double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1241 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1242 else if (strcmp(typestr, "float") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1243 p = &float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1244 detected = detected_float_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1245 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1246 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1247 PyErr_SetString(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1248 "__setformat__() argument 1 must "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1249 "be 'double' or 'float'");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1250 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1251 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1252
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1253 if (strcmp(format, "unknown") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1254 f = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1255 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1256 else if (strcmp(format, "IEEE, little-endian") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1257 f = ieee_little_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1258 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1259 else if (strcmp(format, "IEEE, big-endian") == 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1260 f = ieee_big_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1261 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1262 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1263 PyErr_SetString(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1264 "__setformat__() argument 2 must be "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1265 "'unknown', 'IEEE, little-endian' or "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1266 "'IEEE, big-endian'");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1267 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1268
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1269 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1270
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1271 if (f != unknown_format && f != detected) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1272 PyErr_Format(PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1273 "can only set %s format to 'unknown' or the "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1274 "detected platform value", typestr);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1275 return NULL;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1276 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1277
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1278 *p = f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1279 Py_RETURN_NONE;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1280 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1281
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1282 PyDoc_STRVAR(float_setformat_doc,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1283 "float.__setformat__(typestr, fmt) -> None\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1284 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1285 "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
1286 "used in Python's test suite.\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1287 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1288 "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
1289 "'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
1290 "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
1291 "\n"
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1292 "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
1293 "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
1294
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1295 static PyObject *
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1296 float_getzero(PyObject *v, void *closure)
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1297 {
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1298 return PyFloat_FromDouble(0.0);
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1299 }
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1300
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1301 static PyObject *
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1302 float__format__(PyObject *self, PyObject *args)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1303 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 46790
diff changeset
1304 PyObject *format_spec;
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 46790
diff changeset
1305
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 46790
diff changeset
1306 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 46790
diff changeset
1307 return NULL;
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 46790
diff changeset
1308 return _PyFloat_FormatAdvanced(self,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 46790
diff changeset
1309 PyUnicode_AS_UNICODE(format_spec),
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 46790
diff changeset
1310 PyUnicode_GET_SIZE(format_spec));
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1311 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1312
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1313 PyDoc_STRVAR(float__format__doc,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1314 "float.__format__(format_spec) -> string\n"
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1315 "\n"
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1316 "Formats the float according to format_spec.");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1317
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1318
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1319 static PyMethodDef float_methods[] = {
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1320 {"conjugate", (PyCFunction)float_float, METH_NOARGS,
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1321 "Returns self, the complex conjugate of any float."},
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1322 {"__trunc__", (PyCFunction)float_trunc, METH_NOARGS,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1323 "Returns the Integral closest to x between 0 and x."},
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1324 {"__round__", (PyCFunction)float_round, METH_VARARGS,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1325 "Returns the Integral closest to x, rounding half toward even.\n"
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1326 "When an argument is passed, works like built-in round(x, ndigits)."},
45026
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1327 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS,
ebfbaa1051e2 Merged revisions 60364-60378 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1328 float_as_integer_ratio_doc},
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1329 {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS,
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1330 "Returns True if the float is an integer."},
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1331 #if 0
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1332 {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS,
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1333 "Returns True if the float is positive or negative infinite."},
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1334 {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS,
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1335 "Returns True if the float is finite, neither infinite nor NaN."},
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1336 {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS,
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1337 "Returns True if the float is not a number (NaN)."},
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46161
diff changeset
1338 #endif
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1339 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1340 {"__getformat__", (PyCFunction)float_getformat,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1341 METH_O|METH_CLASS, float_getformat_doc},
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1342 {"__setformat__", (PyCFunction)float_setformat,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1343 METH_VARARGS|METH_CLASS, float_setformat_doc},
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1344 {"__format__", (PyCFunction)float__format__,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
1345 METH_VARARGS, float__format__doc},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1346 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1347 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1348
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1349 static PyGetSetDef float_getset[] = {
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1350 {"real",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1351 (getter)float_float, (setter)NULL,
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1352 "the real part of a complex number",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1353 NULL},
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1354 {"imag",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1355 (getter)float_getzero, (setter)NULL,
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1356 "the imaginary part of a complex number",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1357 NULL},
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1358 {NULL} /* Sentinel */
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1359 };
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1360
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
1361 PyDoc_STRVAR(float_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1362 "float(x) -> floating point number\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1363 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
1364 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
1365
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1366
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1367 static PyNumberMethods float_as_number = {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1368 float_add, /*nb_add*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1369 float_sub, /*nb_subtract*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1370 float_mul, /*nb_multiply*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1371 float_rem, /*nb_remainder*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1372 float_divmod, /*nb_divmod*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1373 float_pow, /*nb_power*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
1374 (unaryfunc)float_neg, /*nb_negative*/
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1375 (unaryfunc)float_float, /*nb_positive*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
1376 (unaryfunc)float_abs, /*nb_absolute*/
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39603
diff changeset
1377 (inquiry)float_bool, /*nb_bool*/
405
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1378 0, /*nb_invert*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1379 0, /*nb_lshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1380 0, /*nb_rshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1381 0, /*nb_and*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1382 0, /*nb_xor*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
1383 0, /*nb_or*/
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1384 float_trunc, /*nb_int*/
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42559
diff changeset
1385 float_trunc, /*nb_long*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1386 float_float, /*nb_float*/
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1387 0, /* nb_inplace_add */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1388 0, /* nb_inplace_subtract */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1389 0, /* nb_inplace_multiply */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1390 0, /* nb_inplace_remainder */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1391 0, /* nb_inplace_power */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1392 0, /* nb_inplace_lshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1393 0, /* nb_inplace_rshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1394 0, /* nb_inplace_and */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1395 0, /* nb_inplace_xor */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1396 0, /* nb_inplace_or */
21353
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
1397 float_floor_div, /* nb_floor_divide */
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1398 float_div, /* nb_true_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1399 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1400 0, /* nb_inplace_true_divide */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1401 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1402
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1403 PyTypeObject PyFloat_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42126
diff changeset
1404 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1405 "float",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1406 sizeof(PyFloatObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1408 (destructor)float_dealloc, /* tp_dealloc */
42477
e7fd327e0c19 Kill all uses and definitions of tp_print under Objects/. (Others will follow.)
Guido van Rossum <guido@python.org>
parents: 42417
diff changeset
1409 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1410 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1411 0, /* tp_setattr */
32150
65f9ad9bc829 Remove float_compare as per
Michael W. Hudson <mwh@python.net>
parents: 31590
diff changeset
1412 0, /* tp_compare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1413 (reprfunc)float_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1414 &float_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1415 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1416 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1417 (hashfunc)float_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1418 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1419 (reprfunc)float_str, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1420 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1421 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1422 0, /* tp_as_buffer */
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
1423 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1424 float_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1425 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1426 0, /* tp_clear */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1427 float_richcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1428 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1429 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1430 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
1431 float_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1432 0, /* tp_members */
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1433 float_getset, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1434 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1435 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1436 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1437 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1438 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1439 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1440 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
1441 float_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1442 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
1443
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
1444 void
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1445 _PyFloat_Init(void)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1446 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1447 /* We attempt to determine if this machine is using IEEE
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1448 floating point formats by peering at the bits of some
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1449 carefully chosen values. If it looks like we are on an
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1450 IEEE platform, the float packing/unpacking routines can
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1451 just copy bits, if not they resort to arithmetic & shifts
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1452 and masks. The shifts & masks approach works on all finite
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1453 values, but what happens to infinities, NaNs and signed
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1454 zeroes on packing is an accident, and attempting to unpack
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1455 a NaN or an infinity will raise an exception.
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1456
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1457 Note that if we're on some whacked-out platform which uses
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1458 IEEE formats but isn't strictly little-endian or big-
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1459 endian, we will fall back to the portable shifts & masks
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1460 method. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1461
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1462 #if SIZEOF_DOUBLE == 8
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1463 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1464 double x = 9006104071832581.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1465 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1466 detected_double_format = ieee_big_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1467 else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1468 detected_double_format = ieee_little_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1469 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1470 detected_double_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1471 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1472 #else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1473 detected_double_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1474 #endif
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1475
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1476 #if SIZEOF_FLOAT == 4
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1477 {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1478 float y = 16711938.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1479 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1480 detected_float_format = ieee_big_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1481 else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1482 detected_float_format = ieee_little_endian_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1483 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1484 detected_float_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1485 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1486 #else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1487 detected_float_format = unknown_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1488 #endif
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1489
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1490 double_format = detected_double_format;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1491 float_format = detected_float_format;
44268
7ae604b3533b Merged revisions 59450-59464 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44260
diff changeset
1492
45071
f380d991390e Merged revisions 60441-60474 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45026
diff changeset
1493 /* Init float info */
f380d991390e Merged revisions 60441-60474 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45026
diff changeset
1494 if (FloatInfoType.tp_name == 0)
f380d991390e Merged revisions 60441-60474 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45026
diff changeset
1495 PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1496 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1497
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1498 void
45145
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1499 PyFloat_CompactFreeList(size_t *pbc, size_t *pbf, size_t *bsum)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
1500 {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1501 PyFloatObject *p;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1502 PyFloatBlock *list, *next;
32533
201986d6d9d6 Fix a couple of signed/unsigned comparison warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
1503 unsigned i;
45145
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1504 size_t bc = 0, bf = 0; /* block count, number of freed blocks */
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1505 size_t fsum = 0; /* total unfreed ints */
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1506 int frem; /* remaining unfreed ints per block */
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
1507
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1508 list = block_list;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1509 block_list = NULL;
10432
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1510 free_list = NULL;
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1511 while (list != NULL) {
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1512 bc++;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1513 frem = 0;
10432
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1514 for (i = 0, p = &list->objects[0];
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1515 i < N_FLOATOBJECTS;
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1516 i++, p++) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
1517 if (PyFloat_CheckExact(p) && Py_REFCNT(p) != 0)
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1518 frem++;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1519 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1520 next = list->next;
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1521 if (frem) {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1522 list->next = block_list;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1523 block_list = list;
10432
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1524 for (i = 0, p = &list->objects[0];
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1525 i < N_FLOATOBJECTS;
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1526 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19753
diff changeset
1527 if (!PyFloat_CheckExact(p) ||
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
1528 Py_REFCNT(p) == 0) {
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
1529 Py_TYPE(p) = (struct _typeobject *)
10432
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1530 free_list;
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1531 free_list = p;
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1532 }
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1533 }
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
1534 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1535 else {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1536 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1537 bf++;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1538 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1539 fsum += frem;
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1540 list = next;
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1541 }
45145
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1542 *pbc = bc;
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1543 *pbf = bf;
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1544 *bsum = fsum;
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1545 }
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1546
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1547 void
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1548 PyFloat_Fini(void)
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1549 {
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1550 PyFloatObject *p;
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1551 PyFloatBlock *list;
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1552 unsigned i;
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1553 size_t bc, bf; /* block count, number of freed blocks */
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1554 size_t fsum; /* total unfreed floats per block */
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1555
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1556 PyFloat_CompactFreeList(&bc, &bf, &fsum);
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1557
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1558 if (!Py_VerboseFlag)
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1559 return;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1560 fprintf(stderr, "# cleanup floats");
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1561 if (!fsum) {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1562 fprintf(stderr, "\n");
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1563 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1564 else {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1565 fprintf(stderr,
46145
da5597a5c195 Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45773
diff changeset
1566 ": %" PY_FORMAT_SIZE_T "d unfreed float%s in %"
45145
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1567 PY_FORMAT_SIZE_T "d out of %"
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45134
diff changeset
1568 PY_FORMAT_SIZE_T "d block%s\n",
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1569 fsum, fsum == 1 ? "" : "s",
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1570 bc - bf, bc, bc == 1 ? "" : "s");
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1571 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1572 if (Py_VerboseFlag > 1) {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1573 list = block_list;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1574 while (list != NULL) {
10432
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1575 for (i = 0, p = &list->objects[0];
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1576 i < N_FLOATOBJECTS;
1cc5d20d2ed8 Fix a problem with Vladimir's PyFloat_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1577 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19753
diff changeset
1578 if (PyFloat_CheckExact(p) &&
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
1579 Py_REFCNT(p) != 0) {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1580 char buf[100];
40527
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1581 format_float(buf, sizeof(buf), p, PREC_STR);
36759
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1582 /* XXX(twouters) cast refcount to
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1583 long until %zd is universally
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1584 available
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1585 */
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1586 fprintf(stderr,
36759
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1587 "# <float at %p, refcnt=%ld, val=%s>\n",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
1588 p, (long)Py_REFCNT(p), buf);
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1589 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1590 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
1591 list = list->next;
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1592 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
1593 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
1594 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1595
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1596 /*----------------------------------------------------------------------------
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1597 * _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
1598 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1599 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1600 _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
1601 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1602 if (float_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1603 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1604 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1605 double f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1606 unsigned int fbits;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1607 int incr = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1608
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1609 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1610 p += 3;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1611 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1612 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1613
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1614 if (x < 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1615 sign = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1616 x = -x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1617 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1618 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1619 sign = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1620
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1621 f = frexp(x, &e);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1622
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1623 /* Normalize f to be in the range [1.0, 2.0) */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1624 if (0.5 <= f && f < 1.0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1625 f *= 2.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1626 e--;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1627 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1628 else if (f == 0.0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1629 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1630 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1631 PyErr_SetString(PyExc_SystemError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1632 "frexp() result out of range");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1633 return -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1634 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1635
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1636 if (e >= 128)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1637 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1638 else if (e < -126) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1639 /* Gradual underflow */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1640 f = ldexp(f, 126 + e);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1641 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1642 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1643 else if (!(e == 0 && f == 0.0)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1644 e += 127;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1645 f -= 1.0; /* Get rid of leading 1 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1646 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1647
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1648 f *= 8388608.0; /* 2**23 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1649 fbits = (unsigned int)(f + 0.5); /* Round */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1650 assert(fbits <= 8388608);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1651 if (fbits >> 23) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1652 /* The carry propagated out of a string of 23 1 bits. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1653 fbits = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1654 ++e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1655 if (e >= 255)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1656 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1657 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1658
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1659 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1660 *p = (sign << 7) | (e >> 1);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1661 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1662
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1663 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1664 *p = (char) (((e & 1) << 7) | (fbits >> 16));
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1665 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1666
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1667 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1668 *p = (fbits >> 8) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1669 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1670
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1671 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1672 *p = fbits & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1673
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1674 /* Done */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1675 return 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1676
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1677 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1678 else {
35178
cf7a849ce61f Apparently some compiler gives a warning on
Michael W. Hudson <mwh@python.net>
parents: 34972
diff changeset
1679 float y = (float)x;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1680 const char *s = (char*)&y;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1681 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
1682
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1683 if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1684 goto Overflow;
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1685
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1686 if ((float_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1687 || (float_format == ieee_big_endian_format && le)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1688 p += 3;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1689 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1690 }
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1691
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1692 for (i = 0; i < 4; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1693 *p = *s++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1694 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1695 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1696 return 0;
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1697 }
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1698 Overflow:
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1699 PyErr_SetString(PyExc_OverflowError,
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1700 "float too large to pack with f format");
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1701 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
1702 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1703
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1704 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1705 _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
1706 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1707 if (double_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1708 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1709 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1710 double f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1711 unsigned int fhi, flo;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1712 int incr = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1713
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1714 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1715 p += 7;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1716 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1717 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1718
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1719 if (x < 0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1720 sign = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1721 x = -x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1722 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1723 else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1724 sign = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1725
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1726 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
1727
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1728 /* Normalize f to be in the range [1.0, 2.0) */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1729 if (0.5 <= f && f < 1.0) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1730 f *= 2.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1731 e--;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1732 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1733 else if (f == 0.0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1734 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1735 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1736 PyErr_SetString(PyExc_SystemError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1737 "frexp() result out of range");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1738 return -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1739 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1740
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1741 if (e >= 1024)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1742 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1743 else if (e < -1022) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1744 /* Gradual underflow */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1745 f = ldexp(f, 1022 + e);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1746 e = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1747 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1748 else if (!(e == 0 && f == 0.0)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1749 e += 1023;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1750 f -= 1.0; /* Get rid of leading 1 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1751 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1752
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1753 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1754 f *= 268435456.0; /* 2**28 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1755 fhi = (unsigned int)f; /* Truncate */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1756 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
1757
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1758 f -= (double)fhi;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1759 f *= 16777216.0; /* 2**24 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1760 flo = (unsigned int)(f + 0.5); /* Round */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1761 assert(flo <= 16777216);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1762 if (flo >> 24) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1763 /* The carry propagated out of a string of 24 1 bits. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1764 flo = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1765 ++fhi;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1766 if (fhi >> 28) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1767 /* And it also progagated out of the next 28 bits. */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1768 fhi = 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1769 ++e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1770 if (e >= 2047)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1771 goto Overflow;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1772 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1773 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1774
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1775 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1776 *p = (sign << 7) | (e >> 4);
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1777 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1778
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1779 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1780 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1781 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
1782
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1783 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1784 *p = (fhi >> 16) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1785 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1786
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1787 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1788 *p = (fhi >> 8) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1789 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1790
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1791 /* Fifth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1792 *p = fhi & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1793 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1794
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1795 /* Sixth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1796 *p = (flo >> 16) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1797 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1798
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1799 /* Seventh byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1800 *p = (flo >> 8) & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1801 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1802
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1803 /* Eighth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1804 *p = flo & 0xFF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1805 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1806
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1807 /* Done */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1808 return 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1809
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1810 Overflow:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1811 PyErr_SetString(PyExc_OverflowError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1812 "float too large to pack with d format");
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1813 return -1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1814 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1815 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1816 const char *s = (char*)&x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1817 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
1818
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1819 if ((double_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1820 || (double_format == ieee_big_endian_format && le)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1821 p += 7;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1822 incr = -1;
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
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1825 for (i = 0; i < 8; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1826 *p = *s++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1827 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1828 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1829 return 0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1830 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1831 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1832
40527
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1833 /* Should only be used by marshal. */
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1834 int
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1835 _PyFloat_Repr(double x, char *p, size_t len)
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1836 {
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1837 format_double(p, len, x, PREC_REPR);
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1838 return (int)strlen(p);
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1839 }
195f689af5af Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
Neal Norwitz <nnorwitz@gmail.com>
parents: 40401
diff changeset
1840
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1841 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1842 _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
1843 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1844 if (float_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1845 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1846 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1847 unsigned int f;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1848 double x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1849 int incr = 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1850
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1851 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1852 p += 3;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1853 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1854 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1855
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1856 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1857 sign = (*p >> 7) & 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1858 e = (*p & 0x7F) << 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1859 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
1860
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1861 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1862 e |= (*p >> 7) & 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1863 f = (*p & 0x7F) << 16;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1864 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
1865
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1866 if (e == 255) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1867 PyErr_SetString(
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1868 PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1869 "can't unpack IEEE 754 special value "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1870 "on non-IEEE platform");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1871 return -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1872 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1873
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1874 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1875 f |= *p << 8;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1876 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
1877
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1878 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1879 f |= *p;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1880
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1881 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
1882
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1883 /* XXX This sadly ignores Inf/NaN issues */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1884 if (e == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1885 e = -126;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1886 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1887 x += 1.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1888 e -= 127;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1889 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1890 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
1891
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1892 if (sign)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1893 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
1894
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1895 return x;
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1896 }
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1897 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1898 float x;
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1899
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1900 if ((float_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1901 || (float_format == ieee_big_endian_format && le)) {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1902 char buf[4];
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1903 char *d = &buf[3];
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1904 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
1905
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1906 for (i = 0; i < 4; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1907 *d-- = *p++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1908 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1909 memcpy(&x, buf, 4);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1910 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1911 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1912 memcpy(&x, p, 4);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1913 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1914
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1915 return x;
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1916 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1917 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1918
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1919 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1920 _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
1921 {
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1922 if (double_format == unknown_format) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1923 unsigned char sign;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1924 int e;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1925 unsigned int fhi, flo;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1926 double x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1927 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
1928
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1929 if (le) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1930 p += 7;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1931 incr = -1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1932 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1933
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1934 /* First byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1935 sign = (*p >> 7) & 1;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1936 e = (*p & 0x7F) << 4;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1937
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1938 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
1939
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1940 /* Second byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1941 e |= (*p >> 4) & 0xF;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1942 fhi = (*p & 0xF) << 24;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1943 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
1944
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1945 if (e == 2047) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1946 PyErr_SetString(
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1947 PyExc_ValueError,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1948 "can't unpack IEEE 754 special value "
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1949 "on non-IEEE platform");
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1950 return -1.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1951 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1952
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1953 /* Third byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1954 fhi |= *p << 16;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1955 p += incr;
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1956
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1957 /* Fourth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1958 fhi |= *p << 8;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1959 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1960
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1961 /* Fifth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1962 fhi |= *p;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1963 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
1964
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1965 /* Sixth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1966 flo = *p << 16;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1967 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1968
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1969 /* Seventh byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1970 flo |= *p << 8;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1971 p += incr;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1973 /* Eighth byte */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1974 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
1975
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1976 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1977 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
1978
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1979 if (e == 0)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1980 e = -1022;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1981 else {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1982 x += 1.0;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1983 e -= 1023;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1984 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1985 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
1986
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1987 if (sign)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1988 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
1989
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1990 return x;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1991 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1992 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1993 double x;
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
1994
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1995 if ((double_format == ieee_little_endian_format && !le)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1996 || (double_format == ieee_big_endian_format && le)) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1997 char buf[8];
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1998 char *d = &buf[7];
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
1999 int i;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2000
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2001 for (i = 0; i < 8; i++) {
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2002 *d-- = *p++;
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2003 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2004 memcpy(&x, buf, 8);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2005 }
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2006 else {
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2007 memcpy(&x, p, 8);
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2008 }
35987
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2009
8eda1a58a490 Fix bug
Michael W. Hudson <mwh@python.net>
parents: 35178
diff changeset
2010 return x;
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2011 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
2012 }