blob: 6e6575695830b2b43f36c74266b42c9c527961c4 [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 Rossume3a8e7e2002-08-19 19:26:42 +000067 /* Inline PyObject_New */
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,
Skip Montanaro71390a92002-05-02 13:03:22 +0000126 "float() argument must be a string or a number");
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
Neil Schemenauer2c77e902002-11-18 16:06:21 +0000205 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 return -1;
208 }
Tim Petersd2364e82001-11-01 20:09:42 +0000209
Neil Schemenauer2c77e902002-11-18 16:06:21 +0000210 if ((nb = op->ob_type->tp_as_number) == NULL || nb->nb_float == NULL) {
211 PyErr_SetString(PyExc_TypeError, "a float is required");
212 return -1;
213 }
214
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216 if (fo == NULL)
217 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 if (!PyFloat_Check(fo)) {
219 PyErr_SetString(PyExc_TypeError,
220 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221 return -1;
222 }
Tim Petersd2364e82001-11-01 20:09:42 +0000223
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 val = PyFloat_AS_DOUBLE(fo);
225 Py_DECREF(fo);
Tim Petersd2364e82001-11-01 20:09:42 +0000226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228}
229
230/* Methods */
231
Tim Peters97019e42001-11-28 22:43:45 +0000232static void
233format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234{
235 register char *cp;
236 /* Subroutine for float_repr and float_print.
237 We want float numbers to be recognizable as such,
238 i.e., they should contain a decimal point or an exponent.
239 However, %g may print the number as an integer;
240 in such cases, we append ".0" to the string. */
Tim Peters97019e42001-11-28 22:43:45 +0000241
242 assert(PyFloat_Check(v));
243 PyOS_snprintf(buf, buflen, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 cp = buf;
245 if (*cp == '-')
246 cp++;
247 for (; *cp != '\0'; cp++) {
248 /* Any non-digit means it's not an integer;
249 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000250 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251 break;
252 }
253 if (*cp == '\0') {
254 *cp++ = '.';
255 *cp++ = '0';
256 *cp++ = '\0';
257 }
258}
259
Tim Peters97019e42001-11-28 22:43:45 +0000260/* XXX PyFloat_AsStringEx should not be a public API function (for one
261 XXX thing, its signature passes a buffer without a length; for another,
262 XXX it isn't useful outside this file).
263*/
264void
265PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
266{
267 format_float(buf, 100, v, precision);
268}
269
Neil Schemenauer32117e52001-01-04 01:44:34 +0000270/* Macro and helper that convert PyObject obj to a C double and store
271 the value in dbl; this replaces the functionality of the coercion
Tim Peters77d8a4f2001-12-11 20:31:34 +0000272 slot function. If conversion to double raises an exception, obj is
273 set to NULL, and the function invoking this macro returns NULL. If
274 obj is not of float, int or long type, Py_NotImplemented is incref'ed,
275 stored in obj, and returned from the function invoking this macro.
276*/
Neil Schemenauer32117e52001-01-04 01:44:34 +0000277#define CONVERT_TO_DOUBLE(obj, dbl) \
278 if (PyFloat_Check(obj)) \
279 dbl = PyFloat_AS_DOUBLE(obj); \
280 else if (convert_to_double(&(obj), &(dbl)) < 0) \
281 return obj;
282
283static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000284convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000285{
286 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000287
Neil Schemenauer32117e52001-01-04 01:44:34 +0000288 if (PyInt_Check(obj)) {
289 *dbl = (double)PyInt_AS_LONG(obj);
290 }
291 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000292 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000293 if (*dbl == -1.0 && PyErr_Occurred()) {
294 *v = NULL;
295 return -1;
296 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000297 }
298 else {
299 Py_INCREF(Py_NotImplemented);
300 *v = Py_NotImplemented;
301 return -1;
302 }
303 return 0;
304}
305
Guido van Rossum57072eb1999-12-23 19:00:28 +0000306/* Precisions used by repr() and str(), respectively.
307
308 The repr() precision (17 significant decimal digits) is the minimal number
309 that is guaranteed to have enough precision so that if the number is read
310 back in the exact same binary value is recreated. This is true for IEEE
311 floating point by design, and also happens to work for all other modern
312 hardware.
313
314 The str() precision is chosen so that in most cases, the rounding noise
315 created by various operations is suppressed, while giving plenty of
316 precision for practical use.
317
318*/
319
320#define PREC_REPR 17
321#define PREC_STR 12
322
Tim Peters97019e42001-11-28 22:43:45 +0000323/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
324 XXX they pass a char buffer without passing a length.
325*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000326void
Fred Drakefd99de62000-07-09 05:02:18 +0000327PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000328{
Tim Peters97019e42001-11-28 22:43:45 +0000329 format_float(buf, 100, v, PREC_STR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000330}
331
Tim Peters72f98e92001-05-08 15:19:57 +0000332void
333PyFloat_AsReprString(char *buf, PyFloatObject *v)
334{
Tim Peters97019e42001-11-28 22:43:45 +0000335 format_float(buf, 100, v, PREC_REPR);
Tim Peters72f98e92001-05-08 15:19:57 +0000336}
337
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000338/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000339static int
Fred Drakefd99de62000-07-09 05:02:18 +0000340float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341{
342 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000343 format_float(buf, sizeof(buf), v,
344 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000346 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347}
348
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000350float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351{
352 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000353 format_float(buf, sizeof(buf), v, PREC_REPR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000354 return PyString_FromString(buf);
355}
356
357static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000358float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000359{
360 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000361 format_float(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363}
364
365static int
Fred Drakefd99de62000-07-09 05:02:18 +0000366float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367{
368 double i = v->ob_fval;
369 double j = w->ob_fval;
370 return (i < j) ? -1 : (i > j) ? 1 : 0;
371}
372
Guido van Rossum9bfef441993-03-29 10:43:31 +0000373static long
Fred Drakefd99de62000-07-09 05:02:18 +0000374float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000375{
Tim Peters39dce292000-08-15 03:34:48 +0000376 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000377}
378
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000380float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000382 double a,b;
383 CONVERT_TO_DOUBLE(v, a);
384 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000385 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000386 a = a + b;
387 PyFPE_END_PROTECT(a)
388 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389}
390
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000392float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000394 double a,b;
395 CONVERT_TO_DOUBLE(v, a);
396 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000397 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000398 a = a - b;
399 PyFPE_END_PROTECT(a)
400 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401}
402
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000404float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000406 double a,b;
407 CONVERT_TO_DOUBLE(v, a);
408 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000409 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000410 a = a * b;
411 PyFPE_END_PROTECT(a)
412 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413}
414
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000415static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000416float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000418 double a,b;
419 CONVERT_TO_DOUBLE(v, a);
420 CONVERT_TO_DOUBLE(w, b);
421 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423 return NULL;
424 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000425 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000426 a = a / b;
427 PyFPE_END_PROTECT(a)
428 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000432float_classic_div(PyObject *v, PyObject *w)
433{
434 double a,b;
435 CONVERT_TO_DOUBLE(v, a);
436 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000437 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000438 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
439 return NULL;
440 if (b == 0.0) {
441 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
442 return NULL;
443 }
444 PyFPE_START_PROTECT("divide", return 0)
445 a = a / b;
446 PyFPE_END_PROTECT(a)
447 return PyFloat_FromDouble(a);
448}
449
450static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000451float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000453 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000454 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000455 CONVERT_TO_DOUBLE(v, vx);
456 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459 return NULL;
460 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000461 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000462 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000463 /* note: checking mod*wx < 0 is incorrect -- underflows to
464 0 if wx < sqrt(smallest nonzero double) */
465 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000466 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000467 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000468 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470}
471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000473float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000474{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000475 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000476 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000477 CONVERT_TO_DOUBLE(v, vx);
478 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000479 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000481 return NULL;
482 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000483 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000484 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000485 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000486 exact multiple of wx. But this is fp arithmetic, and fp
487 vx - mod is an approximation; the result is that div may
488 not be an exact integral value after the division, although
489 it will always be very close to one.
490 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000491 div = (vx - mod) / wx;
Tim Petersd2e40d62001-11-01 23:12:27 +0000492 if (mod) {
493 /* ensure the remainder has the same sign as the denominator */
494 if ((wx < 0) != (mod < 0)) {
495 mod += wx;
496 div -= 1.0;
497 }
498 }
499 else {
500 /* the remainder is zero, and in the presence of signed zeroes
501 fmod returns different results across platforms; ensure
502 it has the same sign as the denominator; we'd like to do
503 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000504 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000505 if (wx < 0.0)
506 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000507 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000508 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000509 if (div) {
510 floordiv = floor(div);
511 if (div - floordiv > 0.5)
512 floordiv += 1.0;
513 }
514 else {
515 /* div is zero - get the same sign as the true quotient */
516 div *= div; /* hide "div = +0" from optimizers */
517 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
518 }
519 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000520 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000521}
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523static PyObject *
Tim Peters63a35712001-12-11 19:57:24 +0000524float_floor_div(PyObject *v, PyObject *w)
525{
526 PyObject *t, *r;
527
528 t = float_divmod(v, w);
Tim Peters77d8a4f2001-12-11 20:31:34 +0000529 if (t == NULL || t == Py_NotImplemented)
530 return t;
531 assert(PyTuple_CheckExact(t));
532 r = PyTuple_GET_ITEM(t, 0);
533 Py_INCREF(r);
534 Py_DECREF(t);
535 return r;
Tim Peters63a35712001-12-11 19:57:24 +0000536}
537
538static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000539float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540{
541 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000542
543 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000544 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000545 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000546 return NULL;
547 }
548
Neil Schemenauer32117e52001-01-04 01:44:34 +0000549 CONVERT_TO_DOUBLE(v, iv);
550 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000551
552 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000553 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000554 PyFPE_START_PROTECT("pow", return NULL)
555 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000556 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000557 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000558 ix = fmod(1.0, iz);
559 if (ix != 0 && iz < 0)
560 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000561 }
Tim Petersc54d1902000-10-06 00:36:09 +0000562 else
563 ix = 1.0;
564 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000565 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000566 }
Tim Peters96685bf2001-08-23 22:31:37 +0000567 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000568 if (iw < 0.0) {
569 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000570 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000571 return NULL;
572 }
573 return PyFloat_FromDouble(0.0);
574 }
Tim Peters96685bf2001-08-23 22:31:37 +0000575 if (iv < 0.0 && iw != floor(iw)) {
576 PyErr_SetString(PyExc_ValueError,
577 "negative number cannot be raised to a fractional power");
578 return NULL;
Guido van Rossum86c04c21996-08-09 20:50:14 +0000579 }
Tim Peters96685bf2001-08-23 22:31:37 +0000580 errno = 0;
581 PyFPE_START_PROTECT("pow", return NULL)
582 ix = pow(iv, iw);
583 PyFPE_END_PROTECT(ix)
Tim Petersdc5a5082002-03-09 04:58:24 +0000584 Py_ADJUST_ERANGE1(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000585 if (errno != 0) {
Tim Petersdc5a5082002-03-09 04:58:24 +0000586 assert(errno == ERANGE);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000589 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591}
592
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000594float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597}
598
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000600float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601{
Tim Peters0280cf72001-09-11 21:53:35 +0000602 if (PyFloat_CheckExact(v)) {
603 Py_INCREF(v);
604 return (PyObject *)v;
605 }
606 else
607 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000608}
609
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000611float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000612{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000613 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614}
615
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000616static int
Fred Drakefd99de62000-07-09 05:02:18 +0000617float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000618{
619 return v->ob_fval != 0.0;
620}
621
Guido van Rossum234f9421993-06-17 12:35:49 +0000622static int
Fred Drakefd99de62000-07-09 05:02:18 +0000623float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000624{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625 if (PyInt_Check(*pw)) {
626 long x = PyInt_AsLong(*pw);
627 *pw = PyFloat_FromDouble((double)x);
628 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000629 return 0;
630 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 else if (PyLong_Check(*pw)) {
Neal Norwitzabcb0c02003-01-28 19:21:24 +0000632 double x = PyLong_AsDouble(*pw);
633 if (x == -1.0 && PyErr_Occurred())
634 return -1;
635 *pw = PyFloat_FromDouble(x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000637 return 0;
638 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000639 else if (PyFloat_Check(*pw)) {
640 Py_INCREF(*pv);
641 Py_INCREF(*pw);
642 return 0;
643 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000644 return 1; /* Can't do it */
645}
646
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +0000648float_long(PyObject *v)
649{
650 double x = PyFloat_AsDouble(v);
651 return PyLong_FromDouble(x);
652}
653
654static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000655float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000656{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000658 double wholepart; /* integral portion of x, rounded toward 0 */
Tim Peters7321ec42001-07-26 20:02:17 +0000659
660 (void)modf(x, &wholepart);
Tim Peters7d791242002-11-21 22:26:37 +0000661 /* Try to get out cheap if this fits in a Python int. The attempt
662 * to cast to long must be protected, as C doesn't define what
663 * happens if the double is too big to fit in a long. Some rare
664 * systems raise an exception then (RISCOS was mentioned as one,
665 * and someone using a non-default option on Sun also bumped into
666 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
667 * still be vulnerable: if a long has more bits of precision than
668 * a double, casting MIN/MAX to double may yield an approximation,
669 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
670 * yield true from the C expression wholepart<=LONG_MAX, despite
671 * that wholepart is actually greater than LONG_MAX.
672 */
673 if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
674 const long aslong = (long)wholepart;
Tim Peters7321ec42001-07-26 20:02:17 +0000675 return PyInt_FromLong(aslong);
Tim Peters7d791242002-11-21 22:26:37 +0000676 }
677 return PyLong_FromDouble(wholepart);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000678}
679
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000681float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000682{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000684 return v;
685}
686
687
Jeremy Hylton938ace62002-07-17 16:30:39 +0000688static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000689float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
690
Tim Peters6d6c1a32001-08-02 04:15:00 +0000691static PyObject *
692float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
693{
694 PyObject *x = Py_False; /* Integer zero */
695 static char *kwlist[] = {"x", 0};
696
Guido van Rossumbef14172001-08-29 15:47:46 +0000697 if (type != &PyFloat_Type)
698 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000699 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
700 return NULL;
701 if (PyString_Check(x))
702 return PyFloat_FromString(x, NULL);
703 return PyNumber_Float(x);
704}
705
Guido van Rossumbef14172001-08-29 15:47:46 +0000706/* Wimpy, slow approach to tp_new calls for subtypes of float:
707 first create a regular float from whatever arguments we got,
708 then allocate a subtype instance and initialize its ob_fval
709 from the regular float. The regular float is then thrown away.
710*/
711static PyObject *
712float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
713{
714 PyObject *tmp, *new;
715
716 assert(PyType_IsSubtype(type, &PyFloat_Type));
717 tmp = float_new(&PyFloat_Type, args, kwds);
718 if (tmp == NULL)
719 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000720 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000721 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000722 if (new == NULL)
723 return NULL;
724 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
725 Py_DECREF(tmp);
726 return new;
727}
728
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000729static PyObject *
730float_getnewargs(PyFloatObject *v)
731{
732 return Py_BuildValue("(d)", v->ob_fval);
733}
734
735static PyMethodDef float_methods[] = {
736 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
737 {NULL, NULL} /* sentinel */
738};
739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000740PyDoc_STRVAR(float_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000741"float(x) -> floating point number\n\
742\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000743Convert a string or number to a floating point number, if possible.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744
745
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000747 (binaryfunc)float_add, /*nb_add*/
748 (binaryfunc)float_sub, /*nb_subtract*/
749 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000750 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751 (binaryfunc)float_rem, /*nb_remainder*/
752 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000753 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000754 (unaryfunc)float_neg, /*nb_negative*/
755 (unaryfunc)float_pos, /*nb_positive*/
756 (unaryfunc)float_abs, /*nb_absolute*/
757 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000758 0, /*nb_invert*/
759 0, /*nb_lshift*/
760 0, /*nb_rshift*/
761 0, /*nb_and*/
762 0, /*nb_xor*/
763 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000764 (coercion)float_coerce, /*nb_coerce*/
765 (unaryfunc)float_int, /*nb_int*/
766 (unaryfunc)float_long, /*nb_long*/
767 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000768 0, /* nb_oct */
769 0, /* nb_hex */
770 0, /* nb_inplace_add */
771 0, /* nb_inplace_subtract */
772 0, /* nb_inplace_multiply */
773 0, /* nb_inplace_divide */
774 0, /* nb_inplace_remainder */
775 0, /* nb_inplace_power */
776 0, /* nb_inplace_lshift */
777 0, /* nb_inplace_rshift */
778 0, /* nb_inplace_and */
779 0, /* nb_inplace_xor */
780 0, /* nb_inplace_or */
Tim Peters63a35712001-12-11 19:57:24 +0000781 float_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +0000782 float_div, /* nb_true_divide */
783 0, /* nb_inplace_floor_divide */
784 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785};
786
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787PyTypeObject PyFloat_Type = {
788 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789 0,
790 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000791 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000793 (destructor)float_dealloc, /* tp_dealloc */
794 (printfunc)float_print, /* tp_print */
795 0, /* tp_getattr */
796 0, /* tp_setattr */
797 (cmpfunc)float_compare, /* tp_compare */
798 (reprfunc)float_repr, /* tp_repr */
799 &float_as_number, /* tp_as_number */
800 0, /* tp_as_sequence */
801 0, /* tp_as_mapping */
802 (hashfunc)float_hash, /* tp_hash */
803 0, /* tp_call */
804 (reprfunc)float_str, /* tp_str */
805 PyObject_GenericGetAttr, /* tp_getattro */
806 0, /* tp_setattro */
807 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000808 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
809 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810 float_doc, /* tp_doc */
811 0, /* tp_traverse */
812 0, /* tp_clear */
813 0, /* tp_richcompare */
814 0, /* tp_weaklistoffset */
815 0, /* tp_iter */
816 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000817 float_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000818 0, /* tp_members */
819 0, /* tp_getset */
820 0, /* tp_base */
821 0, /* tp_dict */
822 0, /* tp_descr_get */
823 0, /* tp_descr_set */
824 0, /* tp_dictoffset */
825 0, /* tp_init */
826 0, /* tp_alloc */
827 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000828};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000829
830void
Fred Drakefd99de62000-07-09 05:02:18 +0000831PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000832{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000833 PyFloatObject *p;
834 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000835 int i;
836 int bc, bf; /* block count, number of freed blocks */
837 int frem, fsum; /* remaining unfreed floats per block, total */
838
839 bc = 0;
840 bf = 0;
841 fsum = 0;
842 list = block_list;
843 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000844 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000845 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000846 bc++;
847 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000848 for (i = 0, p = &list->objects[0];
849 i < N_FLOATOBJECTS;
850 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000851 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000852 frem++;
853 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000854 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000855 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000856 list->next = block_list;
857 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000858 for (i = 0, p = &list->objects[0];
859 i < N_FLOATOBJECTS;
860 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000861 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000862 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000863 p->ob_type = (struct _typeobject *)
864 free_list;
865 free_list = p;
866 }
867 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000868 }
869 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000870 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000871 bf++;
872 }
873 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000874 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000875 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000876 if (!Py_VerboseFlag)
877 return;
878 fprintf(stderr, "# cleanup floats");
879 if (!fsum) {
880 fprintf(stderr, "\n");
881 }
882 else {
883 fprintf(stderr,
884 ": %d unfreed float%s in %d out of %d block%s\n",
885 fsum, fsum == 1 ? "" : "s",
886 bc - bf, bc, bc == 1 ? "" : "s");
887 }
888 if (Py_VerboseFlag > 1) {
889 list = block_list;
890 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000891 for (i = 0, p = &list->objects[0];
892 i < N_FLOATOBJECTS;
893 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000894 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000895 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000896 char buf[100];
897 PyFloat_AsString(buf, p);
898 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000899 "# <float at %p, refcnt=%d, val=%s>\n",
900 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000901 }
902 }
903 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000904 }
905 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000906}