blob: 8ce0ff5f922f631cf9df9d736403ab53f44e5890 [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
268 slot function */
269
270#define CONVERT_TO_DOUBLE(obj, dbl) \
271 if (PyFloat_Check(obj)) \
272 dbl = PyFloat_AS_DOUBLE(obj); \
273 else if (convert_to_double(&(obj), &(dbl)) < 0) \
274 return obj;
275
276static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000277convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000278{
279 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000280
Neil Schemenauer32117e52001-01-04 01:44:34 +0000281 if (PyInt_Check(obj)) {
282 *dbl = (double)PyInt_AS_LONG(obj);
283 }
284 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000285 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000286 if (*dbl == -1.0 && PyErr_Occurred()) {
287 *v = NULL;
288 return -1;
289 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000290 }
291 else {
292 Py_INCREF(Py_NotImplemented);
293 *v = Py_NotImplemented;
294 return -1;
295 }
296 return 0;
297}
298
Guido van Rossum57072eb1999-12-23 19:00:28 +0000299/* Precisions used by repr() and str(), respectively.
300
301 The repr() precision (17 significant decimal digits) is the minimal number
302 that is guaranteed to have enough precision so that if the number is read
303 back in the exact same binary value is recreated. This is true for IEEE
304 floating point by design, and also happens to work for all other modern
305 hardware.
306
307 The str() precision is chosen so that in most cases, the rounding noise
308 created by various operations is suppressed, while giving plenty of
309 precision for practical use.
310
311*/
312
313#define PREC_REPR 17
314#define PREC_STR 12
315
Tim Peters97019e42001-11-28 22:43:45 +0000316/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
317 XXX they pass a char buffer without passing a length.
318*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000319void
Fred Drakefd99de62000-07-09 05:02:18 +0000320PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000321{
Tim Peters97019e42001-11-28 22:43:45 +0000322 format_float(buf, 100, v, PREC_STR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000323}
324
Tim Peters72f98e92001-05-08 15:19:57 +0000325void
326PyFloat_AsReprString(char *buf, PyFloatObject *v)
327{
Tim Peters97019e42001-11-28 22:43:45 +0000328 format_float(buf, 100, v, PREC_REPR);
Tim Peters72f98e92001-05-08 15:19:57 +0000329}
330
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000331/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000332static int
Fred Drakefd99de62000-07-09 05:02:18 +0000333float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334{
335 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000336 format_float(buf, sizeof(buf), v,
337 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000339 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340}
341
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000343float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344{
345 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000346 format_float(buf, sizeof(buf), v, PREC_REPR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000347 return PyString_FromString(buf);
348}
349
350static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000351float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000352{
353 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000354 format_float(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356}
357
358static int
Fred Drakefd99de62000-07-09 05:02:18 +0000359float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360{
361 double i = v->ob_fval;
362 double j = w->ob_fval;
363 return (i < j) ? -1 : (i > j) ? 1 : 0;
364}
365
Guido van Rossum9bfef441993-03-29 10:43:31 +0000366static long
Fred Drakefd99de62000-07-09 05:02:18 +0000367float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000368{
Tim Peters39dce292000-08-15 03:34:48 +0000369 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000370}
371
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000373float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000375 double a,b;
376 CONVERT_TO_DOUBLE(v, a);
377 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000378 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000379 a = a + b;
380 PyFPE_END_PROTECT(a)
381 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382}
383
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000385float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000387 double a,b;
388 CONVERT_TO_DOUBLE(v, a);
389 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000390 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000391 a = a - b;
392 PyFPE_END_PROTECT(a)
393 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394}
395
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000397float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000399 double a,b;
400 CONVERT_TO_DOUBLE(v, a);
401 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000402 PyFPE_START_PROTECT("multiply", 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 *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000409float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000411 double a,b;
412 CONVERT_TO_DOUBLE(v, a);
413 CONVERT_TO_DOUBLE(w, b);
414 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000415 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 return NULL;
417 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000418 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000419 a = a / b;
420 PyFPE_END_PROTECT(a)
421 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422}
423
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000425float_classic_div(PyObject *v, PyObject *w)
426{
427 double a,b;
428 CONVERT_TO_DOUBLE(v, a);
429 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000430 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000431 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
432 return NULL;
433 if (b == 0.0) {
434 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
435 return NULL;
436 }
437 PyFPE_START_PROTECT("divide", return 0)
438 a = a / b;
439 PyFPE_END_PROTECT(a)
440 return PyFloat_FromDouble(a);
441}
442
443static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000444float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000446 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000447 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000448 CONVERT_TO_DOUBLE(v, vx);
449 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452 return NULL;
453 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000454 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000455 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000456 /* note: checking mod*wx < 0 is incorrect -- underflows to
457 0 if wx < sqrt(smallest nonzero double) */
458 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000459 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000460 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000461 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463}
464
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000466float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000467{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000468 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000469 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000470 CONVERT_TO_DOUBLE(v, vx);
471 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000472 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000474 return NULL;
475 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000476 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000477 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000478 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000479 exact multiple of wx. But this is fp arithmetic, and fp
480 vx - mod is an approximation; the result is that div may
481 not be an exact integral value after the division, although
482 it will always be very close to one.
483 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000484 div = (vx - mod) / wx;
Tim Petersd2e40d62001-11-01 23:12:27 +0000485 if (mod) {
486 /* ensure the remainder has the same sign as the denominator */
487 if ((wx < 0) != (mod < 0)) {
488 mod += wx;
489 div -= 1.0;
490 }
491 }
492 else {
493 /* the remainder is zero, and in the presence of signed zeroes
494 fmod returns different results across platforms; ensure
495 it has the same sign as the denominator; we'd like to do
496 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000497 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000498 if (wx < 0.0)
499 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000500 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000501 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000502 if (div) {
503 floordiv = floor(div);
504 if (div - floordiv > 0.5)
505 floordiv += 1.0;
506 }
507 else {
508 /* div is zero - get the same sign as the true quotient */
509 div *= div; /* hide "div = +0" from optimizers */
510 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
511 }
512 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000513 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000514}
515
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516static PyObject *
Tim Peters63a35712001-12-11 19:57:24 +0000517float_floor_div(PyObject *v, PyObject *w)
518{
519 PyObject *t, *r;
520
521 t = float_divmod(v, w);
522 if (t != NULL) {
523 r = PyTuple_GET_ITEM(t, 0);
524 Py_INCREF(r);
525 Py_DECREF(t);
526 return r;
527 }
528 return NULL;
529}
530
531static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000532float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533{
534 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000535
536 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000537 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000538 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000539 return NULL;
540 }
541
Neil Schemenauer32117e52001-01-04 01:44:34 +0000542 CONVERT_TO_DOUBLE(v, iv);
543 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000544
545 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000546 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000547 PyFPE_START_PROTECT("pow", return NULL)
548 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000549 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000550 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000551 ix = fmod(1.0, iz);
552 if (ix != 0 && iz < 0)
553 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000554 }
Tim Petersc54d1902000-10-06 00:36:09 +0000555 else
556 ix = 1.0;
557 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000558 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000559 }
Tim Peters96685bf2001-08-23 22:31:37 +0000560 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000561 if (iw < 0.0) {
562 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000563 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000564 return NULL;
565 }
566 return PyFloat_FromDouble(0.0);
567 }
Tim Peters96685bf2001-08-23 22:31:37 +0000568 if (iv < 0.0 && iw != floor(iw)) {
569 PyErr_SetString(PyExc_ValueError,
570 "negative number cannot be raised to a fractional power");
571 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000572 }
Tim Peters96685bf2001-08-23 22:31:37 +0000573 errno = 0;
574 PyFPE_START_PROTECT("pow", return NULL)
575 ix = pow(iv, iw);
576 PyFPE_END_PROTECT(ix)
Tim Petersa40c7932001-09-05 22:36:56 +0000577 Py_SET_ERANGE_IF_OVERFLOW(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000578 if (errno != 0) {
579 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000582 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584}
585
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000587float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590}
591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000593float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594{
Tim Peters0280cf72001-09-11 21:53:35 +0000595 if (PyFloat_CheckExact(v)) {
596 Py_INCREF(v);
597 return (PyObject *)v;
598 }
599 else
600 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000601}
602
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000604float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000605{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000606 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607}
608
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000609static int
Fred Drakefd99de62000-07-09 05:02:18 +0000610float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000611{
612 return v->ob_fval != 0.0;
613}
614
Guido van Rossum234f9421993-06-17 12:35:49 +0000615static int
Fred Drakefd99de62000-07-09 05:02:18 +0000616float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000617{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 if (PyInt_Check(*pw)) {
619 long x = PyInt_AsLong(*pw);
620 *pw = PyFloat_FromDouble((double)x);
621 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000622 return 0;
623 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 else if (PyLong_Check(*pw)) {
625 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
626 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000627 return 0;
628 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000629 else if (PyFloat_Check(*pw)) {
630 Py_INCREF(*pv);
631 Py_INCREF(*pw);
632 return 0;
633 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000634 return 1; /* Can't do it */
635}
636
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000638float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000639{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000641 double wholepart; /* integral portion of x, rounded toward 0 */
642 long aslong; /* (long)wholepart */
643
644 (void)modf(x, &wholepart);
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000645#ifdef RISCOS
646 /* conversion from floating to integral type would raise exception */
647 if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
648 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
649 return NULL;
650 }
651#endif
Tim Peters7321ec42001-07-26 20:02:17 +0000652 /* doubles may have more bits than longs, or vice versa; and casting
653 to long may yield gibberish in either case. What really matters
654 is whether converting back to double again reproduces what we
655 started with. */
656 aslong = (long)wholepart;
657 if ((double)aslong == wholepart)
658 return PyInt_FromLong(aslong);
659 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
660 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000661}
662
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000664float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000665{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 double x = PyFloat_AsDouble(v);
667 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000668}
669
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000671float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000672{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000674 return v;
675}
676
677
Guido van Rossumbef14172001-08-29 15:47:46 +0000678staticforward PyObject *
679float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
680
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681static PyObject *
682float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
683{
684 PyObject *x = Py_False; /* Integer zero */
685 static char *kwlist[] = {"x", 0};
686
Guido van Rossumbef14172001-08-29 15:47:46 +0000687 if (type != &PyFloat_Type)
688 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
690 return NULL;
691 if (PyString_Check(x))
692 return PyFloat_FromString(x, NULL);
693 return PyNumber_Float(x);
694}
695
Guido van Rossumbef14172001-08-29 15:47:46 +0000696/* Wimpy, slow approach to tp_new calls for subtypes of float:
697 first create a regular float from whatever arguments we got,
698 then allocate a subtype instance and initialize its ob_fval
699 from the regular float. The regular float is then thrown away.
700*/
701static PyObject *
702float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
703{
704 PyObject *tmp, *new;
705
706 assert(PyType_IsSubtype(type, &PyFloat_Type));
707 tmp = float_new(&PyFloat_Type, args, kwds);
708 if (tmp == NULL)
709 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000710 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000711 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000712 if (new == NULL)
713 return NULL;
714 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
715 Py_DECREF(tmp);
716 return new;
717}
718
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719static char float_doc[] =
720"float(x) -> floating point number\n\
721\n\
722Convert a string or number to a floating point number, if possible.";
723
724
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000726 (binaryfunc)float_add, /*nb_add*/
727 (binaryfunc)float_sub, /*nb_subtract*/
728 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000729 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000730 (binaryfunc)float_rem, /*nb_remainder*/
731 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000732 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000733 (unaryfunc)float_neg, /*nb_negative*/
734 (unaryfunc)float_pos, /*nb_positive*/
735 (unaryfunc)float_abs, /*nb_absolute*/
736 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000737 0, /*nb_invert*/
738 0, /*nb_lshift*/
739 0, /*nb_rshift*/
740 0, /*nb_and*/
741 0, /*nb_xor*/
742 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743 (coercion)float_coerce, /*nb_coerce*/
744 (unaryfunc)float_int, /*nb_int*/
745 (unaryfunc)float_long, /*nb_long*/
746 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000747 0, /* nb_oct */
748 0, /* nb_hex */
749 0, /* nb_inplace_add */
750 0, /* nb_inplace_subtract */
751 0, /* nb_inplace_multiply */
752 0, /* nb_inplace_divide */
753 0, /* nb_inplace_remainder */
754 0, /* nb_inplace_power */
755 0, /* nb_inplace_lshift */
756 0, /* nb_inplace_rshift */
757 0, /* nb_inplace_and */
758 0, /* nb_inplace_xor */
759 0, /* nb_inplace_or */
Tim Peters63a35712001-12-11 19:57:24 +0000760 float_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +0000761 float_div, /* nb_true_divide */
762 0, /* nb_inplace_floor_divide */
763 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764};
765
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766PyTypeObject PyFloat_Type = {
767 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768 0,
769 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000770 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772 (destructor)float_dealloc, /* tp_dealloc */
773 (printfunc)float_print, /* tp_print */
774 0, /* tp_getattr */
775 0, /* tp_setattr */
776 (cmpfunc)float_compare, /* tp_compare */
777 (reprfunc)float_repr, /* tp_repr */
778 &float_as_number, /* tp_as_number */
779 0, /* tp_as_sequence */
780 0, /* tp_as_mapping */
781 (hashfunc)float_hash, /* tp_hash */
782 0, /* tp_call */
783 (reprfunc)float_str, /* tp_str */
784 PyObject_GenericGetAttr, /* tp_getattro */
785 0, /* tp_setattro */
786 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000787 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
788 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000789 float_doc, /* tp_doc */
790 0, /* tp_traverse */
791 0, /* tp_clear */
792 0, /* tp_richcompare */
793 0, /* tp_weaklistoffset */
794 0, /* tp_iter */
795 0, /* tp_iternext */
796 0, /* tp_methods */
797 0, /* tp_members */
798 0, /* tp_getset */
799 0, /* tp_base */
800 0, /* tp_dict */
801 0, /* tp_descr_get */
802 0, /* tp_descr_set */
803 0, /* tp_dictoffset */
804 0, /* tp_init */
805 0, /* tp_alloc */
806 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000807};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000808
809void
Fred Drakefd99de62000-07-09 05:02:18 +0000810PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000811{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000812 PyFloatObject *p;
813 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000814 int i;
815 int bc, bf; /* block count, number of freed blocks */
816 int frem, fsum; /* remaining unfreed floats per block, total */
817
818 bc = 0;
819 bf = 0;
820 fsum = 0;
821 list = block_list;
822 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000823 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000824 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000825 bc++;
826 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000827 for (i = 0, p = &list->objects[0];
828 i < N_FLOATOBJECTS;
829 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000830 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000831 frem++;
832 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000833 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000834 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000835 list->next = block_list;
836 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000837 for (i = 0, p = &list->objects[0];
838 i < N_FLOATOBJECTS;
839 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000840 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000841 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000842 p->ob_type = (struct _typeobject *)
843 free_list;
844 free_list = p;
845 }
846 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000847 }
848 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000849 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000850 bf++;
851 }
852 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000853 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000854 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000855 if (!Py_VerboseFlag)
856 return;
857 fprintf(stderr, "# cleanup floats");
858 if (!fsum) {
859 fprintf(stderr, "\n");
860 }
861 else {
862 fprintf(stderr,
863 ": %d unfreed float%s in %d out of %d block%s\n",
864 fsum, fsum == 1 ? "" : "s",
865 bc - bf, bc, bc == 1 ? "" : "s");
866 }
867 if (Py_VerboseFlag > 1) {
868 list = block_list;
869 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000870 for (i = 0, p = &list->objects[0];
871 i < N_FLOATOBJECTS;
872 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000873 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000874 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000875 char buf[100];
876 PyFloat_AsString(buf, p);
877 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000878 "# <float at %p, refcnt=%d, val=%s>\n",
879 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000880 }
881 }
882 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000883 }
884 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000885}