blob: df88736abae291bbdb34f52c44b64279a747ce32 [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 Rossum9575a441993-04-07 14:06:14 +000011#ifdef i860
12/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
13#undef HUGE_VAL
14#endif
15
Guido van Rossum9d81b551996-06-26 18:27:19 +000016#if defined(HUGE_VAL) && !defined(CHECK)
Guido van Rossum7fa52f81991-12-16 15:43:14 +000017#define CHECK(x) if (errno != 0) ; \
18 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
19 else errno = ERANGE
Guido van Rossum9d81b551996-06-26 18:27:19 +000020#endif
21
22#ifndef CHECK
Guido van Rossum7fa52f81991-12-16 15:43:14 +000023#define CHECK(x) /* Don't know how to check */
24#endif
25
Guido van Rossum07e3a7e1995-02-27 10:13:37 +000026#if !defined(__STDC__) && !defined(macintosh)
Tim Petersdbd9ba62000-07-09 03:09:57 +000027extern double fmod(double, double);
28extern double pow(double, double);
Guido van Rossum6923e131990-11-02 17:50:43 +000029#endif
30
Martin v. Löwis01c65262001-03-06 12:14:54 +000031#if defined(sun) && !defined(__SVR4)
Guido van Rossum3c03fa81997-10-31 17:00:30 +000032/* On SunOS4.1 only libm.a exists. Make sure that references to all
33 needed math functions exist in the executable, so that dynamic
34 loading of mathmodule does not fail. */
35double (*_Py_math_funcs_hack[])() = {
36 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
37 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
38};
39#endif
40
Guido van Rossum93ad0df1997-05-13 21:00:42 +000041/* Special free list -- see comments for same code in intobject.c. */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000042#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000043#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
Guido van Rossumf61bbc81999-03-12 00:12:21 +000044#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
Guido van Rossum3fce8831999-03-12 19:43:17 +000045
Guido van Rossum3fce8831999-03-12 19:43:17 +000046struct _floatblock {
47 struct _floatblock *next;
48 PyFloatObject objects[N_FLOATOBJECTS];
49};
50
51typedef struct _floatblock PyFloatBlock;
52
53static PyFloatBlock *block_list = NULL;
54static PyFloatObject *free_list = NULL;
55
Guido van Rossum93ad0df1997-05-13 21:00:42 +000056static PyFloatObject *
Fred Drakefd99de62000-07-09 05:02:18 +000057fill_free_list(void)
Guido van Rossum93ad0df1997-05-13 21:00:42 +000058{
59 PyFloatObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000060 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
61 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
Guido van Rossum93ad0df1997-05-13 21:00:42 +000062 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000063 return (PyFloatObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000064 ((PyFloatBlock *)p)->next = block_list;
65 block_list = (PyFloatBlock *)p;
66 p = &((PyFloatBlock *)p)->objects[0];
Guido van Rossum93ad0df1997-05-13 21:00:42 +000067 q = p + N_FLOATOBJECTS;
68 while (--q > p)
Guido van Rossumf61bbc81999-03-12 00:12:21 +000069 q->ob_type = (struct _typeobject *)(q-1);
70 q->ob_type = NULL;
Guido van Rossum93ad0df1997-05-13 21:00:42 +000071 return p + N_FLOATOBJECTS - 1;
72}
73
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyFloat_FromDouble(double fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076{
Guido van Rossum93ad0df1997-05-13 21:00:42 +000077 register PyFloatObject *op;
78 if (free_list == NULL) {
79 if ((free_list = fill_free_list()) == NULL)
80 return NULL;
81 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000082 /* PyObject_New is inlined */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000083 op = free_list;
Guido van Rossumf61bbc81999-03-12 00:12:21 +000084 free_list = (PyFloatObject *)op->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +000085 PyObject_INIT(op, &PyFloat_Type);
Guido van Rossum2a9096b1990-10-21 22:15:08 +000086 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088}
89
Tim Petersef14d732000-09-23 03:39:17 +000090/**************************************************************************
91RED_FLAG 22-Sep-2000 tim
92PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
93
941. If v was a regular string, *pend was set to point to its terminating
95 null byte. That's useless (the caller can find that without any
96 help from this function!).
97
982. If v was a Unicode string, or an object convertible to a character
99 buffer, *pend was set to point into stack trash (the auto temp
100 vector holding the character buffer). That was downright dangerous.
101
102Since we can't change the interface of a public API function, pend is
103still supported but now *officially* useless: if pend is not NULL,
104*pend is set to NULL.
105**************************************************************************/
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000106PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000107PyFloat_FromString(PyObject *v, char **pend)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000108{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000109 const char *s, *last, *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000110 double x;
Tim Petersef14d732000-09-23 03:39:17 +0000111 char buffer[256]; /* for errors */
112 char s_buffer[256]; /* for objects convertible to a char buffer */
Guido van Rossum4c08d552000-03-10 22:55:18 +0000113 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000114
Tim Petersef14d732000-09-23 03:39:17 +0000115 if (pend)
116 *pend = NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000117 if (PyString_Check(v)) {
118 s = PyString_AS_STRING(v);
119 len = PyString_GET_SIZE(v);
120 }
Guido van Rossum9e896b32000-04-05 20:11:21 +0000121 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000122 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
123 PyErr_SetString(PyExc_ValueError,
Tim Petersef14d732000-09-23 03:39:17 +0000124 "Unicode float() literal too long to convert");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000125 return NULL;
126 }
Tim Petersef14d732000-09-23 03:39:17 +0000127 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000128 PyUnicode_GET_SIZE(v),
129 s_buffer,
130 NULL))
131 return NULL;
132 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000133 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000134 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000135 else if (PyObject_AsCharBuffer(v, &s, &len)) {
136 PyErr_SetString(PyExc_TypeError,
137 "float() needs a string argument");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000139 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000140
Guido van Rossum4c08d552000-03-10 22:55:18 +0000141 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000142 while (*s && isspace(Py_CHARMASK(*s)))
143 s++;
Tim Petersef14d732000-09-23 03:39:17 +0000144 if (*s == '\0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000145 PyErr_SetString(PyExc_ValueError, "empty string for float()");
146 return NULL;
147 }
Tim Petersef14d732000-09-23 03:39:17 +0000148 /* We don't care about overflow or underflow. If the platform supports
149 * them, infinities and signed zeroes (on underflow) are fine.
150 * However, strtod can return 0 for denormalized numbers, where atof
151 * does not. So (alas!) we special-case a zero result. Note that
152 * whether strtod sets errno on underflow is not defined, so we can't
153 * key off errno.
154 */
Tim Peters858346e2000-09-25 21:01:28 +0000155 PyFPE_START_PROTECT("strtod", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000156 x = strtod(s, (char **)&end);
Tim Peters858346e2000-09-25 21:01:28 +0000157 PyFPE_END_PROTECT(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000158 errno = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000159 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
Tim Petersef14d732000-09-23 03:39:17 +0000160 byte at the end of the string, when the input is inf(inity). */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000161 if (end > last)
162 end = last;
Tim Petersef14d732000-09-23 03:39:17 +0000163 if (end == s) {
164 sprintf(buffer, "invalid literal for float(): %.200s", s);
165 PyErr_SetString(PyExc_ValueError, buffer);
166 return NULL;
167 }
168 /* Since end != s, the platform made *some* kind of sense out
169 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000170 while (*end && isspace(Py_CHARMASK(*end)))
171 end++;
172 if (*end != '\0') {
173 sprintf(buffer, "invalid literal for float(): %.200s", s);
174 PyErr_SetString(PyExc_ValueError, buffer);
175 return NULL;
176 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000177 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178 PyErr_SetString(PyExc_ValueError,
179 "null byte in argument for float()");
180 return NULL;
181 }
Tim Petersef14d732000-09-23 03:39:17 +0000182 if (x == 0.0) {
183 /* See above -- may have been strtod being anal
184 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000185 PyFPE_START_PROTECT("atof", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000186 x = atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000187 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000188 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000189 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000190 return PyFloat_FromDouble(x);
191}
192
Guido van Rossum234f9421993-06-17 12:35:49 +0000193static void
Fred Drakefd99de62000-07-09 05:02:18 +0000194float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000195{
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000196 op->ob_type = (struct _typeobject *)free_list;
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000197 free_list = op;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000198}
199
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200double
Fred Drakefd99de62000-07-09 05:02:18 +0000201PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 PyNumberMethods *nb;
204 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205 double val;
206
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 if (op && PyFloat_Check(op))
208 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
210 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
211 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 return -1;
214 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217 if (fo == NULL)
218 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 if (!PyFloat_Check(fo)) {
220 PyErr_SetString(PyExc_TypeError,
221 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222 return -1;
223 }
224
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 val = PyFloat_AS_DOUBLE(fo);
226 Py_DECREF(fo);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227
228 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
231/* Methods */
232
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000233void
Fred Drakefd99de62000-07-09 05:02:18 +0000234PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235{
236 register char *cp;
237 /* Subroutine for float_repr and float_print.
238 We want float numbers to be recognizable as such,
239 i.e., they should contain a decimal point or an exponent.
240 However, %g may print the number as an integer;
241 in such cases, we append ".0" to the string. */
Guido van Rossum57072eb1999-12-23 19:00:28 +0000242 sprintf(buf, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 cp = buf;
244 if (*cp == '-')
245 cp++;
246 for (; *cp != '\0'; cp++) {
247 /* Any non-digit means it's not an integer;
248 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000249 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 break;
251 }
252 if (*cp == '\0') {
253 *cp++ = '.';
254 *cp++ = '0';
255 *cp++ = '\0';
256 }
257}
258
Neil Schemenauer32117e52001-01-04 01:44:34 +0000259/* Macro and helper that convert PyObject obj to a C double and store
260 the value in dbl; this replaces the functionality of the coercion
261 slot function */
262
263#define CONVERT_TO_DOUBLE(obj, dbl) \
264 if (PyFloat_Check(obj)) \
265 dbl = PyFloat_AS_DOUBLE(obj); \
266 else if (convert_to_double(&(obj), &(dbl)) < 0) \
267 return obj;
268
269static int
270convert_to_double(PyObject **v,
271 double *dbl)
272{
273 register PyObject *obj = *v;
274
275 if (PyInt_Check(obj)) {
276 *dbl = (double)PyInt_AS_LONG(obj);
277 }
278 else if (PyLong_Check(obj)) {
279 PyFPE_START_PROTECT("convert_to_double", {*v=NULL;return -1;})
280 *dbl = PyLong_AsDouble(obj);
Jeremy Hylton2492a202001-02-01 23:53:05 +0000281 PyFPE_END_PROTECT(*dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000282 }
283 else {
284 Py_INCREF(Py_NotImplemented);
285 *v = Py_NotImplemented;
286 return -1;
287 }
288 return 0;
289}
290
Guido van Rossum57072eb1999-12-23 19:00:28 +0000291/* Precisions used by repr() and str(), respectively.
292
293 The repr() precision (17 significant decimal digits) is the minimal number
294 that is guaranteed to have enough precision so that if the number is read
295 back in the exact same binary value is recreated. This is true for IEEE
296 floating point by design, and also happens to work for all other modern
297 hardware.
298
299 The str() precision is chosen so that in most cases, the rounding noise
300 created by various operations is suppressed, while giving plenty of
301 precision for practical use.
302
303*/
304
305#define PREC_REPR 17
306#define PREC_STR 12
307
308void
Fred Drakefd99de62000-07-09 05:02:18 +0000309PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000310{
311 PyFloat_AsStringEx(buf, v, PREC_STR);
312}
313
Tim Peters72f98e92001-05-08 15:19:57 +0000314void
315PyFloat_AsReprString(char *buf, PyFloatObject *v)
316{
317 PyFloat_AsStringEx(buf, v, PREC_REPR);
318}
319
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000320/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000321static int
Fred Drakefd99de62000-07-09 05:02:18 +0000322float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323{
324 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000325 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000327 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328}
329
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000331float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332{
333 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000334 PyFloat_AsStringEx(buf, v, PREC_REPR);
335 return PyString_FromString(buf);
336}
337
338static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000339float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000340{
341 char buf[100];
342 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344}
345
346static int
Fred Drakefd99de62000-07-09 05:02:18 +0000347float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348{
349 double i = v->ob_fval;
350 double j = w->ob_fval;
351 return (i < j) ? -1 : (i > j) ? 1 : 0;
352}
353
Guido van Rossum9bfef441993-03-29 10:43:31 +0000354static long
Fred Drakefd99de62000-07-09 05:02:18 +0000355float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000356{
Tim Peters39dce292000-08-15 03:34:48 +0000357 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000358}
359
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000360static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000361float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000363 double a,b;
364 CONVERT_TO_DOUBLE(v, a);
365 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000366 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000367 a = a + b;
368 PyFPE_END_PROTECT(a)
369 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370}
371
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000373float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000375 double a,b;
376 CONVERT_TO_DOUBLE(v, a);
377 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000378 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000379 a = a - b;
380 PyFPE_END_PROTECT(a)
381 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382}
383
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000385float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000387 double a,b;
388 CONVERT_TO_DOUBLE(v, a);
389 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000390 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000391 a = a * b;
392 PyFPE_END_PROTECT(a)
393 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394}
395
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000397float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000399 double a,b;
400 CONVERT_TO_DOUBLE(v, a);
401 CONVERT_TO_DOUBLE(w, b);
402 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404 return NULL;
405 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000406 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000407 a = a / b;
408 PyFPE_END_PROTECT(a)
409 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410}
411
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000413float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000415 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000416 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000417 CONVERT_TO_DOUBLE(v, vx);
418 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421 return NULL;
422 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000423 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000424 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000425 /* note: checking mod*wx < 0 is incorrect -- underflows to
426 0 if wx < sqrt(smallest nonzero double) */
427 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000428 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000429 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000430 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432}
433
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000435float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000436{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000437 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000438 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000439 CONVERT_TO_DOUBLE(v, vx);
440 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000441 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000443 return NULL;
444 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000445 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000446 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000447 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000448 exact multiple of wx. But this is fp arithmetic, and fp
449 vx - mod is an approximation; the result is that div may
450 not be an exact integral value after the division, although
451 it will always be very close to one.
452 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000453 div = (vx - mod) / wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000454 /* note: checking mod*wx < 0 is incorrect -- underflows to
455 0 if wx < sqrt(smallest nonzero double) */
456 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum15ecff41991-10-20 20:16:45 +0000457 mod += wx;
458 div -= 1.0;
459 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000460 /* snap quotient to nearest integral value */
461 floordiv = floor(div);
462 if (div - floordiv > 0.5)
463 floordiv += 1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000464 PyFPE_END_PROTECT(div)
Guido van Rossum9263e781999-05-06 14:26:34 +0000465 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000466}
467
Fred Drakefd99de62000-07-09 05:02:18 +0000468static double powu(double x, long n)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000469{
470 double r = 1.;
471 double p = x;
472 long mask = 1;
473 while (mask > 0 && n >= mask) {
474 if (n & mask)
475 r *= p;
476 mask <<= 1;
477 p *= p;
478 }
479 return r;
480}
481
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000483float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484{
485 double iv, iw, ix;
Guido van Rossum39739ea1996-01-12 01:22:56 +0000486 long intw;
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000487 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
488 * The z parameter is really only going to be useful for integers and
489 * long integers. Maybe something clever with logarithms could be done.
490 * [AMK]
491 */
Neil Schemenauer32117e52001-01-04 01:44:34 +0000492 CONVERT_TO_DOUBLE(v, iv);
493 CONVERT_TO_DOUBLE(w, iw);
Guido van Rossum39739ea1996-01-12 01:22:56 +0000494 intw = (long)iw;
Tim Petersc54d1902000-10-06 00:36:09 +0000495
496 /* Sort out special cases here instead of relying on pow() */
497 if (iw == 0) { /* x**0 is 1, even 0**0 */
498 PyFPE_START_PROTECT("pow", return NULL)
499 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000500 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000501 CONVERT_TO_DOUBLE(z, iz);
Neil Schemenauer32117e52001-01-04 01:44:34 +0000502 ix=fmod(1.0, iz);
503 if (ix!=0 && iz<0) ix+=iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000504 }
Tim Petersc54d1902000-10-06 00:36:09 +0000505 else
506 ix = 1.0;
507 PyFPE_END_PROTECT(ix)
508 return PyFloat_FromDouble(ix);
509 }
510 if (iv == 0.0) {
511 if (iw < 0.0) {
512 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000513 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000514 return NULL;
515 }
516 return PyFloat_FromDouble(0.0);
517 }
518
519 if (iw == intw && intw > LONG_MIN) {
520 /* ruled out LONG_MIN because -LONG_MIN isn't representable */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000521 errno = 0;
Tim Petersc54d1902000-10-06 00:36:09 +0000522 PyFPE_START_PROTECT("pow", return NULL)
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000523 if (intw > 0)
524 ix = powu(iv, intw);
525 else
526 ix = 1./powu(iv, -intw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000527 PyFPE_END_PROTECT(ix)
Guido van Rossum86c04c21996-08-09 20:50:14 +0000528 }
529 else {
530 /* Sort out special cases here instead of relying on pow() */
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000531 if (iv < 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000533 "negative number cannot be raised to a fractional power");
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000534 return NULL;
535 }
Guido van Rossum39739ea1996-01-12 01:22:56 +0000536 errno = 0;
Tim Petersc54d1902000-10-06 00:36:09 +0000537 PyFPE_START_PROTECT("pow", return NULL)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000538 ix = pow(iv, iw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000539 PyFPE_END_PROTECT(ix)
Guido van Rossum70d93461991-05-28 21:57:39 +0000540 }
Guido van Rossum7fa52f81991-12-16 15:43:14 +0000541 CHECK(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000542 if (errno != 0) {
543 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000546 }
Tim Petersc54d1902000-10-06 00:36:09 +0000547 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000548 double iz;
549 CONVERT_TO_DOUBLE(z, iz);
550 PyFPE_START_PROTECT("pow", return 0)
551 ix=fmod(ix, iz); /* XXX To Be Rewritten */
552 if (ix!=0 && ((iv<0 && iz>0) || (iv>0 && iz<0) )) {
553 ix+=iz;
Tim Petersc54d1902000-10-06 00:36:09 +0000554 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000555 PyFPE_END_PROTECT(ix)
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000556 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558}
559
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000561float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564}
565
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000567float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 Py_INCREF(v);
570 return (PyObject *)v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000571}
572
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000574float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000575{
576 if (v->ob_fval < 0)
577 return float_neg(v);
578 else
579 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580}
581
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000582static int
Fred Drakefd99de62000-07-09 05:02:18 +0000583float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000584{
585 return v->ob_fval != 0.0;
586}
587
Guido van Rossum234f9421993-06-17 12:35:49 +0000588static int
Fred Drakefd99de62000-07-09 05:02:18 +0000589float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000590{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 if (PyInt_Check(*pw)) {
592 long x = PyInt_AsLong(*pw);
593 *pw = PyFloat_FromDouble((double)x);
594 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000595 return 0;
596 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597 else if (PyLong_Check(*pw)) {
598 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
599 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000600 return 0;
601 }
602 return 1; /* Can't do it */
603}
604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000606float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000607{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000609 double wholepart; /* integral portion of x, rounded toward 0 */
610 long aslong; /* (long)wholepart */
611
612 (void)modf(x, &wholepart);
613 /* doubles may have more bits than longs, or vice versa; and casting
614 to long may yield gibberish in either case. What really matters
615 is whether converting back to double again reproduces what we
616 started with. */
617 aslong = (long)wholepart;
618 if ((double)aslong == wholepart)
619 return PyInt_FromLong(aslong);
620 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
621 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000622}
623
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000625float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000626{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 double x = PyFloat_AsDouble(v);
628 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000629}
630
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000632float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000633{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000635 return v;
636}
637
638
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639static PyObject *
640float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
641{
642 PyObject *x = Py_False; /* Integer zero */
643 static char *kwlist[] = {"x", 0};
644
645 assert(type == &PyFloat_Type);
646 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
647 return NULL;
648 if (PyString_Check(x))
649 return PyFloat_FromString(x, NULL);
650 return PyNumber_Float(x);
651}
652
653static char float_doc[] =
654"float(x) -> floating point number\n\
655\n\
656Convert a string or number to a floating point number, if possible.";
657
658
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660 (binaryfunc)float_add, /*nb_add*/
661 (binaryfunc)float_sub, /*nb_subtract*/
662 (binaryfunc)float_mul, /*nb_multiply*/
663 (binaryfunc)float_div, /*nb_divide*/
664 (binaryfunc)float_rem, /*nb_remainder*/
665 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000666 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000667 (unaryfunc)float_neg, /*nb_negative*/
668 (unaryfunc)float_pos, /*nb_positive*/
669 (unaryfunc)float_abs, /*nb_absolute*/
670 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000671 0, /*nb_invert*/
672 0, /*nb_lshift*/
673 0, /*nb_rshift*/
674 0, /*nb_and*/
675 0, /*nb_xor*/
676 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000677 (coercion)float_coerce, /*nb_coerce*/
678 (unaryfunc)float_int, /*nb_int*/
679 (unaryfunc)float_long, /*nb_long*/
680 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000681 0, /*nb_oct*/
682 0, /*nb_hex*/
Neil Schemenauer32117e52001-01-04 01:44:34 +0000683 0, /*nb_inplace_add*/
684 0, /*nb_inplace_subtract*/
685 0, /*nb_inplace_multiply*/
686 0, /*nb_inplace_divide*/
687 0, /*nb_inplace_remainder*/
688 0, /*nb_inplace_power*/
689 0, /*nb_inplace_lshift*/
690 0, /*nb_inplace_rshift*/
691 0, /*nb_inplace_and*/
692 0, /*nb_inplace_xor*/
693 0, /*nb_inplace_or*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694};
695
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696PyTypeObject PyFloat_Type = {
697 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000698 0,
699 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000702 (destructor)float_dealloc, /* tp_dealloc */
703 (printfunc)float_print, /* tp_print */
704 0, /* tp_getattr */
705 0, /* tp_setattr */
706 (cmpfunc)float_compare, /* tp_compare */
707 (reprfunc)float_repr, /* tp_repr */
708 &float_as_number, /* tp_as_number */
709 0, /* tp_as_sequence */
710 0, /* tp_as_mapping */
711 (hashfunc)float_hash, /* tp_hash */
712 0, /* tp_call */
713 (reprfunc)float_str, /* tp_str */
714 PyObject_GenericGetAttr, /* tp_getattro */
715 0, /* tp_setattro */
716 0, /* tp_as_buffer */
717 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
718 float_doc, /* tp_doc */
719 0, /* tp_traverse */
720 0, /* tp_clear */
721 0, /* tp_richcompare */
722 0, /* tp_weaklistoffset */
723 0, /* tp_iter */
724 0, /* tp_iternext */
725 0, /* tp_methods */
726 0, /* tp_members */
727 0, /* tp_getset */
728 0, /* tp_base */
729 0, /* tp_dict */
730 0, /* tp_descr_get */
731 0, /* tp_descr_set */
732 0, /* tp_dictoffset */
733 0, /* tp_init */
734 0, /* tp_alloc */
735 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000737
738void
Fred Drakefd99de62000-07-09 05:02:18 +0000739PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000740{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000741 PyFloatObject *p;
742 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000743 int i;
744 int bc, bf; /* block count, number of freed blocks */
745 int frem, fsum; /* remaining unfreed floats per block, total */
746
747 bc = 0;
748 bf = 0;
749 fsum = 0;
750 list = block_list;
751 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000752 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000753 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000754 bc++;
755 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000756 for (i = 0, p = &list->objects[0];
757 i < N_FLOATOBJECTS;
758 i++, p++) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000759 if (PyFloat_Check(p) && p->ob_refcnt != 0)
760 frem++;
761 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000762 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000763 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000764 list->next = block_list;
765 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000766 for (i = 0, p = &list->objects[0];
767 i < N_FLOATOBJECTS;
768 i++, p++) {
769 if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
770 p->ob_type = (struct _typeobject *)
771 free_list;
772 free_list = p;
773 }
774 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000775 }
776 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000777 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000778 bf++;
779 }
780 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000781 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000782 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000783 if (!Py_VerboseFlag)
784 return;
785 fprintf(stderr, "# cleanup floats");
786 if (!fsum) {
787 fprintf(stderr, "\n");
788 }
789 else {
790 fprintf(stderr,
791 ": %d unfreed float%s in %d out of %d block%s\n",
792 fsum, fsum == 1 ? "" : "s",
793 bc - bf, bc, bc == 1 ? "" : "s");
794 }
795 if (Py_VerboseFlag > 1) {
796 list = block_list;
797 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000798 for (i = 0, p = &list->objects[0];
799 i < N_FLOATOBJECTS;
800 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000801 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
802 char buf[100];
803 PyFloat_AsString(buf, p);
804 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000805 "# <float at %p, refcnt=%d, val=%s>\n",
806 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000807 }
808 }
809 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000810 }
811 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000812}