blob: 02a1e1aa57e98fe01e2f846d784b2dd82ca14edb [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) {
Barry Warsawaf8aef92001-11-28 20:52:21 +0000153 PyOS_snprintf(buffer, sizeof(buffer),
154 "invalid literal for float(): %.200s", s);
Tim Petersef14d732000-09-23 03:39:17 +0000155 PyErr_SetString(PyExc_ValueError, buffer);
156 return NULL;
157 }
158 /* Since end != s, the platform made *some* kind of sense out
159 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000160 while (*end && isspace(Py_CHARMASK(*end)))
161 end++;
162 if (*end != '\0') {
Barry Warsawaf8aef92001-11-28 20:52:21 +0000163 PyOS_snprintf(buffer, sizeof(buffer),
164 "invalid literal for float(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000165 PyErr_SetString(PyExc_ValueError, buffer);
166 return NULL;
167 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000168 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000169 PyErr_SetString(PyExc_ValueError,
170 "null byte in argument for float()");
171 return NULL;
172 }
Tim Petersef14d732000-09-23 03:39:17 +0000173 if (x == 0.0) {
174 /* See above -- may have been strtod being anal
175 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000176 PyFPE_START_PROTECT("atof", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000177 x = atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000178 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000179 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000181 return PyFloat_FromDouble(x);
182}
183
Guido van Rossum234f9421993-06-17 12:35:49 +0000184static void
Fred Drakefd99de62000-07-09 05:02:18 +0000185float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000186{
Guido van Rossum9475a232001-10-05 20:51:39 +0000187 if (PyFloat_CheckExact(op)) {
188 op->ob_type = (struct _typeobject *)free_list;
189 free_list = op;
190 }
191 else
192 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000193}
194
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195double
Fred Drakefd99de62000-07-09 05:02:18 +0000196PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198 PyNumberMethods *nb;
199 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000200 double val;
Tim Petersd2364e82001-11-01 20:09:42 +0000201
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 if (op && PyFloat_Check(op))
203 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Tim Petersd2364e82001-11-01 20:09:42 +0000204
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
206 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208 return -1;
209 }
Tim Petersd2364e82001-11-01 20:09:42 +0000210
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212 if (fo == NULL)
213 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 if (!PyFloat_Check(fo)) {
215 PyErr_SetString(PyExc_TypeError,
216 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217 return -1;
218 }
Tim Petersd2364e82001-11-01 20:09:42 +0000219
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220 val = PyFloat_AS_DOUBLE(fo);
221 Py_DECREF(fo);
Tim Petersd2364e82001-11-01 20:09:42 +0000222
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224}
225
226/* Methods */
227
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000228void
Fred Drakefd99de62000-07-09 05:02:18 +0000229PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230{
231 register char *cp;
232 /* Subroutine for float_repr and float_print.
233 We want float numbers to be recognizable as such,
234 i.e., they should contain a decimal point or an exponent.
235 However, %g may print the number as an integer;
236 in such cases, we append ".0" to the string. */
Guido van Rossum57072eb1999-12-23 19:00:28 +0000237 sprintf(buf, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238 cp = buf;
239 if (*cp == '-')
240 cp++;
241 for (; *cp != '\0'; cp++) {
242 /* Any non-digit means it's not an integer;
243 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000244 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245 break;
246 }
247 if (*cp == '\0') {
248 *cp++ = '.';
249 *cp++ = '0';
250 *cp++ = '\0';
251 }
252}
253
Neil Schemenauer32117e52001-01-04 01:44:34 +0000254/* Macro and helper that convert PyObject obj to a C double and store
255 the value in dbl; this replaces the functionality of the coercion
256 slot function */
257
258#define CONVERT_TO_DOUBLE(obj, dbl) \
259 if (PyFloat_Check(obj)) \
260 dbl = PyFloat_AS_DOUBLE(obj); \
261 else if (convert_to_double(&(obj), &(dbl)) < 0) \
262 return obj;
263
264static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000265convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000266{
267 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000268
Neil Schemenauer32117e52001-01-04 01:44:34 +0000269 if (PyInt_Check(obj)) {
270 *dbl = (double)PyInt_AS_LONG(obj);
271 }
272 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000273 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000274 if (*dbl == -1.0 && PyErr_Occurred()) {
275 *v = NULL;
276 return -1;
277 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000278 }
279 else {
280 Py_INCREF(Py_NotImplemented);
281 *v = Py_NotImplemented;
282 return -1;
283 }
284 return 0;
285}
286
Guido van Rossum57072eb1999-12-23 19:00:28 +0000287/* Precisions used by repr() and str(), respectively.
288
289 The repr() precision (17 significant decimal digits) is the minimal number
290 that is guaranteed to have enough precision so that if the number is read
291 back in the exact same binary value is recreated. This is true for IEEE
292 floating point by design, and also happens to work for all other modern
293 hardware.
294
295 The str() precision is chosen so that in most cases, the rounding noise
296 created by various operations is suppressed, while giving plenty of
297 precision for practical use.
298
299*/
300
301#define PREC_REPR 17
302#define PREC_STR 12
303
304void
Fred Drakefd99de62000-07-09 05:02:18 +0000305PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000306{
307 PyFloat_AsStringEx(buf, v, PREC_STR);
308}
309
Tim Peters72f98e92001-05-08 15:19:57 +0000310void
311PyFloat_AsReprString(char *buf, PyFloatObject *v)
312{
313 PyFloat_AsStringEx(buf, v, PREC_REPR);
314}
315
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000316/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000317static int
Fred Drakefd99de62000-07-09 05:02:18 +0000318float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319{
320 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000321 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000323 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000327float_repr(PyFloatObject *v)
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, PREC_REPR);
331 return PyString_FromString(buf);
332}
333
334static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000335float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000336{
337 char buf[100];
338 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340}
341
342static int
Fred Drakefd99de62000-07-09 05:02:18 +0000343float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344{
345 double i = v->ob_fval;
346 double j = w->ob_fval;
347 return (i < j) ? -1 : (i > j) ? 1 : 0;
348}
349
Guido van Rossum9bfef441993-03-29 10:43:31 +0000350static long
Fred Drakefd99de62000-07-09 05:02:18 +0000351float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000352{
Tim Peters39dce292000-08-15 03:34:48 +0000353 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000354}
355
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000357float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000359 double a,b;
360 CONVERT_TO_DOUBLE(v, a);
361 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000362 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000363 a = a + b;
364 PyFPE_END_PROTECT(a)
365 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366}
367
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000369float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000371 double a,b;
372 CONVERT_TO_DOUBLE(v, a);
373 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000374 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000375 a = a - b;
376 PyFPE_END_PROTECT(a)
377 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378}
379
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000381float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000383 double a,b;
384 CONVERT_TO_DOUBLE(v, a);
385 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000386 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000387 a = a * b;
388 PyFPE_END_PROTECT(a)
389 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390}
391
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000393float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000395 double a,b;
396 CONVERT_TO_DOUBLE(v, a);
397 CONVERT_TO_DOUBLE(w, b);
398 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400 return NULL;
401 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000402 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000403 a = a / b;
404 PyFPE_END_PROTECT(a)
405 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406}
407
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000409float_classic_div(PyObject *v, PyObject *w)
410{
411 double a,b;
412 CONVERT_TO_DOUBLE(v, a);
413 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000414 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000415 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
416 return NULL;
417 if (b == 0.0) {
418 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
419 return NULL;
420 }
421 PyFPE_START_PROTECT("divide", return 0)
422 a = a / b;
423 PyFPE_END_PROTECT(a)
424 return PyFloat_FromDouble(a);
425}
426
427static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000428float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000430 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000431 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000432 CONVERT_TO_DOUBLE(v, vx);
433 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436 return NULL;
437 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000438 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000439 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000440 /* note: checking mod*wx < 0 is incorrect -- underflows to
441 0 if wx < sqrt(smallest nonzero double) */
442 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000443 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000444 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000445 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447}
448
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000450float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000451{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000452 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000453 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000454 CONVERT_TO_DOUBLE(v, vx);
455 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000456 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000458 return NULL;
459 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000460 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000461 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000462 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000463 exact multiple of wx. But this is fp arithmetic, and fp
464 vx - mod is an approximation; the result is that div may
465 not be an exact integral value after the division, although
466 it will always be very close to one.
467 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000468 div = (vx - mod) / wx;
Tim Petersd2e40d62001-11-01 23:12:27 +0000469 if (mod) {
470 /* ensure the remainder has the same sign as the denominator */
471 if ((wx < 0) != (mod < 0)) {
472 mod += wx;
473 div -= 1.0;
474 }
475 }
476 else {
477 /* the remainder is zero, and in the presence of signed zeroes
478 fmod returns different results across platforms; ensure
479 it has the same sign as the denominator; we'd like to do
480 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000481 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000482 if (wx < 0.0)
483 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000484 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000485 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000486 if (div) {
487 floordiv = floor(div);
488 if (div - floordiv > 0.5)
489 floordiv += 1.0;
490 }
491 else {
492 /* div is zero - get the same sign as the true quotient */
493 div *= div; /* hide "div = +0" from optimizers */
494 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
495 }
496 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000497 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000498}
499
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000501float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502{
503 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000504
505 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000506 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000507 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000508 return NULL;
509 }
510
Neil Schemenauer32117e52001-01-04 01:44:34 +0000511 CONVERT_TO_DOUBLE(v, iv);
512 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000513
514 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000515 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000516 PyFPE_START_PROTECT("pow", return NULL)
517 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000518 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000519 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000520 ix = fmod(1.0, iz);
521 if (ix != 0 && iz < 0)
522 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000523 }
Tim Petersc54d1902000-10-06 00:36:09 +0000524 else
525 ix = 1.0;
526 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000527 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000528 }
Tim Peters96685bf2001-08-23 22:31:37 +0000529 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000530 if (iw < 0.0) {
531 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000532 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000533 return NULL;
534 }
535 return PyFloat_FromDouble(0.0);
536 }
Tim Peters96685bf2001-08-23 22:31:37 +0000537 if (iv < 0.0 && iw != floor(iw)) {
538 PyErr_SetString(PyExc_ValueError,
539 "negative number cannot be raised to a fractional power");
540 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000541 }
Tim Peters96685bf2001-08-23 22:31:37 +0000542 errno = 0;
543 PyFPE_START_PROTECT("pow", return NULL)
544 ix = pow(iv, iw);
545 PyFPE_END_PROTECT(ix)
Tim Petersa40c7932001-09-05 22:36:56 +0000546 Py_SET_ERANGE_IF_OVERFLOW(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000547 if (errno != 0) {
548 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000551 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553}
554
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000556float_int_div(PyObject *v, PyObject *w)
557{
558 PyObject *t, *r;
Tim Petersd2364e82001-11-01 20:09:42 +0000559
Guido van Rossum4668b002001-08-08 05:00:18 +0000560 t = float_divmod(v, w);
561 if (t != NULL) {
562 r = PyTuple_GET_ITEM(t, 0);
563 Py_INCREF(r);
564 Py_DECREF(t);
565 return r;
566 }
567 return NULL;
568}
569
570static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000571float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574}
575
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000577float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578{
Tim Peters0280cf72001-09-11 21:53:35 +0000579 if (PyFloat_CheckExact(v)) {
580 Py_INCREF(v);
581 return (PyObject *)v;
582 }
583 else
584 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000585}
586
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000588float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000589{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000590 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591}
592
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000593static int
Fred Drakefd99de62000-07-09 05:02:18 +0000594float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000595{
596 return v->ob_fval != 0.0;
597}
598
Guido van Rossum234f9421993-06-17 12:35:49 +0000599static int
Fred Drakefd99de62000-07-09 05:02:18 +0000600float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000601{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 if (PyInt_Check(*pw)) {
603 long x = PyInt_AsLong(*pw);
604 *pw = PyFloat_FromDouble((double)x);
605 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000606 return 0;
607 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 else if (PyLong_Check(*pw)) {
609 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
610 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000611 return 0;
612 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000613 else if (PyFloat_Check(*pw)) {
614 Py_INCREF(*pv);
615 Py_INCREF(*pw);
616 return 0;
617 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000618 return 1; /* Can't do it */
619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000622float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000623{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000625 double wholepart; /* integral portion of x, rounded toward 0 */
626 long aslong; /* (long)wholepart */
627
628 (void)modf(x, &wholepart);
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000629#ifdef RISCOS
630 /* conversion from floating to integral type would raise exception */
631 if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
632 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
633 return NULL;
634 }
635#endif
Tim Peters7321ec42001-07-26 20:02:17 +0000636 /* doubles may have more bits than longs, or vice versa; and casting
637 to long may yield gibberish in either case. What really matters
638 is whether converting back to double again reproduces what we
639 started with. */
640 aslong = (long)wholepart;
641 if ((double)aslong == wholepart)
642 return PyInt_FromLong(aslong);
643 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
644 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000645}
646
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000648float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000649{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 double x = PyFloat_AsDouble(v);
651 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000652}
653
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000655float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000656{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000658 return v;
659}
660
661
Guido van Rossumbef14172001-08-29 15:47:46 +0000662staticforward PyObject *
663float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
664
Tim Peters6d6c1a32001-08-02 04:15:00 +0000665static PyObject *
666float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
667{
668 PyObject *x = Py_False; /* Integer zero */
669 static char *kwlist[] = {"x", 0};
670
Guido van Rossumbef14172001-08-29 15:47:46 +0000671 if (type != &PyFloat_Type)
672 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000673 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
674 return NULL;
675 if (PyString_Check(x))
676 return PyFloat_FromString(x, NULL);
677 return PyNumber_Float(x);
678}
679
Guido van Rossumbef14172001-08-29 15:47:46 +0000680/* Wimpy, slow approach to tp_new calls for subtypes of float:
681 first create a regular float from whatever arguments we got,
682 then allocate a subtype instance and initialize its ob_fval
683 from the regular float. The regular float is then thrown away.
684*/
685static PyObject *
686float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
687{
688 PyObject *tmp, *new;
689
690 assert(PyType_IsSubtype(type, &PyFloat_Type));
691 tmp = float_new(&PyFloat_Type, args, kwds);
692 if (tmp == NULL)
693 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000694 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000695 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000696 if (new == NULL)
697 return NULL;
698 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
699 Py_DECREF(tmp);
700 return new;
701}
702
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703static char float_doc[] =
704"float(x) -> floating point number\n\
705\n\
706Convert a string or number to a floating point number, if possible.";
707
708
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000710 (binaryfunc)float_add, /*nb_add*/
711 (binaryfunc)float_sub, /*nb_subtract*/
712 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000713 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000714 (binaryfunc)float_rem, /*nb_remainder*/
715 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000716 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000717 (unaryfunc)float_neg, /*nb_negative*/
718 (unaryfunc)float_pos, /*nb_positive*/
719 (unaryfunc)float_abs, /*nb_absolute*/
720 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000721 0, /*nb_invert*/
722 0, /*nb_lshift*/
723 0, /*nb_rshift*/
724 0, /*nb_and*/
725 0, /*nb_xor*/
726 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000727 (coercion)float_coerce, /*nb_coerce*/
728 (unaryfunc)float_int, /*nb_int*/
729 (unaryfunc)float_long, /*nb_long*/
730 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000731 0, /* nb_oct */
732 0, /* nb_hex */
733 0, /* nb_inplace_add */
734 0, /* nb_inplace_subtract */
735 0, /* nb_inplace_multiply */
736 0, /* nb_inplace_divide */
737 0, /* nb_inplace_remainder */
738 0, /* nb_inplace_power */
739 0, /* nb_inplace_lshift */
740 0, /* nb_inplace_rshift */
741 0, /* nb_inplace_and */
742 0, /* nb_inplace_xor */
743 0, /* nb_inplace_or */
744 float_int_div, /* nb_floor_divide */
745 float_div, /* nb_true_divide */
746 0, /* nb_inplace_floor_divide */
747 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000748};
749
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750PyTypeObject PyFloat_Type = {
751 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000752 0,
753 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756 (destructor)float_dealloc, /* tp_dealloc */
757 (printfunc)float_print, /* tp_print */
758 0, /* tp_getattr */
759 0, /* tp_setattr */
760 (cmpfunc)float_compare, /* tp_compare */
761 (reprfunc)float_repr, /* tp_repr */
762 &float_as_number, /* tp_as_number */
763 0, /* tp_as_sequence */
764 0, /* tp_as_mapping */
765 (hashfunc)float_hash, /* tp_hash */
766 0, /* tp_call */
767 (reprfunc)float_str, /* tp_str */
768 PyObject_GenericGetAttr, /* tp_getattro */
769 0, /* tp_setattro */
770 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000771 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
772 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773 float_doc, /* tp_doc */
774 0, /* tp_traverse */
775 0, /* tp_clear */
776 0, /* tp_richcompare */
777 0, /* tp_weaklistoffset */
778 0, /* tp_iter */
779 0, /* tp_iternext */
780 0, /* tp_methods */
781 0, /* tp_members */
782 0, /* tp_getset */
783 0, /* tp_base */
784 0, /* tp_dict */
785 0, /* tp_descr_get */
786 0, /* tp_descr_set */
787 0, /* tp_dictoffset */
788 0, /* tp_init */
789 0, /* tp_alloc */
790 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000792
793void
Fred Drakefd99de62000-07-09 05:02:18 +0000794PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000795{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000796 PyFloatObject *p;
797 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000798 int i;
799 int bc, bf; /* block count, number of freed blocks */
800 int frem, fsum; /* remaining unfreed floats per block, total */
801
802 bc = 0;
803 bf = 0;
804 fsum = 0;
805 list = block_list;
806 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000807 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000808 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000809 bc++;
810 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000811 for (i = 0, p = &list->objects[0];
812 i < N_FLOATOBJECTS;
813 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000814 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000815 frem++;
816 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000817 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000818 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000819 list->next = block_list;
820 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000821 for (i = 0, p = &list->objects[0];
822 i < N_FLOATOBJECTS;
823 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000824 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000825 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000826 p->ob_type = (struct _typeobject *)
827 free_list;
828 free_list = p;
829 }
830 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000831 }
832 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000833 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000834 bf++;
835 }
836 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000837 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000838 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000839 if (!Py_VerboseFlag)
840 return;
841 fprintf(stderr, "# cleanup floats");
842 if (!fsum) {
843 fprintf(stderr, "\n");
844 }
845 else {
846 fprintf(stderr,
847 ": %d unfreed float%s in %d out of %d block%s\n",
848 fsum, fsum == 1 ? "" : "s",
849 bc - bf, bc, bc == 1 ? "" : "s");
850 }
851 if (Py_VerboseFlag > 1) {
852 list = block_list;
853 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000854 for (i = 0, p = &list->objects[0];
855 i < N_FLOATOBJECTS;
856 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000857 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000858 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000859 char buf[100];
860 PyFloat_AsString(buf, p);
861 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000862 "# <float at %p, refcnt=%d, val=%s>\n",
863 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000864 }
865 }
866 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000867 }
868 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000869}