blob: adeaa9e27057e038dd66dafd88e2278913e6bf24 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Float object implementation */
3
Guido van Rossum2a9096b1990-10-21 22:15:08 +00004/* XXX There should be overflow checks here, but it's hard to check
5 for any kind of float exception without losing portability. */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossum3f5da241990-12-20 15:06:42 +00009#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Guido van Rossum07e3a7e1995-02-27 10:13:37 +000011#if !defined(__STDC__) && !defined(macintosh)
Tim Petersdbd9ba62000-07-09 03:09:57 +000012extern double fmod(double, double);
13extern double pow(double, double);
Guido van Rossum6923e131990-11-02 17:50:43 +000014#endif
15
Martin v. Löwis01c65262001-03-06 12:14:54 +000016#if defined(sun) && !defined(__SVR4)
Guido van Rossum3c03fa81997-10-31 17:00:30 +000017/* On SunOS4.1 only libm.a exists. Make sure that references to all
18 needed math functions exist in the executable, so that dynamic
19 loading of mathmodule does not fail. */
20double (*_Py_math_funcs_hack[])() = {
21 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
22 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
23};
24#endif
25
Guido van Rossum93ad0df1997-05-13 21:00:42 +000026/* Special free list -- see comments for same code in intobject.c. */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000027#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000028#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
Guido van Rossumf61bbc81999-03-12 00:12:21 +000029#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
Guido van Rossum3fce8831999-03-12 19:43:17 +000030
Guido van Rossum3fce8831999-03-12 19:43:17 +000031struct _floatblock {
32 struct _floatblock *next;
33 PyFloatObject objects[N_FLOATOBJECTS];
34};
35
36typedef struct _floatblock PyFloatBlock;
37
38static PyFloatBlock *block_list = NULL;
39static PyFloatObject *free_list = NULL;
40
Guido van Rossum93ad0df1997-05-13 21:00:42 +000041static PyFloatObject *
Fred Drakefd99de62000-07-09 05:02:18 +000042fill_free_list(void)
Guido van Rossum93ad0df1997-05-13 21:00:42 +000043{
44 PyFloatObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000045 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
46 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
Guido van Rossum93ad0df1997-05-13 21:00:42 +000047 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000048 return (PyFloatObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000049 ((PyFloatBlock *)p)->next = block_list;
50 block_list = (PyFloatBlock *)p;
51 p = &((PyFloatBlock *)p)->objects[0];
Guido van Rossum93ad0df1997-05-13 21:00:42 +000052 q = p + N_FLOATOBJECTS;
53 while (--q > p)
Guido van Rossumf61bbc81999-03-12 00:12:21 +000054 q->ob_type = (struct _typeobject *)(q-1);
55 q->ob_type = NULL;
Guido van Rossum93ad0df1997-05-13 21:00:42 +000056 return p + N_FLOATOBJECTS - 1;
57}
58
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060PyFloat_FromDouble(double fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
Guido van Rossum93ad0df1997-05-13 21:00:42 +000062 register PyFloatObject *op;
63 if (free_list == NULL) {
64 if ((free_list = fill_free_list()) == NULL)
65 return NULL;
66 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000067 /* PyObject_New is inlined */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000068 op = free_list;
Guido van Rossumf61bbc81999-03-12 00:12:21 +000069 free_list = (PyFloatObject *)op->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +000070 PyObject_INIT(op, &PyFloat_Type);
Guido van Rossum2a9096b1990-10-21 22:15:08 +000071 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Tim Petersef14d732000-09-23 03:39:17 +000075/**************************************************************************
76RED_FLAG 22-Sep-2000 tim
77PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
78
791. If v was a regular string, *pend was set to point to its terminating
80 null byte. That's useless (the caller can find that without any
81 help from this function!).
82
832. If v was a Unicode string, or an object convertible to a character
84 buffer, *pend was set to point into stack trash (the auto temp
85 vector holding the character buffer). That was downright dangerous.
86
87Since we can't change the interface of a public API function, pend is
88still supported but now *officially* useless: if pend is not NULL,
89*pend is set to NULL.
90**************************************************************************/
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000092PyFloat_FromString(PyObject *v, char **pend)
Barry Warsaw226ae6c1999-10-12 19:54:53 +000093{
Guido van Rossum4c08d552000-03-10 22:55:18 +000094 const char *s, *last, *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +000095 double x;
Tim Petersef14d732000-09-23 03:39:17 +000096 char buffer[256]; /* for errors */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000097#ifdef Py_USING_UNICODE
Tim Petersef14d732000-09-23 03:39:17 +000098 char s_buffer[256]; /* for objects convertible to a char buffer */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000099#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000100 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000101
Tim Petersef14d732000-09-23 03:39:17 +0000102 if (pend)
103 *pend = NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000104 if (PyString_Check(v)) {
105 s = PyString_AS_STRING(v);
106 len = PyString_GET_SIZE(v);
107 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000108#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000109 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000110 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
111 PyErr_SetString(PyExc_ValueError,
Tim Petersef14d732000-09-23 03:39:17 +0000112 "Unicode float() literal too long to convert");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000113 return NULL;
114 }
Tim Petersef14d732000-09-23 03:39:17 +0000115 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000116 PyUnicode_GET_SIZE(v),
117 s_buffer,
118 NULL))
119 return NULL;
120 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000121 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000122 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000123#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000124 else if (PyObject_AsCharBuffer(v, &s, &len)) {
125 PyErr_SetString(PyExc_TypeError,
126 "float() needs a string argument");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000127 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000128 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000129
Guido van Rossum4c08d552000-03-10 22:55:18 +0000130 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000131 while (*s && isspace(Py_CHARMASK(*s)))
132 s++;
Tim Petersef14d732000-09-23 03:39:17 +0000133 if (*s == '\0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000134 PyErr_SetString(PyExc_ValueError, "empty string for float()");
135 return NULL;
136 }
Tim Petersef14d732000-09-23 03:39:17 +0000137 /* We don't care about overflow or underflow. If the platform supports
138 * them, infinities and signed zeroes (on underflow) are fine.
139 * However, strtod can return 0 for denormalized numbers, where atof
140 * does not. So (alas!) we special-case a zero result. Note that
141 * whether strtod sets errno on underflow is not defined, so we can't
142 * key off errno.
143 */
Tim Peters858346e2000-09-25 21:01:28 +0000144 PyFPE_START_PROTECT("strtod", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000145 x = strtod(s, (char **)&end);
Tim Peters858346e2000-09-25 21:01:28 +0000146 PyFPE_END_PROTECT(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000147 errno = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000148 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
Tim Petersef14d732000-09-23 03:39:17 +0000149 byte at the end of the string, when the input is inf(inity). */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000150 if (end > last)
151 end = last;
Tim Petersef14d732000-09-23 03:39:17 +0000152 if (end == s) {
153 sprintf(buffer, "invalid literal for float(): %.200s", s);
154 PyErr_SetString(PyExc_ValueError, buffer);
155 return NULL;
156 }
157 /* Since end != s, the platform made *some* kind of sense out
158 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000159 while (*end && isspace(Py_CHARMASK(*end)))
160 end++;
161 if (*end != '\0') {
162 sprintf(buffer, "invalid literal for float(): %.200s", s);
163 PyErr_SetString(PyExc_ValueError, buffer);
164 return NULL;
165 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000166 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000167 PyErr_SetString(PyExc_ValueError,
168 "null byte in argument for float()");
169 return NULL;
170 }
Tim Petersef14d732000-09-23 03:39:17 +0000171 if (x == 0.0) {
172 /* See above -- may have been strtod being anal
173 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000174 PyFPE_START_PROTECT("atof", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000175 x = atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000176 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000177 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000179 return PyFloat_FromDouble(x);
180}
181
Guido van Rossum234f9421993-06-17 12:35:49 +0000182static void
Fred Drakefd99de62000-07-09 05:02:18 +0000183float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000184{
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000185 op->ob_type = (struct _typeobject *)free_list;
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000186 free_list = op;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000187}
188
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189double
Fred Drakefd99de62000-07-09 05:02:18 +0000190PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 PyNumberMethods *nb;
193 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000194 double val;
195
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 if (op && PyFloat_Check(op))
197 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
199 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
200 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 return -1;
203 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206 if (fo == NULL)
207 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 if (!PyFloat_Check(fo)) {
209 PyErr_SetString(PyExc_TypeError,
210 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211 return -1;
212 }
213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 val = PyFloat_AS_DOUBLE(fo);
215 Py_DECREF(fo);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
217 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
220/* Methods */
221
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000222void
Fred Drakefd99de62000-07-09 05:02:18 +0000223PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224{
225 register char *cp;
226 /* Subroutine for float_repr and float_print.
227 We want float numbers to be recognizable as such,
228 i.e., they should contain a decimal point or an exponent.
229 However, %g may print the number as an integer;
230 in such cases, we append ".0" to the string. */
Guido van Rossum57072eb1999-12-23 19:00:28 +0000231 sprintf(buf, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232 cp = buf;
233 if (*cp == '-')
234 cp++;
235 for (; *cp != '\0'; cp++) {
236 /* Any non-digit means it's not an integer;
237 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000238 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239 break;
240 }
241 if (*cp == '\0') {
242 *cp++ = '.';
243 *cp++ = '0';
244 *cp++ = '\0';
245 }
246}
247
Neil Schemenauer32117e52001-01-04 01:44:34 +0000248/* Macro and helper that convert PyObject obj to a C double and store
249 the value in dbl; this replaces the functionality of the coercion
250 slot function */
251
252#define CONVERT_TO_DOUBLE(obj, dbl) \
253 if (PyFloat_Check(obj)) \
254 dbl = PyFloat_AS_DOUBLE(obj); \
255 else if (convert_to_double(&(obj), &(dbl)) < 0) \
256 return obj;
257
258static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000259convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000260{
261 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000262
Neil Schemenauer32117e52001-01-04 01:44:34 +0000263 if (PyInt_Check(obj)) {
264 *dbl = (double)PyInt_AS_LONG(obj);
265 }
266 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000267 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000268 if (*dbl == -1.0 && PyErr_Occurred()) {
269 *v = NULL;
270 return -1;
271 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000272 }
273 else {
274 Py_INCREF(Py_NotImplemented);
275 *v = Py_NotImplemented;
276 return -1;
277 }
278 return 0;
279}
280
Guido van Rossum57072eb1999-12-23 19:00:28 +0000281/* Precisions used by repr() and str(), respectively.
282
283 The repr() precision (17 significant decimal digits) is the minimal number
284 that is guaranteed to have enough precision so that if the number is read
285 back in the exact same binary value is recreated. This is true for IEEE
286 floating point by design, and also happens to work for all other modern
287 hardware.
288
289 The str() precision is chosen so that in most cases, the rounding noise
290 created by various operations is suppressed, while giving plenty of
291 precision for practical use.
292
293*/
294
295#define PREC_REPR 17
296#define PREC_STR 12
297
298void
Fred Drakefd99de62000-07-09 05:02:18 +0000299PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000300{
301 PyFloat_AsStringEx(buf, v, PREC_STR);
302}
303
Tim Peters72f98e92001-05-08 15:19:57 +0000304void
305PyFloat_AsReprString(char *buf, PyFloatObject *v)
306{
307 PyFloat_AsStringEx(buf, v, PREC_REPR);
308}
309
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000310/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000311static int
Fred Drakefd99de62000-07-09 05:02:18 +0000312float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313{
314 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000315 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000317 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318}
319
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000321float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322{
323 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000324 PyFloat_AsStringEx(buf, v, PREC_REPR);
325 return PyString_FromString(buf);
326}
327
328static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000329float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000330{
331 char buf[100];
332 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334}
335
336static int
Fred Drakefd99de62000-07-09 05:02:18 +0000337float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338{
339 double i = v->ob_fval;
340 double j = w->ob_fval;
341 return (i < j) ? -1 : (i > j) ? 1 : 0;
342}
343
Guido van Rossum9bfef441993-03-29 10:43:31 +0000344static long
Fred Drakefd99de62000-07-09 05:02:18 +0000345float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000346{
Tim Peters39dce292000-08-15 03:34:48 +0000347 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000348}
349
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000351float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000353 double a,b;
354 CONVERT_TO_DOUBLE(v, a);
355 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000356 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000357 a = a + b;
358 PyFPE_END_PROTECT(a)
359 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360}
361
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000363float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000365 double a,b;
366 CONVERT_TO_DOUBLE(v, a);
367 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000368 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000369 a = a - b;
370 PyFPE_END_PROTECT(a)
371 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372}
373
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000375float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000377 double a,b;
378 CONVERT_TO_DOUBLE(v, a);
379 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000380 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000381 a = a * b;
382 PyFPE_END_PROTECT(a)
383 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384}
385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000387float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000389 double a,b;
390 CONVERT_TO_DOUBLE(v, a);
391 CONVERT_TO_DOUBLE(w, b);
392 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 return NULL;
395 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000396 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000397 a = a / b;
398 PyFPE_END_PROTECT(a)
399 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400}
401
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000403float_classic_div(PyObject *v, PyObject *w)
404{
405 double a,b;
406 CONVERT_TO_DOUBLE(v, a);
407 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000408 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000409 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
410 return NULL;
411 if (b == 0.0) {
412 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
413 return NULL;
414 }
415 PyFPE_START_PROTECT("divide", return 0)
416 a = a / b;
417 PyFPE_END_PROTECT(a)
418 return PyFloat_FromDouble(a);
419}
420
421static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000422float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000424 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000425 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000426 CONVERT_TO_DOUBLE(v, vx);
427 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 return NULL;
431 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000432 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000433 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000434 /* note: checking mod*wx < 0 is incorrect -- underflows to
435 0 if wx < sqrt(smallest nonzero double) */
436 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000437 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000438 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000439 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
442
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000444float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000445{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000446 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000447 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000448 CONVERT_TO_DOUBLE(v, vx);
449 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000450 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000452 return NULL;
453 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000454 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000455 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000456 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000457 exact multiple of wx. But this is fp arithmetic, and fp
458 vx - mod is an approximation; the result is that div may
459 not be an exact integral value after the division, although
460 it will always be very close to one.
461 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000462 div = (vx - mod) / wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000463 /* note: checking mod*wx < 0 is incorrect -- underflows to
464 0 if wx < sqrt(smallest nonzero double) */
465 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum15ecff41991-10-20 20:16:45 +0000466 mod += wx;
467 div -= 1.0;
468 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000469 /* snap quotient to nearest integral value */
470 floordiv = floor(div);
471 if (div - floordiv > 0.5)
472 floordiv += 1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000473 PyFPE_END_PROTECT(div)
Guido van Rossum9263e781999-05-06 14:26:34 +0000474 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000475}
476
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000478float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479{
480 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000481
482 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000483 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
484 "allowed unless all other arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000485 return NULL;
486 }
487
Neil Schemenauer32117e52001-01-04 01:44:34 +0000488 CONVERT_TO_DOUBLE(v, iv);
489 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000490
491 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000492 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000493 PyFPE_START_PROTECT("pow", return NULL)
494 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000495 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000496 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000497 ix = fmod(1.0, iz);
498 if (ix != 0 && iz < 0)
499 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000500 }
Tim Petersc54d1902000-10-06 00:36:09 +0000501 else
502 ix = 1.0;
503 PyFPE_END_PROTECT(ix)
504 return PyFloat_FromDouble(ix);
505 }
Tim Peters96685bf2001-08-23 22:31:37 +0000506 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000507 if (iw < 0.0) {
508 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000509 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000510 return NULL;
511 }
512 return PyFloat_FromDouble(0.0);
513 }
Tim Peters96685bf2001-08-23 22:31:37 +0000514 if (iv < 0.0 && iw != floor(iw)) {
515 PyErr_SetString(PyExc_ValueError,
516 "negative number cannot be raised to a fractional power");
517 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000518 }
Tim Peters96685bf2001-08-23 22:31:37 +0000519 errno = 0;
520 PyFPE_START_PROTECT("pow", return NULL)
521 ix = pow(iv, iw);
522 PyFPE_END_PROTECT(ix)
Tim Petersa40c7932001-09-05 22:36:56 +0000523 Py_SET_ERANGE_IF_OVERFLOW(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000524 if (errno != 0) {
525 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000528 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530}
531
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000533float_int_div(PyObject *v, PyObject *w)
534{
535 PyObject *t, *r;
536
537 t = float_divmod(v, w);
538 if (t != NULL) {
539 r = PyTuple_GET_ITEM(t, 0);
540 Py_INCREF(r);
541 Py_DECREF(t);
542 return r;
543 }
544 return NULL;
545}
546
547static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000548float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551}
552
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000554float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 Py_INCREF(v);
557 return (PyObject *)v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000558}
559
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000561float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000562{
563 if (v->ob_fval < 0)
564 return float_neg(v);
565 else
566 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567}
568
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000569static int
Fred Drakefd99de62000-07-09 05:02:18 +0000570float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000571{
572 return v->ob_fval != 0.0;
573}
574
Guido van Rossum234f9421993-06-17 12:35:49 +0000575static int
Fred Drakefd99de62000-07-09 05:02:18 +0000576float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000577{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 if (PyInt_Check(*pw)) {
579 long x = PyInt_AsLong(*pw);
580 *pw = PyFloat_FromDouble((double)x);
581 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000582 return 0;
583 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 else if (PyLong_Check(*pw)) {
585 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
586 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000587 return 0;
588 }
589 return 1; /* Can't do it */
590}
591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000593float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000594{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000596 double wholepart; /* integral portion of x, rounded toward 0 */
597 long aslong; /* (long)wholepart */
598
599 (void)modf(x, &wholepart);
600 /* doubles may have more bits than longs, or vice versa; and casting
601 to long may yield gibberish in either case. What really matters
602 is whether converting back to double again reproduces what we
603 started with. */
604 aslong = (long)wholepart;
605 if ((double)aslong == wholepart)
606 return PyInt_FromLong(aslong);
607 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
608 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000609}
610
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000612float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000613{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614 double x = PyFloat_AsDouble(v);
615 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000616}
617
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000619float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000620{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000622 return v;
623}
624
625
Guido van Rossumbef14172001-08-29 15:47:46 +0000626staticforward PyObject *
627float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
628
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629static PyObject *
630float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
631{
632 PyObject *x = Py_False; /* Integer zero */
633 static char *kwlist[] = {"x", 0};
634
Guido van Rossumbef14172001-08-29 15:47:46 +0000635 if (type != &PyFloat_Type)
636 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
638 return NULL;
639 if (PyString_Check(x))
640 return PyFloat_FromString(x, NULL);
641 return PyNumber_Float(x);
642}
643
Guido van Rossumbef14172001-08-29 15:47:46 +0000644/* Wimpy, slow approach to tp_new calls for subtypes of float:
645 first create a regular float from whatever arguments we got,
646 then allocate a subtype instance and initialize its ob_fval
647 from the regular float. The regular float is then thrown away.
648*/
649static PyObject *
650float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
651{
652 PyObject *tmp, *new;
653
654 assert(PyType_IsSubtype(type, &PyFloat_Type));
655 tmp = float_new(&PyFloat_Type, args, kwds);
656 if (tmp == NULL)
657 return NULL;
658 assert(PyFloat_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000659 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000660 if (new == NULL)
661 return NULL;
662 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
663 Py_DECREF(tmp);
664 return new;
665}
666
Tim Peters6d6c1a32001-08-02 04:15:00 +0000667static char float_doc[] =
668"float(x) -> floating point number\n\
669\n\
670Convert a string or number to a floating point number, if possible.";
671
672
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000674 (binaryfunc)float_add, /*nb_add*/
675 (binaryfunc)float_sub, /*nb_subtract*/
676 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000677 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000678 (binaryfunc)float_rem, /*nb_remainder*/
679 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000680 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000681 (unaryfunc)float_neg, /*nb_negative*/
682 (unaryfunc)float_pos, /*nb_positive*/
683 (unaryfunc)float_abs, /*nb_absolute*/
684 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000685 0, /*nb_invert*/
686 0, /*nb_lshift*/
687 0, /*nb_rshift*/
688 0, /*nb_and*/
689 0, /*nb_xor*/
690 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000691 (coercion)float_coerce, /*nb_coerce*/
692 (unaryfunc)float_int, /*nb_int*/
693 (unaryfunc)float_long, /*nb_long*/
694 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000695 0, /* nb_oct */
696 0, /* nb_hex */
697 0, /* nb_inplace_add */
698 0, /* nb_inplace_subtract */
699 0, /* nb_inplace_multiply */
700 0, /* nb_inplace_divide */
701 0, /* nb_inplace_remainder */
702 0, /* nb_inplace_power */
703 0, /* nb_inplace_lshift */
704 0, /* nb_inplace_rshift */
705 0, /* nb_inplace_and */
706 0, /* nb_inplace_xor */
707 0, /* nb_inplace_or */
708 float_int_div, /* nb_floor_divide */
709 float_div, /* nb_true_divide */
710 0, /* nb_inplace_floor_divide */
711 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712};
713
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714PyTypeObject PyFloat_Type = {
715 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716 0,
717 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720 (destructor)float_dealloc, /* tp_dealloc */
721 (printfunc)float_print, /* tp_print */
722 0, /* tp_getattr */
723 0, /* tp_setattr */
724 (cmpfunc)float_compare, /* tp_compare */
725 (reprfunc)float_repr, /* tp_repr */
726 &float_as_number, /* tp_as_number */
727 0, /* tp_as_sequence */
728 0, /* tp_as_mapping */
729 (hashfunc)float_hash, /* tp_hash */
730 0, /* tp_call */
731 (reprfunc)float_str, /* tp_str */
732 PyObject_GenericGetAttr, /* tp_getattro */
733 0, /* tp_setattro */
734 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000735 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
736 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000737 float_doc, /* tp_doc */
738 0, /* tp_traverse */
739 0, /* tp_clear */
740 0, /* tp_richcompare */
741 0, /* tp_weaklistoffset */
742 0, /* tp_iter */
743 0, /* tp_iternext */
744 0, /* tp_methods */
745 0, /* tp_members */
746 0, /* tp_getset */
747 0, /* tp_base */
748 0, /* tp_dict */
749 0, /* tp_descr_get */
750 0, /* tp_descr_set */
751 0, /* tp_dictoffset */
752 0, /* tp_init */
753 0, /* tp_alloc */
754 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000756
757void
Fred Drakefd99de62000-07-09 05:02:18 +0000758PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000759{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000760 PyFloatObject *p;
761 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000762 int i;
763 int bc, bf; /* block count, number of freed blocks */
764 int frem, fsum; /* remaining unfreed floats per block, total */
765
766 bc = 0;
767 bf = 0;
768 fsum = 0;
769 list = block_list;
770 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000771 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000772 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000773 bc++;
774 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000775 for (i = 0, p = &list->objects[0];
776 i < N_FLOATOBJECTS;
777 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000778 if (p->ob_type == &PyFloat_Type && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000779 frem++;
780 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000781 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000782 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000783 list->next = block_list;
784 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000785 for (i = 0, p = &list->objects[0];
786 i < N_FLOATOBJECTS;
787 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000788 if (p->ob_type != &PyFloat_Type ||
789 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000790 p->ob_type = (struct _typeobject *)
791 free_list;
792 free_list = p;
793 }
794 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000795 }
796 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000797 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000798 bf++;
799 }
800 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000801 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000802 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000803 if (!Py_VerboseFlag)
804 return;
805 fprintf(stderr, "# cleanup floats");
806 if (!fsum) {
807 fprintf(stderr, "\n");
808 }
809 else {
810 fprintf(stderr,
811 ": %d unfreed float%s in %d out of %d block%s\n",
812 fsum, fsum == 1 ? "" : "s",
813 bc - bf, bc, bc == 1 ? "" : "s");
814 }
815 if (Py_VerboseFlag > 1) {
816 list = block_list;
817 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000818 for (i = 0, p = &list->objects[0];
819 i < N_FLOATOBJECTS;
820 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000821 if (p->ob_type == &PyFloat_Type &&
822 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000823 char buf[100];
824 PyFloat_AsString(buf, p);
825 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000826 "# <float at %p, refcnt=%d, val=%s>\n",
827 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000828 }
829 }
830 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000831 }
832 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000833}