annotate Objects/floatobject.c @ 11865:2fa89e657a4e legacy-trunk

Fix a typo in Barry's checkin. Reported both by Gerrit Holl and Mark Favas.
author Guido van Rossum <guido@python.org>
date Sun, 27 Feb 2000 15:35:47 +0000
parents 5e6e9e893acd
children 74b7213fb609
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 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4282
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 91
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* Float object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
34 /* 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
35 for any kind of float exception without losing portability. */
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
36
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
37 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 60
diff changeset
39 #include <ctype.h>
2433
6cbd2830b5a2 for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents: 2327
diff changeset
40 #include "mymath.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
1271
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
42 #ifdef i860
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
43 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
44 #undef HUGE_VAL
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
45 #endif
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
46
3704
17030515f623 Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents: 3608
diff changeset
47 #if defined(HUGE_VAL) && !defined(CHECK)
496
0ac392b7f76e Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents: 470
diff changeset
48 #define CHECK(x) if (errno != 0) ; \
0ac392b7f76e Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents: 470
diff changeset
49 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
0ac392b7f76e Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents: 470
diff changeset
50 else errno = ERANGE
3704
17030515f623 Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents: 3608
diff changeset
51 #endif
17030515f623 Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents: 3608
diff changeset
52
17030515f623 Different logic for defining CHECK(); 16-bit MSC specific define.
Guido van Rossum <guido@python.org>
parents: 3608
diff changeset
53 #ifndef CHECK
496
0ac392b7f76e Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents: 470
diff changeset
54 #define CHECK(x) /* Don't know how to check */
0ac392b7f76e Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents: 470
diff changeset
55 #endif
0ac392b7f76e Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents: 470
diff changeset
56
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
57 #ifdef HAVE_LIMITS_H
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
58 #include <limits.h>
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
59 #endif
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
60
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
61 #ifndef LONG_MAX
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
62 #define LONG_MAX 0X7FFFFFFFL
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
63 #endif
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
64
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
65 #ifndef LONG_MIN
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
66 #define LONG_MIN (-LONG_MAX-1)
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
67 #endif
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
68
2327
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
69 #ifdef __NeXT__
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
70 #ifdef __sparc__
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
71 /*
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
72 * This works around a bug in the NS/Sparc 3.3 pre-release
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
73 * limits.h header file.
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
74 * 10-Feb-1995 bwarsaw@cnri.reston.va.us
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
75 */
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
76 #undef LONG_MIN
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
77 #define LONG_MIN (-LONG_MAX-1)
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
78 #endif
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
79 #endif
43f9e7549c70 NeXT/Sparc 3.3 fix
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
80
2433
6cbd2830b5a2 for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents: 2327
diff changeset
81 #if !defined(__STDC__) && !defined(macintosh)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
82 extern double fmod Py_PROTO((double, double));
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
83 extern double pow Py_PROTO((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
84 #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
85
6537
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
86 #ifdef sun
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
87 /* On SunOS4.1 only libm.a exists. Make sure that references to all
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
88 needed math functions exist in the executable, so that dynamic
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
89 loading of mathmodule does not fail. */
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
90 double (*_Py_math_funcs_hack[])() = {
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
91 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
92 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
93 };
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
94 #endif
25505e563b37 Hack suggested by Matthias Klose to pull in all relevant entry points
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
95
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
96 /* 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
97 #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
98 #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
99 #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
100
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
101 #define PyMem_MALLOC malloc
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
102 #define PyMem_FREE free
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
103
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
104 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
105 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
106 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
107 };
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
108
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
109 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
110
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
111 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
112 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
113
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
114 static PyFloatObject *
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
115 fill_free_list()
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
116 {
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
117 PyFloatObject *p, *q;
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
118 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
119 if (p == NULL)
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
120 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
121 ((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
122 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
123 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
124 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
125 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
126 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
127 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
128 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
129 }
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
130
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
131 PyObject *
2433
6cbd2830b5a2 for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents: 2327
diff changeset
132 #ifdef __SC__
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
133 PyFloat_FromDouble(double fval)
2433
6cbd2830b5a2 for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents: 2327
diff changeset
134 #else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
135 PyFloat_FromDouble(fval)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 double fval;
2433
6cbd2830b5a2 for MPW __SC__ compiler
Guido van Rossum <guido@python.org>
parents: 2327
diff changeset
137 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 {
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
139 register PyFloatObject *op;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
140 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
141 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
142 return NULL;
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
143 }
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
144 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
145 free_list = (PyFloatObject *)op->ob_type;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
146 op->ob_type = &PyFloat_Type;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
147 op->ob_fval = fval;
11673
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11631
diff changeset
148 _Py_NewReference((PyObject *)op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
149 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
152 PyObject *
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
153 PyFloat_FromString(v, pend)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
154 PyObject *v;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
155 char **pend;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
156 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
157 extern double strtod Py_PROTO((const char *, char **));
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
158 char *s, *last, *end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
159 double x;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
160 char buffer[256]; /* For errors */
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
161
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
162 if (!PyString_Check(v))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
163 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
164 s = PyString_AS_STRING(v);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
165
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
166 last = s + PyString_GET_SIZE(v);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
167 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
168 s++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
169 if (s[0] == '\0') {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
170 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
171 return NULL;
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 errno = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
174 PyFPE_START_PROTECT("PyFloat_FromString", return 0)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
175 x = strtod(s, &end);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
176 PyFPE_END_PROTECT(x)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
177 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
178 byte at the end of the string, when the input is inf(inity) */
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
179 if (end > last)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
180 end = last;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
181 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
182 end++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
183 if (*end != '\0') {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
184 sprintf(buffer, "invalid literal for float(): %.200s", s);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
185 PyErr_SetString(PyExc_ValueError, buffer);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
186 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
187 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
188 else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
189 PyErr_SetString(PyExc_ValueError,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
190 "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
191 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
192 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
193 else if (errno != 0) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
194 sprintf(buffer, "float() literal too large: %.200s", s);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
195 PyErr_SetString(PyExc_ValueError, buffer);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
196 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
197 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
198 if (pend)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
199 *pend = end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
200 return PyFloat_FromDouble(x);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
201 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10833
diff changeset
202
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
203 static void
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
204 float_dealloc(op)
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
205 PyFloatObject *op;
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
206 {
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
207 op->ob_type = (struct _typeobject *)free_list;
5504
c88b9b868995 Faster floating point allocator, same idea as the int allocator.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
208 free_list = op;
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
209 }
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
210
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 double
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
212 PyFloat_AsDouble(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
213 PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
215 PyNumberMethods *nb;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
216 PyFloatObject *fo;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
217 double val;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
218
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
219 if (op && PyFloat_Check(op))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
220 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
221
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
222 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
223 nb->nb_float == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
224 PyErr_BadArgument();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
227
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
228 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
229 if (fo == NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
230 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
231 if (!PyFloat_Check(fo)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
232 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
233 "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
234 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
235 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
236
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
237 val = PyFloat_AS_DOUBLE(fo);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
238 Py_DECREF(fo);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
239
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
240 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
268
0fe2471166f0 Export float_buf_repr() interface, for marshalling of floats.
Guido van Rossum <guido@python.org>
parents: 249
diff changeset
245 void
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
246 PyFloat_AsStringEx(buf, v, precision)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 char *buf;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
248 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
249 int precision;
6
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 register char *cp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 /* Subroutine for float_repr and float_print.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 We want float numbers to be recognizable as such,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 i.e., they should contain a decimal point or an exponent.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 However, %g may print the number as an integer;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 in such cases, we append ".0" to the string. */
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
257 sprintf(buf, "%.*g", precision, v->ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 cp = buf;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 if (*cp == '-')
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 cp++;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 for (; *cp != '\0'; cp++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 /* Any non-digit means it's not an integer;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 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
264 if (!isdigit(Py_CHARMASK(*cp)))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 break;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 if (*cp == '\0') {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 *cp++ = '.';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 *cp++ = '0';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 *cp++ = '\0';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
274 /* 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
275
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
276 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
277 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
278 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
279 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
280 hardware.
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
281
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
282 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
283 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
284 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
285
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
286 */
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
287
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
288 #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
289 #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
290
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
291 void
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
292 PyFloat_AsString(buf, v)
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
293 char *buf;
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
294 PyFloatObject *v;
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
295 {
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
296 PyFloat_AsStringEx(buf, v, PREC_STR);
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
297 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
298
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
299 /* ARGSUSED */
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 268
diff changeset
300 static int
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 float_print(v, fp, flags)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
302 PyFloatObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 FILE *fp;
745
c7b23cc8cded answer lint's complaints
Guido van Rossum <guido@python.org>
parents: 632
diff changeset
304 int flags; /* Not used but required by interface */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 char buf[100];
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
307 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 fputs(buf, fp);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 268
diff changeset
309 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
312 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 float_repr(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
314 PyFloatObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 char buf[100];
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
317 PyFloat_AsStringEx(buf, v, PREC_REPR);
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
318 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
319 }
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
320
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
321 static PyObject *
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
322 float_str(v)
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
323 PyFloatObject *v;
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
324 {
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
325 char buf[100];
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
326 PyFloat_AsStringEx(buf, v, PREC_STR);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
327 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 static int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 float_compare(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
332 PyFloatObject *v, *w;
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 double i = v->ob_fval;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 double j = w->ob_fval;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 return (i < j) ? -1 : (i > j) ? 1 : 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
339 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
340 float_hash(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
341 PyFloatObject *v;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
342 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
343 double intpart, fractpart;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
344 int expo;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
345 long x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
346 /* This is designed so that Python numbers with the same
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
347 value hash to the same value, otherwise comparisons
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
348 of mapping keys will turn out weird */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
349
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
350 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
351 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
352 extended e;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
353 fractpart = modf(v->ob_fval, &e);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
354 intpart = e;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
355 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
356 #else
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
357 fractpart = modf(v->ob_fval, &intpart);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
358 #endif
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
359
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
360 if (fractpart == 0.0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
361 if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
362 /* Convert to long int and use its hash... */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
363 PyObject *w = PyLong_FromDouble(v->ob_fval);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
364 if (w == NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
365 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
366 x = PyObject_Hash(w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
367 Py_DECREF(w);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
368 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
369 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
370 x = (long)intpart;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
371 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
372 else {
4933
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
373 /* Note -- if you change this code, also change the copy
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
374 in complexobject.c */
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
375 long hipart;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
376 fractpart = frexp(fractpart, &expo);
4933
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
377 fractpart = fractpart * 2147483648.0; /* 2**31 */
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
378 hipart = (long)fractpart; /* Take the top 32 bits */
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
379 fractpart = (fractpart - (double)hipart) * 2147483648.0;
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
380 /* Get the next 32 bits */
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
381 x = hipart + (long)fractpart + (long)intpart + (expo << 15);
b56bc25aec85 New, better hash for floating point and complex
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
382 /* Combine everything */
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
383 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
384 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
385 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
386 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
387 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
388
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
389 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 float_add(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
391 PyFloatObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
392 PyFloatObject *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
394 double result;
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
395 PyFPE_START_PROTECT("add", return 0)
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
396 result = v->ob_fval + w->ob_fval;
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
397 PyFPE_END_PROTECT(result)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
398 return PyFloat_FromDouble(result);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
401 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 float_sub(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
403 PyFloatObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
404 PyFloatObject *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
406 double result;
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
407 PyFPE_START_PROTECT("subtract", return 0)
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
408 result = v->ob_fval - w->ob_fval;
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
409 PyFPE_END_PROTECT(result)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
410 return PyFloat_FromDouble(result);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
413 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 float_mul(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
415 PyFloatObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
416 PyFloatObject *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
418 double result;
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
419
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
420 PyFPE_START_PROTECT("multiply", return 0)
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
421 result = v->ob_fval * w->ob_fval;
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
422 PyFPE_END_PROTECT(result)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
423 return PyFloat_FromDouble(result);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
425
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
426 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 float_div(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
428 PyFloatObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
429 PyFloatObject *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
431 double result;
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
432 if (w->ob_fval == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
433 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
436 PyFPE_START_PROTECT("divide", return 0)
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
437 result = v->ob_fval / w->ob_fval;
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
438 PyFPE_END_PROTECT(result)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
439 return PyFloat_FromDouble(result);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
442 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 float_rem(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
444 PyFloatObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
445 PyFloatObject *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
447 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
448 double mod;
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
449 wx = w->ob_fval;
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
450 if (wx == 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
451 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
454 PyFPE_START_PROTECT("modulo", return 0)
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
455 vx = v->ob_fval;
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
456 mod = fmod(vx, wx);
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
457 /* note: checking mod*wx < 0 is incorrect -- underflows to
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
458 0 if wx < sqrt(smallest nonzero double) */
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
459 if (mod && ((wx < 0) != (mod < 0))) {
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
460 mod += wx;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 }
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
462 PyFPE_END_PROTECT(mod)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
463 return PyFloat_FromDouble(mod);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
465
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
466 static PyObject *
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
467 float_divmod(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
468 PyFloatObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
469 PyFloatObject *w;
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
470 {
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
471 double vx, wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
472 double div, mod, floordiv;
632
0c1938f2e0c0 Get rid of redundant type checks.
Guido van Rossum <guido@python.org>
parents: 496
diff changeset
473 wx = w->ob_fval;
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
474 if (wx == 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
475 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
476 return NULL;
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
477 }
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
478 PyFPE_START_PROTECT("divmod", return 0)
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
479 vx = v->ob_fval;
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
480 mod = fmod(vx, wx);
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
481 /* fmod is typically exact, so vx-mod is *mathemtically* an
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
482 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
483 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
484 not be an exact integral value after the division, although
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
485 it will always be very close to one.
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
486 */
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
487 div = (vx - mod) / wx;
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
488 /* note: checking mod*wx < 0 is incorrect -- underflows to
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
489 0 if wx < sqrt(smallest nonzero double) */
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
490 if (mod && ((wx < 0) != (mod < 0))) {
391
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
491 mod += wx;
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
492 div -= 1.0;
caf8466dcc9c Finally implemented divmod().
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
493 }
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
494 /* snap quotient to nearest integral value */
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
495 floordiv = floor(div);
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
496 if (div - floordiv > 0.5)
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
497 floordiv += 1.0;
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
498 PyFPE_END_PROTECT(div)
10833
135c1ec9636c Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 10432
diff changeset
499 return Py_BuildValue("(dd)", floordiv, mod);
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
500 }
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
501
3608
8573e65fa948 make some things static
Guido van Rossum <guido@python.org>
parents: 3329
diff changeset
502 static double powu(x, n)
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
503 double x;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
504 long n;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
505 {
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
506 double r = 1.;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
507 double p = x;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
508 long mask = 1;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
509 while (mask > 0 && n >= mask) {
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
510 if (n & mask)
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
511 r *= p;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
512 mask <<= 1;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
513 p *= p;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
514 }
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
515 return r;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
516 }
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
517
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
518 static PyObject *
1775
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
519 float_pow(v, w, z)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
520 PyFloatObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
521 PyObject *w;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
522 PyFloatObject *z;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 double iv, iw, ix;
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
525 long intw;
1775
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
526 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
527 * The z parameter is really only going to be useful for integers and
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
528 * long integers. Maybe something clever with logarithms could be done.
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
529 * [AMK]
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
530 */
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
531 iv = v->ob_fval;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
532 iw = ((PyFloatObject *)w)->ob_fval;
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
533 intw = (long)iw;
4007
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
534 if (iw == intw && -10000 < intw && intw < 10000) {
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
535 /* Sort out special cases here instead of relying on pow() */
3984
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
536 if (intw == 0) { /* x**0 is 1, even 0**0 */
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
537 PyFPE_START_PROTECT("pow", return 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
538 if ((PyObject *)z!=Py_None) {
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
539 ix=fmod(1.0, z->ob_fval);
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
540 if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
541 }
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
542 else ix=1.0;
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
543 PyFPE_END_PROTECT(ix)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
544 return PyFloat_FromDouble(ix);
249
bab1f5494b8e Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents: 239
diff changeset
545 }
3984
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
546 errno = 0;
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
547 PyFPE_START_PROTECT("pow", return 0)
4007
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
548 if (intw > 0)
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
549 ix = powu(iv, intw);
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
550 else
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
551 ix = 1./powu(iv, -intw);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
552 PyFPE_END_PROTECT(ix)
3984
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
553 }
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
554 else {
0613f17dcfd8 Correct wrong calculation of pow(0.0, 0.0, negative_number)
Guido van Rossum <guido@python.org>
parents: 3704
diff changeset
555 /* Sort out special cases here instead of relying on pow() */
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
556 if (iv == 0.0) {
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
557 if (iw < 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
558 PyErr_SetString(PyExc_ValueError,
4007
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
559 "0.0 to a negative power");
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
560 return NULL;
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
561 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
562 return PyFloat_FromDouble(0.0);
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
563 }
4007
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
564 if (iv < 0.0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
565 PyErr_SetString(PyExc_ValueError,
4007
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
566 "negative number to a float power");
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
567 return NULL;
1a0aa986f6fa Test for negative # to the nonintegral float power here.
Guido van Rossum <guido@python.org>
parents: 3984
diff changeset
568 }
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
569 errno = 0;
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
570 PyFPE_START_PROTECT("pow", return 0)
3329
67d347c32fff better power implementation
Guido van Rossum <guido@python.org>
parents: 2433
diff changeset
571 ix = pow(iv, iw);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
572 PyFPE_END_PROTECT(ix)
249
bab1f5494b8e Fix special cases in pow()
Guido van Rossum <guido@python.org>
parents: 239
diff changeset
573 }
496
0ac392b7f76e Explicitly check for weird values after calling pow().
Guido van Rossum <guido@python.org>
parents: 470
diff changeset
574 CHECK(ix);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
575 if (errno != 0) {
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
576 /* XXX could it be another type of error? */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
577 PyErr_SetFromErrno(PyExc_OverflowError);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 return NULL;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
579 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
580 if ((PyObject *)z!=Py_None) {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4933
diff changeset
581 PyFPE_START_PROTECT("pow", return 0)
1775
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
582 ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
583 if ( ix!=0 &&
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
584 ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
585 ix+=z->ob_fval;
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
586 }
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
587 PyFPE_END_PROTECT(ix)
1775
17c80a952914 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
588 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
589 return PyFloat_FromDouble(ix);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
591
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
592 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 float_neg(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
594 PyFloatObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
596 return PyFloat_FromDouble(-v->ob_fval);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
598
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
599 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 float_pos(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
601 PyFloatObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
603 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
604 return (PyObject *)v;
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
605 }
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
606
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
607 static PyObject *
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
608 float_abs(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
609 PyFloatObject *v;
227
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
610 {
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
611 if (v->ob_fval < 0)
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
612 return float_neg(v);
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
613 else
7ec6058b539b Added (dummy) divmod and (functional) abs.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
614 return float_pos(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
616
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
617 static int
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
618 float_nonzero(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
619 PyFloatObject *v;
239
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
620 {
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
621 return v->ob_fval != 0.0;
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
622 }
fda973f2a512 Added nonzero test
Guido van Rossum <guido@python.org>
parents: 227
diff changeset
623
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
624 static int
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
625 float_coerce(pv, pw)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
626 PyObject **pv;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
627 PyObject **pw;
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
628 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
629 if (PyInt_Check(*pw)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
630 long x = PyInt_AsLong(*pw);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
631 *pw = PyFloat_FromDouble((double)x);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
632 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
633 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
634 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
635 else if (PyLong_Check(*pw)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
636 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
637 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
638 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
639 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
640 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
641 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
642
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
643 static PyObject *
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
644 float_int(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
645 PyObject *v;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
646 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
647 double x = PyFloat_AsDouble(v);
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
648 if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
649 : (x = floor(x)) > (double)LONG_MAX) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
650 PyErr_SetString(PyExc_OverflowError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
651 "float too large to convert");
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
652 return NULL;
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1775
diff changeset
653 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
654 return PyInt_FromLong((long)x);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
655 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
656
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
657 static PyObject *
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
658 float_long(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
659 PyObject *v;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
660 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
661 double x = PyFloat_AsDouble(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
662 return PyLong_FromDouble(x);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
663 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
664
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
665 static PyObject *
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
666 float_float(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
667 PyObject *v;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
668 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
669 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
670 return v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
671 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
672
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
673
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
674 static PyNumberMethods float_as_number = {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
675 (binaryfunc)float_add, /*nb_add*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
676 (binaryfunc)float_sub, /*nb_subtract*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
677 (binaryfunc)float_mul, /*nb_multiply*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
678 (binaryfunc)float_div, /*nb_divide*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
679 (binaryfunc)float_rem, /*nb_remainder*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
680 (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
681 (ternaryfunc)float_pow, /*nb_power*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
682 (unaryfunc)float_neg, /*nb_negative*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
683 (unaryfunc)float_pos, /*nb_positive*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
684 (unaryfunc)float_abs, /*nb_absolute*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
685 (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
686 0, /*nb_invert*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
687 0, /*nb_lshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
688 0, /*nb_rshift*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
689 0, /*nb_and*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
690 0, /*nb_xor*/
d3d328680328 Added NULL function pointers for shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 391
diff changeset
691 0, /*nb_or*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
692 (coercion)float_coerce, /*nb_coerce*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
693 (unaryfunc)float_int, /*nb_int*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
694 (unaryfunc)float_long, /*nb_long*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
695 (unaryfunc)float_float, /*nb_float*/
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
696 0, /*nb_oct*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
697 0, /*nb_hex*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
700 PyTypeObject PyFloat_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
701 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 "float",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
704 sizeof(PyFloatObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 0,
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
706 (destructor)float_dealloc, /*tp_dealloc*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
707 (printfunc)float_print, /*tp_print*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 0, /*tp_getattr*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 0, /*tp_setattr*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
710 (cmpfunc)float_compare, /*tp_compare*/
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
711 (reprfunc)float_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 &float_as_number, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 0, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 0, /*tp_as_mapping*/
11631
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
715 (hashfunc)float_hash, /*tp_hash*/
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
716 0, /*tp_call*/
cf7a0f1b7b48 Implement the other easy thing: repr() of a float now uses %.17g,
Guido van Rossum <guido@python.org>
parents: 11383
diff changeset
717 (reprfunc)float_str, /*tp_str*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
719
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
720 void
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
721 PyFloat_Fini()
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
722 {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
723 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
724 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
725 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
726 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
727 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
728
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
729 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
730 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
731 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
732 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
733 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
734 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
735 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
736 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
737 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
738 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
739 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
740 i++, 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
741 if (PyFloat_Check(p) && p->ob_refcnt != 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
742 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
743 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
744 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
745 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
746 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
747 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
748 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
749 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
750 i++, 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
751 if (!PyFloat_Check(p) || p->ob_refcnt == 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
752 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
753 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
754 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
755 }
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
756 }
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
757 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
758 else {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
759 PyMem_FREE(list);
10382
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
760 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
761 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
762 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
763 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
764 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
765 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
766 return;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
767 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
768 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
769 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
770 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
771 else {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
772 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
773 ": %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
774 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
775 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
776 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
777 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
778 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
779 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
780 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
781 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
782 i++, p++) {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
783 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
784 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
785 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
786 fprintf(stderr,
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
787 "# <float at %lx, refcnt=%d, val=%s>\n",
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
788 p, p->ob_refcnt, buf);
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
789 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
790 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10382
diff changeset
791 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
792 }
e2a08a81e56f OK, try again. Vladimir gave me a fix for the alignment bus error,
Guido van Rossum <guido@python.org>
parents: 10375
diff changeset
793 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5504
diff changeset
794 }