blob: 46c05b2ba7c545b926dddffb9b0fb40e2b80aa10 [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
387 else if (Py_IS_INFINITY(i)) {
388 /* XXX If we had a reliable way to check whether i is a
389 * XXX NaN, it would belong in this branch too.
390 */
391 if (PyInt_Check(w) || PyLong_Check(w))
392 /* The magnitude of i exceeds any finite integer,
393 * so it doesn't matter which int we compare i with.
394 */
395 j = 0.0;
396 else
397 goto Unimplemented;
398 }
399
400 else if (PyInt_Check(w)) {
401 long jj = PyInt_AS_LONG(w);
402 /* In the worst realistic case I can imagine, C double is a
403 * Cray single with 48 bits of precision, and long has 64
404 * bits.
405 */
406#if SIZEOF_LONG > 4
407 unsigned long abs = (unsigned long)(jj < 0 ? -jj : jj);
408 if (abs >> 48) {
409 /* Needs more than 48 bits. Make it take the
410 * PyLong path.
411 */
412 PyObject *result;
413 PyObject *ww = PyLong_FromLong(jj);
414
415 if (ww == NULL)
416 return NULL;
417 result = float_richcompare(v, ww, op);
418 Py_DECREF(ww);
419 return result;
420 }
421#endif
422 j = (double)jj;
423 assert((long)j == jj);
424 }
425
426 else if (PyLong_Check(w)) {
427 int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
428 int wsign = _PyLong_Sign(w);
429 size_t nbits;
430 double mant;
431 int exponent;
432
433 if (vsign != wsign) {
434 /* Magnitudes are irrelevant -- the signs alone
435 * determine the outcome.
436 */
437 i = (double)vsign;
438 j = (double)wsign;
439 goto Compare;
440 }
441 /* The signs are the same. */
442 /* Convert w to a double if it fits. In particular, 0 fits. */
443 nbits = _PyLong_NumBits(w);
444 if (nbits == (size_t)-1 && PyErr_Occurred()) {
445 /* This long is so large that size_t isn't big enough
446 * to hold the # of Python digits. Replace with
447 * little doubles that give the same outcome --
448 * w is so large that its magnitude must exceed
449 * the magnitude of any finite float.
450 */
451 PyErr_Clear();
452 i = (double)vsign;
453 assert(wsign != 0);
454 j = wsign * 2.0;
455 goto Compare;
456 }
457 if (nbits <= 48) {
458 j = PyLong_AsDouble(w);
459 /* It's impossible that <= 48 bits overflowed. */
460 assert(j != -1.0 || ! PyErr_Occurred());
461 goto Compare;
462 }
463 assert(wsign != 0); /* else nbits was 0 */
464 assert(vsign != 0); /* if vsign were 0, then since wsign is
465 * not 0, we would have taken the
466 * vsign != wsign branch at the start */
467 /* We want to work with non-negative numbers. */
468 if (vsign < 0) {
469 /* "Multiply both sides" by -1; this also swaps the
470 * comparator.
471 */
472 i = -i;
473 op = _Py_SwappedOp[op];
474 }
475 assert(i > 0.0);
476 mant = frexp(i, &exponent);
477 /* exponent is the # of bits in v before the radix point;
478 * we know that nbits (the # of bits in w) > 48 at this point
479 */
480 if (exponent < 0 || (size_t)exponent < nbits) {
481 i = 1.0;
482 j = 2.0;
483 goto Compare;
484 }
485 if ((size_t)exponent > nbits) {
486 i = 2.0;
487 j = 1.0;
488 goto Compare;
489 }
490 /* v and w have the same number of bits before the radix
491 * point. Construct two longs that have the same comparison
492 * outcome.
493 */
494 {
495 double fracpart;
496 double intpart;
497 PyObject *result = NULL;
498 PyObject *one = NULL;
499 PyObject *vv = NULL;
500 PyObject *ww = w;
501
502 if (wsign < 0) {
503 ww = PyNumber_Negative(w);
504 if (ww == NULL)
505 goto Error;
506 }
507 else
508 Py_INCREF(ww);
509
510 fracpart = modf(i, &intpart);
511 vv = PyLong_FromDouble(intpart);
512 if (vv == NULL)
513 goto Error;
514
515 if (fracpart != 0.0) {
516 /* Shift left, and or a 1 bit into vv
517 * to represent the lost fraction.
518 */
519 PyObject *temp;
520
521 one = PyInt_FromLong(1);
522 if (one == NULL)
523 goto Error;
524
525 temp = PyNumber_Lshift(ww, one);
526 if (temp == NULL)
527 goto Error;
528 Py_DECREF(ww);
529 ww = temp;
530
531 temp = PyNumber_Lshift(vv, one);
532 if (temp == NULL)
533 goto Error;
534 Py_DECREF(vv);
535 vv = temp;
536
537 temp = PyNumber_Or(vv, one);
538 if (temp == NULL)
539 goto Error;
540 Py_DECREF(vv);
541 vv = temp;
542 }
543
544 r = PyObject_RichCompareBool(vv, ww, op);
545 if (r < 0)
546 goto Error;
547 result = PyBool_FromLong(r);
548 Error:
549 Py_XDECREF(vv);
550 Py_XDECREF(ww);
551 Py_XDECREF(one);
552 return result;
553 }
554 } /* else if (PyLong_Check(w)) */
555
556 else /* w isn't float, int, or long */
557 goto Unimplemented;
558
559 Compare:
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000560 PyFPE_START_PROTECT("richcompare", return NULL)
561 switch (op) {
562 case Py_EQ:
Tim Peters307fa782004-09-23 08:06:40 +0000563 r = i == j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000564 break;
565 case Py_NE:
Tim Peters307fa782004-09-23 08:06:40 +0000566 r = i != j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000567 break;
568 case Py_LE:
Tim Peters307fa782004-09-23 08:06:40 +0000569 r = i <= j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000570 break;
571 case Py_GE:
Tim Peters307fa782004-09-23 08:06:40 +0000572 r = i >= j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000573 break;
574 case Py_LT:
Tim Peters307fa782004-09-23 08:06:40 +0000575 r = i < j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000576 break;
577 case Py_GT:
Tim Peters307fa782004-09-23 08:06:40 +0000578 r = i > j;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000579 break;
580 }
Michael W. Hudson957f9772004-02-26 12:33:09 +0000581 PyFPE_END_PROTECT(r)
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000582 return PyBool_FromLong(r);
Tim Peters307fa782004-09-23 08:06:40 +0000583
584 Unimplemented:
585 Py_INCREF(Py_NotImplemented);
586 return Py_NotImplemented;
Michael W. Hudsond3b33b52004-02-19 19:35:22 +0000587}
588
Guido van Rossum9bfef441993-03-29 10:43:31 +0000589static long
Fred Drakefd99de62000-07-09 05:02:18 +0000590float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000591{
Tim Peters39dce292000-08-15 03:34:48 +0000592 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000593}
594
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000596float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000598 double a,b;
599 CONVERT_TO_DOUBLE(v, a);
600 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000601 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000602 a = a + b;
603 PyFPE_END_PROTECT(a)
604 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605}
606
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000608float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000610 double a,b;
611 CONVERT_TO_DOUBLE(v, a);
612 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000613 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000614 a = a - b;
615 PyFPE_END_PROTECT(a)
616 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617}
618
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000620float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000622 double a,b;
623 CONVERT_TO_DOUBLE(v, a);
624 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000625 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000626 a = a * b;
627 PyFPE_END_PROTECT(a)
628 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629}
630
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000632float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000634 double a,b;
635 CONVERT_TO_DOUBLE(v, a);
636 CONVERT_TO_DOUBLE(w, b);
637 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return NULL;
640 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000641 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000642 a = a / b;
643 PyFPE_END_PROTECT(a)
644 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645}
646
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000648float_classic_div(PyObject *v, PyObject *w)
649{
650 double a,b;
651 CONVERT_TO_DOUBLE(v, a);
652 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000653 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000654 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
655 return NULL;
656 if (b == 0.0) {
657 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
658 return NULL;
659 }
660 PyFPE_START_PROTECT("divide", return 0)
661 a = a / b;
662 PyFPE_END_PROTECT(a)
663 return PyFloat_FromDouble(a);
664}
665
666static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000667float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000669 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000670 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000671 CONVERT_TO_DOUBLE(v, vx);
672 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675 return NULL;
676 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000677 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000678 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000679 /* note: checking mod*wx < 0 is incorrect -- underflows to
680 0 if wx < sqrt(smallest nonzero double) */
681 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000682 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000683 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000684 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686}
687
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000689float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000690{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000691 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000692 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000693 CONVERT_TO_DOUBLE(v, vx);
694 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000695 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000697 return NULL;
698 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000699 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000700 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000701 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000702 exact multiple of wx. But this is fp arithmetic, and fp
703 vx - mod is an approximation; the result is that div may
704 not be an exact integral value after the division, although
705 it will always be very close to one.
706 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000707 div = (vx - mod) / wx;
Tim Petersd2e40d62001-11-01 23:12:27 +0000708 if (mod) {
709 /* ensure the remainder has the same sign as the denominator */
710 if ((wx < 0) != (mod < 0)) {
711 mod += wx;
712 div -= 1.0;
713 }
714 }
715 else {
716 /* the remainder is zero, and in the presence of signed zeroes
717 fmod returns different results across platforms; ensure
718 it has the same sign as the denominator; we'd like to do
719 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000720 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000721 if (wx < 0.0)
722 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000723 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000724 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000725 if (div) {
726 floordiv = floor(div);
727 if (div - floordiv > 0.5)
728 floordiv += 1.0;
729 }
730 else {
731 /* div is zero - get the same sign as the true quotient */
732 div *= div; /* hide "div = +0" from optimizers */
733 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
734 }
735 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000736 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000737}
738
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739static PyObject *
Tim Peters63a35712001-12-11 19:57:24 +0000740float_floor_div(PyObject *v, PyObject *w)
741{
742 PyObject *t, *r;
743
744 t = float_divmod(v, w);
Tim Peters77d8a4f2001-12-11 20:31:34 +0000745 if (t == NULL || t == Py_NotImplemented)
746 return t;
747 assert(PyTuple_CheckExact(t));
748 r = PyTuple_GET_ITEM(t, 0);
749 Py_INCREF(r);
750 Py_DECREF(t);
751 return r;
Tim Peters63a35712001-12-11 19:57:24 +0000752}
753
754static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000755float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000756{
757 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000758
759 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000760 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000761 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000762 return NULL;
763 }
764
Neil Schemenauer32117e52001-01-04 01:44:34 +0000765 CONVERT_TO_DOUBLE(v, iv);
766 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000767
768 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000769 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000770 PyFPE_START_PROTECT("pow", return NULL)
771 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000772 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000773 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000774 ix = fmod(1.0, iz);
775 if (ix != 0 && iz < 0)
776 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000777 }
Tim Petersc54d1902000-10-06 00:36:09 +0000778 else
779 ix = 1.0;
780 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000781 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000782 }
Tim Peters96685bf2001-08-23 22:31:37 +0000783 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000784 if (iw < 0.0) {
785 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000786 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000787 return NULL;
788 }
789 return PyFloat_FromDouble(0.0);
790 }
Tim Peterse87568d2003-05-24 20:18:24 +0000791 if (iv < 0.0) {
792 /* Whether this is an error is a mess, and bumps into libm
793 * bugs so we have to figure it out ourselves.
794 */
795 if (iw != floor(iw)) {
796 PyErr_SetString(PyExc_ValueError, "negative number "
797 "cannot be raised to a fractional power");
798 return NULL;
799 }
800 /* iw is an exact integer, albeit perhaps a very large one.
801 * -1 raised to an exact integer should never be exceptional.
802 * Alas, some libms (chiefly glibc as of early 2003) return
803 * NaN and set EDOM on pow(-1, large_int) if the int doesn't
804 * happen to be representable in a *C* integer. That's a
805 * bug; we let that slide in math.pow() (which currently
806 * reflects all platform accidents), but not for Python's **.
807 */
808 if (iv == -1.0 && !Py_IS_INFINITY(iw) && iw == iw) {
809 /* XXX the "iw == iw" was to weed out NaNs. This
810 * XXX doesn't actually work on all platforms.
811 */
812 /* Return 1 if iw is even, -1 if iw is odd; there's
813 * no guarantee that any C integral type is big
814 * enough to hold iw, so we have to check this
815 * indirectly.
816 */
817 ix = floor(iw * 0.5) * 2.0;
818 return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);
819 }
820 /* Else iv != -1.0, and overflow or underflow are possible.
821 * Unless we're to write pow() ourselves, we have to trust
822 * the platform to do this correctly.
823 */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000824 }
Tim Peters96685bf2001-08-23 22:31:37 +0000825 errno = 0;
826 PyFPE_START_PROTECT("pow", return NULL)
827 ix = pow(iv, iw);
828 PyFPE_END_PROTECT(ix)
Tim Petersdc5a5082002-03-09 04:58:24 +0000829 Py_ADJUST_ERANGE1(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000830 if (errno != 0) {
Tim Peterse87568d2003-05-24 20:18:24 +0000831 /* We don't expect any errno value other than ERANGE, but
832 * the range of libm bugs appears unbounded.
833 */
834 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
835 PyExc_ValueError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000837 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839}
840
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000842float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000844 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000845}
846
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000848float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000849{
Tim Peters0280cf72001-09-11 21:53:35 +0000850 if (PyFloat_CheckExact(v)) {
851 Py_INCREF(v);
852 return (PyObject *)v;
853 }
854 else
855 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000856}
857
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000859float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000860{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000861 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862}
863
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000864static int
Fred Drakefd99de62000-07-09 05:02:18 +0000865float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000866{
867 return v->ob_fval != 0.0;
868}
869
Guido van Rossum234f9421993-06-17 12:35:49 +0000870static int
Fred Drakefd99de62000-07-09 05:02:18 +0000871float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000872{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 if (PyInt_Check(*pw)) {
874 long x = PyInt_AsLong(*pw);
875 *pw = PyFloat_FromDouble((double)x);
876 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000877 return 0;
878 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 else if (PyLong_Check(*pw)) {
Neal Norwitzabcb0c02003-01-28 19:21:24 +0000880 double x = PyLong_AsDouble(*pw);
881 if (x == -1.0 && PyErr_Occurred())
882 return -1;
883 *pw = PyFloat_FromDouble(x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000885 return 0;
886 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000887 else if (PyFloat_Check(*pw)) {
888 Py_INCREF(*pv);
889 Py_INCREF(*pw);
890 return 0;
891 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000892 return 1; /* Can't do it */
893}
894
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +0000896float_long(PyObject *v)
897{
898 double x = PyFloat_AsDouble(v);
899 return PyLong_FromDouble(x);
900}
901
902static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000903float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000904{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000905 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000906 double wholepart; /* integral portion of x, rounded toward 0 */
Tim Peters7321ec42001-07-26 20:02:17 +0000907
908 (void)modf(x, &wholepart);
Tim Peters7d791242002-11-21 22:26:37 +0000909 /* Try to get out cheap if this fits in a Python int. The attempt
910 * to cast to long must be protected, as C doesn't define what
911 * happens if the double is too big to fit in a long. Some rare
912 * systems raise an exception then (RISCOS was mentioned as one,
913 * and someone using a non-default option on Sun also bumped into
914 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
915 * still be vulnerable: if a long has more bits of precision than
916 * a double, casting MIN/MAX to double may yield an approximation,
917 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
918 * yield true from the C expression wholepart<=LONG_MAX, despite
919 * that wholepart is actually greater than LONG_MAX.
920 */
921 if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
922 const long aslong = (long)wholepart;
Tim Peters7321ec42001-07-26 20:02:17 +0000923 return PyInt_FromLong(aslong);
Tim Peters7d791242002-11-21 22:26:37 +0000924 }
925 return PyLong_FromDouble(wholepart);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000926}
927
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000929float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000930{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000931 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000932 return v;
933}
934
935
Jeremy Hylton938ace62002-07-17 16:30:39 +0000936static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000937float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
938
Tim Peters6d6c1a32001-08-02 04:15:00 +0000939static PyObject *
940float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
941{
942 PyObject *x = Py_False; /* Integer zero */
943 static char *kwlist[] = {"x", 0};
944
Guido van Rossumbef14172001-08-29 15:47:46 +0000945 if (type != &PyFloat_Type)
946 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000947 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
948 return NULL;
949 if (PyString_Check(x))
950 return PyFloat_FromString(x, NULL);
951 return PyNumber_Float(x);
952}
953
Guido van Rossumbef14172001-08-29 15:47:46 +0000954/* Wimpy, slow approach to tp_new calls for subtypes of float:
955 first create a regular float from whatever arguments we got,
956 then allocate a subtype instance and initialize its ob_fval
957 from the regular float. The regular float is then thrown away.
958*/
959static PyObject *
960float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
961{
962 PyObject *tmp, *new;
963
964 assert(PyType_IsSubtype(type, &PyFloat_Type));
965 tmp = float_new(&PyFloat_Type, args, kwds);
966 if (tmp == NULL)
967 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000968 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000969 new = type->tp_alloc(type, 0);
Raymond Hettingerf4667932003-06-28 20:04:25 +0000970 if (new == NULL) {
971 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000972 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000973 }
Guido van Rossumbef14172001-08-29 15:47:46 +0000974 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
975 Py_DECREF(tmp);
976 return new;
977}
978
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000979static PyObject *
980float_getnewargs(PyFloatObject *v)
981{
982 return Py_BuildValue("(d)", v->ob_fval);
983}
984
985static PyMethodDef float_methods[] = {
986 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
987 {NULL, NULL} /* sentinel */
988};
989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000990PyDoc_STRVAR(float_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991"float(x) -> floating point number\n\
992\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000993Convert a string or number to a floating point number, if possible.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000994
995
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000996static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000997 (binaryfunc)float_add, /*nb_add*/
998 (binaryfunc)float_sub, /*nb_subtract*/
999 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00001000 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001001 (binaryfunc)float_rem, /*nb_remainder*/
1002 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +00001003 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001004 (unaryfunc)float_neg, /*nb_negative*/
1005 (unaryfunc)float_pos, /*nb_positive*/
1006 (unaryfunc)float_abs, /*nb_absolute*/
1007 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +00001008 0, /*nb_invert*/
1009 0, /*nb_lshift*/
1010 0, /*nb_rshift*/
1011 0, /*nb_and*/
1012 0, /*nb_xor*/
1013 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001014 (coercion)float_coerce, /*nb_coerce*/
1015 (unaryfunc)float_int, /*nb_int*/
1016 (unaryfunc)float_long, /*nb_long*/
1017 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001018 0, /* nb_oct */
1019 0, /* nb_hex */
1020 0, /* nb_inplace_add */
1021 0, /* nb_inplace_subtract */
1022 0, /* nb_inplace_multiply */
1023 0, /* nb_inplace_divide */
1024 0, /* nb_inplace_remainder */
1025 0, /* nb_inplace_power */
1026 0, /* nb_inplace_lshift */
1027 0, /* nb_inplace_rshift */
1028 0, /* nb_inplace_and */
1029 0, /* nb_inplace_xor */
1030 0, /* nb_inplace_or */
Tim Peters63a35712001-12-11 19:57:24 +00001031 float_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001032 float_div, /* nb_true_divide */
1033 0, /* nb_inplace_floor_divide */
1034 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001035};
1036
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037PyTypeObject PyFloat_Type = {
1038 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001039 0,
1040 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001041 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001042 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001043 (destructor)float_dealloc, /* tp_dealloc */
1044 (printfunc)float_print, /* tp_print */
1045 0, /* tp_getattr */
1046 0, /* tp_setattr */
Michael W. Hudson08678a12004-05-26 17:36:12 +00001047 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001048 (reprfunc)float_repr, /* tp_repr */
1049 &float_as_number, /* tp_as_number */
1050 0, /* tp_as_sequence */
1051 0, /* tp_as_mapping */
1052 (hashfunc)float_hash, /* tp_hash */
1053 0, /* tp_call */
1054 (reprfunc)float_str, /* tp_str */
1055 PyObject_GenericGetAttr, /* tp_getattro */
1056 0, /* tp_setattro */
1057 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001058 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1059 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001060 float_doc, /* tp_doc */
1061 0, /* tp_traverse */
1062 0, /* tp_clear */
Michael W. Hudsond3b33b52004-02-19 19:35:22 +00001063 (richcmpfunc)float_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001064 0, /* tp_weaklistoffset */
1065 0, /* tp_iter */
1066 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001067 float_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001068 0, /* tp_members */
1069 0, /* tp_getset */
1070 0, /* tp_base */
1071 0, /* tp_dict */
1072 0, /* tp_descr_get */
1073 0, /* tp_descr_set */
1074 0, /* tp_dictoffset */
1075 0, /* tp_init */
1076 0, /* tp_alloc */
1077 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001078};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001079
1080void
Fred Drakefd99de62000-07-09 05:02:18 +00001081PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001082{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001083 PyFloatObject *p;
1084 PyFloatBlock *list, *next;
Neal Norwitz739a8f82004-07-08 01:55:58 +00001085 unsigned i;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001086 int bc, bf; /* block count, number of freed blocks */
1087 int frem, fsum; /* remaining unfreed floats per block, total */
1088
1089 bc = 0;
1090 bf = 0;
1091 fsum = 0;
1092 list = block_list;
1093 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001094 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001095 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001096 bc++;
1097 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001098 for (i = 0, p = &list->objects[0];
1099 i < N_FLOATOBJECTS;
1100 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001101 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001102 frem++;
1103 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001104 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001105 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001106 list->next = block_list;
1107 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001108 for (i = 0, p = &list->objects[0];
1109 i < N_FLOATOBJECTS;
1110 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001111 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001112 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001113 p->ob_type = (struct _typeobject *)
1114 free_list;
1115 free_list = p;
1116 }
1117 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001118 }
1119 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001120 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001121 bf++;
1122 }
1123 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001124 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001125 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001126 if (!Py_VerboseFlag)
1127 return;
1128 fprintf(stderr, "# cleanup floats");
1129 if (!fsum) {
1130 fprintf(stderr, "\n");
1131 }
1132 else {
1133 fprintf(stderr,
1134 ": %d unfreed float%s in %d out of %d block%s\n",
1135 fsum, fsum == 1 ? "" : "s",
1136 bc - bf, bc, bc == 1 ? "" : "s");
1137 }
1138 if (Py_VerboseFlag > 1) {
1139 list = block_list;
1140 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +00001141 for (i = 0, p = &list->objects[0];
1142 i < N_FLOATOBJECTS;
1143 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001144 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +00001145 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001146 char buf[100];
1147 PyFloat_AsString(buf, p);
1148 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001149 "# <float at %p, refcnt=%d, val=%s>\n",
1150 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001151 }
1152 }
1153 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +00001154 }
1155 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001156}
Tim Peters9905b942003-03-20 20:53:32 +00001157
1158/*----------------------------------------------------------------------------
1159 * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
1160 *
1161 * TODO: On platforms that use the standard IEEE-754 single and double
1162 * formats natively, these routines could simply copy the bytes.
1163 */
1164int
1165_PyFloat_Pack4(double x, unsigned char *p, int le)
1166{
1167 unsigned char sign;
1168 int e;
1169 double f;
1170 unsigned int fbits;
1171 int incr = 1;
1172
1173 if (le) {
1174 p += 3;
1175 incr = -1;
1176 }
1177
1178 if (x < 0) {
1179 sign = 1;
1180 x = -x;
1181 }
1182 else
1183 sign = 0;
1184
1185 f = frexp(x, &e);
1186
1187 /* Normalize f to be in the range [1.0, 2.0) */
1188 if (0.5 <= f && f < 1.0) {
1189 f *= 2.0;
1190 e--;
1191 }
1192 else if (f == 0.0)
1193 e = 0;
1194 else {
1195 PyErr_SetString(PyExc_SystemError,
1196 "frexp() result out of range");
1197 return -1;
1198 }
1199
1200 if (e >= 128)
1201 goto Overflow;
1202 else if (e < -126) {
1203 /* Gradual underflow */
1204 f = ldexp(f, 126 + e);
1205 e = 0;
1206 }
1207 else if (!(e == 0 && f == 0.0)) {
1208 e += 127;
1209 f -= 1.0; /* Get rid of leading 1 */
1210 }
1211
1212 f *= 8388608.0; /* 2**23 */
Tim Petersf1ed9342003-03-21 17:10:03 +00001213 fbits = (unsigned int)(f + 0.5); /* Round */
Tim Peters9905b942003-03-20 20:53:32 +00001214 assert(fbits <= 8388608);
1215 if (fbits >> 23) {
1216 /* The carry propagated out of a string of 23 1 bits. */
1217 fbits = 0;
1218 ++e;
1219 if (e >= 255)
1220 goto Overflow;
1221 }
1222
1223 /* First byte */
1224 *p = (sign << 7) | (e >> 1);
1225 p += incr;
1226
1227 /* Second byte */
1228 *p = (char) (((e & 1) << 7) | (fbits >> 16));
1229 p += incr;
1230
1231 /* Third byte */
1232 *p = (fbits >> 8) & 0xFF;
1233 p += incr;
1234
1235 /* Fourth byte */
1236 *p = fbits & 0xFF;
1237
1238 /* Done */
1239 return 0;
1240
1241 Overflow:
1242 PyErr_SetString(PyExc_OverflowError,
1243 "float too large to pack with f format");
1244 return -1;
1245}
1246
1247int
1248_PyFloat_Pack8(double x, unsigned char *p, int le)
1249{
1250 unsigned char sign;
1251 int e;
1252 double f;
1253 unsigned int fhi, flo;
1254 int incr = 1;
1255
1256 if (le) {
1257 p += 7;
1258 incr = -1;
1259 }
1260
1261 if (x < 0) {
1262 sign = 1;
1263 x = -x;
1264 }
1265 else
1266 sign = 0;
1267
1268 f = frexp(x, &e);
1269
1270 /* Normalize f to be in the range [1.0, 2.0) */
1271 if (0.5 <= f && f < 1.0) {
1272 f *= 2.0;
1273 e--;
1274 }
1275 else if (f == 0.0)
1276 e = 0;
1277 else {
1278 PyErr_SetString(PyExc_SystemError,
1279 "frexp() result out of range");
1280 return -1;
1281 }
1282
1283 if (e >= 1024)
1284 goto Overflow;
1285 else if (e < -1022) {
1286 /* Gradual underflow */
1287 f = ldexp(f, 1022 + e);
1288 e = 0;
1289 }
1290 else if (!(e == 0 && f == 0.0)) {
1291 e += 1023;
1292 f -= 1.0; /* Get rid of leading 1 */
1293 }
1294
1295 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1296 f *= 268435456.0; /* 2**28 */
1297 fhi = (unsigned int)f; /* Truncate */
1298 assert(fhi < 268435456);
1299
1300 f -= (double)fhi;
1301 f *= 16777216.0; /* 2**24 */
1302 flo = (unsigned int)(f + 0.5); /* Round */
1303 assert(flo <= 16777216);
1304 if (flo >> 24) {
1305 /* The carry propagated out of a string of 24 1 bits. */
1306 flo = 0;
1307 ++fhi;
1308 if (fhi >> 28) {
1309 /* And it also progagated out of the next 28 bits. */
1310 fhi = 0;
1311 ++e;
1312 if (e >= 2047)
1313 goto Overflow;
1314 }
1315 }
1316
1317 /* First byte */
1318 *p = (sign << 7) | (e >> 4);
1319 p += incr;
1320
1321 /* Second byte */
1322 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
1323 p += incr;
1324
1325 /* Third byte */
1326 *p = (fhi >> 16) & 0xFF;
1327 p += incr;
1328
1329 /* Fourth byte */
1330 *p = (fhi >> 8) & 0xFF;
1331 p += incr;
1332
1333 /* Fifth byte */
1334 *p = fhi & 0xFF;
1335 p += incr;
1336
1337 /* Sixth byte */
1338 *p = (flo >> 16) & 0xFF;
1339 p += incr;
1340
1341 /* Seventh byte */
1342 *p = (flo >> 8) & 0xFF;
1343 p += incr;
1344
1345 /* Eighth byte */
1346 *p = flo & 0xFF;
1347 p += incr;
1348
1349 /* Done */
1350 return 0;
1351
1352 Overflow:
1353 PyErr_SetString(PyExc_OverflowError,
1354 "float too large to pack with d format");
1355 return -1;
1356}
1357
1358double
1359_PyFloat_Unpack4(const unsigned char *p, int le)
1360{
1361 unsigned char sign;
1362 int e;
1363 unsigned int f;
1364 double x;
1365 int incr = 1;
1366
1367 if (le) {
1368 p += 3;
1369 incr = -1;
1370 }
1371
1372 /* First byte */
1373 sign = (*p >> 7) & 1;
1374 e = (*p & 0x7F) << 1;
1375 p += incr;
1376
1377 /* Second byte */
1378 e |= (*p >> 7) & 1;
1379 f = (*p & 0x7F) << 16;
1380 p += incr;
1381
1382 /* Third byte */
1383 f |= *p << 8;
1384 p += incr;
1385
1386 /* Fourth byte */
1387 f |= *p;
1388
1389 x = (double)f / 8388608.0;
1390
1391 /* XXX This sadly ignores Inf/NaN issues */
1392 if (e == 0)
1393 e = -126;
1394 else {
1395 x += 1.0;
1396 e -= 127;
1397 }
1398 x = ldexp(x, e);
1399
1400 if (sign)
1401 x = -x;
1402
1403 return x;
1404}
1405
1406double
1407_PyFloat_Unpack8(const unsigned char *p, int le)
1408{
1409 unsigned char sign;
1410 int e;
1411 unsigned int fhi, flo;
1412 double x;
1413 int incr = 1;
1414
1415 if (le) {
1416 p += 7;
1417 incr = -1;
1418 }
1419
1420 /* First byte */
1421 sign = (*p >> 7) & 1;
1422 e = (*p & 0x7F) << 4;
1423 p += incr;
1424
1425 /* Second byte */
1426 e |= (*p >> 4) & 0xF;
1427 fhi = (*p & 0xF) << 24;
1428 p += incr;
1429
1430 /* Third byte */
1431 fhi |= *p << 16;
1432 p += incr;
1433
1434 /* Fourth byte */
1435 fhi |= *p << 8;
1436 p += incr;
1437
1438 /* Fifth byte */
1439 fhi |= *p;
1440 p += incr;
1441
1442 /* Sixth byte */
1443 flo = *p << 16;
1444 p += incr;
1445
1446 /* Seventh byte */
1447 flo |= *p << 8;
1448 p += incr;
1449
1450 /* Eighth byte */
1451 flo |= *p;
1452
1453 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
1454 x /= 268435456.0; /* 2**28 */
1455
1456 /* XXX This sadly ignores Inf/NaN */
1457 if (e == 0)
1458 e = -1022;
1459 else {
1460 x += 1.0;
1461 e -= 1023;
1462 }
1463 x = ldexp(x, e);
1464
1465 if (sign)
1466 x = -x;
1467
1468 return x;
1469}