annotate Objects/floatobject.c @ 13255:916fb024ce94 legacy-trunk

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