blob: 258c4dd3a6f11a516c556b3724f4ed226d2958b2 [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
Tim Peters9fffa3e2001-09-04 05:14:19 +0000274convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000275{
276 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000277
Neil Schemenauer32117e52001-01-04 01:44:34 +0000278 if (PyInt_Check(obj)) {
279 *dbl = (double)PyInt_AS_LONG(obj);
280 }
281 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000282 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000283 if (*dbl == -1.0 && PyErr_Occurred()) {
284 *v = NULL;
285 return -1;
286 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000287 }
288 else {
289 Py_INCREF(Py_NotImplemented);
290 *v = Py_NotImplemented;
291 return -1;
292 }
293 return 0;
294}
295
Guido van Rossum57072eb1999-12-23 19:00:28 +0000296/* Precisions used by repr() and str(), respectively.
297
298 The repr() precision (17 significant decimal digits) is the minimal number
299 that is guaranteed to have enough precision so that if the number is read
300 back in the exact same binary value is recreated. This is true for IEEE
301 floating point by design, and also happens to work for all other modern
302 hardware.
303
304 The str() precision is chosen so that in most cases, the rounding noise
305 created by various operations is suppressed, while giving plenty of
306 precision for practical use.
307
308*/
309
310#define PREC_REPR 17
311#define PREC_STR 12
312
313void
Fred Drakefd99de62000-07-09 05:02:18 +0000314PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000315{
316 PyFloat_AsStringEx(buf, v, PREC_STR);
317}
318
Tim Peters72f98e92001-05-08 15:19:57 +0000319void
320PyFloat_AsReprString(char *buf, PyFloatObject *v)
321{
322 PyFloat_AsStringEx(buf, v, PREC_REPR);
323}
324
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000325/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000326static int
Fred Drakefd99de62000-07-09 05:02:18 +0000327float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
329 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000330 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000332 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333}
334
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000336float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337{
338 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000339 PyFloat_AsStringEx(buf, v, PREC_REPR);
340 return PyString_FromString(buf);
341}
342
343static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000344float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000345{
346 char buf[100];
347 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349}
350
351static int
Fred Drakefd99de62000-07-09 05:02:18 +0000352float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353{
354 double i = v->ob_fval;
355 double j = w->ob_fval;
356 return (i < j) ? -1 : (i > j) ? 1 : 0;
357}
358
Guido van Rossum9bfef441993-03-29 10:43:31 +0000359static long
Fred Drakefd99de62000-07-09 05:02:18 +0000360float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000361{
Tim Peters39dce292000-08-15 03:34:48 +0000362 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000363}
364
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000366float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000368 double a,b;
369 CONVERT_TO_DOUBLE(v, a);
370 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000371 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000372 a = a + b;
373 PyFPE_END_PROTECT(a)
374 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
376
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000377static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000378float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000380 double a,b;
381 CONVERT_TO_DOUBLE(v, a);
382 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000383 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000384 a = a - b;
385 PyFPE_END_PROTECT(a)
386 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387}
388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000390float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000392 double a,b;
393 CONVERT_TO_DOUBLE(v, a);
394 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000395 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000396 a = a * b;
397 PyFPE_END_PROTECT(a)
398 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399}
400
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000402float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000404 double a,b;
405 CONVERT_TO_DOUBLE(v, a);
406 CONVERT_TO_DOUBLE(w, b);
407 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 return NULL;
410 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000411 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000412 a = a / b;
413 PyFPE_END_PROTECT(a)
414 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415}
416
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000418float_classic_div(PyObject *v, PyObject *w)
419{
420 double a,b;
421 CONVERT_TO_DOUBLE(v, a);
422 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000423 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000424 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
425 return NULL;
426 if (b == 0.0) {
427 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
428 return NULL;
429 }
430 PyFPE_START_PROTECT("divide", return 0)
431 a = a / b;
432 PyFPE_END_PROTECT(a)
433 return PyFloat_FromDouble(a);
434}
435
436static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000437float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000439 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000440 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000441 CONVERT_TO_DOUBLE(v, vx);
442 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445 return NULL;
446 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000447 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000448 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000449 /* note: checking mod*wx < 0 is incorrect -- underflows to
450 0 if wx < sqrt(smallest nonzero double) */
451 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000452 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000453 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000454 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456}
457
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000459float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000460{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000461 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000462 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000463 CONVERT_TO_DOUBLE(v, vx);
464 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000465 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000467 return NULL;
468 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000469 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000470 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000471 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000472 exact multiple of wx. But this is fp arithmetic, and fp
473 vx - mod is an approximation; the result is that div may
474 not be an exact integral value after the division, although
475 it will always be very close to one.
476 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000477 div = (vx - mod) / wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000478 /* note: checking mod*wx < 0 is incorrect -- underflows to
479 0 if wx < sqrt(smallest nonzero double) */
480 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum15ecff41991-10-20 20:16:45 +0000481 mod += wx;
482 div -= 1.0;
483 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000484 /* snap quotient to nearest integral value */
485 floordiv = floor(div);
486 if (div - floordiv > 0.5)
487 floordiv += 1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000488 PyFPE_END_PROTECT(div)
Guido van Rossum9263e781999-05-06 14:26:34 +0000489 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000490}
491
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000493float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494{
495 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000496
497 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000498 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
499 "allowed unless all other arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000500 return NULL;
501 }
502
Neil Schemenauer32117e52001-01-04 01:44:34 +0000503 CONVERT_TO_DOUBLE(v, iv);
504 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000505
506 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000507 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000508 PyFPE_START_PROTECT("pow", return NULL)
509 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000510 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000511 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000512 ix = fmod(1.0, iz);
513 if (ix != 0 && iz < 0)
514 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000515 }
Tim Petersc54d1902000-10-06 00:36:09 +0000516 else
517 ix = 1.0;
518 PyFPE_END_PROTECT(ix)
519 return PyFloat_FromDouble(ix);
520 }
Tim Peters96685bf2001-08-23 22:31:37 +0000521 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000522 if (iw < 0.0) {
523 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000524 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000525 return NULL;
526 }
527 return PyFloat_FromDouble(0.0);
528 }
Tim Peters96685bf2001-08-23 22:31:37 +0000529 if (iv < 0.0 && iw != floor(iw)) {
530 PyErr_SetString(PyExc_ValueError,
531 "negative number cannot be raised to a fractional power");
532 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000533 }
Tim Peters96685bf2001-08-23 22:31:37 +0000534 errno = 0;
535 PyFPE_START_PROTECT("pow", return NULL)
536 ix = pow(iv, iw);
537 PyFPE_END_PROTECT(ix)
Guido van Rossum7fa52f81991-12-16 15:43:14 +0000538 CHECK(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000539 if (errno != 0) {
540 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000543 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545}
546
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000548float_int_div(PyObject *v, PyObject *w)
549{
550 PyObject *t, *r;
551
552 t = float_divmod(v, w);
553 if (t != NULL) {
554 r = PyTuple_GET_ITEM(t, 0);
555 Py_INCREF(r);
556 Py_DECREF(t);
557 return r;
558 }
559 return NULL;
560}
561
562static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000563float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566}
567
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000569float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 Py_INCREF(v);
572 return (PyObject *)v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000573}
574
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000576float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000577{
578 if (v->ob_fval < 0)
579 return float_neg(v);
580 else
581 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000582}
583
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000584static int
Fred Drakefd99de62000-07-09 05:02:18 +0000585float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000586{
587 return v->ob_fval != 0.0;
588}
589
Guido van Rossum234f9421993-06-17 12:35:49 +0000590static int
Fred Drakefd99de62000-07-09 05:02:18 +0000591float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000592{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 if (PyInt_Check(*pw)) {
594 long x = PyInt_AsLong(*pw);
595 *pw = PyFloat_FromDouble((double)x);
596 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000597 return 0;
598 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 else if (PyLong_Check(*pw)) {
600 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
601 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000602 return 0;
603 }
604 return 1; /* Can't do it */
605}
606
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000608float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000609{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000611 double wholepart; /* integral portion of x, rounded toward 0 */
612 long aslong; /* (long)wholepart */
613
614 (void)modf(x, &wholepart);
615 /* doubles may have more bits than longs, or vice versa; and casting
616 to long may yield gibberish in either case. What really matters
617 is whether converting back to double again reproduces what we
618 started with. */
619 aslong = (long)wholepart;
620 if ((double)aslong == wholepart)
621 return PyInt_FromLong(aslong);
622 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
623 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000624}
625
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000627float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000628{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 double x = PyFloat_AsDouble(v);
630 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000631}
632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000634float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000635{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000637 return v;
638}
639
640
Guido van Rossumbef14172001-08-29 15:47:46 +0000641staticforward PyObject *
642float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
643
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644static PyObject *
645float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
646{
647 PyObject *x = Py_False; /* Integer zero */
648 static char *kwlist[] = {"x", 0};
649
Guido van Rossumbef14172001-08-29 15:47:46 +0000650 if (type != &PyFloat_Type)
651 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
653 return NULL;
654 if (PyString_Check(x))
655 return PyFloat_FromString(x, NULL);
656 return PyNumber_Float(x);
657}
658
Guido van Rossumbef14172001-08-29 15:47:46 +0000659/* Wimpy, slow approach to tp_new calls for subtypes of float:
660 first create a regular float from whatever arguments we got,
661 then allocate a subtype instance and initialize its ob_fval
662 from the regular float. The regular float is then thrown away.
663*/
664static PyObject *
665float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
666{
667 PyObject *tmp, *new;
668
669 assert(PyType_IsSubtype(type, &PyFloat_Type));
670 tmp = float_new(&PyFloat_Type, args, kwds);
671 if (tmp == NULL)
672 return NULL;
673 assert(PyFloat_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000674 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000675 if (new == NULL)
676 return NULL;
677 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
678 Py_DECREF(tmp);
679 return new;
680}
681
Tim Peters6d6c1a32001-08-02 04:15:00 +0000682static char float_doc[] =
683"float(x) -> floating point number\n\
684\n\
685Convert a string or number to a floating point number, if possible.";
686
687
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000689 (binaryfunc)float_add, /*nb_add*/
690 (binaryfunc)float_sub, /*nb_subtract*/
691 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000692 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000693 (binaryfunc)float_rem, /*nb_remainder*/
694 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000695 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000696 (unaryfunc)float_neg, /*nb_negative*/
697 (unaryfunc)float_pos, /*nb_positive*/
698 (unaryfunc)float_abs, /*nb_absolute*/
699 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000700 0, /*nb_invert*/
701 0, /*nb_lshift*/
702 0, /*nb_rshift*/
703 0, /*nb_and*/
704 0, /*nb_xor*/
705 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000706 (coercion)float_coerce, /*nb_coerce*/
707 (unaryfunc)float_int, /*nb_int*/
708 (unaryfunc)float_long, /*nb_long*/
709 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000710 0, /* nb_oct */
711 0, /* nb_hex */
712 0, /* nb_inplace_add */
713 0, /* nb_inplace_subtract */
714 0, /* nb_inplace_multiply */
715 0, /* nb_inplace_divide */
716 0, /* nb_inplace_remainder */
717 0, /* nb_inplace_power */
718 0, /* nb_inplace_lshift */
719 0, /* nb_inplace_rshift */
720 0, /* nb_inplace_and */
721 0, /* nb_inplace_xor */
722 0, /* nb_inplace_or */
723 float_int_div, /* nb_floor_divide */
724 float_div, /* nb_true_divide */
725 0, /* nb_inplace_floor_divide */
726 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727};
728
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729PyTypeObject PyFloat_Type = {
730 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731 0,
732 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000735 (destructor)float_dealloc, /* tp_dealloc */
736 (printfunc)float_print, /* tp_print */
737 0, /* tp_getattr */
738 0, /* tp_setattr */
739 (cmpfunc)float_compare, /* tp_compare */
740 (reprfunc)float_repr, /* tp_repr */
741 &float_as_number, /* tp_as_number */
742 0, /* tp_as_sequence */
743 0, /* tp_as_mapping */
744 (hashfunc)float_hash, /* tp_hash */
745 0, /* tp_call */
746 (reprfunc)float_str, /* tp_str */
747 PyObject_GenericGetAttr, /* tp_getattro */
748 0, /* tp_setattro */
749 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000750 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
751 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752 float_doc, /* tp_doc */
753 0, /* tp_traverse */
754 0, /* tp_clear */
755 0, /* tp_richcompare */
756 0, /* tp_weaklistoffset */
757 0, /* tp_iter */
758 0, /* tp_iternext */
759 0, /* tp_methods */
760 0, /* tp_members */
761 0, /* tp_getset */
762 0, /* tp_base */
763 0, /* tp_dict */
764 0, /* tp_descr_get */
765 0, /* tp_descr_set */
766 0, /* tp_dictoffset */
767 0, /* tp_init */
768 0, /* tp_alloc */
769 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000771
772void
Fred Drakefd99de62000-07-09 05:02:18 +0000773PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000774{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000775 PyFloatObject *p;
776 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000777 int i;
778 int bc, bf; /* block count, number of freed blocks */
779 int frem, fsum; /* remaining unfreed floats per block, total */
780
781 bc = 0;
782 bf = 0;
783 fsum = 0;
784 list = block_list;
785 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000786 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000787 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000788 bc++;
789 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000790 for (i = 0, p = &list->objects[0];
791 i < N_FLOATOBJECTS;
792 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000793 if (p->ob_type == &PyFloat_Type && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000794 frem++;
795 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000796 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000797 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000798 list->next = block_list;
799 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000800 for (i = 0, p = &list->objects[0];
801 i < N_FLOATOBJECTS;
802 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000803 if (p->ob_type != &PyFloat_Type ||
804 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000805 p->ob_type = (struct _typeobject *)
806 free_list;
807 free_list = p;
808 }
809 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000810 }
811 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000812 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000813 bf++;
814 }
815 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000816 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000817 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000818 if (!Py_VerboseFlag)
819 return;
820 fprintf(stderr, "# cleanup floats");
821 if (!fsum) {
822 fprintf(stderr, "\n");
823 }
824 else {
825 fprintf(stderr,
826 ": %d unfreed float%s in %d out of %d block%s\n",
827 fsum, fsum == 1 ? "" : "s",
828 bc - bf, bc, bc == 1 ? "" : "s");
829 }
830 if (Py_VerboseFlag > 1) {
831 list = block_list;
832 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000833 for (i = 0, p = &list->objects[0];
834 i < N_FLOATOBJECTS;
835 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000836 if (p->ob_type == &PyFloat_Type &&
837 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000838 char buf[100];
839 PyFloat_AsString(buf, p);
840 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000841 "# <float at %p, refcnt=%d, val=%s>\n",
842 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000843 }
844 }
845 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000846 }
847 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000848}