annotate Objects/floatobject.c @ 14541:561c0b1d514b legacy-trunk

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