blob: 83987baa6b20cda13e2559def0843d0ce53abfc2 [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
Tim Peters97019e42001-11-28 22:43:45 +0000228static void
229format_float(char *buf, size_t buflen, 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. */
Tim Peters97019e42001-11-28 22:43:45 +0000237
238 assert(PyFloat_Check(v));
239 PyOS_snprintf(buf, buflen, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240 cp = buf;
241 if (*cp == '-')
242 cp++;
243 for (; *cp != '\0'; cp++) {
244 /* Any non-digit means it's not an integer;
245 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000246 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 break;
248 }
249 if (*cp == '\0') {
250 *cp++ = '.';
251 *cp++ = '0';
252 *cp++ = '\0';
253 }
254}
255
Tim Peters97019e42001-11-28 22:43:45 +0000256/* XXX PyFloat_AsStringEx should not be a public API function (for one
257 XXX thing, its signature passes a buffer without a length; for another,
258 XXX it isn't useful outside this file).
259*/
260void
261PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
262{
263 format_float(buf, 100, v, precision);
264}
265
Neil Schemenauer32117e52001-01-04 01:44:34 +0000266/* Macro and helper that convert PyObject obj to a C double and store
267 the value in dbl; this replaces the functionality of the coercion
Tim Peters77d8a4f2001-12-11 20:31:34 +0000268 slot function. If conversion to double raises an exception, obj is
269 set to NULL, and the function invoking this macro returns NULL. If
270 obj is not of float, int or long type, Py_NotImplemented is incref'ed,
271 stored in obj, and returned from the function invoking this macro.
272*/
Neil Schemenauer32117e52001-01-04 01:44:34 +0000273#define CONVERT_TO_DOUBLE(obj, dbl) \
274 if (PyFloat_Check(obj)) \
275 dbl = PyFloat_AS_DOUBLE(obj); \
276 else if (convert_to_double(&(obj), &(dbl)) < 0) \
277 return obj;
278
279static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000280convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000281{
282 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000283
Neil Schemenauer32117e52001-01-04 01:44:34 +0000284 if (PyInt_Check(obj)) {
285 *dbl = (double)PyInt_AS_LONG(obj);
286 }
287 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000288 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000289 if (*dbl == -1.0 && PyErr_Occurred()) {
290 *v = NULL;
291 return -1;
292 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000293 }
294 else {
295 Py_INCREF(Py_NotImplemented);
296 *v = Py_NotImplemented;
297 return -1;
298 }
299 return 0;
300}
301
Guido van Rossum57072eb1999-12-23 19:00:28 +0000302/* Precisions used by repr() and str(), respectively.
303
304 The repr() precision (17 significant decimal digits) is the minimal number
305 that is guaranteed to have enough precision so that if the number is read
306 back in the exact same binary value is recreated. This is true for IEEE
307 floating point by design, and also happens to work for all other modern
308 hardware.
309
310 The str() precision is chosen so that in most cases, the rounding noise
311 created by various operations is suppressed, while giving plenty of
312 precision for practical use.
313
314*/
315
316#define PREC_REPR 17
317#define PREC_STR 12
318
Tim Peters97019e42001-11-28 22:43:45 +0000319/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
320 XXX they pass a char buffer without passing a length.
321*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000322void
Fred Drakefd99de62000-07-09 05:02:18 +0000323PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000324{
Tim Peters97019e42001-11-28 22:43:45 +0000325 format_float(buf, 100, v, PREC_STR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000326}
327
Tim Peters72f98e92001-05-08 15:19:57 +0000328void
329PyFloat_AsReprString(char *buf, PyFloatObject *v)
330{
Tim Peters97019e42001-11-28 22:43:45 +0000331 format_float(buf, 100, v, PREC_REPR);
Tim Peters72f98e92001-05-08 15:19:57 +0000332}
333
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000334/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000335static int
Fred Drakefd99de62000-07-09 05:02:18 +0000336float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337{
338 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000339 format_float(buf, sizeof(buf), v,
340 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000342 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343}
344
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000345static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000346float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347{
348 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000349 format_float(buf, sizeof(buf), v, PREC_REPR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000350 return PyString_FromString(buf);
351}
352
353static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000354float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000355{
356 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000357 format_float(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359}
360
361static int
Fred Drakefd99de62000-07-09 05:02:18 +0000362float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363{
364 double i = v->ob_fval;
365 double j = w->ob_fval;
366 return (i < j) ? -1 : (i > j) ? 1 : 0;
367}
368
Guido van Rossum9bfef441993-03-29 10:43:31 +0000369static long
Fred Drakefd99de62000-07-09 05:02:18 +0000370float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000371{
Tim Peters39dce292000-08-15 03:34:48 +0000372 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000373}
374
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000376float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000378 double a,b;
379 CONVERT_TO_DOUBLE(v, a);
380 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000381 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000382 a = a + b;
383 PyFPE_END_PROTECT(a)
384 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385}
386
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000387static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000388float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000390 double a,b;
391 CONVERT_TO_DOUBLE(v, a);
392 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000393 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000394 a = a - b;
395 PyFPE_END_PROTECT(a)
396 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397}
398
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000400float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000402 double a,b;
403 CONVERT_TO_DOUBLE(v, a);
404 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000405 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000406 a = a * b;
407 PyFPE_END_PROTECT(a)
408 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409}
410
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000411static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000412float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000414 double a,b;
415 CONVERT_TO_DOUBLE(v, a);
416 CONVERT_TO_DOUBLE(w, b);
417 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419 return NULL;
420 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000421 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000422 a = a / b;
423 PyFPE_END_PROTECT(a)
424 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425}
426
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000428float_classic_div(PyObject *v, PyObject *w)
429{
430 double a,b;
431 CONVERT_TO_DOUBLE(v, a);
432 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000433 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000434 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
435 return NULL;
436 if (b == 0.0) {
437 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
438 return NULL;
439 }
440 PyFPE_START_PROTECT("divide", return 0)
441 a = a / b;
442 PyFPE_END_PROTECT(a)
443 return PyFloat_FromDouble(a);
444}
445
446static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000447float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000449 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000450 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000451 CONVERT_TO_DOUBLE(v, vx);
452 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455 return NULL;
456 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000457 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000458 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000459 /* note: checking mod*wx < 0 is incorrect -- underflows to
460 0 if wx < sqrt(smallest nonzero double) */
461 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000462 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000463 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000464 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466}
467
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000469float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000470{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000471 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000472 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000473 CONVERT_TO_DOUBLE(v, vx);
474 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000475 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000477 return NULL;
478 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000479 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000480 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000481 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000482 exact multiple of wx. But this is fp arithmetic, and fp
483 vx - mod is an approximation; the result is that div may
484 not be an exact integral value after the division, although
485 it will always be very close to one.
486 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000487 div = (vx - mod) / wx;
Tim Petersd2e40d62001-11-01 23:12:27 +0000488 if (mod) {
489 /* ensure the remainder has the same sign as the denominator */
490 if ((wx < 0) != (mod < 0)) {
491 mod += wx;
492 div -= 1.0;
493 }
494 }
495 else {
496 /* the remainder is zero, and in the presence of signed zeroes
497 fmod returns different results across platforms; ensure
498 it has the same sign as the denominator; we'd like to do
499 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000500 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000501 if (wx < 0.0)
502 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000503 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000504 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000505 if (div) {
506 floordiv = floor(div);
507 if (div - floordiv > 0.5)
508 floordiv += 1.0;
509 }
510 else {
511 /* div is zero - get the same sign as the true quotient */
512 div *= div; /* hide "div = +0" from optimizers */
513 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
514 }
515 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000516 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000517}
518
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519static PyObject *
Tim Peters63a35712001-12-11 19:57:24 +0000520float_floor_div(PyObject *v, PyObject *w)
521{
522 PyObject *t, *r;
523
524 t = float_divmod(v, w);
Tim Peters77d8a4f2001-12-11 20:31:34 +0000525 if (t == NULL || t == Py_NotImplemented)
526 return t;
527 assert(PyTuple_CheckExact(t));
528 r = PyTuple_GET_ITEM(t, 0);
529 Py_INCREF(r);
530 Py_DECREF(t);
531 return r;
Tim Peters63a35712001-12-11 19:57:24 +0000532}
533
534static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000535float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000536{
537 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000538
539 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000540 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000541 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000542 return NULL;
543 }
544
Neil Schemenauer32117e52001-01-04 01:44:34 +0000545 CONVERT_TO_DOUBLE(v, iv);
546 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000547
548 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000549 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000550 PyFPE_START_PROTECT("pow", return NULL)
551 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000552 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000553 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000554 ix = fmod(1.0, iz);
555 if (ix != 0 && iz < 0)
556 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000557 }
Tim Petersc54d1902000-10-06 00:36:09 +0000558 else
559 ix = 1.0;
560 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000561 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000562 }
Tim Peters96685bf2001-08-23 22:31:37 +0000563 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000564 if (iw < 0.0) {
565 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000566 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000567 return NULL;
568 }
569 return PyFloat_FromDouble(0.0);
570 }
Tim Peters96685bf2001-08-23 22:31:37 +0000571 if (iv < 0.0 && iw != floor(iw)) {
572 PyErr_SetString(PyExc_ValueError,
573 "negative number cannot be raised to a fractional power");
574 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000575 }
Tim Peters96685bf2001-08-23 22:31:37 +0000576 errno = 0;
577 PyFPE_START_PROTECT("pow", return NULL)
578 ix = pow(iv, iw);
579 PyFPE_END_PROTECT(ix)
Tim Petersdc5a5082002-03-09 04:58:24 +0000580 Py_ADJUST_ERANGE1(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000581 if (errno != 0) {
Tim Petersdc5a5082002-03-09 04:58:24 +0000582 assert(errno == ERANGE);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000585 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587}
588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000590float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593}
594
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000596float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597{
Tim Peters0280cf72001-09-11 21:53:35 +0000598 if (PyFloat_CheckExact(v)) {
599 Py_INCREF(v);
600 return (PyObject *)v;
601 }
602 else
603 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000604}
605
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000607float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000608{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000609 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610}
611
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000612static int
Fred Drakefd99de62000-07-09 05:02:18 +0000613float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000614{
615 return v->ob_fval != 0.0;
616}
617
Guido van Rossum234f9421993-06-17 12:35:49 +0000618static int
Fred Drakefd99de62000-07-09 05:02:18 +0000619float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000620{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621 if (PyInt_Check(*pw)) {
622 long x = PyInt_AsLong(*pw);
623 *pw = PyFloat_FromDouble((double)x);
624 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000625 return 0;
626 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 else if (PyLong_Check(*pw)) {
628 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
629 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000630 return 0;
631 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000632 else if (PyFloat_Check(*pw)) {
633 Py_INCREF(*pv);
634 Py_INCREF(*pw);
635 return 0;
636 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000637 return 1; /* Can't do it */
638}
639
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000641float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000642{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000644 double wholepart; /* integral portion of x, rounded toward 0 */
645 long aslong; /* (long)wholepart */
646
647 (void)modf(x, &wholepart);
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000648#ifdef RISCOS
649 /* conversion from floating to integral type would raise exception */
650 if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
651 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
652 return NULL;
653 }
654#endif
Tim Peters7321ec42001-07-26 20:02:17 +0000655 /* doubles may have more bits than longs, or vice versa; and casting
656 to long may yield gibberish in either case. What really matters
657 is whether converting back to double again reproduces what we
658 started with. */
659 aslong = (long)wholepart;
660 if ((double)aslong == wholepart)
661 return PyInt_FromLong(aslong);
662 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
663 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000664}
665
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000667float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000668{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 double x = PyFloat_AsDouble(v);
670 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000671}
672
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000674float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000675{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000677 return v;
678}
679
680
Guido van Rossumbef14172001-08-29 15:47:46 +0000681staticforward PyObject *
682float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
683
Tim Peters6d6c1a32001-08-02 04:15:00 +0000684static PyObject *
685float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
686{
687 PyObject *x = Py_False; /* Integer zero */
688 static char *kwlist[] = {"x", 0};
689
Guido van Rossumbef14172001-08-29 15:47:46 +0000690 if (type != &PyFloat_Type)
691 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000692 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
693 return NULL;
694 if (PyString_Check(x))
695 return PyFloat_FromString(x, NULL);
696 return PyNumber_Float(x);
697}
698
Guido van Rossumbef14172001-08-29 15:47:46 +0000699/* Wimpy, slow approach to tp_new calls for subtypes of float:
700 first create a regular float from whatever arguments we got,
701 then allocate a subtype instance and initialize its ob_fval
702 from the regular float. The regular float is then thrown away.
703*/
704static PyObject *
705float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
706{
707 PyObject *tmp, *new;
708
709 assert(PyType_IsSubtype(type, &PyFloat_Type));
710 tmp = float_new(&PyFloat_Type, args, kwds);
711 if (tmp == NULL)
712 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000713 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000714 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000715 if (new == NULL)
716 return NULL;
717 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
718 Py_DECREF(tmp);
719 return new;
720}
721
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722static char float_doc[] =
723"float(x) -> floating point number\n\
724\n\
725Convert a string or number to a floating point number, if possible.";
726
727
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000729 (binaryfunc)float_add, /*nb_add*/
730 (binaryfunc)float_sub, /*nb_subtract*/
731 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000732 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000733 (binaryfunc)float_rem, /*nb_remainder*/
734 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000735 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000736 (unaryfunc)float_neg, /*nb_negative*/
737 (unaryfunc)float_pos, /*nb_positive*/
738 (unaryfunc)float_abs, /*nb_absolute*/
739 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000740 0, /*nb_invert*/
741 0, /*nb_lshift*/
742 0, /*nb_rshift*/
743 0, /*nb_and*/
744 0, /*nb_xor*/
745 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746 (coercion)float_coerce, /*nb_coerce*/
747 (unaryfunc)float_int, /*nb_int*/
748 (unaryfunc)float_long, /*nb_long*/
749 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000750 0, /* nb_oct */
751 0, /* nb_hex */
752 0, /* nb_inplace_add */
753 0, /* nb_inplace_subtract */
754 0, /* nb_inplace_multiply */
755 0, /* nb_inplace_divide */
756 0, /* nb_inplace_remainder */
757 0, /* nb_inplace_power */
758 0, /* nb_inplace_lshift */
759 0, /* nb_inplace_rshift */
760 0, /* nb_inplace_and */
761 0, /* nb_inplace_xor */
762 0, /* nb_inplace_or */
Tim Peters63a35712001-12-11 19:57:24 +0000763 float_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +0000764 float_div, /* nb_true_divide */
765 0, /* nb_inplace_floor_divide */
766 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767};
768
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769PyTypeObject PyFloat_Type = {
770 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771 0,
772 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775 (destructor)float_dealloc, /* tp_dealloc */
776 (printfunc)float_print, /* tp_print */
777 0, /* tp_getattr */
778 0, /* tp_setattr */
779 (cmpfunc)float_compare, /* tp_compare */
780 (reprfunc)float_repr, /* tp_repr */
781 &float_as_number, /* tp_as_number */
782 0, /* tp_as_sequence */
783 0, /* tp_as_mapping */
784 (hashfunc)float_hash, /* tp_hash */
785 0, /* tp_call */
786 (reprfunc)float_str, /* tp_str */
787 PyObject_GenericGetAttr, /* tp_getattro */
788 0, /* tp_setattro */
789 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000790 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
791 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000792 float_doc, /* tp_doc */
793 0, /* tp_traverse */
794 0, /* tp_clear */
795 0, /* tp_richcompare */
796 0, /* tp_weaklistoffset */
797 0, /* tp_iter */
798 0, /* tp_iternext */
799 0, /* tp_methods */
800 0, /* tp_members */
801 0, /* tp_getset */
802 0, /* tp_base */
803 0, /* tp_dict */
804 0, /* tp_descr_get */
805 0, /* tp_descr_set */
806 0, /* tp_dictoffset */
807 0, /* tp_init */
808 0, /* tp_alloc */
809 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000810};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000811
812void
Fred Drakefd99de62000-07-09 05:02:18 +0000813PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000814{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000815 PyFloatObject *p;
816 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000817 int i;
818 int bc, bf; /* block count, number of freed blocks */
819 int frem, fsum; /* remaining unfreed floats per block, total */
820
821 bc = 0;
822 bf = 0;
823 fsum = 0;
824 list = block_list;
825 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000826 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000827 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000828 bc++;
829 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000830 for (i = 0, p = &list->objects[0];
831 i < N_FLOATOBJECTS;
832 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000833 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000834 frem++;
835 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000836 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000837 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000838 list->next = block_list;
839 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000840 for (i = 0, p = &list->objects[0];
841 i < N_FLOATOBJECTS;
842 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000843 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000844 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000845 p->ob_type = (struct _typeobject *)
846 free_list;
847 free_list = p;
848 }
849 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000850 }
851 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000852 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000853 bf++;
854 }
855 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000856 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000857 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000858 if (!Py_VerboseFlag)
859 return;
860 fprintf(stderr, "# cleanup floats");
861 if (!fsum) {
862 fprintf(stderr, "\n");
863 }
864 else {
865 fprintf(stderr,
866 ": %d unfreed float%s in %d out of %d block%s\n",
867 fsum, fsum == 1 ? "" : "s",
868 bc - bf, bc, bc == 1 ? "" : "s");
869 }
870 if (Py_VerboseFlag > 1) {
871 list = block_list;
872 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000873 for (i = 0, p = &list->objects[0];
874 i < N_FLOATOBJECTS;
875 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000876 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000877 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000878 char buf[100];
879 PyFloat_AsString(buf, p);
880 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000881 "# <float at %p, refcnt=%d, val=%s>\n",
882 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000883 }
884 }
885 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000886 }
887 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000888}