blob: 044d1d3af8c2a126ee90d4366e42fe52cd8b26e9 [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 */
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000112#ifdef Py_USING_UNICODE
Tim Petersef14d732000-09-23 03:39:17 +0000113 char s_buffer[256]; /* for objects convertible to a char buffer */
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000114#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000115 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000116
Tim Petersef14d732000-09-23 03:39:17 +0000117 if (pend)
118 *pend = NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000119 if (PyString_Check(v)) {
120 s = PyString_AS_STRING(v);
121 len = PyString_GET_SIZE(v);
122 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000123#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000124 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000125 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
126 PyErr_SetString(PyExc_ValueError,
Tim Petersef14d732000-09-23 03:39:17 +0000127 "Unicode float() literal too long to convert");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000128 return NULL;
129 }
Tim Petersef14d732000-09-23 03:39:17 +0000130 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000131 PyUnicode_GET_SIZE(v),
132 s_buffer,
133 NULL))
134 return NULL;
135 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000136 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000137 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000138#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000139 else if (PyObject_AsCharBuffer(v, &s, &len)) {
140 PyErr_SetString(PyExc_TypeError,
141 "float() needs a string argument");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000142 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000143 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000144
Guido van Rossum4c08d552000-03-10 22:55:18 +0000145 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000146 while (*s && isspace(Py_CHARMASK(*s)))
147 s++;
Tim Petersef14d732000-09-23 03:39:17 +0000148 if (*s == '\0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000149 PyErr_SetString(PyExc_ValueError, "empty string for float()");
150 return NULL;
151 }
Tim Petersef14d732000-09-23 03:39:17 +0000152 /* We don't care about overflow or underflow. If the platform supports
153 * them, infinities and signed zeroes (on underflow) are fine.
154 * However, strtod can return 0 for denormalized numbers, where atof
155 * does not. So (alas!) we special-case a zero result. Note that
156 * whether strtod sets errno on underflow is not defined, so we can't
157 * key off errno.
158 */
Tim Peters858346e2000-09-25 21:01:28 +0000159 PyFPE_START_PROTECT("strtod", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000160 x = strtod(s, (char **)&end);
Tim Peters858346e2000-09-25 21:01:28 +0000161 PyFPE_END_PROTECT(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000162 errno = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000163 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
Tim Petersef14d732000-09-23 03:39:17 +0000164 byte at the end of the string, when the input is inf(inity). */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000165 if (end > last)
166 end = last;
Tim Petersef14d732000-09-23 03:39:17 +0000167 if (end == s) {
168 sprintf(buffer, "invalid literal for float(): %.200s", s);
169 PyErr_SetString(PyExc_ValueError, buffer);
170 return NULL;
171 }
172 /* Since end != s, the platform made *some* kind of sense out
173 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000174 while (*end && isspace(Py_CHARMASK(*end)))
175 end++;
176 if (*end != '\0') {
177 sprintf(buffer, "invalid literal for float(): %.200s", s);
178 PyErr_SetString(PyExc_ValueError, buffer);
179 return NULL;
180 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000181 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000182 PyErr_SetString(PyExc_ValueError,
183 "null byte in argument for float()");
184 return NULL;
185 }
Tim Petersef14d732000-09-23 03:39:17 +0000186 if (x == 0.0) {
187 /* See above -- may have been strtod being anal
188 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000189 PyFPE_START_PROTECT("atof", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000190 x = atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000191 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000192 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000193 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000194 return PyFloat_FromDouble(x);
195}
196
Guido van Rossum234f9421993-06-17 12:35:49 +0000197static void
Fred Drakefd99de62000-07-09 05:02:18 +0000198float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000199{
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000200 op->ob_type = (struct _typeobject *)free_list;
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000201 free_list = op;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000202}
203
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204double
Fred Drakefd99de62000-07-09 05:02:18 +0000205PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 PyNumberMethods *nb;
208 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209 double val;
210
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 if (op && PyFloat_Check(op))
212 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213
214 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
215 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217 return -1;
218 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221 if (fo == NULL)
222 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223 if (!PyFloat_Check(fo)) {
224 PyErr_SetString(PyExc_TypeError,
225 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226 return -1;
227 }
228
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 val = PyFloat_AS_DOUBLE(fo);
230 Py_DECREF(fo);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231
232 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233}
234
235/* Methods */
236
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000237void
Fred Drakefd99de62000-07-09 05:02:18 +0000238PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239{
240 register char *cp;
241 /* Subroutine for float_repr and float_print.
242 We want float numbers to be recognizable as such,
243 i.e., they should contain a decimal point or an exponent.
244 However, %g may print the number as an integer;
245 in such cases, we append ".0" to the string. */
Guido van Rossum57072eb1999-12-23 19:00:28 +0000246 sprintf(buf, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 cp = buf;
248 if (*cp == '-')
249 cp++;
250 for (; *cp != '\0'; cp++) {
251 /* Any non-digit means it's not an integer;
252 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000253 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254 break;
255 }
256 if (*cp == '\0') {
257 *cp++ = '.';
258 *cp++ = '0';
259 *cp++ = '\0';
260 }
261}
262
Neil Schemenauer32117e52001-01-04 01:44:34 +0000263/* Macro and helper that convert PyObject obj to a C double and store
264 the value in dbl; this replaces the functionality of the coercion
265 slot function */
266
267#define CONVERT_TO_DOUBLE(obj, dbl) \
268 if (PyFloat_Check(obj)) \
269 dbl = PyFloat_AS_DOUBLE(obj); \
270 else if (convert_to_double(&(obj), &(dbl)) < 0) \
271 return obj;
272
273static int
274convert_to_double(PyObject **v,
275 double *dbl)
276{
277 register PyObject *obj = *v;
278
279 if (PyInt_Check(obj)) {
280 *dbl = (double)PyInt_AS_LONG(obj);
281 }
282 else if (PyLong_Check(obj)) {
283 PyFPE_START_PROTECT("convert_to_double", {*v=NULL;return -1;})
284 *dbl = PyLong_AsDouble(obj);
Jeremy Hylton2492a202001-02-01 23:53:05 +0000285 PyFPE_END_PROTECT(*dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000286 }
287 else {
288 Py_INCREF(Py_NotImplemented);
289 *v = Py_NotImplemented;
290 return -1;
291 }
292 return 0;
293}
294
Guido van Rossum57072eb1999-12-23 19:00:28 +0000295/* Precisions used by repr() and str(), respectively.
296
297 The repr() precision (17 significant decimal digits) is the minimal number
298 that is guaranteed to have enough precision so that if the number is read
299 back in the exact same binary value is recreated. This is true for IEEE
300 floating point by design, and also happens to work for all other modern
301 hardware.
302
303 The str() precision is chosen so that in most cases, the rounding noise
304 created by various operations is suppressed, while giving plenty of
305 precision for practical use.
306
307*/
308
309#define PREC_REPR 17
310#define PREC_STR 12
311
312void
Fred Drakefd99de62000-07-09 05:02:18 +0000313PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000314{
315 PyFloat_AsStringEx(buf, v, PREC_STR);
316}
317
Tim Peters72f98e92001-05-08 15:19:57 +0000318void
319PyFloat_AsReprString(char *buf, PyFloatObject *v)
320{
321 PyFloat_AsStringEx(buf, v, PREC_REPR);
322}
323
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000324/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000325static int
Fred Drakefd99de62000-07-09 05:02:18 +0000326float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327{
328 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000329 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000331 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332}
333
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000335float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336{
337 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000338 PyFloat_AsStringEx(buf, v, PREC_REPR);
339 return PyString_FromString(buf);
340}
341
342static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000343float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000344{
345 char buf[100];
346 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348}
349
350static int
Fred Drakefd99de62000-07-09 05:02:18 +0000351float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352{
353 double i = v->ob_fval;
354 double j = w->ob_fval;
355 return (i < j) ? -1 : (i > j) ? 1 : 0;
356}
357
Guido van Rossum9bfef441993-03-29 10:43:31 +0000358static long
Fred Drakefd99de62000-07-09 05:02:18 +0000359float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000360{
Tim Peters39dce292000-08-15 03:34:48 +0000361 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000362}
363
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000365float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000367 double a,b;
368 CONVERT_TO_DOUBLE(v, a);
369 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000370 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000371 a = a + b;
372 PyFPE_END_PROTECT(a)
373 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374}
375
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000377float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000379 double a,b;
380 CONVERT_TO_DOUBLE(v, a);
381 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000382 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000383 a = a - b;
384 PyFPE_END_PROTECT(a)
385 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386}
387
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000388static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000389float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000391 double a,b;
392 CONVERT_TO_DOUBLE(v, a);
393 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000394 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000395 a = a * b;
396 PyFPE_END_PROTECT(a)
397 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000401float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000403 double a,b;
404 CONVERT_TO_DOUBLE(v, a);
405 CONVERT_TO_DOUBLE(w, b);
406 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408 return NULL;
409 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000410 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000411 a = a / b;
412 PyFPE_END_PROTECT(a)
413 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414}
415
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000417float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000419 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000420 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000421 CONVERT_TO_DOUBLE(v, vx);
422 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425 return NULL;
426 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000427 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000428 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000429 /* note: checking mod*wx < 0 is incorrect -- underflows to
430 0 if wx < sqrt(smallest nonzero double) */
431 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000432 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000433 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000434 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000439float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000440{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000441 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000442 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000443 CONVERT_TO_DOUBLE(v, vx);
444 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000445 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000447 return NULL;
448 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000449 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000450 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000451 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000452 exact multiple of wx. But this is fp arithmetic, and fp
453 vx - mod is an approximation; the result is that div may
454 not be an exact integral value after the division, although
455 it will always be very close to one.
456 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000457 div = (vx - mod) / wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000458 /* note: checking mod*wx < 0 is incorrect -- underflows to
459 0 if wx < sqrt(smallest nonzero double) */
460 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum15ecff41991-10-20 20:16:45 +0000461 mod += wx;
462 div -= 1.0;
463 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000464 /* snap quotient to nearest integral value */
465 floordiv = floor(div);
466 if (div - floordiv > 0.5)
467 floordiv += 1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000468 PyFPE_END_PROTECT(div)
Guido van Rossum9263e781999-05-06 14:26:34 +0000469 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000470}
471
Fred Drakefd99de62000-07-09 05:02:18 +0000472static double powu(double x, long n)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000473{
474 double r = 1.;
475 double p = x;
476 long mask = 1;
477 while (mask > 0 && n >= mask) {
478 if (n & mask)
479 r *= p;
480 mask <<= 1;
481 p *= p;
482 }
483 return r;
484}
485
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000487float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488{
489 double iv, iw, ix;
Guido van Rossum39739ea1996-01-12 01:22:56 +0000490 long intw;
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000491 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
492 * The z parameter is really only going to be useful for integers and
493 * long integers. Maybe something clever with logarithms could be done.
494 * [AMK]
495 */
Neil Schemenauer32117e52001-01-04 01:44:34 +0000496 CONVERT_TO_DOUBLE(v, iv);
497 CONVERT_TO_DOUBLE(w, iw);
Guido van Rossum39739ea1996-01-12 01:22:56 +0000498 intw = (long)iw;
Tim Petersc54d1902000-10-06 00:36:09 +0000499
500 /* Sort out special cases here instead of relying on pow() */
501 if (iw == 0) { /* x**0 is 1, even 0**0 */
502 PyFPE_START_PROTECT("pow", return NULL)
503 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000504 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000505 CONVERT_TO_DOUBLE(z, iz);
Neil Schemenauer32117e52001-01-04 01:44:34 +0000506 ix=fmod(1.0, iz);
507 if (ix!=0 && iz<0) ix+=iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000508 }
Tim Petersc54d1902000-10-06 00:36:09 +0000509 else
510 ix = 1.0;
511 PyFPE_END_PROTECT(ix)
512 return PyFloat_FromDouble(ix);
513 }
514 if (iv == 0.0) {
515 if (iw < 0.0) {
516 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000517 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000518 return NULL;
519 }
520 return PyFloat_FromDouble(0.0);
521 }
522
523 if (iw == intw && intw > LONG_MIN) {
524 /* ruled out LONG_MIN because -LONG_MIN isn't representable */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000525 errno = 0;
Tim Petersc54d1902000-10-06 00:36:09 +0000526 PyFPE_START_PROTECT("pow", return NULL)
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000527 if (intw > 0)
528 ix = powu(iv, intw);
529 else
530 ix = 1./powu(iv, -intw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000531 PyFPE_END_PROTECT(ix)
Guido van Rossum86c04c21996-08-09 20:50:14 +0000532 }
533 else {
534 /* Sort out special cases here instead of relying on pow() */
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000535 if (iv < 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000537 "negative number cannot be raised to a fractional power");
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000538 return NULL;
539 }
Guido van Rossum39739ea1996-01-12 01:22:56 +0000540 errno = 0;
Tim Petersc54d1902000-10-06 00:36:09 +0000541 PyFPE_START_PROTECT("pow", return NULL)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000542 ix = pow(iv, iw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000543 PyFPE_END_PROTECT(ix)
Guido van Rossum70d93461991-05-28 21:57:39 +0000544 }
Guido van Rossum7fa52f81991-12-16 15:43:14 +0000545 CHECK(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000546 if (errno != 0) {
547 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000550 }
Tim Petersc54d1902000-10-06 00:36:09 +0000551 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000552 double iz;
553 CONVERT_TO_DOUBLE(z, iz);
554 PyFPE_START_PROTECT("pow", return 0)
555 ix=fmod(ix, iz); /* XXX To Be Rewritten */
556 if (ix!=0 && ((iv<0 && iz>0) || (iv>0 && iz<0) )) {
557 ix+=iz;
Tim Petersc54d1902000-10-06 00:36:09 +0000558 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000559 PyFPE_END_PROTECT(ix)
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000560 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562}
563
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000565float_int_div(PyObject *v, PyObject *w)
566{
567 PyObject *t, *r;
568
569 t = float_divmod(v, w);
570 if (t != NULL) {
571 r = PyTuple_GET_ITEM(t, 0);
572 Py_INCREF(r);
573 Py_DECREF(t);
574 return r;
575 }
576 return NULL;
577}
578
579static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000580float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583}
584
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000586float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 Py_INCREF(v);
589 return (PyObject *)v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000590}
591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000593float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000594{
595 if (v->ob_fval < 0)
596 return float_neg(v);
597 else
598 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599}
600
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000601static int
Fred Drakefd99de62000-07-09 05:02:18 +0000602float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000603{
604 return v->ob_fval != 0.0;
605}
606
Guido van Rossum234f9421993-06-17 12:35:49 +0000607static int
Fred Drakefd99de62000-07-09 05:02:18 +0000608float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000609{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 if (PyInt_Check(*pw)) {
611 long x = PyInt_AsLong(*pw);
612 *pw = PyFloat_FromDouble((double)x);
613 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000614 return 0;
615 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 else if (PyLong_Check(*pw)) {
617 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
618 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000619 return 0;
620 }
621 return 1; /* Can't do it */
622}
623
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000625float_int(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);
Tim Peters7321ec42001-07-26 20:02:17 +0000628 double wholepart; /* integral portion of x, rounded toward 0 */
629 long aslong; /* (long)wholepart */
630
631 (void)modf(x, &wholepart);
632 /* doubles may have more bits than longs, or vice versa; and casting
633 to long may yield gibberish in either case. What really matters
634 is whether converting back to double again reproduces what we
635 started with. */
636 aslong = (long)wholepart;
637 if ((double)aslong == wholepart)
638 return PyInt_FromLong(aslong);
639 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
640 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000641}
642
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000644float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000645{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 double x = PyFloat_AsDouble(v);
647 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000648}
649
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000651float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000652{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000654 return v;
655}
656
657
Tim Peters6d6c1a32001-08-02 04:15:00 +0000658static PyObject *
659float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
660{
661 PyObject *x = Py_False; /* Integer zero */
662 static char *kwlist[] = {"x", 0};
663
664 assert(type == &PyFloat_Type);
665 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
666 return NULL;
667 if (PyString_Check(x))
668 return PyFloat_FromString(x, NULL);
669 return PyNumber_Float(x);
670}
671
672static char float_doc[] =
673"float(x) -> floating point number\n\
674\n\
675Convert a string or number to a floating point number, if possible.";
676
677
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000679 (binaryfunc)float_add, /*nb_add*/
680 (binaryfunc)float_sub, /*nb_subtract*/
681 (binaryfunc)float_mul, /*nb_multiply*/
682 (binaryfunc)float_div, /*nb_divide*/
683 (binaryfunc)float_rem, /*nb_remainder*/
684 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000685 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000686 (unaryfunc)float_neg, /*nb_negative*/
687 (unaryfunc)float_pos, /*nb_positive*/
688 (unaryfunc)float_abs, /*nb_absolute*/
689 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000690 0, /*nb_invert*/
691 0, /*nb_lshift*/
692 0, /*nb_rshift*/
693 0, /*nb_and*/
694 0, /*nb_xor*/
695 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000696 (coercion)float_coerce, /*nb_coerce*/
697 (unaryfunc)float_int, /*nb_int*/
698 (unaryfunc)float_long, /*nb_long*/
699 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000700 0, /* nb_oct */
701 0, /* nb_hex */
702 0, /* nb_inplace_add */
703 0, /* nb_inplace_subtract */
704 0, /* nb_inplace_multiply */
705 0, /* nb_inplace_divide */
706 0, /* nb_inplace_remainder */
707 0, /* nb_inplace_power */
708 0, /* nb_inplace_lshift */
709 0, /* nb_inplace_rshift */
710 0, /* nb_inplace_and */
711 0, /* nb_inplace_xor */
712 0, /* nb_inplace_or */
713 float_int_div, /* nb_floor_divide */
714 float_div, /* nb_true_divide */
715 0, /* nb_inplace_floor_divide */
716 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717};
718
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719PyTypeObject PyFloat_Type = {
720 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721 0,
722 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000724 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725 (destructor)float_dealloc, /* tp_dealloc */
726 (printfunc)float_print, /* tp_print */
727 0, /* tp_getattr */
728 0, /* tp_setattr */
729 (cmpfunc)float_compare, /* tp_compare */
730 (reprfunc)float_repr, /* tp_repr */
731 &float_as_number, /* tp_as_number */
732 0, /* tp_as_sequence */
733 0, /* tp_as_mapping */
734 (hashfunc)float_hash, /* tp_hash */
735 0, /* tp_call */
736 (reprfunc)float_str, /* tp_str */
737 PyObject_GenericGetAttr, /* tp_getattro */
738 0, /* tp_setattro */
739 0, /* tp_as_buffer */
740 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
741 float_doc, /* tp_doc */
742 0, /* tp_traverse */
743 0, /* tp_clear */
744 0, /* tp_richcompare */
745 0, /* tp_weaklistoffset */
746 0, /* tp_iter */
747 0, /* tp_iternext */
748 0, /* tp_methods */
749 0, /* tp_members */
750 0, /* tp_getset */
751 0, /* tp_base */
752 0, /* tp_dict */
753 0, /* tp_descr_get */
754 0, /* tp_descr_set */
755 0, /* tp_dictoffset */
756 0, /* tp_init */
757 0, /* tp_alloc */
758 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000759};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000760
761void
Fred Drakefd99de62000-07-09 05:02:18 +0000762PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000763{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000764 PyFloatObject *p;
765 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000766 int i;
767 int bc, bf; /* block count, number of freed blocks */
768 int frem, fsum; /* remaining unfreed floats per block, total */
769
770 bc = 0;
771 bf = 0;
772 fsum = 0;
773 list = block_list;
774 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000775 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000776 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000777 bc++;
778 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000779 for (i = 0, p = &list->objects[0];
780 i < N_FLOATOBJECTS;
781 i++, p++) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000782 if (PyFloat_Check(p) && p->ob_refcnt != 0)
783 frem++;
784 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000785 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000786 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000787 list->next = block_list;
788 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000789 for (i = 0, p = &list->objects[0];
790 i < N_FLOATOBJECTS;
791 i++, p++) {
792 if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
793 p->ob_type = (struct _typeobject *)
794 free_list;
795 free_list = p;
796 }
797 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000798 }
799 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000800 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000801 bf++;
802 }
803 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000804 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000805 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000806 if (!Py_VerboseFlag)
807 return;
808 fprintf(stderr, "# cleanup floats");
809 if (!fsum) {
810 fprintf(stderr, "\n");
811 }
812 else {
813 fprintf(stderr,
814 ": %d unfreed float%s in %d out of %d block%s\n",
815 fsum, fsum == 1 ? "" : "s",
816 bc - bf, bc, bc == 1 ? "" : "s");
817 }
818 if (Py_VerboseFlag > 1) {
819 list = block_list;
820 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000821 for (i = 0, p = &list->objects[0];
822 i < N_FLOATOBJECTS;
823 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000824 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
825 char buf[100];
826 PyFloat_AsString(buf, p);
827 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000828 "# <float at %p, refcnt=%d, val=%s>\n",
829 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000830 }
831 }
832 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000833 }
834 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000835}