blob: f36479f9789351eb1bda1262e535f7002acc69f2 [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 Peterse87568d2003-05-24 20:18:24 +0000575 if (iv < 0.0) {
576 /* Whether this is an error is a mess, and bumps into libm
577 * bugs so we have to figure it out ourselves.
578 */
579 if (iw != floor(iw)) {
580 PyErr_SetString(PyExc_ValueError, "negative number "
581 "cannot be raised to a fractional power");
582 return NULL;
583 }
584 /* iw is an exact integer, albeit perhaps a very large one.
585 * -1 raised to an exact integer should never be exceptional.
586 * Alas, some libms (chiefly glibc as of early 2003) return
587 * NaN and set EDOM on pow(-1, large_int) if the int doesn't
588 * happen to be representable in a *C* integer. That's a
589 * bug; we let that slide in math.pow() (which currently
590 * reflects all platform accidents), but not for Python's **.
591 */
592 if (iv == -1.0 && !Py_IS_INFINITY(iw) && iw == iw) {
593 /* XXX the "iw == iw" was to weed out NaNs. This
594 * XXX doesn't actually work on all platforms.
595 */
596 /* Return 1 if iw is even, -1 if iw is odd; there's
597 * no guarantee that any C integral type is big
598 * enough to hold iw, so we have to check this
599 * indirectly.
600 */
601 ix = floor(iw * 0.5) * 2.0;
602 return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);
603 }
604 /* Else iv != -1.0, and overflow or underflow are possible.
605 * Unless we're to write pow() ourselves, we have to trust
606 * the platform to do this correctly.
607 */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000608 }
Tim Peters96685bf2001-08-23 22:31:37 +0000609 errno = 0;
610 PyFPE_START_PROTECT("pow", return NULL)
611 ix = pow(iv, iw);
612 PyFPE_END_PROTECT(ix)
Tim Petersdc5a5082002-03-09 04:58:24 +0000613 Py_ADJUST_ERANGE1(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000614 if (errno != 0) {
Tim Peterse87568d2003-05-24 20:18:24 +0000615 /* We don't expect any errno value other than ERANGE, but
616 * the range of libm bugs appears unbounded.
617 */
618 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
619 PyExc_ValueError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000621 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000622 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623}
624
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000626float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629}
630
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000632float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633{
Tim Peters0280cf72001-09-11 21:53:35 +0000634 if (PyFloat_CheckExact(v)) {
635 Py_INCREF(v);
636 return (PyObject *)v;
637 }
638 else
639 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000640}
641
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000643float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000644{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000645 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646}
647
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000648static int
Fred Drakefd99de62000-07-09 05:02:18 +0000649float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000650{
651 return v->ob_fval != 0.0;
652}
653
Guido van Rossum234f9421993-06-17 12:35:49 +0000654static int
Fred Drakefd99de62000-07-09 05:02:18 +0000655float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000656{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 if (PyInt_Check(*pw)) {
658 long x = PyInt_AsLong(*pw);
659 *pw = PyFloat_FromDouble((double)x);
660 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000661 return 0;
662 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663 else if (PyLong_Check(*pw)) {
Neal Norwitzabcb0c02003-01-28 19:21:24 +0000664 double x = PyLong_AsDouble(*pw);
665 if (x == -1.0 && PyErr_Occurred())
666 return -1;
667 *pw = PyFloat_FromDouble(x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000669 return 0;
670 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000671 else if (PyFloat_Check(*pw)) {
672 Py_INCREF(*pv);
673 Py_INCREF(*pw);
674 return 0;
675 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000676 return 1; /* Can't do it */
677}
678
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +0000680float_long(PyObject *v)
681{
682 double x = PyFloat_AsDouble(v);
683 return PyLong_FromDouble(x);
684}
685
686static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000687float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000688{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000690 double wholepart; /* integral portion of x, rounded toward 0 */
Tim Peters7321ec42001-07-26 20:02:17 +0000691
692 (void)modf(x, &wholepart);
Tim Peters7d791242002-11-21 22:26:37 +0000693 /* Try to get out cheap if this fits in a Python int. The attempt
694 * to cast to long must be protected, as C doesn't define what
695 * happens if the double is too big to fit in a long. Some rare
696 * systems raise an exception then (RISCOS was mentioned as one,
697 * and someone using a non-default option on Sun also bumped into
698 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
699 * still be vulnerable: if a long has more bits of precision than
700 * a double, casting MIN/MAX to double may yield an approximation,
701 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
702 * yield true from the C expression wholepart<=LONG_MAX, despite
703 * that wholepart is actually greater than LONG_MAX.
704 */
705 if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
706 const long aslong = (long)wholepart;
Tim Peters7321ec42001-07-26 20:02:17 +0000707 return PyInt_FromLong(aslong);
Tim Peters7d791242002-11-21 22:26:37 +0000708 }
709 return PyLong_FromDouble(wholepart);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000710}
711
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000713float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000714{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000716 return v;
717}
718
719
Jeremy Hylton938ace62002-07-17 16:30:39 +0000720static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000721float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
722
Tim Peters6d6c1a32001-08-02 04:15:00 +0000723static PyObject *
724float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
725{
726 PyObject *x = Py_False; /* Integer zero */
727 static char *kwlist[] = {"x", 0};
728
Guido van Rossumbef14172001-08-29 15:47:46 +0000729 if (type != &PyFloat_Type)
730 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
732 return NULL;
733 if (PyString_Check(x))
734 return PyFloat_FromString(x, NULL);
735 return PyNumber_Float(x);
736}
737
Guido van Rossumbef14172001-08-29 15:47:46 +0000738/* Wimpy, slow approach to tp_new calls for subtypes of float:
739 first create a regular float from whatever arguments we got,
740 then allocate a subtype instance and initialize its ob_fval
741 from the regular float. The regular float is then thrown away.
742*/
743static PyObject *
744float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
745{
746 PyObject *tmp, *new;
747
748 assert(PyType_IsSubtype(type, &PyFloat_Type));
749 tmp = float_new(&PyFloat_Type, args, kwds);
750 if (tmp == NULL)
751 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000752 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000753 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000754 if (new == NULL)
755 return NULL;
756 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
757 Py_DECREF(tmp);
758 return new;
759}
760
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000761static PyObject *
762float_getnewargs(PyFloatObject *v)
763{
764 return Py_BuildValue("(d)", v->ob_fval);
765}
766
767static PyMethodDef float_methods[] = {
768 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
769 {NULL, NULL} /* sentinel */
770};
771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000772PyDoc_STRVAR(float_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773"float(x) -> floating point number\n\
774\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000775Convert a string or number to a floating point number, if possible.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000776
777
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000779 (binaryfunc)float_add, /*nb_add*/
780 (binaryfunc)float_sub, /*nb_subtract*/
781 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000782 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000783 (binaryfunc)float_rem, /*nb_remainder*/
784 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000785 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786 (unaryfunc)float_neg, /*nb_negative*/
787 (unaryfunc)float_pos, /*nb_positive*/
788 (unaryfunc)float_abs, /*nb_absolute*/
789 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000790 0, /*nb_invert*/
791 0, /*nb_lshift*/
792 0, /*nb_rshift*/
793 0, /*nb_and*/
794 0, /*nb_xor*/
795 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 (coercion)float_coerce, /*nb_coerce*/
797 (unaryfunc)float_int, /*nb_int*/
798 (unaryfunc)float_long, /*nb_long*/
799 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000800 0, /* nb_oct */
801 0, /* nb_hex */
802 0, /* nb_inplace_add */
803 0, /* nb_inplace_subtract */
804 0, /* nb_inplace_multiply */
805 0, /* nb_inplace_divide */
806 0, /* nb_inplace_remainder */
807 0, /* nb_inplace_power */
808 0, /* nb_inplace_lshift */
809 0, /* nb_inplace_rshift */
810 0, /* nb_inplace_and */
811 0, /* nb_inplace_xor */
812 0, /* nb_inplace_or */
Tim Peters63a35712001-12-11 19:57:24 +0000813 float_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +0000814 float_div, /* nb_true_divide */
815 0, /* nb_inplace_floor_divide */
816 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817};
818
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819PyTypeObject PyFloat_Type = {
820 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000821 0,
822 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000824 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000825 (destructor)float_dealloc, /* tp_dealloc */
826 (printfunc)float_print, /* tp_print */
827 0, /* tp_getattr */
828 0, /* tp_setattr */
829 (cmpfunc)float_compare, /* tp_compare */
830 (reprfunc)float_repr, /* tp_repr */
831 &float_as_number, /* tp_as_number */
832 0, /* tp_as_sequence */
833 0, /* tp_as_mapping */
834 (hashfunc)float_hash, /* tp_hash */
835 0, /* tp_call */
836 (reprfunc)float_str, /* tp_str */
837 PyObject_GenericGetAttr, /* tp_getattro */
838 0, /* tp_setattro */
839 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000840 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
841 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000842 float_doc, /* tp_doc */
843 0, /* tp_traverse */
844 0, /* tp_clear */
845 0, /* tp_richcompare */
846 0, /* tp_weaklistoffset */
847 0, /* tp_iter */
848 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000849 float_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850 0, /* tp_members */
851 0, /* tp_getset */
852 0, /* tp_base */
853 0, /* tp_dict */
854 0, /* tp_descr_get */
855 0, /* tp_descr_set */
856 0, /* tp_dictoffset */
857 0, /* tp_init */
858 0, /* tp_alloc */
859 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000861
862void
Fred Drakefd99de62000-07-09 05:02:18 +0000863PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000864{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000865 PyFloatObject *p;
866 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000867 int i;
868 int bc, bf; /* block count, number of freed blocks */
869 int frem, fsum; /* remaining unfreed floats per block, total */
870
871 bc = 0;
872 bf = 0;
873 fsum = 0;
874 list = block_list;
875 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000876 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000877 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000878 bc++;
879 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000880 for (i = 0, p = &list->objects[0];
881 i < N_FLOATOBJECTS;
882 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000883 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000884 frem++;
885 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000886 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000887 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000888 list->next = block_list;
889 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000890 for (i = 0, p = &list->objects[0];
891 i < N_FLOATOBJECTS;
892 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000893 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000894 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000895 p->ob_type = (struct _typeobject *)
896 free_list;
897 free_list = p;
898 }
899 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000900 }
901 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000902 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000903 bf++;
904 }
905 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000906 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000907 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000908 if (!Py_VerboseFlag)
909 return;
910 fprintf(stderr, "# cleanup floats");
911 if (!fsum) {
912 fprintf(stderr, "\n");
913 }
914 else {
915 fprintf(stderr,
916 ": %d unfreed float%s in %d out of %d block%s\n",
917 fsum, fsum == 1 ? "" : "s",
918 bc - bf, bc, bc == 1 ? "" : "s");
919 }
920 if (Py_VerboseFlag > 1) {
921 list = block_list;
922 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000923 for (i = 0, p = &list->objects[0];
924 i < N_FLOATOBJECTS;
925 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000926 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000927 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000928 char buf[100];
929 PyFloat_AsString(buf, p);
930 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000931 "# <float at %p, refcnt=%d, val=%s>\n",
932 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000933 }
934 }
935 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000936 }
937 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000938}
Tim Peters9905b942003-03-20 20:53:32 +0000939
940/*----------------------------------------------------------------------------
941 * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
942 *
943 * TODO: On platforms that use the standard IEEE-754 single and double
944 * formats natively, these routines could simply copy the bytes.
945 */
946int
947_PyFloat_Pack4(double x, unsigned char *p, int le)
948{
949 unsigned char sign;
950 int e;
951 double f;
952 unsigned int fbits;
953 int incr = 1;
954
955 if (le) {
956 p += 3;
957 incr = -1;
958 }
959
960 if (x < 0) {
961 sign = 1;
962 x = -x;
963 }
964 else
965 sign = 0;
966
967 f = frexp(x, &e);
968
969 /* Normalize f to be in the range [1.0, 2.0) */
970 if (0.5 <= f && f < 1.0) {
971 f *= 2.0;
972 e--;
973 }
974 else if (f == 0.0)
975 e = 0;
976 else {
977 PyErr_SetString(PyExc_SystemError,
978 "frexp() result out of range");
979 return -1;
980 }
981
982 if (e >= 128)
983 goto Overflow;
984 else if (e < -126) {
985 /* Gradual underflow */
986 f = ldexp(f, 126 + e);
987 e = 0;
988 }
989 else if (!(e == 0 && f == 0.0)) {
990 e += 127;
991 f -= 1.0; /* Get rid of leading 1 */
992 }
993
994 f *= 8388608.0; /* 2**23 */
Tim Petersf1ed9342003-03-21 17:10:03 +0000995 fbits = (unsigned int)(f + 0.5); /* Round */
Tim Peters9905b942003-03-20 20:53:32 +0000996 assert(fbits <= 8388608);
997 if (fbits >> 23) {
998 /* The carry propagated out of a string of 23 1 bits. */
999 fbits = 0;
1000 ++e;
1001 if (e >= 255)
1002 goto Overflow;
1003 }
1004
1005 /* First byte */
1006 *p = (sign << 7) | (e >> 1);
1007 p += incr;
1008
1009 /* Second byte */
1010 *p = (char) (((e & 1) << 7) | (fbits >> 16));
1011 p += incr;
1012
1013 /* Third byte */
1014 *p = (fbits >> 8) & 0xFF;
1015 p += incr;
1016
1017 /* Fourth byte */
1018 *p = fbits & 0xFF;
1019
1020 /* Done */
1021 return 0;
1022
1023 Overflow:
1024 PyErr_SetString(PyExc_OverflowError,
1025 "float too large to pack with f format");
1026 return -1;
1027}
1028
1029int
1030_PyFloat_Pack8(double x, unsigned char *p, int le)
1031{
1032 unsigned char sign;
1033 int e;
1034 double f;
1035 unsigned int fhi, flo;
1036 int incr = 1;
1037
1038 if (le) {
1039 p += 7;
1040 incr = -1;
1041 }
1042
1043 if (x < 0) {
1044 sign = 1;
1045 x = -x;
1046 }
1047 else
1048 sign = 0;
1049
1050 f = frexp(x, &e);
1051
1052 /* Normalize f to be in the range [1.0, 2.0) */
1053 if (0.5 <= f && f < 1.0) {
1054 f *= 2.0;
1055 e--;
1056 }
1057 else if (f == 0.0)
1058 e = 0;
1059 else {
1060 PyErr_SetString(PyExc_SystemError,
1061 "frexp() result out of range");
1062 return -1;
1063 }
1064
1065 if (e >= 1024)
1066 goto Overflow;
1067 else if (e < -1022) {
1068 /* Gradual underflow */
1069 f = ldexp(f, 1022 + e);
1070 e = 0;
1071 }
1072 else if (!(e == 0 && f == 0.0)) {
1073 e += 1023;
1074 f -= 1.0; /* Get rid of leading 1 */
1075 }
1076
1077 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1078 f *= 268435456.0; /* 2**28 */
1079 fhi = (unsigned int)f; /* Truncate */
1080 assert(fhi < 268435456);
1081
1082 f -= (double)fhi;
1083 f *= 16777216.0; /* 2**24 */
1084 flo = (unsigned int)(f + 0.5); /* Round */
1085 assert(flo <= 16777216);
1086 if (flo >> 24) {
1087 /* The carry propagated out of a string of 24 1 bits. */
1088 flo = 0;
1089 ++fhi;
1090 if (fhi >> 28) {
1091 /* And it also progagated out of the next 28 bits. */
1092 fhi = 0;
1093 ++e;
1094 if (e >= 2047)
1095 goto Overflow;
1096 }
1097 }
1098
1099 /* First byte */
1100 *p = (sign << 7) | (e >> 4);
1101 p += incr;
1102
1103 /* Second byte */
1104 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
1105 p += incr;
1106
1107 /* Third byte */
1108 *p = (fhi >> 16) & 0xFF;
1109 p += incr;
1110
1111 /* Fourth byte */
1112 *p = (fhi >> 8) & 0xFF;
1113 p += incr;
1114
1115 /* Fifth byte */
1116 *p = fhi & 0xFF;
1117 p += incr;
1118
1119 /* Sixth byte */
1120 *p = (flo >> 16) & 0xFF;
1121 p += incr;
1122
1123 /* Seventh byte */
1124 *p = (flo >> 8) & 0xFF;
1125 p += incr;
1126
1127 /* Eighth byte */
1128 *p = flo & 0xFF;
1129 p += incr;
1130
1131 /* Done */
1132 return 0;
1133
1134 Overflow:
1135 PyErr_SetString(PyExc_OverflowError,
1136 "float too large to pack with d format");
1137 return -1;
1138}
1139
1140double
1141_PyFloat_Unpack4(const unsigned char *p, int le)
1142{
1143 unsigned char sign;
1144 int e;
1145 unsigned int f;
1146 double x;
1147 int incr = 1;
1148
1149 if (le) {
1150 p += 3;
1151 incr = -1;
1152 }
1153
1154 /* First byte */
1155 sign = (*p >> 7) & 1;
1156 e = (*p & 0x7F) << 1;
1157 p += incr;
1158
1159 /* Second byte */
1160 e |= (*p >> 7) & 1;
1161 f = (*p & 0x7F) << 16;
1162 p += incr;
1163
1164 /* Third byte */
1165 f |= *p << 8;
1166 p += incr;
1167
1168 /* Fourth byte */
1169 f |= *p;
1170
1171 x = (double)f / 8388608.0;
1172
1173 /* XXX This sadly ignores Inf/NaN issues */
1174 if (e == 0)
1175 e = -126;
1176 else {
1177 x += 1.0;
1178 e -= 127;
1179 }
1180 x = ldexp(x, e);
1181
1182 if (sign)
1183 x = -x;
1184
1185 return x;
1186}
1187
1188double
1189_PyFloat_Unpack8(const unsigned char *p, int le)
1190{
1191 unsigned char sign;
1192 int e;
1193 unsigned int fhi, flo;
1194 double x;
1195 int incr = 1;
1196
1197 if (le) {
1198 p += 7;
1199 incr = -1;
1200 }
1201
1202 /* First byte */
1203 sign = (*p >> 7) & 1;
1204 e = (*p & 0x7F) << 4;
1205 p += incr;
1206
1207 /* Second byte */
1208 e |= (*p >> 4) & 0xF;
1209 fhi = (*p & 0xF) << 24;
1210 p += incr;
1211
1212 /* Third byte */
1213 fhi |= *p << 16;
1214 p += incr;
1215
1216 /* Fourth byte */
1217 fhi |= *p << 8;
1218 p += incr;
1219
1220 /* Fifth byte */
1221 fhi |= *p;
1222 p += incr;
1223
1224 /* Sixth byte */
1225 flo = *p << 16;
1226 p += incr;
1227
1228 /* Seventh byte */
1229 flo |= *p << 8;
1230 p += incr;
1231
1232 /* Eighth byte */
1233 flo |= *p;
1234
1235 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
1236 x /= 268435456.0; /* 2**28 */
1237
1238 /* XXX This sadly ignores Inf/NaN */
1239 if (e == 0)
1240 e = -1022;
1241 else {
1242 x += 1.0;
1243 e -= 1023;
1244 }
1245 x = ldexp(x, e);
1246
1247 if (sign)
1248 x = -x;
1249
1250 return x;
1251}