blob: b8c37721dbeaeb46e712fda81db515e39d704160 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Float object implementation */
3
Guido van Rossum2a9096b1990-10-21 22:15:08 +00004/* XXX There should be overflow checks here, but it's hard to check
5 for any kind of float exception without losing portability. */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossum3f5da241990-12-20 15:06:42 +00009#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Guido van Rossum07e3a7e1995-02-27 10:13:37 +000011#if !defined(__STDC__) && !defined(macintosh)
Tim Petersdbd9ba62000-07-09 03:09:57 +000012extern double fmod(double, double);
13extern double pow(double, double);
Guido van Rossum6923e131990-11-02 17:50:43 +000014#endif
15
Martin v. Löwis01c65262001-03-06 12:14:54 +000016#if defined(sun) && !defined(__SVR4)
Guido van Rossum3c03fa81997-10-31 17:00:30 +000017/* On SunOS4.1 only libm.a exists. Make sure that references to all
18 needed math functions exist in the executable, so that dynamic
19 loading of mathmodule does not fail. */
20double (*_Py_math_funcs_hack[])() = {
21 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
22 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
23};
24#endif
25
Guido van Rossum93ad0df1997-05-13 21:00:42 +000026/* Special free list -- see comments for same code in intobject.c. */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000027#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000028#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
Guido van Rossumf61bbc81999-03-12 00:12:21 +000029#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
Guido van Rossum3fce8831999-03-12 19:43:17 +000030
Guido van Rossum3fce8831999-03-12 19:43:17 +000031struct _floatblock {
32 struct _floatblock *next;
33 PyFloatObject objects[N_FLOATOBJECTS];
34};
35
36typedef struct _floatblock PyFloatBlock;
37
38static PyFloatBlock *block_list = NULL;
39static PyFloatObject *free_list = NULL;
40
Guido van Rossum93ad0df1997-05-13 21:00:42 +000041static PyFloatObject *
Fred Drakefd99de62000-07-09 05:02:18 +000042fill_free_list(void)
Guido van Rossum93ad0df1997-05-13 21:00:42 +000043{
44 PyFloatObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000045 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
46 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
Guido van Rossum93ad0df1997-05-13 21:00:42 +000047 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000048 return (PyFloatObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000049 ((PyFloatBlock *)p)->next = block_list;
50 block_list = (PyFloatBlock *)p;
51 p = &((PyFloatBlock *)p)->objects[0];
Guido van Rossum93ad0df1997-05-13 21:00:42 +000052 q = p + N_FLOATOBJECTS;
53 while (--q > p)
Guido van Rossumf61bbc81999-03-12 00:12:21 +000054 q->ob_type = (struct _typeobject *)(q-1);
55 q->ob_type = NULL;
Guido van Rossum93ad0df1997-05-13 21:00:42 +000056 return p + N_FLOATOBJECTS - 1;
57}
58
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060PyFloat_FromDouble(double fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
Guido van Rossum93ad0df1997-05-13 21:00:42 +000062 register PyFloatObject *op;
63 if (free_list == NULL) {
64 if ((free_list = fill_free_list()) == NULL)
65 return NULL;
66 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000067 /* PyObject_New is inlined */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000068 op = free_list;
Guido van Rossumf61bbc81999-03-12 00:12:21 +000069 free_list = (PyFloatObject *)op->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +000070 PyObject_INIT(op, &PyFloat_Type);
Guido van Rossum2a9096b1990-10-21 22:15:08 +000071 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Tim Petersef14d732000-09-23 03:39:17 +000075/**************************************************************************
76RED_FLAG 22-Sep-2000 tim
77PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
78
791. If v was a regular string, *pend was set to point to its terminating
80 null byte. That's useless (the caller can find that without any
81 help from this function!).
82
832. If v was a Unicode string, or an object convertible to a character
84 buffer, *pend was set to point into stack trash (the auto temp
85 vector holding the character buffer). That was downright dangerous.
86
87Since we can't change the interface of a public API function, pend is
88still supported but now *officially* useless: if pend is not NULL,
89*pend is set to NULL.
90**************************************************************************/
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000092PyFloat_FromString(PyObject *v, char **pend)
Barry Warsaw226ae6c1999-10-12 19:54:53 +000093{
Guido van Rossum4c08d552000-03-10 22:55:18 +000094 const char *s, *last, *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +000095 double x;
Tim Petersef14d732000-09-23 03:39:17 +000096 char buffer[256]; /* for errors */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000097#ifdef Py_USING_UNICODE
Tim Petersef14d732000-09-23 03:39:17 +000098 char s_buffer[256]; /* for objects convertible to a char buffer */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000099#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000100 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000101
Tim Petersef14d732000-09-23 03:39:17 +0000102 if (pend)
103 *pend = NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000104 if (PyString_Check(v)) {
105 s = PyString_AS_STRING(v);
106 len = PyString_GET_SIZE(v);
107 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000108#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000109 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000110 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
111 PyErr_SetString(PyExc_ValueError,
Tim Petersef14d732000-09-23 03:39:17 +0000112 "Unicode float() literal too long to convert");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000113 return NULL;
114 }
Tim Petersef14d732000-09-23 03:39:17 +0000115 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000116 PyUnicode_GET_SIZE(v),
117 s_buffer,
118 NULL))
119 return NULL;
120 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000121 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000122 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000123#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000124 else if (PyObject_AsCharBuffer(v, &s, &len)) {
125 PyErr_SetString(PyExc_TypeError,
126 "float() needs a string argument");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000127 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000128 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000129
Guido van Rossum4c08d552000-03-10 22:55:18 +0000130 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000131 while (*s && isspace(Py_CHARMASK(*s)))
132 s++;
Tim Petersef14d732000-09-23 03:39:17 +0000133 if (*s == '\0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000134 PyErr_SetString(PyExc_ValueError, "empty string for float()");
135 return NULL;
136 }
Tim Petersef14d732000-09-23 03:39:17 +0000137 /* We don't care about overflow or underflow. If the platform supports
138 * them, infinities and signed zeroes (on underflow) are fine.
139 * However, strtod can return 0 for denormalized numbers, where atof
140 * does not. So (alas!) we special-case a zero result. Note that
141 * whether strtod sets errno on underflow is not defined, so we can't
142 * key off errno.
143 */
Tim Peters858346e2000-09-25 21:01:28 +0000144 PyFPE_START_PROTECT("strtod", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000145 x = strtod(s, (char **)&end);
Tim Peters858346e2000-09-25 21:01:28 +0000146 PyFPE_END_PROTECT(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000147 errno = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000148 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
Tim Petersef14d732000-09-23 03:39:17 +0000149 byte at the end of the string, when the input is inf(inity). */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000150 if (end > last)
151 end = last;
Tim Petersef14d732000-09-23 03:39:17 +0000152 if (end == s) {
153 sprintf(buffer, "invalid literal for float(): %.200s", s);
154 PyErr_SetString(PyExc_ValueError, buffer);
155 return NULL;
156 }
157 /* Since end != s, the platform made *some* kind of sense out
158 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000159 while (*end && isspace(Py_CHARMASK(*end)))
160 end++;
161 if (*end != '\0') {
162 sprintf(buffer, "invalid literal for float(): %.200s", s);
163 PyErr_SetString(PyExc_ValueError, buffer);
164 return NULL;
165 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000166 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000167 PyErr_SetString(PyExc_ValueError,
168 "null byte in argument for float()");
169 return NULL;
170 }
Tim Petersef14d732000-09-23 03:39:17 +0000171 if (x == 0.0) {
172 /* See above -- may have been strtod being anal
173 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000174 PyFPE_START_PROTECT("atof", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000175 x = atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000176 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000177 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000179 return PyFloat_FromDouble(x);
180}
181
Guido van Rossum234f9421993-06-17 12:35:49 +0000182static void
Fred Drakefd99de62000-07-09 05:02:18 +0000183float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000184{
Guido van Rossum9475a232001-10-05 20:51:39 +0000185 if (PyFloat_CheckExact(op)) {
186 op->ob_type = (struct _typeobject *)free_list;
187 free_list = op;
188 }
189 else
190 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000191}
192
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193double
Fred Drakefd99de62000-07-09 05:02:18 +0000194PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyNumberMethods *nb;
197 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198 double val;
199
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 if (op && PyFloat_Check(op))
201 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202
203 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
204 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206 return -1;
207 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210 if (fo == NULL)
211 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 if (!PyFloat_Check(fo)) {
213 PyErr_SetString(PyExc_TypeError,
214 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215 return -1;
216 }
217
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 val = PyFloat_AS_DOUBLE(fo);
219 Py_DECREF(fo);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220
221 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222}
223
224/* Methods */
225
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000226void
Fred Drakefd99de62000-07-09 05:02:18 +0000227PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
229 register char *cp;
230 /* Subroutine for float_repr and float_print.
231 We want float numbers to be recognizable as such,
232 i.e., they should contain a decimal point or an exponent.
233 However, %g may print the number as an integer;
234 in such cases, we append ".0" to the string. */
Guido van Rossum57072eb1999-12-23 19:00:28 +0000235 sprintf(buf, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 cp = buf;
237 if (*cp == '-')
238 cp++;
239 for (; *cp != '\0'; cp++) {
240 /* Any non-digit means it's not an integer;
241 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000242 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 break;
244 }
245 if (*cp == '\0') {
246 *cp++ = '.';
247 *cp++ = '0';
248 *cp++ = '\0';
249 }
250}
251
Neil Schemenauer32117e52001-01-04 01:44:34 +0000252/* Macro and helper that convert PyObject obj to a C double and store
253 the value in dbl; this replaces the functionality of the coercion
254 slot function */
255
256#define CONVERT_TO_DOUBLE(obj, dbl) \
257 if (PyFloat_Check(obj)) \
258 dbl = PyFloat_AS_DOUBLE(obj); \
259 else if (convert_to_double(&(obj), &(dbl)) < 0) \
260 return obj;
261
262static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000263convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000264{
265 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000266
Neil Schemenauer32117e52001-01-04 01:44:34 +0000267 if (PyInt_Check(obj)) {
268 *dbl = (double)PyInt_AS_LONG(obj);
269 }
270 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000271 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000272 if (*dbl == -1.0 && PyErr_Occurred()) {
273 *v = NULL;
274 return -1;
275 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000276 }
277 else {
278 Py_INCREF(Py_NotImplemented);
279 *v = Py_NotImplemented;
280 return -1;
281 }
282 return 0;
283}
284
Guido van Rossum57072eb1999-12-23 19:00:28 +0000285/* Precisions used by repr() and str(), respectively.
286
287 The repr() precision (17 significant decimal digits) is the minimal number
288 that is guaranteed to have enough precision so that if the number is read
289 back in the exact same binary value is recreated. This is true for IEEE
290 floating point by design, and also happens to work for all other modern
291 hardware.
292
293 The str() precision is chosen so that in most cases, the rounding noise
294 created by various operations is suppressed, while giving plenty of
295 precision for practical use.
296
297*/
298
299#define PREC_REPR 17
300#define PREC_STR 12
301
302void
Fred Drakefd99de62000-07-09 05:02:18 +0000303PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000304{
305 PyFloat_AsStringEx(buf, v, PREC_STR);
306}
307
Tim Peters72f98e92001-05-08 15:19:57 +0000308void
309PyFloat_AsReprString(char *buf, PyFloatObject *v)
310{
311 PyFloat_AsStringEx(buf, v, PREC_REPR);
312}
313
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000314/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000315static int
Fred Drakefd99de62000-07-09 05:02:18 +0000316float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317{
318 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000319 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000321 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322}
323
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000325float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326{
327 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000328 PyFloat_AsStringEx(buf, v, PREC_REPR);
329 return PyString_FromString(buf);
330}
331
332static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000333float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000334{
335 char buf[100];
336 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338}
339
340static int
Fred Drakefd99de62000-07-09 05:02:18 +0000341float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342{
343 double i = v->ob_fval;
344 double j = w->ob_fval;
345 return (i < j) ? -1 : (i > j) ? 1 : 0;
346}
347
Guido van Rossum9bfef441993-03-29 10:43:31 +0000348static long
Fred Drakefd99de62000-07-09 05:02:18 +0000349float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000350{
Tim Peters39dce292000-08-15 03:34:48 +0000351 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000352}
353
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000355float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000357 double a,b;
358 CONVERT_TO_DOUBLE(v, a);
359 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000360 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000361 a = a + b;
362 PyFPE_END_PROTECT(a)
363 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364}
365
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000367float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000369 double a,b;
370 CONVERT_TO_DOUBLE(v, a);
371 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000372 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000373 a = a - b;
374 PyFPE_END_PROTECT(a)
375 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376}
377
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000378static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000379float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000381 double a,b;
382 CONVERT_TO_DOUBLE(v, a);
383 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000384 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000385 a = a * b;
386 PyFPE_END_PROTECT(a)
387 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388}
389
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000391float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000393 double a,b;
394 CONVERT_TO_DOUBLE(v, a);
395 CONVERT_TO_DOUBLE(w, b);
396 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 return NULL;
399 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000400 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000401 a = a / b;
402 PyFPE_END_PROTECT(a)
403 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404}
405
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000407float_classic_div(PyObject *v, PyObject *w)
408{
409 double a,b;
410 CONVERT_TO_DOUBLE(v, a);
411 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000412 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000413 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
414 return NULL;
415 if (b == 0.0) {
416 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
417 return NULL;
418 }
419 PyFPE_START_PROTECT("divide", return 0)
420 a = a / b;
421 PyFPE_END_PROTECT(a)
422 return PyFloat_FromDouble(a);
423}
424
425static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000426float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000428 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000429 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000430 CONVERT_TO_DOUBLE(v, vx);
431 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434 return NULL;
435 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000436 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000437 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000438 /* note: checking mod*wx < 0 is incorrect -- underflows to
439 0 if wx < sqrt(smallest nonzero double) */
440 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000441 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000442 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000443 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445}
446
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000448float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000449{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000450 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000451 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000452 CONVERT_TO_DOUBLE(v, vx);
453 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000454 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000456 return NULL;
457 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000458 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000459 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000460 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000461 exact multiple of wx. But this is fp arithmetic, and fp
462 vx - mod is an approximation; the result is that div may
463 not be an exact integral value after the division, although
464 it will always be very close to one.
465 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000466 div = (vx - mod) / wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000467 /* note: checking mod*wx < 0 is incorrect -- underflows to
468 0 if wx < sqrt(smallest nonzero double) */
469 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum15ecff41991-10-20 20:16:45 +0000470 mod += wx;
471 div -= 1.0;
472 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000473 /* snap quotient to nearest integral value */
474 floordiv = floor(div);
475 if (div - floordiv > 0.5)
476 floordiv += 1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000477 PyFPE_END_PROTECT(div)
Guido van Rossum9263e781999-05-06 14:26:34 +0000478 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000479}
480
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000482float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483{
484 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000485
486 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000487 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000488 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000489 return NULL;
490 }
491
Neil Schemenauer32117e52001-01-04 01:44:34 +0000492 CONVERT_TO_DOUBLE(v, iv);
493 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000494
495 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000496 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000497 PyFPE_START_PROTECT("pow", return NULL)
498 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000499 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000500 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000501 ix = fmod(1.0, iz);
502 if (ix != 0 && iz < 0)
503 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 }
Tim Peters96685bf2001-08-23 22:31:37 +0000510 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000511 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 }
Tim Peters96685bf2001-08-23 22:31:37 +0000518 if (iv < 0.0 && iw != floor(iw)) {
519 PyErr_SetString(PyExc_ValueError,
520 "negative number cannot be raised to a fractional power");
521 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000522 }
Tim Peters96685bf2001-08-23 22:31:37 +0000523 errno = 0;
524 PyFPE_START_PROTECT("pow", return NULL)
525 ix = pow(iv, iw);
526 PyFPE_END_PROTECT(ix)
Tim Petersa40c7932001-09-05 22:36:56 +0000527 Py_SET_ERANGE_IF_OVERFLOW(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000528 if (errno != 0) {
529 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000532 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534}
535
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000537float_int_div(PyObject *v, PyObject *w)
538{
539 PyObject *t, *r;
540
541 t = float_divmod(v, w);
542 if (t != NULL) {
543 r = PyTuple_GET_ITEM(t, 0);
544 Py_INCREF(r);
545 Py_DECREF(t);
546 return r;
547 }
548 return NULL;
549}
550
551static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000552float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555}
556
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000558float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559{
Tim Peters0280cf72001-09-11 21:53:35 +0000560 if (PyFloat_CheckExact(v)) {
561 Py_INCREF(v);
562 return (PyObject *)v;
563 }
564 else
565 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000566}
567
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000569float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000570{
571 if (v->ob_fval < 0)
572 return float_neg(v);
573 else
574 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575}
576
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000577static int
Fred Drakefd99de62000-07-09 05:02:18 +0000578float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000579{
580 return v->ob_fval != 0.0;
581}
582
Guido van Rossum234f9421993-06-17 12:35:49 +0000583static int
Fred Drakefd99de62000-07-09 05:02:18 +0000584float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000585{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 if (PyInt_Check(*pw)) {
587 long x = PyInt_AsLong(*pw);
588 *pw = PyFloat_FromDouble((double)x);
589 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000590 return 0;
591 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592 else if (PyLong_Check(*pw)) {
593 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
594 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000595 return 0;
596 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000597 else if (PyFloat_Check(*pw)) {
598 Py_INCREF(*pv);
599 Py_INCREF(*pw);
600 return 0;
601 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000602 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);
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000613#ifdef RISCOS
614 /* conversion from floating to integral type would raise exception */
615 if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
616 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
617 return NULL;
618 }
619#endif
Tim Peters7321ec42001-07-26 20:02:17 +0000620 /* doubles may have more bits than longs, or vice versa; and casting
621 to long may yield gibberish in either case. What really matters
622 is whether converting back to double again reproduces what we
623 started with. */
624 aslong = (long)wholepart;
625 if ((double)aslong == wholepart)
626 return PyInt_FromLong(aslong);
627 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
628 return NULL;
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_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000633{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 double x = PyFloat_AsDouble(v);
635 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000636}
637
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000639float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000640{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000642 return v;
643}
644
645
Guido van Rossumbef14172001-08-29 15:47:46 +0000646staticforward PyObject *
647float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
648
Tim Peters6d6c1a32001-08-02 04:15:00 +0000649static PyObject *
650float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
651{
652 PyObject *x = Py_False; /* Integer zero */
653 static char *kwlist[] = {"x", 0};
654
Guido van Rossumbef14172001-08-29 15:47:46 +0000655 if (type != &PyFloat_Type)
656 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000657 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
658 return NULL;
659 if (PyString_Check(x))
660 return PyFloat_FromString(x, NULL);
661 return PyNumber_Float(x);
662}
663
Guido van Rossumbef14172001-08-29 15:47:46 +0000664/* Wimpy, slow approach to tp_new calls for subtypes of float:
665 first create a regular float from whatever arguments we got,
666 then allocate a subtype instance and initialize its ob_fval
667 from the regular float. The regular float is then thrown away.
668*/
669static PyObject *
670float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
671{
672 PyObject *tmp, *new;
673
674 assert(PyType_IsSubtype(type, &PyFloat_Type));
675 tmp = float_new(&PyFloat_Type, args, kwds);
676 if (tmp == NULL)
677 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000678 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000679 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000680 if (new == NULL)
681 return NULL;
682 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
683 Py_DECREF(tmp);
684 return new;
685}
686
Tim Peters6d6c1a32001-08-02 04:15:00 +0000687static char float_doc[] =
688"float(x) -> floating point number\n\
689\n\
690Convert a string or number to a floating point number, if possible.";
691
692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000694 (binaryfunc)float_add, /*nb_add*/
695 (binaryfunc)float_sub, /*nb_subtract*/
696 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000697 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698 (binaryfunc)float_rem, /*nb_remainder*/
699 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000700 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000701 (unaryfunc)float_neg, /*nb_negative*/
702 (unaryfunc)float_pos, /*nb_positive*/
703 (unaryfunc)float_abs, /*nb_absolute*/
704 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000705 0, /*nb_invert*/
706 0, /*nb_lshift*/
707 0, /*nb_rshift*/
708 0, /*nb_and*/
709 0, /*nb_xor*/
710 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000711 (coercion)float_coerce, /*nb_coerce*/
712 (unaryfunc)float_int, /*nb_int*/
713 (unaryfunc)float_long, /*nb_long*/
714 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000715 0, /* nb_oct */
716 0, /* nb_hex */
717 0, /* nb_inplace_add */
718 0, /* nb_inplace_subtract */
719 0, /* nb_inplace_multiply */
720 0, /* nb_inplace_divide */
721 0, /* nb_inplace_remainder */
722 0, /* nb_inplace_power */
723 0, /* nb_inplace_lshift */
724 0, /* nb_inplace_rshift */
725 0, /* nb_inplace_and */
726 0, /* nb_inplace_xor */
727 0, /* nb_inplace_or */
728 float_int_div, /* nb_floor_divide */
729 float_div, /* nb_true_divide */
730 0, /* nb_inplace_floor_divide */
731 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732};
733
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734PyTypeObject PyFloat_Type = {
735 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736 0,
737 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000739 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740 (destructor)float_dealloc, /* tp_dealloc */
741 (printfunc)float_print, /* tp_print */
742 0, /* tp_getattr */
743 0, /* tp_setattr */
744 (cmpfunc)float_compare, /* tp_compare */
745 (reprfunc)float_repr, /* tp_repr */
746 &float_as_number, /* tp_as_number */
747 0, /* tp_as_sequence */
748 0, /* tp_as_mapping */
749 (hashfunc)float_hash, /* tp_hash */
750 0, /* tp_call */
751 (reprfunc)float_str, /* tp_str */
752 PyObject_GenericGetAttr, /* tp_getattro */
753 0, /* tp_setattro */
754 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000755 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
756 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757 float_doc, /* tp_doc */
758 0, /* tp_traverse */
759 0, /* tp_clear */
760 0, /* tp_richcompare */
761 0, /* tp_weaklistoffset */
762 0, /* tp_iter */
763 0, /* tp_iternext */
764 0, /* tp_methods */
765 0, /* tp_members */
766 0, /* tp_getset */
767 0, /* tp_base */
768 0, /* tp_dict */
769 0, /* tp_descr_get */
770 0, /* tp_descr_set */
771 0, /* tp_dictoffset */
772 0, /* tp_init */
773 0, /* tp_alloc */
774 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000776
777void
Fred Drakefd99de62000-07-09 05:02:18 +0000778PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000779{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000780 PyFloatObject *p;
781 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000782 int i;
783 int bc, bf; /* block count, number of freed blocks */
784 int frem, fsum; /* remaining unfreed floats per block, total */
785
786 bc = 0;
787 bf = 0;
788 fsum = 0;
789 list = block_list;
790 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000791 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000792 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000793 bc++;
794 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000795 for (i = 0, p = &list->objects[0];
796 i < N_FLOATOBJECTS;
797 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000798 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000799 frem++;
800 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000801 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000802 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000803 list->next = block_list;
804 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000805 for (i = 0, p = &list->objects[0];
806 i < N_FLOATOBJECTS;
807 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000808 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000809 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000810 p->ob_type = (struct _typeobject *)
811 free_list;
812 free_list = p;
813 }
814 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000815 }
816 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000817 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000818 bf++;
819 }
820 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000821 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000822 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000823 if (!Py_VerboseFlag)
824 return;
825 fprintf(stderr, "# cleanup floats");
826 if (!fsum) {
827 fprintf(stderr, "\n");
828 }
829 else {
830 fprintf(stderr,
831 ": %d unfreed float%s in %d out of %d block%s\n",
832 fsum, fsum == 1 ? "" : "s",
833 bc - bf, bc, bc == 1 ? "" : "s");
834 }
835 if (Py_VerboseFlag > 1) {
836 list = block_list;
837 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000838 for (i = 0, p = &list->objects[0];
839 i < N_FLOATOBJECTS;
840 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000841 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000842 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000843 char buf[100];
844 PyFloat_AsString(buf, p);
845 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000846 "# <float at %p, refcnt=%d, val=%s>\n",
847 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000848 }
849 }
850 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000851 }
852 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000853}