annotate Objects/floatobject.c @ 32305:13656d58d450 legacy-trunk

Patch #774665: Make Python LC_NUMERIC agnostic.
author Martin v. Löwis <martin@v.loewis.de>
date Tue, 08 Jun 2004 18:52:54 +0000
parents 65f9ad9bc829
children 201986d6d9d6
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"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 60
diff changeset
9 #include <ctype.h>
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
30979
86fdf8fe7f17 Getting rid of all the code inside #ifdef macintosh too.
Jack Jansen <jack.jansen@cwi.nl>
parents: 29641
diff changeset
11 #if !defined(__STDC__)
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
12 extern double fmod(double, double);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
13 extern double pow(double, double);
60
4f593b38d061 Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents: 46
diff changeset
14 #endif
4f593b38d061 Moved prototyes for fmod and pow, and don't use them for THINK C.
Guido van Rossum <guido@python.org>
parents: 46
diff changeset
15
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
16 /* Special free list -- see comments for same code in intobject.c. */
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
17 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
18 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
19 #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
20
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
21 struct _floatblock {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
22 struct _floatblock *next;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
23 PyFloatObject objects[N_FLOATOBJECTS];
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
24 };
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
25
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
26 typedef struct _floatblock PyFloatBlock;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
27
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
28 static PyFloatBlock *block_list = NULL;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
29 static PyFloatObject *free_list = NULL;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
30
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
31 static PyFloatObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
32 fill_free_list(void)
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
33 {
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
34 PyFloatObject *p, *q;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
35 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
36 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
37 if (p == NULL)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
38 return (PyFloatObject *) PyErr_NoMemory();
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
39 ((PyFloatBlock *)p)->next = block_list;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
40 block_list = (PyFloatBlock *)p;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
41 p = &((PyFloatBlock *)p)->objects[0];
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
42 q = p + N_FLOATOBJECTS;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
43 while (--q > p)
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
44 q->ob_type = (struct _typeobject *)(q-1);
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
45 q->ob_type = NULL;
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
46 return p + N_FLOATOBJECTS - 1;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
47 }
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
48
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
49 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
50 PyFloat_FromDouble(double fval)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 {
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
52 register PyFloatObject *op;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
53 if (free_list == NULL) {
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
54 if ((free_list = fill_free_list()) == NULL)
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
55 return NULL;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
56 }
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
57 /* Inline PyObject_New */
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
58 op = free_list;
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
59 free_list = (PyFloatObject *)op->ob_type;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
60 PyObject_INIT(op, &PyFloat_Type);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
61 op->ob_fval = fval;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
62 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
65 /**************************************************************************
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
66 RED_FLAG 22-Sep-2000 tim
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
67 PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
68
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
69 1. If v was a regular string, *pend was set to point to its terminating
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
70 null byte. That's useless (the caller can find that without any
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
71 help from this function!).
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
72
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
73 2. If v was a Unicode string, or an object convertible to a character
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
74 buffer, *pend was set to point into stack trash (the auto temp
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
75 vector holding the character buffer). That was downright dangerous.
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
76
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
77 Since we can't change the interface of a public API function, pend is
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
78 still supported but now *officially* useless: if pend is not NULL,
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
79 *pend is set to NULL.
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
80 **************************************************************************/
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
81 PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
82 PyFloat_FromString(PyObject *v, char **pend)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
83 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
84 const char *s, *last, *end;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
85 double x;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
86 char buffer[256]; /* for errors */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
87 #ifdef Py_USING_UNICODE
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
88 char s_buffer[256]; /* for objects convertible to a char buffer */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
89 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
90 int len;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
91
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
92 if (pend)
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
93 *pend = NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
94 if (PyString_Check(v)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
95 s = PyString_AS_STRING(v);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
96 len = PyString_GET_SIZE(v);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
97 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
98 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
99 else if (PyUnicode_Check(v)) {
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
100 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
101 PyErr_SetString(PyExc_ValueError,
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
102 "Unicode float() literal too long to convert");
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
103 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
104 }
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
105 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
106 PyUnicode_GET_SIZE(v),
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
107 s_buffer,
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
108 NULL))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
109 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
110 s = s_buffer;
13944
648ba4044455 Clean up a warning on Win64. The downcast of the strlen size_t
Trent Mick <trentm@activestate.com>
parents: 13761
diff changeset
111 len = (int)strlen(s);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
112 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
113 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
114 else if (PyObject_AsCharBuffer(v, &s, &len)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
115 PyErr_SetString(PyExc_TypeError,
23344
bffc7236c4ba clarify message when raising TypeError to indicate that float() accepts
Skip Montanaro <skip@pobox.com>
parents: 22228
diff changeset
116 "float() argument must be a string or a number");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
117 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
118 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
119
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
120 last = s + len;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
121 while (*s && isspace(Py_CHARMASK(*s)))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
122 s++;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
123 if (*s == '\0') {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
124 PyErr_SetString(PyExc_ValueError, "empty string for float()");
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
125 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
126 }
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
127 /* We don't care about overflow or underflow. If the platform supports
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
128 * them, infinities and signed zeroes (on underflow) are fine.
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
129 * However, strtod can return 0 for denormalized numbers, where atof
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
130 * does not. So (alas!) we special-case a zero result. Note that
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
131 * whether strtod sets errno on underflow is not defined, so we can't
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
132 * key off errno.
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
133 */
14851
5449302c4d2e Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents: 14783
diff changeset
134 PyFPE_START_PROTECT("strtod", return NULL)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
135 x = PyOS_ascii_strtod(s, (char **)&end);
14851
5449302c4d2e Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents: 14783
diff changeset
136 PyFPE_END_PROTECT(x)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
137 errno = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
138 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
139 byte at the end of the string, when the input is inf(inity). */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
140 if (end > last)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
141 end = last;
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
142 if (end == s) {
21079
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
143 PyOS_snprintf(buffer, sizeof(buffer),
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
144 "invalid literal for float(): %.200s", s);
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
145 PyErr_SetString(PyExc_ValueError, buffer);
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
146 return NULL;
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
147 }
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
148 /* Since end != s, the platform made *some* kind of sense out
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
149 of the input. Trust it. */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
150 while (*end && isspace(Py_CHARMASK(*end)))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
151 end++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
152 if (*end != '\0') {
21079
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
153 PyOS_snprintf(buffer, sizeof(buffer),
8cef75cacb01 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
Barry Warsaw <barry@python.org>
parents: 20829
diff changeset
154 "invalid literal for float(): %.200s", s);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
155 PyErr_SetString(PyExc_ValueError, buffer);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
156 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
157 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
158 else if (end != last) {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
159 PyErr_SetString(PyExc_ValueError,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
160 "null byte in argument for float()");
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
161 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
162 }
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
163 if (x == 0.0) {
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
164 /* See above -- may have been strtod being anal
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
165 about denorms. */
14851
5449302c4d2e Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents: 14783
diff changeset
166 PyFPE_START_PROTECT("atof", return NULL)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
167 x = PyOS_ascii_atof(s);
14851
5449302c4d2e Replace SIGFPE paranoia around strtod and atof. I don't believe these
Tim Peters <tim.peters@gmail.com>
parents: 14783
diff changeset
168 PyFPE_END_PROTECT(x)
14783
77eddddbb7cc Fix for SF bug 110624: float literals behave inconsistently.
Tim Peters <tim.peters@gmail.com>
parents: 14616
diff changeset
169 errno = 0; /* whether atof ever set errno is undefined */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
170 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
171 return PyFloat_FromDouble(x);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
172 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
173
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
174 static void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
175 float_dealloc(PyFloatObject *op)
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
176 {
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
177 if (PyFloat_CheckExact(op)) {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
178 op->ob_type = (struct _typeobject *)free_list;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
179 free_list = op;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
180 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
181 else
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19976
diff changeset
182 op->ob_type->tp_free((PyObject *)op);
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
183 }
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
184
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 double
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
186 PyFloat_AsDouble(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
188 PyNumberMethods *nb;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
189 PyFloatObject *fo;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
190 double val;
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
191
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
192 if (op && PyFloat_Check(op))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
193 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
194
26272
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
195 if (op == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
196 PyErr_BadArgument();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
199
26272
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
200 if ((nb = op->ob_type->tp_as_number) == NULL || nb->nb_float == NULL) {
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
201 PyErr_SetString(PyExc_TypeError, "a float is required");
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
202 return -1;
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
203 }
fbccef8b3153 Improve exception message raised by PyFloat_AsDouble if the object does not
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24898
diff changeset
204
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
205 fo = (PyFloatObject*) (*nb->nb_float) (op);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
206 if (fo == NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
207 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
208 if (!PyFloat_Check(fo)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
209 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
210 "nb_float should return float object");
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
211 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
212 }
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
213
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
214 val = PyFloat_AS_DOUBLE(fo);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
215 Py_DECREF(fo);
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
216
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
217 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
222 static void
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
223 format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 register char *cp;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
226 char format[32];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 /* Subroutine for float_repr and float_print.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 We want float numbers to be recognizable as such,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 i.e., they should contain a decimal point or an exponent.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 However, %g may print the number as an integer;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 in such cases, we append ".0" to the string. */
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
232
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
233 assert(PyFloat_Check(v));
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
234 PyOS_snprintf(format, 32, "%%.%ig", precision);
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32150
diff changeset
235 PyOS_ascii_formatd(buf, buflen, format, v->ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 cp = buf;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 if (*cp == '-')
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 cp++;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 for (; *cp != '\0'; cp++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 /* Any non-digit means it's not an integer;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 this takes care of NAN and INF as well. */
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2156
diff changeset
242 if (!isdigit(Py_CHARMASK(*cp)))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 break;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 if (*cp == '\0') {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 *cp++ = '.';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 *cp++ = '0';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 *cp++ = '\0';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
252 /* XXX PyFloat_AsStringEx should not be a public API function (for one
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
253 XXX thing, its signature passes a buffer without a length; for another,
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
254 XXX it isn't useful outside this file).
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
255 */
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
256 void
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
257 PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
258 {
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
259 format_float(buf, 100, v, precision);
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
260 }
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
261
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
262 /* Macro and helper that convert PyObject obj to a C double and store
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
263 the value in dbl; this replaces the functionality of the coercion
21354
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
264 slot function. If conversion to double raises an exception, obj is
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
265 set to NULL, and the function invoking this macro returns NULL. If
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
266 obj is not of float, int or long type, Py_NotImplemented is incref'ed,
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
267 stored in obj, and returned from the function invoking this macro.
fbddb64d21c2 float_floor_div: An expression like 3.//1j crashed the interpreter, or
Tim Peters <tim.peters@gmail.com>
parents: 21353
diff changeset
268 */
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
269 #define CONVERT_TO_DOUBLE(obj, dbl) \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
270 if (PyFloat_Check(obj)) \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
271 dbl = PyFloat_AS_DOUBLE(obj); \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
272 else if (convert_to_double(&(obj), &(dbl)) < 0) \
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
273 return obj;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
274
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
275 static int
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
276 convert_to_double(PyObject **v, double *dbl)
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
277 {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
278 register PyObject *obj = *v;
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
279
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
280 if (PyInt_Check(obj)) {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
281 *dbl = (double)PyInt_AS_LONG(obj);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
282 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
283 else if (PyLong_Check(obj)) {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
284 *dbl = PyLong_AsDouble(obj);
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
285 if (*dbl == -1.0 && PyErr_Occurred()) {
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
286 *v = NULL;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
287 return -1;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19673
diff changeset
288 }
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
289 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
290 else {
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
291 Py_INCREF(Py_NotImplemented);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
292 *v = Py_NotImplemented;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
293 return -1;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
294 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
295 return 0;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
296 }
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
297
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
298 /* Precisions used by repr() and str(), respectively.
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
299
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
300 The repr() precision (17 significant decimal digits) is the minimal number
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
301 that is guaranteed to have enough precision so that if the number is read
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
302 back in the exact same binary value is recreated. This is true for IEEE
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
303 floating point by design, and also happens to work for all other modern
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
304 hardware.
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
305
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
306 The str() precision is chosen so that in most cases, the rounding noise
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
307 created by various operations is suppressed, while giving plenty of
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
308 precision for practical use.
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
309
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
310 */
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
311
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
312 #define PREC_REPR 17
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
313 #define PREC_STR 12
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
314
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
315 /* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
316 XXX they pass a char buffer without passing a length.
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
317 */
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
318 void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
319 PyFloat_AsString(char *buf, PyFloatObject *v)
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
320 {
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
321 format_float(buf, 100, v, PREC_STR);
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
322 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
323
17707
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
324 void
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
325 PyFloat_AsReprString(char *buf, PyFloatObject *v)
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
326 {
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
327 format_float(buf, 100, v, PREC_REPR);
17707
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
328 }
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17029
diff changeset
329
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
330 /* ARGSUSED */
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 268
diff changeset
331 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
332 float_print(PyFloatObject *v, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 char buf[100];
21097
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
335 format_float(buf, sizeof(buf), v,
9ba3c82946b8 PyFloat_AsStringEx(): This function takes an output char* but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21079
diff changeset
336 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 fputs(buf, fp);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 268
diff changeset
338 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
341 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
342 float_repr(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 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
345 format_float(buf, sizeof(buf), v, PREC_REPR);
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
346 return PyString_FromString(buf);
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
347 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
348
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
349 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
350 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
351 {
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
352 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
353 format_float(buf, sizeof(buf), v, PREC_STR);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
354 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
357 static PyObject*
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
358 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
359 {
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
360 double i, j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
361 int r = 0;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
362
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
363 CONVERT_TO_DOUBLE(v, i);
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
364 CONVERT_TO_DOUBLE(w, j);
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
365
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
366 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
367 switch (op) {
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
368 case Py_EQ:
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
369 r = i==j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
370 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
371 case Py_NE:
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
372 r = i!=j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
373 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
374 case Py_LE:
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
375 r = i<=j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
376 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
377 case Py_GE:
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
378 r = i>=j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
379 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
380 case Py_LT:
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
381 r = i<j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
382 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
383 case Py_GT:
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
384 r = i>j;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
385 break;
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
386 }
31589
ca3c998afa55 Pass a variable that actually exists to PyFPE_END_PROTECT in
Michael W. Hudson <mwh@python.net>
parents: 31553
diff changeset
387 PyFPE_END_PROTECT(r)
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
388 return PyBool_FromLong(r);
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
389 }
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
390
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
391 static long
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
392 float_hash(PyFloatObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
393 {
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
394 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
395 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
396
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
397 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
398 float_add(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
400 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
401 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
402 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
403 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
404 a = a + b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
405 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
406 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
408
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
409 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
410 float_sub(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
412 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
413 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
414 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
415 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
416 a = a - b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
417 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
418 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
421 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
422 float_mul(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
424 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
425 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
426 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
427 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
428 a = a * b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
429 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
430 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
433 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
434 float_div(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
436 double a,b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
437 CONVERT_TO_DOUBLE(v, a);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
438 CONVERT_TO_DOUBLE(w, b);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
439 if (b == 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
440 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
443 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
444 a = a / b;
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
445 PyFPE_END_PROTECT(a)
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
446 return PyFloat_FromDouble(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
449 static PyObject *
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
450 float_classic_div(PyObject *v, PyObject *w)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
451 {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
452 double a,b;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
453 CONVERT_TO_DOUBLE(v, a);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
454 CONVERT_TO_DOUBLE(w, b);
19673
b3271297b984 PEP 238 documented -Qwarn as warning only for classic int or long
Guido van Rossum <guido@python.org>
parents: 19666
diff changeset
455 if (Py_DivisionWarningFlag >= 2 &&
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
456 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
457 return NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
458 if (b == 0.0) {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
459 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
460 return NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
461 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
462 PyFPE_START_PROTECT("divide", return 0)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
463 a = a / b;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
464 PyFPE_END_PROTECT(a)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
465 return PyFloat_FromDouble(a);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
466 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
467
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
468 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
469 float_rem(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
471 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
472 double mod;
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
473 CONVERT_TO_DOUBLE(v, vx);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
474 CONVERT_TO_DOUBLE(w, wx);
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
475 if (wx == 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
476 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
479 PyFPE_START_PROTECT("modulo", return 0)
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
480 mod = fmod(vx, wx);
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
481 /* note: checking mod*wx < 0 is incorrect -- underflows to
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
482 0 if wx < sqrt(smallest nonzero double) */
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
483 if (mod && ((wx < 0) != (mod < 0))) {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
484 mod += wx;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 }
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
486 PyFPE_END_PROTECT(mod)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
487 return PyFloat_FromDouble(mod);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
489
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
490 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
491 float_divmod(PyObject *v, PyObject *w)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
492 {
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
493 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
494 double div, mod, floordiv;
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
495 CONVERT_TO_DOUBLE(v, vx);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
496 CONVERT_TO_DOUBLE(w, wx);
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
497 if (wx == 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
498 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
499 return NULL;
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
500 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
501 PyFPE_START_PROTECT("divmod", return 0)
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
502 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
503 /* 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
504 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
505 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
506 not be an exact integral value after the division, although
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
507 it will always be very close to one.
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
508 */
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
509 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
510 if (mod) {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
511 /* 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
512 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
513 mod += wx;
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
514 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
515 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
516 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
517 else {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
518 /* 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
519 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
520 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
521 "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
522 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
523 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
524 mod = -mod;
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
525 }
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
526 /* 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
527 if (div) {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
528 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
529 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
530 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
531 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
532 else {
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
533 /* 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
534 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
535 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
536 }
95201153251c SF bug #477221: abs and divmod act oddly with -0.0
Tim Peters <tim.peters@gmail.com>
parents: 20826
diff changeset
537 PyFPE_END_PROTECT(floordiv)
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
538 return Py_BuildValue("(dd)", floordiv, mod);
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
539 }
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
540
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
541 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
542 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
543 {
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
544 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
545
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
546 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
547 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
548 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
549 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
550 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
551 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
552 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
553 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
554 }
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
555
257c938900a6 float_int_div(): For clarity, move this closer to the other float
Tim Peters <tim.peters@gmail.com>
parents: 21097
diff changeset
556 static PyObject *
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
557 float_pow(PyObject *v, PyObject *w, PyObject *z)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 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
560
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
561 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
562 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
563 "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
564 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
565 }
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
566
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
567 CONVERT_TO_DOUBLE(v, iv);
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
568 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
569
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
570 /* 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
571 if (iw == 0) { /* v**0 is 1, even 0**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
572 PyFPE_START_PROTECT("pow", 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
573 if ((PyObject *)z != Py_None) {
15723
ac7ae3ead88a Make float a new style number type.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
574 double iz;
15761
95ee79dfd746 Fix a silly bug in float_pow. Sorry Tim.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15723
diff changeset
575 CONVERT_TO_DOUBLE(z, iz);
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
576 ix = fmod(1.0, iz);
6c2b7b624ef2 float_pow: Put *all* of the burden on the libm pow in normal
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
577 if (ix != 0 && iz < 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
578 ix += iz;
249
bab1f5494b8e Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents: 239
diff changeset
579 }
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
580 else
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
581 ix = 1.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
582 PyFPE_END_PROTECT(ix)
20823
c7aa332a2ffa SF bug #477221: abs and divmod act oddly with -0.0.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
583 return PyFloat_FromDouble(ix);
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
584 }
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
585 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
586 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
587 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
588 "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
589 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
590 }
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
591 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
592 }
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
593 if (iv < 0.0) {
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
594 /* 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
595 * 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
596 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
597 if (iw != floor(iw)) {
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
598 PyErr_SetString(PyExc_ValueError, "negative number "
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
599 "cannot be raised to a fractional power");
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
600 return NULL;
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
601 }
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
602 /* 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
603 * -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
604 * 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
605 * 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
606 * 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
607 * 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
608 * 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
609 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
610 if (iv == -1.0 && !Py_IS_INFINITY(iw) && iw == iw) {
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
611 /* XXX the "iw == iw" was to weed out NaNs. This
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
612 * XXX doesn't actually work on all platforms.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
613 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
614 /* 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
615 * 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
616 * 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
617 * indirectly.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
618 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
619 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
620 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
621 }
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
622 /* 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
623 * 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
624 * the platform to do this correctly.
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
625 */
3984
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
626 }
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
627 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
628 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
629 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
630 PyFPE_END_PROTECT(ix)
22228
4b6eda7d0c8f backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21354
diff changeset
631 Py_ADJUST_ERANGE1(ix);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
632 if (errno != 0) {
29252
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
633 /* 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
634 * 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
635 */
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
636 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
0b76679bf3f9 SF bug 705231: Assertion failed, python aborts.
Tim Peters <tim.peters@gmail.com>
parents: 28368
diff changeset
637 PyExc_ValueError);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 return NULL;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
639 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
640 return PyFloat_FromDouble(ix);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
643 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
644 float_neg(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
646 return PyFloat_FromDouble(-v->ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
648
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
649 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
650 float_pos(PyFloatObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 {
19879
bc50581b3d7c More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
652 if (PyFloat_CheckExact(v)) {
bc50581b3d7c More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
653 Py_INCREF(v);
bc50581b3d7c More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
654 return (PyObject *)v;
bc50581b3d7c More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
655 }
bc50581b3d7c More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
656 else
bc50581b3d7c More bug 460020: when F is a subclass of float, disable the unary plus
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
657 return PyFloat_FromDouble(v->ob_fval);
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
658 }
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
659
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
660 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
661 float_abs(PyFloatObject *v)
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
662 {
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
663 return PyFloat_FromDouble(fabs(v->ob_fval));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
665
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
666 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
667 float_nonzero(PyFloatObject *v)
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
668 {
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
669 return v->ob_fval != 0.0;
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
670 }
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
671
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
672 static int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
673 float_coerce(PyObject **pv, PyObject **pw)
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
674 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
675 if (PyInt_Check(*pw)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
676 long x = PyInt_AsLong(*pw);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
677 *pw = PyFloat_FromDouble((double)x);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
678 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
679 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
680 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
681 else if (PyLong_Check(*pw)) {
27484
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
682 double x = PyLong_AsDouble(*pw);
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
683 if (x == -1.0 && PyErr_Occurred())
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
684 return -1;
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 26328
diff changeset
685 *pw = PyFloat_FromDouble(x);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
686 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
687 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
688 }
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
689 else if (PyFloat_Check(*pw)) {
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
690 Py_INCREF(*pv);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
691 Py_INCREF(*pw);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
692 return 0;
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19889
diff changeset
693 }
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
694 return 1; /* Can't do it */
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
695 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
696
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
697 static PyObject *
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26272
diff changeset
698 float_long(PyObject *v)
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26272
diff changeset
699 {
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26272
diff changeset
700 double x = PyFloat_AsDouble(v);
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26272
diff changeset
701 return PyLong_FromDouble(x);
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26272
diff changeset
702 }
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26272
diff changeset
703
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26272
diff changeset
704 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
705 float_int(PyObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
706 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
707 double x = PyFloat_AsDouble(v);
18838
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
708 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
709
a8575f358b9f SF bug #444510: int() should guarantee truncation.
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
710 (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
711 /* 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
712 * 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
713 * 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
714 * 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
715 * 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
716 * 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
717 * 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
718 * 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
719 * 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
720 * 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
721 * 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
722 */
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
723 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
724 const long aslong = (long)wholepart;
f3a69037fdf0 float_int(): Some systems raise an exception if a double is cast to
Tim Peters <tim.peters@gmail.com>
parents: 26291
diff changeset
725 return PyInt_FromLong(aslong);
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
726 }
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
727 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
728 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
729
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
730 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
731 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
732 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
733 Py_INCREF(v);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
734 return v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
735 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
736
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
737
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
738 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
739 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
740
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
741 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
742 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
743 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
744 PyObject *x = Py_False; /* Integer zero */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
745 static char *kwlist[] = {"x", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
746
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
747 if (type != &PyFloat_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
748 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
749 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
750 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
751 if (PyString_Check(x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
752 return PyFloat_FromString(x, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
753 return PyNumber_Float(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
754 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
755
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
756 /* 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
757 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
758 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
759 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
760 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
761 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
762 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
763 {
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
764 PyObject *tmp, *new;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
765
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
766 assert(PyType_IsSubtype(type, &PyFloat_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
767 tmp = float_new(&PyFloat_Type, args, kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
768 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
769 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
770 assert(PyFloat_CheckExact(tmp));
19590
bf2c77840f1c Fix typo: double semicolons.
Guido van Rossum <guido@python.org>
parents: 19552
diff changeset
771 new = type->tp_alloc(type, 0);
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
772 if (new == NULL) {
4909aa64405f SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents: 29252
diff changeset
773 Py_DECREF(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
774 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
775 }
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
776 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
777 Py_DECREF(tmp);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
778 return new;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
779 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
780
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
781 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
782 float_getnewargs(PyFloatObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
783 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
784 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
785 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
786
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
787 static PyMethodDef float_methods[] = {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
788 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
789 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
790 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
791
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
792 PyDoc_STRVAR(float_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
793 "float(x) -> floating point number\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
794 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23344
diff changeset
795 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
796
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
797
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
798 static PyNumberMethods float_as_number = {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
799 (binaryfunc)float_add, /*nb_add*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
800 (binaryfunc)float_sub, /*nb_subtract*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
801 (binaryfunc)float_mul, /*nb_multiply*/
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
802 (binaryfunc)float_classic_div, /*nb_divide*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
803 (binaryfunc)float_rem, /*nb_remainder*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
804 (binaryfunc)float_divmod, /*nb_divmod*/
1775
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
805 (ternaryfunc)float_pow, /*nb_power*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
806 (unaryfunc)float_neg, /*nb_negative*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
807 (unaryfunc)float_pos, /*nb_positive*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
808 (unaryfunc)float_abs, /*nb_absolute*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
809 (inquiry)float_nonzero, /*nb_nonzero*/
405
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
810 0, /*nb_invert*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
811 0, /*nb_lshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
812 0, /*nb_rshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
813 0, /*nb_and*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
814 0, /*nb_xor*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
815 0, /*nb_or*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
816 (coercion)float_coerce, /*nb_coerce*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
817 (unaryfunc)float_int, /*nb_int*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
818 (unaryfunc)float_long, /*nb_long*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
819 (unaryfunc)float_float, /*nb_float*/
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
820 0, /* nb_oct */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
821 0, /* nb_hex */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
822 0, /* nb_inplace_add */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
823 0, /* nb_inplace_subtract */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
824 0, /* nb_inplace_multiply */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
825 0, /* nb_inplace_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
826 0, /* nb_inplace_remainder */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
827 0, /* nb_inplace_power */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
828 0, /* nb_inplace_lshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
829 0, /* nb_inplace_rshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
830 0, /* nb_inplace_and */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
831 0, /* nb_inplace_xor */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
832 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
833 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
834 float_div, /* nb_true_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
835 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
836 0, /* nb_inplace_true_divide */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
838
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
839 PyTypeObject PyFloat_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
840 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 "float",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
843 sizeof(PyFloatObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
844 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
845 (destructor)float_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
846 (printfunc)float_print, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
847 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
848 0, /* tp_setattr */
32150
65f9ad9bc829 Remove float_compare as per
Michael W. Hudson <mwh@python.net>
parents: 31590
diff changeset
849 0, /* tp_compare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
850 (reprfunc)float_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
851 &float_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
852 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
853 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
854 (hashfunc)float_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
855 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
856 (reprfunc)float_str, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
857 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
858 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
859 0, /* tp_as_buffer */
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
860 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
861 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
862 float_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
863 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
864 0, /* tp_clear */
31553
a6d8d5ace6a7 "Fix" (for certain configurations of the planets, including
Michael W. Hudson <mwh@python.net>
parents: 31384
diff changeset
865 (richcmpfunc)float_richcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
866 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
867 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
868 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27484
diff changeset
869 float_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
870 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
871 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
872 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
873 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
874 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
875 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
876 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
877 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
878 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18838
diff changeset
879 float_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
881
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
882 void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
883 PyFloat_Fini(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
884 {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
885 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
886 PyFloatBlock *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
887 int i;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
888 int bc, bf; /* block count, number of freed blocks */
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
889 int frem, fsum; /* remaining unfreed floats per block, total */
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
890
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
891 bc = 0;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
892 bf = 0;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
893 fsum = 0;
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
894 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
895 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
896 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
897 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
898 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
899 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
900 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
901 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
902 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
903 if (PyFloat_CheckExact(p) && p->ob_refcnt != 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
904 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
905 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
906 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
907 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
908 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
909 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
910 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
911 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
912 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
913 if (!PyFloat_CheckExact(p) ||
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
914 p->ob_refcnt == 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
915 p->ob_type = (struct _typeobject *)
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
916 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
917 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
918 }
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
919 }
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
920 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
921 else {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
922 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
923 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
924 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
925 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
926 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
927 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
928 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
929 return;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
930 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
931 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
932 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
933 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
934 else {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
935 fprintf(stderr,
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
936 ": %d unfreed float%s in %d out of %d block%s\n",
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
937 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
938 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
939 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
940 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
941 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
942 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
943 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
944 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
945 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
946 if (PyFloat_CheckExact(p) &&
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19457
diff changeset
947 p->ob_refcnt != 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
948 char buf[100];
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
949 PyFloat_AsString(buf, p);
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
950 fprintf(stderr,
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13141
diff changeset
951 "# <float at %p, refcnt=%d, val=%s>\n",
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13141
diff changeset
952 p, p->ob_refcnt, 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
953 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
954 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
955 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
956 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
957 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
958 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
959
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
960 /*----------------------------------------------------------------------------
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
961 * _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
962 *
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
963 * TODO: On platforms that use the standard IEEE-754 single and double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
964 * formats natively, these routines could simply copy the bytes.
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
965 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
966 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
967 _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
968 {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
969 unsigned char sign;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
970 int e;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
971 double f;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
972 unsigned int fbits;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
973 int incr = 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
974
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
975 if (le) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
976 p += 3;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
977 incr = -1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
978 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
979
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
980 if (x < 0) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
981 sign = 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
982 x = -x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
983 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
984 else
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
985 sign = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
986
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
987 f = frexp(x, &e);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
988
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
989 /* Normalize f to be in the range [1.0, 2.0) */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
990 if (0.5 <= f && f < 1.0) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
991 f *= 2.0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
992 e--;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
993 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
994 else if (f == 0.0)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
995 e = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
996 else {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
997 PyErr_SetString(PyExc_SystemError,
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
998 "frexp() result out of range");
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
999 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
1000 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1001
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1002 if (e >= 128)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1003 goto Overflow;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1004 else if (e < -126) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1005 /* Gradual underflow */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1006 f = ldexp(f, 126 + e);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1007 e = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1008 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1009 else if (!(e == 0 && f == 0.0)) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1010 e += 127;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1011 f -= 1.0; /* Get rid of leading 1 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1012 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1013
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1014 f *= 8388608.0; /* 2**23 */
28368
ff78c3c3ebc4 _PyFloat_Pack4(): Removed needless call of floor().
Tim Peters <tim.peters@gmail.com>
parents: 28335
diff changeset
1015 fbits = (unsigned int)(f + 0.5); /* Round */
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1016 assert(fbits <= 8388608);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1017 if (fbits >> 23) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1018 /* The carry propagated out of a string of 23 1 bits. */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1019 fbits = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1020 ++e;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1021 if (e >= 255)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1022 goto Overflow;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1023 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1024
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1025 /* First byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1026 *p = (sign << 7) | (e >> 1);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1027 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1028
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1029 /* Second byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1030 *p = (char) (((e & 1) << 7) | (fbits >> 16));
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1031 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1032
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1033 /* Third byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1034 *p = (fbits >> 8) & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1035 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1036
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1037 /* Fourth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1038 *p = fbits & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1039
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1040 /* Done */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1041 return 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1042
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1043 Overflow:
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1044 PyErr_SetString(PyExc_OverflowError,
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1045 "float too large to pack with f format");
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1046 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
1047 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1048
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1049 int
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1050 _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
1051 {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1052 unsigned char sign;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1053 int e;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1054 double f;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1055 unsigned int fhi, flo;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1056 int incr = 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1057
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1058 if (le) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1059 p += 7;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1060 incr = -1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1061 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1062
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1063 if (x < 0) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1064 sign = 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1065 x = -x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1066 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1067 else
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1068 sign = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1069
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1070 f = frexp(x, &e);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1071
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1072 /* Normalize f to be in the range [1.0, 2.0) */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1073 if (0.5 <= f && f < 1.0) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1074 f *= 2.0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1075 e--;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1076 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1077 else if (f == 0.0)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1078 e = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1079 else {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1080 PyErr_SetString(PyExc_SystemError,
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1081 "frexp() result out of range");
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1082 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
1083 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1084
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1085 if (e >= 1024)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1086 goto Overflow;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1087 else if (e < -1022) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1088 /* Gradual underflow */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1089 f = ldexp(f, 1022 + e);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1090 e = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1091 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1092 else if (!(e == 0 && f == 0.0)) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1093 e += 1023;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1094 f -= 1.0; /* Get rid of leading 1 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1095 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1096
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1097 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1098 f *= 268435456.0; /* 2**28 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1099 fhi = (unsigned int)f; /* Truncate */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1100 assert(fhi < 268435456);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1101
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1102 f -= (double)fhi;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1103 f *= 16777216.0; /* 2**24 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1104 flo = (unsigned int)(f + 0.5); /* Round */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1105 assert(flo <= 16777216);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1106 if (flo >> 24) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1107 /* The carry propagated out of a string of 24 1 bits. */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1108 flo = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1109 ++fhi;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1110 if (fhi >> 28) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1111 /* And it also progagated out of the next 28 bits. */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1112 fhi = 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1113 ++e;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1114 if (e >= 2047)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1115 goto Overflow;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1116 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1117 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1118
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1119 /* First byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1120 *p = (sign << 7) | (e >> 4);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1121 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1122
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1123 /* Second byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1124 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1125 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1126
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1127 /* Third byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1128 *p = (fhi >> 16) & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1129 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1130
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1131 /* Fourth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1132 *p = (fhi >> 8) & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1133 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1134
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1135 /* Fifth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1136 *p = fhi & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1137 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1138
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1139 /* Sixth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1140 *p = (flo >> 16) & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1141 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1142
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1143 /* Seventh byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1144 *p = (flo >> 8) & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1145 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1146
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1147 /* Eighth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1148 *p = flo & 0xFF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1149 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1150
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1151 /* Done */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1152 return 0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1153
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1154 Overflow:
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1155 PyErr_SetString(PyExc_OverflowError,
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1156 "float too large to pack with d format");
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1157 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
1158 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1159
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1160 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1161 _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
1162 {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1163 unsigned char sign;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1164 int e;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1165 unsigned int f;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1166 double x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1167 int incr = 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1168
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1169 if (le) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1170 p += 3;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1171 incr = -1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1172 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1173
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1174 /* First byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1175 sign = (*p >> 7) & 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1176 e = (*p & 0x7F) << 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1177 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1178
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1179 /* Second byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1180 e |= (*p >> 7) & 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1181 f = (*p & 0x7F) << 16;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1182 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1183
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1184 /* Third byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1185 f |= *p << 8;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1186 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1187
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1188 /* Fourth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1189 f |= *p;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1190
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1191 x = (double)f / 8388608.0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1192
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1193 /* XXX This sadly ignores Inf/NaN issues */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1194 if (e == 0)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1195 e = -126;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1196 else {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1197 x += 1.0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1198 e -= 127;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1199 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1200 x = ldexp(x, e);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1201
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1202 if (sign)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1203 x = -x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1204
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1205 return x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1206 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1207
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1208 double
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1209 _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
1210 {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1211 unsigned char sign;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1212 int e;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1213 unsigned int fhi, flo;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1214 double x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1215 int incr = 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1216
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1217 if (le) {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1218 p += 7;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1219 incr = -1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1220 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1221
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1222 /* First byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1223 sign = (*p >> 7) & 1;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1224 e = (*p & 0x7F) << 4;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1225 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1226
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1227 /* Second byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1228 e |= (*p >> 4) & 0xF;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1229 fhi = (*p & 0xF) << 24;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1230 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1231
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1232 /* Third byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1233 fhi |= *p << 16;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1234 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1235
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1236 /* Fourth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1237 fhi |= *p << 8;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1238 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1239
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1240 /* Fifth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1241 fhi |= *p;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1242 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1243
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1244 /* Sixth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1245 flo = *p << 16;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1246 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1247
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1248 /* Seventh byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1249 flo |= *p << 8;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1250 p += incr;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1251
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1252 /* Eighth byte */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1253 flo |= *p;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1254
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1255 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1256 x /= 268435456.0; /* 2**28 */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1257
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1258 /* XXX This sadly ignores Inf/NaN */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1259 if (e == 0)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1260 e = -1022;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1261 else {
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1262 x += 1.0;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1263 e -= 1023;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1264 }
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1265 x = ldexp(x, e);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1266
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1267 if (sign)
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1268 x = -x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1269
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1270 return x;
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
1271 }