blob: cdc9620c0204c21ea6227af57ce520175a54ddaa [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),
Tim Petersd2364e82001-11-01 20:09:42 +0000117 s_buffer,
Guido van Rossum9e896b32000-04-05 20:11:21 +0000118 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;
Tim Petersd2364e82001-11-01 20:09:42 +0000199
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 if (op && PyFloat_Check(op))
201 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Tim Petersd2364e82001-11-01 20:09:42 +0000202
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203 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 }
Tim Petersd2364e82001-11-01 20:09:42 +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 }
Tim Petersd2364e82001-11-01 20:09:42 +0000217
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 val = PyFloat_AS_DOUBLE(fo);
219 Py_DECREF(fo);
Tim Petersd2364e82001-11-01 20:09:42 +0000220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221 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;
Tim Petersd2e40d62001-11-01 23:12:27 +0000467 if (mod) {
468 /* ensure the remainder has the same sign as the denominator */
469 if ((wx < 0) != (mod < 0)) {
470 mod += wx;
471 div -= 1.0;
472 }
473 }
474 else {
475 /* the remainder is zero, and in the presence of signed zeroes
476 fmod returns different results across platforms; ensure
477 it has the same sign as the denominator; we'd like to do
478 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000479 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000480 if (wx < 0.0)
481 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000482 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000483 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000484 if (div) {
485 floordiv = floor(div);
486 if (div - floordiv > 0.5)
487 floordiv += 1.0;
488 }
489 else {
490 /* div is zero - get the same sign as the true quotient */
491 div *= div; /* hide "div = +0" from optimizers */
492 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
493 }
494 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000495 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000496}
497
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000498static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000499float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500{
501 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000502
503 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000504 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000505 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000506 return NULL;
507 }
508
Neil Schemenauer32117e52001-01-04 01:44:34 +0000509 CONVERT_TO_DOUBLE(v, iv);
510 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000511
512 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000513 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000514 PyFPE_START_PROTECT("pow", return NULL)
515 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000516 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000517 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000518 ix = fmod(1.0, iz);
519 if (ix != 0 && iz < 0)
520 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000521 }
Tim Petersc54d1902000-10-06 00:36:09 +0000522 else
523 ix = 1.0;
524 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000525 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000526 }
Tim Peters96685bf2001-08-23 22:31:37 +0000527 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000528 if (iw < 0.0) {
529 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000530 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000531 return NULL;
532 }
533 return PyFloat_FromDouble(0.0);
534 }
Tim Peters96685bf2001-08-23 22:31:37 +0000535 if (iv < 0.0 && iw != floor(iw)) {
536 PyErr_SetString(PyExc_ValueError,
537 "negative number cannot be raised to a fractional power");
538 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000539 }
Tim Peters96685bf2001-08-23 22:31:37 +0000540 errno = 0;
541 PyFPE_START_PROTECT("pow", return NULL)
542 ix = pow(iv, iw);
543 PyFPE_END_PROTECT(ix)
Tim Petersa40c7932001-09-05 22:36:56 +0000544 Py_SET_ERANGE_IF_OVERFLOW(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000545 if (errno != 0) {
546 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000549 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551}
552
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000554float_int_div(PyObject *v, PyObject *w)
555{
556 PyObject *t, *r;
Tim Petersd2364e82001-11-01 20:09:42 +0000557
Guido van Rossum4668b002001-08-08 05:00:18 +0000558 t = float_divmod(v, w);
559 if (t != NULL) {
560 r = PyTuple_GET_ITEM(t, 0);
561 Py_INCREF(r);
562 Py_DECREF(t);
563 return r;
564 }
565 return NULL;
566}
567
568static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000569float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572}
573
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000575float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576{
Tim Peters0280cf72001-09-11 21:53:35 +0000577 if (PyFloat_CheckExact(v)) {
578 Py_INCREF(v);
579 return (PyObject *)v;
580 }
581 else
582 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000583}
584
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000586float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000587{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000588 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589}
590
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000591static int
Fred Drakefd99de62000-07-09 05:02:18 +0000592float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000593{
594 return v->ob_fval != 0.0;
595}
596
Guido van Rossum234f9421993-06-17 12:35:49 +0000597static int
Fred Drakefd99de62000-07-09 05:02:18 +0000598float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000599{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600 if (PyInt_Check(*pw)) {
601 long x = PyInt_AsLong(*pw);
602 *pw = PyFloat_FromDouble((double)x);
603 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000604 return 0;
605 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 else if (PyLong_Check(*pw)) {
607 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
608 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000609 return 0;
610 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000611 else if (PyFloat_Check(*pw)) {
612 Py_INCREF(*pv);
613 Py_INCREF(*pw);
614 return 0;
615 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000616 return 1; /* Can't do it */
617}
618
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000620float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000621{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000622 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000623 double wholepart; /* integral portion of x, rounded toward 0 */
624 long aslong; /* (long)wholepart */
625
626 (void)modf(x, &wholepart);
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000627#ifdef RISCOS
628 /* conversion from floating to integral type would raise exception */
629 if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
630 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
631 return NULL;
632 }
633#endif
Tim Peters7321ec42001-07-26 20:02:17 +0000634 /* doubles may have more bits than longs, or vice versa; and casting
635 to long may yield gibberish in either case. What really matters
636 is whether converting back to double again reproduces what we
637 started with. */
638 aslong = (long)wholepart;
639 if ((double)aslong == wholepart)
640 return PyInt_FromLong(aslong);
641 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
642 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000643}
644
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000646float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000647{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 double x = PyFloat_AsDouble(v);
649 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000650}
651
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000653float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000654{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000656 return v;
657}
658
659
Guido van Rossumbef14172001-08-29 15:47:46 +0000660staticforward PyObject *
661float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
662
Tim Peters6d6c1a32001-08-02 04:15:00 +0000663static PyObject *
664float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
665{
666 PyObject *x = Py_False; /* Integer zero */
667 static char *kwlist[] = {"x", 0};
668
Guido van Rossumbef14172001-08-29 15:47:46 +0000669 if (type != &PyFloat_Type)
670 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000671 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
672 return NULL;
673 if (PyString_Check(x))
674 return PyFloat_FromString(x, NULL);
675 return PyNumber_Float(x);
676}
677
Guido van Rossumbef14172001-08-29 15:47:46 +0000678/* Wimpy, slow approach to tp_new calls for subtypes of float:
679 first create a regular float from whatever arguments we got,
680 then allocate a subtype instance and initialize its ob_fval
681 from the regular float. The regular float is then thrown away.
682*/
683static PyObject *
684float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
685{
686 PyObject *tmp, *new;
687
688 assert(PyType_IsSubtype(type, &PyFloat_Type));
689 tmp = float_new(&PyFloat_Type, args, kwds);
690 if (tmp == NULL)
691 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000692 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000693 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000694 if (new == NULL)
695 return NULL;
696 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
697 Py_DECREF(tmp);
698 return new;
699}
700
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701static char float_doc[] =
702"float(x) -> floating point number\n\
703\n\
704Convert a string or number to a floating point number, if possible.";
705
706
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000708 (binaryfunc)float_add, /*nb_add*/
709 (binaryfunc)float_sub, /*nb_subtract*/
710 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000711 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000712 (binaryfunc)float_rem, /*nb_remainder*/
713 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000714 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000715 (unaryfunc)float_neg, /*nb_negative*/
716 (unaryfunc)float_pos, /*nb_positive*/
717 (unaryfunc)float_abs, /*nb_absolute*/
718 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000719 0, /*nb_invert*/
720 0, /*nb_lshift*/
721 0, /*nb_rshift*/
722 0, /*nb_and*/
723 0, /*nb_xor*/
724 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000725 (coercion)float_coerce, /*nb_coerce*/
726 (unaryfunc)float_int, /*nb_int*/
727 (unaryfunc)float_long, /*nb_long*/
728 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000729 0, /* nb_oct */
730 0, /* nb_hex */
731 0, /* nb_inplace_add */
732 0, /* nb_inplace_subtract */
733 0, /* nb_inplace_multiply */
734 0, /* nb_inplace_divide */
735 0, /* nb_inplace_remainder */
736 0, /* nb_inplace_power */
737 0, /* nb_inplace_lshift */
738 0, /* nb_inplace_rshift */
739 0, /* nb_inplace_and */
740 0, /* nb_inplace_xor */
741 0, /* nb_inplace_or */
742 float_int_div, /* nb_floor_divide */
743 float_div, /* nb_true_divide */
744 0, /* nb_inplace_floor_divide */
745 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000746};
747
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748PyTypeObject PyFloat_Type = {
749 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000750 0,
751 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000754 (destructor)float_dealloc, /* tp_dealloc */
755 (printfunc)float_print, /* tp_print */
756 0, /* tp_getattr */
757 0, /* tp_setattr */
758 (cmpfunc)float_compare, /* tp_compare */
759 (reprfunc)float_repr, /* tp_repr */
760 &float_as_number, /* tp_as_number */
761 0, /* tp_as_sequence */
762 0, /* tp_as_mapping */
763 (hashfunc)float_hash, /* tp_hash */
764 0, /* tp_call */
765 (reprfunc)float_str, /* tp_str */
766 PyObject_GenericGetAttr, /* tp_getattro */
767 0, /* tp_setattro */
768 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000769 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
770 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771 float_doc, /* tp_doc */
772 0, /* tp_traverse */
773 0, /* tp_clear */
774 0, /* tp_richcompare */
775 0, /* tp_weaklistoffset */
776 0, /* tp_iter */
777 0, /* tp_iternext */
778 0, /* tp_methods */
779 0, /* tp_members */
780 0, /* tp_getset */
781 0, /* tp_base */
782 0, /* tp_dict */
783 0, /* tp_descr_get */
784 0, /* tp_descr_set */
785 0, /* tp_dictoffset */
786 0, /* tp_init */
787 0, /* tp_alloc */
788 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000790
791void
Fred Drakefd99de62000-07-09 05:02:18 +0000792PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000793{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000794 PyFloatObject *p;
795 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000796 int i;
797 int bc, bf; /* block count, number of freed blocks */
798 int frem, fsum; /* remaining unfreed floats per block, total */
799
800 bc = 0;
801 bf = 0;
802 fsum = 0;
803 list = block_list;
804 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000805 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000806 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000807 bc++;
808 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000809 for (i = 0, p = &list->objects[0];
810 i < N_FLOATOBJECTS;
811 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000812 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000813 frem++;
814 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000815 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000816 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000817 list->next = block_list;
818 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000819 for (i = 0, p = &list->objects[0];
820 i < N_FLOATOBJECTS;
821 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000822 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000823 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000824 p->ob_type = (struct _typeobject *)
825 free_list;
826 free_list = p;
827 }
828 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000829 }
830 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000831 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000832 bf++;
833 }
834 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000835 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000836 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000837 if (!Py_VerboseFlag)
838 return;
839 fprintf(stderr, "# cleanup floats");
840 if (!fsum) {
841 fprintf(stderr, "\n");
842 }
843 else {
844 fprintf(stderr,
845 ": %d unfreed float%s in %d out of %d block%s\n",
846 fsum, fsum == 1 ? "" : "s",
847 bc - bf, bc, bc == 1 ? "" : "s");
848 }
849 if (Py_VerboseFlag > 1) {
850 list = block_list;
851 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000852 for (i = 0, p = &list->objects[0];
853 i < N_FLOATOBJECTS;
854 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000855 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000856 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000857 char buf[100];
858 PyFloat_AsString(buf, p);
859 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000860 "# <float at %p, refcnt=%d, val=%s>\n",
861 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000862 }
863 }
864 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000865 }
866 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000867}