blob: 539c4a9f4ce21dedaaf6a1382affb966e9aeb763 [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
Jack Janseneddc1442003-11-20 01:44:59 +000011#if !defined(__STDC__)
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
Guido van Rossum93ad0df1997-05-13 21:00:42 +000016/* Special free list -- see comments for same code in intobject.c. */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000017#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000018#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
Guido van Rossumf61bbc81999-03-12 00:12:21 +000019#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
Guido van Rossum3fce8831999-03-12 19:43:17 +000020
Guido van Rossum3fce8831999-03-12 19:43:17 +000021struct _floatblock {
22 struct _floatblock *next;
23 PyFloatObject objects[N_FLOATOBJECTS];
24};
25
26typedef struct _floatblock PyFloatBlock;
27
28static PyFloatBlock *block_list = NULL;
29static PyFloatObject *free_list = NULL;
30
Guido van Rossum93ad0df1997-05-13 21:00:42 +000031static PyFloatObject *
Fred Drakefd99de62000-07-09 05:02:18 +000032fill_free_list(void)
Guido van Rossum93ad0df1997-05-13 21:00:42 +000033{
34 PyFloatObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000035 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
36 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
Guido van Rossum93ad0df1997-05-13 21:00:42 +000037 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000038 return (PyFloatObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000039 ((PyFloatBlock *)p)->next = block_list;
40 block_list = (PyFloatBlock *)p;
41 p = &((PyFloatBlock *)p)->objects[0];
Guido van Rossum93ad0df1997-05-13 21:00:42 +000042 q = p + N_FLOATOBJECTS;
43 while (--q > p)
Guido van Rossumf61bbc81999-03-12 00:12:21 +000044 q->ob_type = (struct _typeobject *)(q-1);
45 q->ob_type = NULL;
Guido van Rossum93ad0df1997-05-13 21:00:42 +000046 return p + N_FLOATOBJECTS - 1;
47}
48
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050PyFloat_FromDouble(double fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Guido van Rossum93ad0df1997-05-13 21:00:42 +000052 register PyFloatObject *op;
53 if (free_list == NULL) {
54 if ((free_list = fill_free_list()) == NULL)
55 return NULL;
56 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +000057 /* Inline PyObject_New */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000058 op = free_list;
Guido van Rossumf61bbc81999-03-12 00:12:21 +000059 free_list = (PyFloatObject *)op->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +000060 PyObject_INIT(op, &PyFloat_Type);
Guido van Rossum2a9096b1990-10-21 22:15:08 +000061 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063}
64
Tim Petersef14d732000-09-23 03:39:17 +000065/**************************************************************************
66RED_FLAG 22-Sep-2000 tim
67PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
68
691. If v was a regular string, *pend was set to point to its terminating
70 null byte. That's useless (the caller can find that without any
71 help from this function!).
72
732. If v was a Unicode string, or an object convertible to a character
74 buffer, *pend was set to point into stack trash (the auto temp
75 vector holding the character buffer). That was downright dangerous.
76
77Since we can't change the interface of a public API function, pend is
78still supported but now *officially* useless: if pend is not NULL,
79*pend is set to NULL.
80**************************************************************************/
Barry Warsaw226ae6c1999-10-12 19:54:53 +000081PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000082PyFloat_FromString(PyObject *v, char **pend)
Barry Warsaw226ae6c1999-10-12 19:54:53 +000083{
Guido van Rossum4c08d552000-03-10 22:55:18 +000084 const char *s, *last, *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +000085 double x;
Tim Petersef14d732000-09-23 03:39:17 +000086 char buffer[256]; /* for errors */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000087#ifdef Py_USING_UNICODE
Tim Petersef14d732000-09-23 03:39:17 +000088 char s_buffer[256]; /* for objects convertible to a char buffer */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000089#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +000090 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091
Tim Petersef14d732000-09-23 03:39:17 +000092 if (pend)
93 *pend = NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +000094 if (PyString_Check(v)) {
95 s = PyString_AS_STRING(v);
96 len = PyString_GET_SIZE(v);
97 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +000098#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +000099 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000100 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
101 PyErr_SetString(PyExc_ValueError,
Tim Petersef14d732000-09-23 03:39:17 +0000102 "Unicode float() literal too long to convert");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000103 return NULL;
104 }
Tim Petersef14d732000-09-23 03:39:17 +0000105 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000106 PyUnicode_GET_SIZE(v),
Tim Petersd2364e82001-11-01 20:09:42 +0000107 s_buffer,
Guido van Rossum9e896b32000-04-05 20:11:21 +0000108 NULL))
109 return NULL;
110 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000111 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000112 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000113#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000114 else if (PyObject_AsCharBuffer(v, &s, &len)) {
115 PyErr_SetString(PyExc_TypeError,
Skip Montanaro71390a92002-05-02 13:03:22 +0000116 "float() argument must be a string or a number");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000117 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000118 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000119
Guido van Rossum4c08d552000-03-10 22:55:18 +0000120 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000121 while (*s && isspace(Py_CHARMASK(*s)))
122 s++;
Tim Petersef14d732000-09-23 03:39:17 +0000123 if (*s == '\0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000124 PyErr_SetString(PyExc_ValueError, "empty string for float()");
125 return NULL;
126 }
Tim Petersef14d732000-09-23 03:39:17 +0000127 /* We don't care about overflow or underflow. If the platform supports
128 * them, infinities and signed zeroes (on underflow) are fine.
129 * However, strtod can return 0 for denormalized numbers, where atof
130 * does not. So (alas!) we special-case a zero result. Note that
131 * whether strtod sets errno on underflow is not defined, so we can't
132 * key off errno.
133 */
Tim Peters858346e2000-09-25 21:01:28 +0000134 PyFPE_START_PROTECT("strtod", return NULL)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000135 x = PyOS_ascii_strtod(s, (char **)&end);
Tim Peters858346e2000-09-25 21:01:28 +0000136 PyFPE_END_PROTECT(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000137 errno = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
Tim Petersef14d732000-09-23 03:39:17 +0000139 byte at the end of the string, when the input is inf(inity). */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000140 if (end > last)
141 end = last;
Tim Petersef14d732000-09-23 03:39:17 +0000142 if (end == s) {
Barry Warsawaf8aef92001-11-28 20:52:21 +0000143 PyOS_snprintf(buffer, sizeof(buffer),
144 "invalid literal for float(): %.200s", s);
Tim Petersef14d732000-09-23 03:39:17 +0000145 PyErr_SetString(PyExc_ValueError, buffer);
146 return NULL;
147 }
148 /* Since end != s, the platform made *some* kind of sense out
149 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000150 while (*end && isspace(Py_CHARMASK(*end)))
151 end++;
152 if (*end != '\0') {
Barry Warsawaf8aef92001-11-28 20:52:21 +0000153 PyOS_snprintf(buffer, sizeof(buffer),
154 "invalid literal for float(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000155 PyErr_SetString(PyExc_ValueError, buffer);
156 return NULL;
157 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000158 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000159 PyErr_SetString(PyExc_ValueError,
160 "null byte in argument for float()");
161 return NULL;
162 }
Tim Petersef14d732000-09-23 03:39:17 +0000163 if (x == 0.0) {
164 /* See above -- may have been strtod being anal
165 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000166 PyFPE_START_PROTECT("atof", return NULL)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000167 x = PyOS_ascii_atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000168 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000169 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000170 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000171 return PyFloat_FromDouble(x);
172}
173
Guido van Rossum234f9421993-06-17 12:35:49 +0000174static void
Fred Drakefd99de62000-07-09 05:02:18 +0000175float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000176{
Guido van Rossum9475a232001-10-05 20:51:39 +0000177 if (PyFloat_CheckExact(op)) {
178 op->ob_type = (struct _typeobject *)free_list;
179 free_list = op;
180 }
181 else
182 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000183}
184
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185double
Fred Drakefd99de62000-07-09 05:02:18 +0000186PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 PyNumberMethods *nb;
189 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190 double val;
Tim Petersd2364e82001-11-01 20:09:42 +0000191
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 if (op && PyFloat_Check(op))
193 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Tim Petersd2364e82001-11-01 20:09:42 +0000194
Neil Schemenauer2c77e902002-11-18 16:06:21 +0000195 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 return -1;
198 }
Tim Petersd2364e82001-11-01 20:09:42 +0000199
Neil Schemenauer2c77e902002-11-18 16:06:21 +0000200 if ((nb = op->ob_type->tp_as_number) == NULL || nb->nb_float == NULL) {
201 PyErr_SetString(PyExc_TypeError, "a float is required");
202 return -1;
203 }
204
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206 if (fo == NULL)
207 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 if (!PyFloat_Check(fo)) {
209 PyErr_SetString(PyExc_TypeError,
210 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211 return -1;
212 }
Tim Petersd2364e82001-11-01 20:09:42 +0000213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 val = PyFloat_AS_DOUBLE(fo);
215 Py_DECREF(fo);
Tim Petersd2364e82001-11-01 20:09:42 +0000216
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
220/* Methods */
221
Tim Peters97019e42001-11-28 22:43:45 +0000222static void
223format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224{
225 register char *cp;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000226 char format[32];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227 /* Subroutine for float_repr and float_print.
228 We want float numbers to be recognizable as such,
229 i.e., they should contain a decimal point or an exponent.
230 However, %g may print the number as an integer;
231 in such cases, we append ".0" to the string. */
Tim Peters97019e42001-11-28 22:43:45 +0000232
233 assert(PyFloat_Check(v));
Martin v. Löwis737ea822004-06-08 18:52:54 +0000234 PyOS_snprintf(format, 32, "%%.%ig", precision);
235 PyOS_ascii_formatd(buf, buflen, format, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 cp = buf;
237 if (*cp == '-')
238 cp++;
239 for (; *cp != '\0'; cp++) {
240 /* Any non-digit means it's not an integer;
241 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000242 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 break;
244 }
245 if (*cp == '\0') {
246 *cp++ = '.';
247 *cp++ = '0';
248 *cp++ = '\0';
249 }
250}
251
Tim Peters97019e42001-11-28 22:43:45 +0000252/* XXX PyFloat_AsStringEx should not be a public API function (for one
253 XXX thing, its signature passes a buffer without a length; for another,
254 XXX it isn't useful outside this file).
255*/
256void
257PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
258{
259 format_float(buf, 100, v, precision);
260}
261
Neil Schemenauer32117e52001-01-04 01:44:34 +0000262/* Macro and helper that convert PyObject obj to a C double and store
263 the value in dbl; this replaces the functionality of the coercion
Tim Peters77d8a4f2001-12-11 20:31:34 +0000264 slot function. If conversion to double raises an exception, obj is
265 set to NULL, and the function invoking this macro returns NULL. If
266 obj is not of float, int or long type, Py_NotImplemented is incref'ed,
267 stored in obj, and returned from the function invoking this macro.
268*/
Neil Schemenauer32117e52001-01-04 01:44:34 +0000269#define CONVERT_TO_DOUBLE(obj, dbl) \
270 if (PyFloat_Check(obj)) \
271 dbl = PyFloat_AS_DOUBLE(obj); \
272 else if (convert_to_double(&(obj), &(dbl)) < 0) \
273 return obj;
274
275static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000276convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000277{
278 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000279
Neil Schemenauer32117e52001-01-04 01:44:34 +0000280 if (PyInt_Check(obj)) {
281 *dbl = (double)PyInt_AS_LONG(obj);
282 }
283 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000284 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000285 if (*dbl == -1.0 && PyErr_Occurred()) {
286 *v = NULL;
287 return -1;
288 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000289 }
290 else {
291 Py_INCREF(Py_NotImplemented);
292 *v = Py_NotImplemented;
293 return -1;
294 }
295 return 0;
296}
297
Guido van Rossum57072eb1999-12-23 19:00:28 +0000298/* Precisions used by repr() and str(), respectively.
299
300 The repr() precision (17 significant decimal digits) is the minimal number
301 that is guaranteed to have enough precision so that if the number is read
302 back in the exact same binary value is recreated. This is true for IEEE
303 floating point by design, and also happens to work for all other modern
304 hardware.
305
306 The str() precision is chosen so that in most cases, the rounding noise
307 created by various operations is suppressed, while giving plenty of
308 precision for practical use.
309
310*/
311
312#define PREC_REPR 17
313#define PREC_STR 12
314
Tim Peters97019e42001-11-28 22:43:45 +0000315/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
316 XXX they pass a char buffer without passing a length.
317*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000318void
Fred Drakefd99de62000-07-09 05:02:18 +0000319PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000320{
Tim Peters97019e42001-11-28 22:43:45 +0000321 format_float(buf, 100, v, PREC_STR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000322}
323
Tim Peters72f98e92001-05-08 15:19:57 +0000324void
325PyFloat_AsReprString(char *buf, PyFloatObject *v)
326{
Tim Peters97019e42001-11-28 22:43:45 +0000327 format_float(buf, 100, v, PREC_REPR);
Tim Peters72f98e92001-05-08 15:19:57 +0000328}
329
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000330/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000331static int
Fred Drakefd99de62000-07-09 05:02:18 +0000332float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333{
334 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000335 format_float(buf, sizeof(buf), v,
336 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000338 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339}
340
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000342float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
344 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000345 format_float(buf, sizeof(buf), v, PREC_REPR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000346 return PyString_FromString(buf);
347}
348
349static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000350float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000351{
352 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000353 format_float(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355}
356
Tim Peters307fa782004-09-23 08:06:40 +0000357/* Comparison is pretty much a nightmare. When comparing float to float,
358 * we do it as straightforwardly (and long-windedly) as conceivable, so
359 * that, e.g., Python x == y delivers the same result as the platform
360 * C x == y when x and/or y is a NaN.
361 * When mixing float with an integer type, there's no good *uniform* approach.
362 * Converting the double to an integer obviously doesn't work, since we
363 * may lose info from fractional bits. Converting the integer to a double
364 * also has two failure modes: (1) a long int may trigger overflow (too
365 * large to fit in the dynamic range of a C double); (2) even a C long may have
366 * more bits than fit in a C double (e.g., on a a 64-bit box long may have
367 * 63 bits of precision, but a C double probably has only 53), and then
368 * we can falsely claim equality when low-order integer bits are lost by
369 * coercion to double. So this part is painful too.
370 */
371
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000372static PyObject*
373float_richcompare(PyObject *v, PyObject *w, int op)
374{
375 double i, j;
376 int r = 0;
377
Tim Peters307fa782004-09-23 08:06:40 +0000378 assert(PyFloat_Check(v));
379 i = PyFloat_AS_DOUBLE(v);
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000380
Tim Peters307fa782004-09-23 08:06:40 +0000381 /* Switch on the type of w. Set i and j to doubles to be compared,
382 * and op to the richcomp to use.
383 */
384 if (PyFloat_Check(w))
385 j = PyFloat_AS_DOUBLE(w);
386
Tim Peterse1c69b32004-09-23 19:22:41 +0000387 else if (Py_IS_INFINITY(i) || Py_IS_NAN(i)) {
Tim Peters307fa782004-09-23 08:06:40 +0000388 if (PyInt_Check(w) || PyLong_Check(w))
Tim Peterse1c69b32004-09-23 19:22:41 +0000389 /* If i is an infinity, its magnitude exceeds any
390 * finite integer, so it doesn't matter which int we
391 * compare i with. If i is a NaN, similarly.
Tim Peters307fa782004-09-23 08:06:40 +0000392 */
393 j = 0.0;
394 else
395 goto Unimplemented;
396 }
397
398 else if (PyInt_Check(w)) {
399 long jj = PyInt_AS_LONG(w);
400 /* In the worst realistic case I can imagine, C double is a
401 * Cray single with 48 bits of precision, and long has 64
402 * bits.
403 */
Tim Peterse1c69b32004-09-23 19:22:41 +0000404#if SIZEOF_LONG > 6
Tim Peters307fa782004-09-23 08:06:40 +0000405 unsigned long abs = (unsigned long)(jj < 0 ? -jj : jj);
406 if (abs >> 48) {
407 /* Needs more than 48 bits. Make it take the
408 * PyLong path.
409 */
410 PyObject *result;
411 PyObject *ww = PyLong_FromLong(jj);
412
413 if (ww == NULL)
414 return NULL;
415 result = float_richcompare(v, ww, op);
416 Py_DECREF(ww);
417 return result;
418 }
419#endif
420 j = (double)jj;
421 assert((long)j == jj);
422 }
423
424 else if (PyLong_Check(w)) {
425 int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
426 int wsign = _PyLong_Sign(w);
427 size_t nbits;
428 double mant;
429 int exponent;
430
431 if (vsign != wsign) {
432 /* Magnitudes are irrelevant -- the signs alone
433 * determine the outcome.
434 */
435 i = (double)vsign;
436 j = (double)wsign;
437 goto Compare;
438 }
439 /* The signs are the same. */
440 /* Convert w to a double if it fits. In particular, 0 fits. */
441 nbits = _PyLong_NumBits(w);
442 if (nbits == (size_t)-1 && PyErr_Occurred()) {
443 /* This long is so large that size_t isn't big enough
Tim Peterse1c69b32004-09-23 19:22:41 +0000444 * to hold the # of bits. Replace with little doubles
445 * that give the same outcome -- w is so large that
446 * its magnitude must exceed the magnitude of any
447 * finite float.
Tim Peters307fa782004-09-23 08:06:40 +0000448 */
449 PyErr_Clear();
450 i = (double)vsign;
451 assert(wsign != 0);
452 j = wsign * 2.0;
453 goto Compare;
454 }
455 if (nbits <= 48) {
456 j = PyLong_AsDouble(w);
457 /* It's impossible that <= 48 bits overflowed. */
458 assert(j != -1.0 || ! PyErr_Occurred());
459 goto Compare;
460 }
461 assert(wsign != 0); /* else nbits was 0 */
462 assert(vsign != 0); /* if vsign were 0, then since wsign is
463 * not 0, we would have taken the
464 * vsign != wsign branch at the start */
465 /* We want to work with non-negative numbers. */
466 if (vsign < 0) {
467 /* "Multiply both sides" by -1; this also swaps the
468 * comparator.
469 */
470 i = -i;
471 op = _Py_SwappedOp[op];
472 }
473 assert(i > 0.0);
474 mant = frexp(i, &exponent);
475 /* exponent is the # of bits in v before the radix point;
476 * we know that nbits (the # of bits in w) > 48 at this point
477 */
478 if (exponent < 0 || (size_t)exponent < nbits) {
479 i = 1.0;
480 j = 2.0;
481 goto Compare;
482 }
483 if ((size_t)exponent > nbits) {
484 i = 2.0;
485 j = 1.0;
486 goto Compare;
487 }
488 /* v and w have the same number of bits before the radix
489 * point. Construct two longs that have the same comparison
490 * outcome.
491 */
492 {
493 double fracpart;
494 double intpart;
495 PyObject *result = NULL;
496 PyObject *one = NULL;
497 PyObject *vv = NULL;
498 PyObject *ww = w;
499
500 if (wsign < 0) {
501 ww = PyNumber_Negative(w);
502 if (ww == NULL)
503 goto Error;
504 }
505 else
506 Py_INCREF(ww);
507
508 fracpart = modf(i, &intpart);
509 vv = PyLong_FromDouble(intpart);
510 if (vv == NULL)
511 goto Error;
512
513 if (fracpart != 0.0) {
514 /* Shift left, and or a 1 bit into vv
515 * to represent the lost fraction.
516 */
517 PyObject *temp;
518
519 one = PyInt_FromLong(1);
520 if (one == NULL)
521 goto Error;
522
523 temp = PyNumber_Lshift(ww, one);
524 if (temp == NULL)
525 goto Error;
526 Py_DECREF(ww);
527 ww = temp;
528
529 temp = PyNumber_Lshift(vv, one);
530 if (temp == NULL)
531 goto Error;
532 Py_DECREF(vv);
533 vv = temp;
534
535 temp = PyNumber_Or(vv, one);
536 if (temp == NULL)
537 goto Error;
538 Py_DECREF(vv);
539 vv = temp;
540 }
541
542 r = PyObject_RichCompareBool(vv, ww, op);
543 if (r < 0)
544 goto Error;
545 result = PyBool_FromLong(r);
546 Error:
547 Py_XDECREF(vv);
548 Py_XDECREF(ww);
549 Py_XDECREF(one);
550 return result;
551 }
552 } /* else if (PyLong_Check(w)) */
553
554 else /* w isn't float, int, or long */
555 goto Unimplemented;
556
557 Compare:
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000558 PyFPE_START_PROTECT("richcompare", return NULL)
559 switch (op) {
560 case Py_EQ:
Tim Peters307fa782004-09-23 08:06:40 +0000561 r = i == j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000562 break;
563 case Py_NE:
Tim Peters307fa782004-09-23 08:06:40 +0000564 r = i != j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000565 break;
566 case Py_LE:
Tim Peters307fa782004-09-23 08:06:40 +0000567 r = i <= j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000568 break;
569 case Py_GE:
Tim Peters307fa782004-09-23 08:06:40 +0000570 r = i >= j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000571 break;
572 case Py_LT:
Tim Peters307fa782004-09-23 08:06:40 +0000573 r = i < j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000574 break;
575 case Py_GT:
Tim Peters307fa782004-09-23 08:06:40 +0000576 r = i > j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000577 break;
578 }
Michael W. Hudson957f9772004-02-26 12:33:09 +0000579 PyFPE_END_PROTECT(r)
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000580 return PyBool_FromLong(r);
Tim Peters307fa782004-09-23 08:06:40 +0000581
582 Unimplemented:
583 Py_INCREF(Py_NotImplemented);
584 return Py_NotImplemented;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000585}
586
Guido van Rossum9bfef441993-03-29 10:43:31 +0000587static long
Fred Drakefd99de62000-07-09 05:02:18 +0000588float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000589{
Tim Peters39dce292000-08-15 03:34:48 +0000590 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000591}
592
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000594float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000596 double a,b;
597 CONVERT_TO_DOUBLE(v, a);
598 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000599 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000600 a = a + b;
601 PyFPE_END_PROTECT(a)
602 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000603}
604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000606float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000608 double a,b;
609 CONVERT_TO_DOUBLE(v, a);
610 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000611 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000612 a = a - b;
613 PyFPE_END_PROTECT(a)
614 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615}
616
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000618float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000620 double a,b;
621 CONVERT_TO_DOUBLE(v, a);
622 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000623 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000624 a = a * b;
625 PyFPE_END_PROTECT(a)
626 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627}
628
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000630float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000632 double a,b;
633 CONVERT_TO_DOUBLE(v, a);
634 CONVERT_TO_DOUBLE(w, b);
635 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000637 return NULL;
638 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000639 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000640 a = a / b;
641 PyFPE_END_PROTECT(a)
642 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643}
644
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000646float_classic_div(PyObject *v, PyObject *w)
647{
648 double a,b;
649 CONVERT_TO_DOUBLE(v, a);
650 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000651 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000652 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
653 return NULL;
654 if (b == 0.0) {
655 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
656 return NULL;
657 }
658 PyFPE_START_PROTECT("divide", return 0)
659 a = a / b;
660 PyFPE_END_PROTECT(a)
661 return PyFloat_FromDouble(a);
662}
663
664static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000665float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000667 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000668 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000669 CONVERT_TO_DOUBLE(v, vx);
670 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673 return NULL;
674 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000675 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000676 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000677 /* note: checking mod*wx < 0 is incorrect -- underflows to
678 0 if wx < sqrt(smallest nonzero double) */
679 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000680 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000681 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000682 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000684}
685
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000687float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000688{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000689 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000690 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000691 CONVERT_TO_DOUBLE(v, vx);
692 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000693 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000695 return NULL;
696 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000697 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000698 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000699 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000700 exact multiple of wx. But this is fp arithmetic, and fp
701 vx - mod is an approximation; the result is that div may
702 not be an exact integral value after the division, although
703 it will always be very close to one.
704 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000705 div = (vx - mod) / wx;
Tim Petersd2e40d62001-11-01 23:12:27 +0000706 if (mod) {
707 /* ensure the remainder has the same sign as the denominator */
708 if ((wx < 0) != (mod < 0)) {
709 mod += wx;
710 div -= 1.0;
711 }
712 }
713 else {
714 /* the remainder is zero, and in the presence of signed zeroes
715 fmod returns different results across platforms; ensure
716 it has the same sign as the denominator; we'd like to do
717 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000718 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000719 if (wx < 0.0)
720 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000721 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000722 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000723 if (div) {
724 floordiv = floor(div);
725 if (div - floordiv > 0.5)
726 floordiv += 1.0;
727 }
728 else {
729 /* div is zero - get the same sign as the true quotient */
730 div *= div; /* hide "div = +0" from optimizers */
731 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
732 }
733 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000734 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000735}
736
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737static PyObject *
Tim Peters63a35712001-12-11 19:57:24 +0000738float_floor_div(PyObject *v, PyObject *w)
739{
740 PyObject *t, *r;
741
742 t = float_divmod(v, w);
Tim Peters77d8a4f2001-12-11 20:31:34 +0000743 if (t == NULL || t == Py_NotImplemented)
744 return t;
745 assert(PyTuple_CheckExact(t));
746 r = PyTuple_GET_ITEM(t, 0);
747 Py_INCREF(r);
748 Py_DECREF(t);
749 return r;
Tim Peters63a35712001-12-11 19:57:24 +0000750}
751
752static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000753float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000754{
755 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000756
757 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000758 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000759 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000760 return NULL;
761 }
762
Neil Schemenauer32117e52001-01-04 01:44:34 +0000763 CONVERT_TO_DOUBLE(v, iv);
764 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000765
766 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000767 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000768 PyFPE_START_PROTECT("pow", return NULL)
769 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000770 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000771 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000772 ix = fmod(1.0, iz);
773 if (ix != 0 && iz < 0)
774 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000775 }
Tim Petersc54d1902000-10-06 00:36:09 +0000776 else
777 ix = 1.0;
778 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000779 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000780 }
Tim Peters96685bf2001-08-23 22:31:37 +0000781 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000782 if (iw < 0.0) {
783 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000784 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000785 return NULL;
786 }
787 return PyFloat_FromDouble(0.0);
788 }
Tim Peterse87568d2003-05-24 20:18:24 +0000789 if (iv < 0.0) {
790 /* Whether this is an error is a mess, and bumps into libm
791 * bugs so we have to figure it out ourselves.
792 */
793 if (iw != floor(iw)) {
794 PyErr_SetString(PyExc_ValueError, "negative number "
795 "cannot be raised to a fractional power");
796 return NULL;
797 }
798 /* iw is an exact integer, albeit perhaps a very large one.
799 * -1 raised to an exact integer should never be exceptional.
800 * Alas, some libms (chiefly glibc as of early 2003) return
801 * NaN and set EDOM on pow(-1, large_int) if the int doesn't
802 * happen to be representable in a *C* integer. That's a
803 * bug; we let that slide in math.pow() (which currently
804 * reflects all platform accidents), but not for Python's **.
805 */
806 if (iv == -1.0 && !Py_IS_INFINITY(iw) && iw == iw) {
807 /* XXX the "iw == iw" was to weed out NaNs. This
808 * XXX doesn't actually work on all platforms.
809 */
810 /* Return 1 if iw is even, -1 if iw is odd; there's
811 * no guarantee that any C integral type is big
812 * enough to hold iw, so we have to check this
813 * indirectly.
814 */
815 ix = floor(iw * 0.5) * 2.0;
816 return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);
817 }
818 /* Else iv != -1.0, and overflow or underflow are possible.
819 * Unless we're to write pow() ourselves, we have to trust
820 * the platform to do this correctly.
821 */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000822 }
Tim Peters96685bf2001-08-23 22:31:37 +0000823 errno = 0;
824 PyFPE_START_PROTECT("pow", return NULL)
825 ix = pow(iv, iw);
826 PyFPE_END_PROTECT(ix)
Tim Petersdc5a5082002-03-09 04:58:24 +0000827 Py_ADJUST_ERANGE1(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000828 if (errno != 0) {
Tim Peterse87568d2003-05-24 20:18:24 +0000829 /* We don't expect any errno value other than ERANGE, but
830 * the range of libm bugs appears unbounded.
831 */
832 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
833 PyExc_ValueError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000835 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000836 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837}
838
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000840float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000841{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000842 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843}
844
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000846float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847{
Tim Peters0280cf72001-09-11 21:53:35 +0000848 if (PyFloat_CheckExact(v)) {
849 Py_INCREF(v);
850 return (PyObject *)v;
851 }
852 else
853 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000854}
855
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000857float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000858{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000859 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000862static int
Fred Drakefd99de62000-07-09 05:02:18 +0000863float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000864{
865 return v->ob_fval != 0.0;
866}
867
Guido van Rossum234f9421993-06-17 12:35:49 +0000868static int
Fred Drakefd99de62000-07-09 05:02:18 +0000869float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000870{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871 if (PyInt_Check(*pw)) {
872 long x = PyInt_AsLong(*pw);
873 *pw = PyFloat_FromDouble((double)x);
874 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000875 return 0;
876 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877 else if (PyLong_Check(*pw)) {
Neal Norwitzabcb0c02003-01-28 19:21:24 +0000878 double x = PyLong_AsDouble(*pw);
879 if (x == -1.0 && PyErr_Occurred())
880 return -1;
881 *pw = PyFloat_FromDouble(x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000883 return 0;
884 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000885 else if (PyFloat_Check(*pw)) {
886 Py_INCREF(*pv);
887 Py_INCREF(*pw);
888 return 0;
889 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000890 return 1; /* Can't do it */
891}
892
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +0000894float_long(PyObject *v)
895{
896 double x = PyFloat_AsDouble(v);
897 return PyLong_FromDouble(x);
898}
899
900static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000901float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000902{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000903 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000904 double wholepart; /* integral portion of x, rounded toward 0 */
Tim Peters7321ec42001-07-26 20:02:17 +0000905
906 (void)modf(x, &wholepart);
Tim Peters7d791242002-11-21 22:26:37 +0000907 /* Try to get out cheap if this fits in a Python int. The attempt
908 * to cast to long must be protected, as C doesn't define what
909 * happens if the double is too big to fit in a long. Some rare
910 * systems raise an exception then (RISCOS was mentioned as one,
911 * and someone using a non-default option on Sun also bumped into
912 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
913 * still be vulnerable: if a long has more bits of precision than
914 * a double, casting MIN/MAX to double may yield an approximation,
915 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
916 * yield true from the C expression wholepart<=LONG_MAX, despite
917 * that wholepart is actually greater than LONG_MAX.
918 */
919 if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
920 const long aslong = (long)wholepart;
Tim Peters7321ec42001-07-26 20:02:17 +0000921 return PyInt_FromLong(aslong);
Tim Peters7d791242002-11-21 22:26:37 +0000922 }
923 return PyLong_FromDouble(wholepart);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000924}
925
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000926static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000927float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000928{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000930 return v;
931}
932
933
Jeremy Hylton938ace62002-07-17 16:30:39 +0000934static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000935float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
936
Tim Peters6d6c1a32001-08-02 04:15:00 +0000937static PyObject *
938float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
939{
940 PyObject *x = Py_False; /* Integer zero */
941 static char *kwlist[] = {"x", 0};
942
Guido van Rossumbef14172001-08-29 15:47:46 +0000943 if (type != &PyFloat_Type)
944 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
946 return NULL;
947 if (PyString_Check(x))
948 return PyFloat_FromString(x, NULL);
949 return PyNumber_Float(x);
950}
951
Guido van Rossumbef14172001-08-29 15:47:46 +0000952/* Wimpy, slow approach to tp_new calls for subtypes of float:
953 first create a regular float from whatever arguments we got,
954 then allocate a subtype instance and initialize its ob_fval
955 from the regular float. The regular float is then thrown away.
956*/
957static PyObject *
958float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
959{
960 PyObject *tmp, *new;
961
962 assert(PyType_IsSubtype(type, &PyFloat_Type));
963 tmp = float_new(&PyFloat_Type, args, kwds);
964 if (tmp == NULL)
965 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000966 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000967 new = type->tp_alloc(type, 0);
Raymond Hettingerf4667932003-06-28 20:04:25 +0000968 if (new == NULL) {
969 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000970 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000971 }
Guido van Rossumbef14172001-08-29 15:47:46 +0000972 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
973 Py_DECREF(tmp);
974 return new;
975}
976
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000977static PyObject *
978float_getnewargs(PyFloatObject *v)
979{
980 return Py_BuildValue("(d)", v->ob_fval);
981}
982
983static PyMethodDef float_methods[] = {
984 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
985 {NULL, NULL} /* sentinel */
986};
987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000988PyDoc_STRVAR(float_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989"float(x) -> floating point number\n\
990\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000991Convert a string or number to a floating point number, if possible.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992
993
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000995 (binaryfunc)float_add, /*nb_add*/
996 (binaryfunc)float_sub, /*nb_subtract*/
997 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000998 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000999 (binaryfunc)float_rem, /*nb_remainder*/
1000 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +00001001 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001002 (unaryfunc)float_neg, /*nb_negative*/
1003 (unaryfunc)float_pos, /*nb_positive*/
1004 (unaryfunc)float_abs, /*nb_absolute*/
1005 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +00001006 0, /*nb_invert*/
1007 0, /*nb_lshift*/
1008 0, /*nb_rshift*/
1009 0, /*nb_and*/
1010 0, /*nb_xor*/
1011 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001012 (coercion)float_coerce, /*nb_coerce*/
1013 (unaryfunc)float_int, /*nb_int*/
1014 (unaryfunc)float_long, /*nb_long*/
1015 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001016 0, /* nb_oct */
1017 0, /* nb_hex */
1018 0, /* nb_inplace_add */
1019 0, /* nb_inplace_subtract */
1020 0, /* nb_inplace_multiply */
1021 0, /* nb_inplace_divide */
1022 0, /* nb_inplace_remainder */
1023 0, /* nb_inplace_power */
1024 0, /* nb_inplace_lshift */
1025 0, /* nb_inplace_rshift */
1026 0, /* nb_inplace_and */
1027 0, /* nb_inplace_xor */
1028 0, /* nb_inplace_or */
Tim Peters63a35712001-12-11 19:57:24 +00001029 float_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001030 float_div, /* nb_true_divide */
1031 0, /* nb_inplace_floor_divide */
1032 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001033};
1034
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035PyTypeObject PyFloat_Type = {
1036 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001037 0,
1038 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001039 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001040 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001041 (destructor)float_dealloc, /* tp_dealloc */
1042 (printfunc)float_print, /* tp_print */
1043 0, /* tp_getattr */
1044 0, /* tp_setattr */
Michael W. Hudson08678a12004-05-26 17:36:12 +00001045 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001046 (reprfunc)float_repr, /* tp_repr */
1047 &float_as_number, /* tp_as_number */
1048 0, /* tp_as_sequence */
1049 0, /* tp_as_mapping */
1050 (hashfunc)float_hash, /* tp_hash */
1051 0, /* tp_call */
1052 (reprfunc)float_str, /* tp_str */
1053 PyObject_GenericGetAttr, /* tp_getattro */
1054 0, /* tp_setattro */
1055 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001056 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1057 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001058 float_doc, /* tp_doc */
1059 0, /* tp_traverse */
1060 0, /* tp_clear */
Michael W. Hudsond3b33b52004-02-19 19:35:22 +00001061 (richcmpfunc)float_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001062 0, /* tp_weaklistoffset */
1063 0, /* tp_iter */
1064 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001065 float_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001066 0, /* tp_members */
1067 0, /* tp_getset */
1068 0, /* tp_base */
1069 0, /* tp_dict */
1070 0, /* tp_descr_get */
1071 0, /* tp_descr_set */
1072 0, /* tp_dictoffset */
1073 0, /* tp_init */
1074 0, /* tp_alloc */
1075 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001076};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001077
1078void
Fred Drakefd99de62000-07-09 05:02:18 +00001079PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001080{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001081 PyFloatObject *p;
1082 PyFloatBlock *list, *next;
Neal Norwitz739a8f82004-07-08 01:55:58 +00001083 unsigned i;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001084 int bc, bf; /* block count, number of freed blocks */
1085 int frem, fsum; /* remaining unfreed floats per block, total */
1086
1087 bc = 0;
1088 bf = 0;
1089 fsum = 0;
1090 list = block_list;
1091 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001092 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001093 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001094 bc++;
1095 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001096 for (i = 0, p = &list->objects[0];
1097 i < N_FLOATOBJECTS;
1098 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001099 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001100 frem++;
1101 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001102 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001103 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001104 list->next = block_list;
1105 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001106 for (i = 0, p = &list->objects[0];
1107 i < N_FLOATOBJECTS;
1108 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001109 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001110 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001111 p->ob_type = (struct _typeobject *)
1112 free_list;
1113 free_list = p;
1114 }
1115 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001116 }
1117 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001118 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001119 bf++;
1120 }
1121 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001122 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001123 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001124 if (!Py_VerboseFlag)
1125 return;
1126 fprintf(stderr, "# cleanup floats");
1127 if (!fsum) {
1128 fprintf(stderr, "\n");
1129 }
1130 else {
1131 fprintf(stderr,
1132 ": %d unfreed float%s in %d out of %d block%s\n",
1133 fsum, fsum == 1 ? "" : "s",
1134 bc - bf, bc, bc == 1 ? "" : "s");
1135 }
1136 if (Py_VerboseFlag > 1) {
1137 list = block_list;
1138 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001139 for (i = 0, p = &list->objects[0];
1140 i < N_FLOATOBJECTS;
1141 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001142 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +00001143 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001144 char buf[100];
1145 PyFloat_AsString(buf, p);
1146 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001147 "# <float at %p, refcnt=%d, val=%s>\n",
1148 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001149 }
1150 }
1151 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001152 }
1153 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001154}
Tim Peters9905b942003-03-20 20:53:32 +00001155
1156/*----------------------------------------------------------------------------
1157 * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
1158 *
1159 * TODO: On platforms that use the standard IEEE-754 single and double
1160 * formats natively, these routines could simply copy the bytes.
1161 */
1162int
1163_PyFloat_Pack4(double x, unsigned char *p, int le)
1164{
1165 unsigned char sign;
1166 int e;
1167 double f;
1168 unsigned int fbits;
1169 int incr = 1;
1170
1171 if (le) {
1172 p += 3;
1173 incr = -1;
1174 }
1175
1176 if (x < 0) {
1177 sign = 1;
1178 x = -x;
1179 }
1180 else
1181 sign = 0;
1182
1183 f = frexp(x, &e);
1184
1185 /* Normalize f to be in the range [1.0, 2.0) */
1186 if (0.5 <= f && f < 1.0) {
1187 f *= 2.0;
1188 e--;
1189 }
1190 else if (f == 0.0)
1191 e = 0;
1192 else {
1193 PyErr_SetString(PyExc_SystemError,
1194 "frexp() result out of range");
1195 return -1;
1196 }
1197
1198 if (e >= 128)
1199 goto Overflow;
1200 else if (e < -126) {
1201 /* Gradual underflow */
1202 f = ldexp(f, 126 + e);
1203 e = 0;
1204 }
1205 else if (!(e == 0 && f == 0.0)) {
1206 e += 127;
1207 f -= 1.0; /* Get rid of leading 1 */
1208 }
1209
1210 f *= 8388608.0; /* 2**23 */
Tim Petersf1ed9342003-03-21 17:10:03 +00001211 fbits = (unsigned int)(f + 0.5); /* Round */
Tim Peters9905b942003-03-20 20:53:32 +00001212 assert(fbits <= 8388608);
1213 if (fbits >> 23) {
1214 /* The carry propagated out of a string of 23 1 bits. */
1215 fbits = 0;
1216 ++e;
1217 if (e >= 255)
1218 goto Overflow;
1219 }
1220
1221 /* First byte */
1222 *p = (sign << 7) | (e >> 1);
1223 p += incr;
1224
1225 /* Second byte */
1226 *p = (char) (((e & 1) << 7) | (fbits >> 16));
1227 p += incr;
1228
1229 /* Third byte */
1230 *p = (fbits >> 8) & 0xFF;
1231 p += incr;
1232
1233 /* Fourth byte */
1234 *p = fbits & 0xFF;
1235
1236 /* Done */
1237 return 0;
1238
1239 Overflow:
1240 PyErr_SetString(PyExc_OverflowError,
1241 "float too large to pack with f format");
1242 return -1;
1243}
1244
1245int
1246_PyFloat_Pack8(double x, unsigned char *p, int le)
1247{
1248 unsigned char sign;
1249 int e;
1250 double f;
1251 unsigned int fhi, flo;
1252 int incr = 1;
1253
1254 if (le) {
1255 p += 7;
1256 incr = -1;
1257 }
1258
1259 if (x < 0) {
1260 sign = 1;
1261 x = -x;
1262 }
1263 else
1264 sign = 0;
1265
1266 f = frexp(x, &e);
1267
1268 /* Normalize f to be in the range [1.0, 2.0) */
1269 if (0.5 <= f && f < 1.0) {
1270 f *= 2.0;
1271 e--;
1272 }
1273 else if (f == 0.0)
1274 e = 0;
1275 else {
1276 PyErr_SetString(PyExc_SystemError,
1277 "frexp() result out of range");
1278 return -1;
1279 }
1280
1281 if (e >= 1024)
1282 goto Overflow;
1283 else if (e < -1022) {
1284 /* Gradual underflow */
1285 f = ldexp(f, 1022 + e);
1286 e = 0;
1287 }
1288 else if (!(e == 0 && f == 0.0)) {
1289 e += 1023;
1290 f -= 1.0; /* Get rid of leading 1 */
1291 }
1292
1293 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1294 f *= 268435456.0; /* 2**28 */
1295 fhi = (unsigned int)f; /* Truncate */
1296 assert(fhi < 268435456);
1297
1298 f -= (double)fhi;
1299 f *= 16777216.0; /* 2**24 */
1300 flo = (unsigned int)(f + 0.5); /* Round */
1301 assert(flo <= 16777216);
1302 if (flo >> 24) {
1303 /* The carry propagated out of a string of 24 1 bits. */
1304 flo = 0;
1305 ++fhi;
1306 if (fhi >> 28) {
1307 /* And it also progagated out of the next 28 bits. */
1308 fhi = 0;
1309 ++e;
1310 if (e >= 2047)
1311 goto Overflow;
1312 }
1313 }
1314
1315 /* First byte */
1316 *p = (sign << 7) | (e >> 4);
1317 p += incr;
1318
1319 /* Second byte */
1320 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
1321 p += incr;
1322
1323 /* Third byte */
1324 *p = (fhi >> 16) & 0xFF;
1325 p += incr;
1326
1327 /* Fourth byte */
1328 *p = (fhi >> 8) & 0xFF;
1329 p += incr;
1330
1331 /* Fifth byte */
1332 *p = fhi & 0xFF;
1333 p += incr;
1334
1335 /* Sixth byte */
1336 *p = (flo >> 16) & 0xFF;
1337 p += incr;
1338
1339 /* Seventh byte */
1340 *p = (flo >> 8) & 0xFF;
1341 p += incr;
1342
1343 /* Eighth byte */
1344 *p = flo & 0xFF;
1345 p += incr;
1346
1347 /* Done */
1348 return 0;
1349
1350 Overflow:
1351 PyErr_SetString(PyExc_OverflowError,
1352 "float too large to pack with d format");
1353 return -1;
1354}
1355
1356double
1357_PyFloat_Unpack4(const unsigned char *p, int le)
1358{
1359 unsigned char sign;
1360 int e;
1361 unsigned int f;
1362 double x;
1363 int incr = 1;
1364
1365 if (le) {
1366 p += 3;
1367 incr = -1;
1368 }
1369
1370 /* First byte */
1371 sign = (*p >> 7) & 1;
1372 e = (*p & 0x7F) << 1;
1373 p += incr;
1374
1375 /* Second byte */
1376 e |= (*p >> 7) & 1;
1377 f = (*p & 0x7F) << 16;
1378 p += incr;
1379
1380 /* Third byte */
1381 f |= *p << 8;
1382 p += incr;
1383
1384 /* Fourth byte */
1385 f |= *p;
1386
1387 x = (double)f / 8388608.0;
1388
1389 /* XXX This sadly ignores Inf/NaN issues */
1390 if (e == 0)
1391 e = -126;
1392 else {
1393 x += 1.0;
1394 e -= 127;
1395 }
1396 x = ldexp(x, e);
1397
1398 if (sign)
1399 x = -x;
1400
1401 return x;
1402}
1403
1404double
1405_PyFloat_Unpack8(const unsigned char *p, int le)
1406{
1407 unsigned char sign;
1408 int e;
1409 unsigned int fhi, flo;
1410 double x;
1411 int incr = 1;
1412
1413 if (le) {
1414 p += 7;
1415 incr = -1;
1416 }
1417
1418 /* First byte */
1419 sign = (*p >> 7) & 1;
1420 e = (*p & 0x7F) << 4;
1421 p += incr;
1422
1423 /* Second byte */
1424 e |= (*p >> 4) & 0xF;
1425 fhi = (*p & 0xF) << 24;
1426 p += incr;
1427
1428 /* Third byte */
1429 fhi |= *p << 16;
1430 p += incr;
1431
1432 /* Fourth byte */
1433 fhi |= *p << 8;
1434 p += incr;
1435
1436 /* Fifth byte */
1437 fhi |= *p;
1438 p += incr;
1439
1440 /* Sixth byte */
1441 flo = *p << 16;
1442 p += incr;
1443
1444 /* Seventh byte */
1445 flo |= *p << 8;
1446 p += incr;
1447
1448 /* Eighth byte */
1449 flo |= *p;
1450
1451 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
1452 x /= 268435456.0; /* 2**28 */
1453
1454 /* XXX This sadly ignores Inf/NaN */
1455 if (e == 0)
1456 e = -1022;
1457 else {
1458 x += 1.0;
1459 e -= 1023;
1460 }
1461 x = ldexp(x, e);
1462
1463 if (sign)
1464 x = -x;
1465
1466 return x;
1467}