Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Float object implementation */ |
| 3 | |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4 | /* 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 7 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 10 | #include <float.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 6923e13 | 1990-11-02 17:50:43 +0000 | [diff] [blame] | 12 | |
Mark Dickinson | d19052c | 2010-06-27 18:19:09 +0000 | [diff] [blame] | 13 | /* Special free list |
Mark Dickinson | d19052c | 2010-06-27 18:19:09 +0000 | [diff] [blame] | 14 | free_list is a singly-linked list of available PyFloatObjects, linked |
| 15 | via abuse of their ob_type members. |
| 16 | */ |
| 17 | |
Kristján Valur Jónsson | daa0654 | 2012-03-30 09:18:15 +0000 | [diff] [blame] | 18 | #ifndef PyFloat_MAXFREELIST |
| 19 | #define PyFloat_MAXFREELIST 100 |
| 20 | #endif |
| 21 | static int numfree = 0; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 22 | static PyFloatObject *free_list = NULL; |
| 23 | |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 24 | double |
| 25 | PyFloat_GetMax(void) |
| 26 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | return DBL_MAX; |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | double |
| 31 | PyFloat_GetMin(void) |
| 32 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | return DBL_MIN; |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 36 | static PyTypeObject FloatInfoType; |
| 37 | |
| 38 | PyDoc_STRVAR(floatinfo__doc__, |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 39 | "sys.float_info\n\ |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 40 | \n\ |
| 41 | A structseq holding information about the float type. It contains low level\n\ |
| 42 | information about the precision and internal representation. Please study\n\ |
| 43 | your system's :file:`float.h` for more information."); |
| 44 | |
| 45 | static PyStructSequence_Field floatinfo_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | {"max", "DBL_MAX -- maximum representable finite float"}, |
| 47 | {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) " |
| 48 | "is representable"}, |
| 49 | {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e " |
| 50 | "is representable"}, |
| 51 | {"min", "DBL_MIN -- Minimum positive normalizer float"}, |
| 52 | {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) " |
| 53 | "is a normalized float"}, |
| 54 | {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is " |
| 55 | "a normalized"}, |
| 56 | {"dig", "DBL_DIG -- digits"}, |
| 57 | {"mant_dig", "DBL_MANT_DIG -- mantissa digits"}, |
| 58 | {"epsilon", "DBL_EPSILON -- Difference between 1 and the next " |
| 59 | "representable float"}, |
| 60 | {"radix", "FLT_RADIX -- radix of exponent"}, |
| 61 | {"rounds", "FLT_ROUNDS -- addition rounds"}, |
| 62 | {0} |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static PyStructSequence_Desc floatinfo_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | "sys.float_info", /* name */ |
| 67 | floatinfo__doc__, /* doc */ |
| 68 | floatinfo_fields, /* fields */ |
| 69 | 11 |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 72 | PyObject * |
| 73 | PyFloat_GetInfo(void) |
| 74 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | PyObject* floatinfo; |
| 76 | int pos = 0; |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | floatinfo = PyStructSequence_New(&FloatInfoType); |
| 79 | if (floatinfo == NULL) { |
| 80 | return NULL; |
| 81 | } |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 82 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 83 | #define SetIntFlag(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag)) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 85 | #define SetDblFlag(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag)) |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | SetDblFlag(DBL_MAX); |
| 89 | SetIntFlag(DBL_MAX_EXP); |
| 90 | SetIntFlag(DBL_MAX_10_EXP); |
| 91 | SetDblFlag(DBL_MIN); |
| 92 | SetIntFlag(DBL_MIN_EXP); |
| 93 | SetIntFlag(DBL_MIN_10_EXP); |
| 94 | SetIntFlag(DBL_DIG); |
| 95 | SetIntFlag(DBL_MANT_DIG); |
| 96 | SetDblFlag(DBL_EPSILON); |
| 97 | SetIntFlag(FLT_RADIX); |
| 98 | SetIntFlag(FLT_ROUNDS); |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 99 | #undef SetIntFlag |
| 100 | #undef SetDblFlag |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | |
| 102 | if (PyErr_Occurred()) { |
| 103 | Py_CLEAR(floatinfo); |
| 104 | return NULL; |
| 105 | } |
| 106 | return floatinfo; |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 110 | PyFloat_FromDouble(double fval) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 112 | PyFloatObject *op = free_list; |
Kristján Valur Jónsson | daa0654 | 2012-03-30 09:18:15 +0000 | [diff] [blame] | 113 | if (op != NULL) { |
| 114 | free_list = (PyFloatObject *) Py_TYPE(op); |
| 115 | numfree--; |
| 116 | } else { |
| 117 | op = (PyFloatObject*) PyObject_MALLOC(sizeof(PyFloatObject)); |
| 118 | if (!op) |
| 119 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | } |
| 121 | /* Inline PyObject_New */ |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame^] | 122 | (void)PyObject_INIT(op, &PyFloat_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | op->ob_fval = fval; |
| 124 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 127 | PyObject * |
Georg Brandl | 428f064 | 2007-03-18 18:35:15 +0000 | [diff] [blame] | 128 | PyFloat_FromString(PyObject *v) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | const char *s, *last, *end; |
| 131 | double x; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 132 | PyObject *s_buffer = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | Py_ssize_t len; |
| 134 | PyObject *result = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 137 | s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | if (s_buffer == NULL) |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 139 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 140 | s = PyUnicode_AsUTF8AndSize(s_buffer, &len); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 141 | if (s == NULL) { |
| 142 | Py_DECREF(s_buffer); |
| 143 | return NULL; |
| 144 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | } |
| 146 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
Ezio Melotti | a5b9599 | 2013-11-07 19:18:34 +0200 | [diff] [blame] | 147 | PyErr_Format(PyExc_TypeError, |
| 148 | "float() argument must be a string or a number, not '%.200s'", |
| 149 | Py_TYPE(v)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
| 152 | last = s + len; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 153 | /* strip space */ |
| 154 | while (s < last && Py_ISSPACE(*s)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | s++; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 156 | while (s < last - 1 && Py_ISSPACE(last[-1])) |
| 157 | last--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | /* We don't care about overflow or underflow. If the platform |
| 159 | * supports them, infinities and signed zeroes (on underflow) are |
| 160 | * fine. */ |
| 161 | x = PyOS_string_to_double(s, (char **)&end, NULL); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 162 | if (end != last) { |
| 163 | PyErr_Format(PyExc_ValueError, |
| 164 | "could not convert string to float: " |
| 165 | "%R", v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | result = NULL; |
| 167 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 168 | else if (x == -1.0 && PyErr_Occurred()) |
| 169 | result = NULL; |
| 170 | else |
| 171 | result = PyFloat_FromDouble(x); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 172 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 173 | Py_XDECREF(s_buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 177 | static void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 178 | float_dealloc(PyFloatObject *op) |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | if (PyFloat_CheckExact(op)) { |
Kristján Valur Jónsson | daa0654 | 2012-03-30 09:18:15 +0000 | [diff] [blame] | 181 | if (numfree >= PyFloat_MAXFREELIST) { |
| 182 | PyObject_FREE(op); |
| 183 | return; |
| 184 | } |
| 185 | numfree++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | Py_TYPE(op) = (struct _typeobject *)free_list; |
| 187 | free_list = op; |
| 188 | } |
| 189 | else |
| 190 | Py_TYPE(op)->tp_free((PyObject *)op); |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | double |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 194 | PyFloat_AsDouble(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | PyNumberMethods *nb; |
| 197 | PyFloatObject *fo; |
| 198 | double val; |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | if (op && PyFloat_Check(op)) |
| 201 | return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | if (op == NULL) { |
| 204 | PyErr_BadArgument(); |
| 205 | return -1; |
| 206 | } |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | if ((nb = Py_TYPE(op)->tp_as_number) == NULL || nb->nb_float == NULL) { |
| 209 | PyErr_SetString(PyExc_TypeError, "a float is required"); |
| 210 | return -1; |
| 211 | } |
Neil Schemenauer | 2c77e90 | 2002-11-18 16:06:21 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | fo = (PyFloatObject*) (*nb->nb_float) (op); |
| 214 | if (fo == NULL) |
| 215 | return -1; |
| 216 | if (!PyFloat_Check(fo)) { |
| 217 | PyErr_SetString(PyExc_TypeError, |
| 218 | "nb_float should return float object"); |
| 219 | return -1; |
| 220 | } |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | val = PyFloat_AS_DOUBLE(fo); |
| 223 | Py_DECREF(fo); |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 228 | /* Macro and helper that convert PyObject obj to a C double and store |
Neil Schemenauer | 16c7075 | 2007-09-21 20:19:23 +0000 | [diff] [blame] | 229 | the value in dbl. If conversion to double raises an exception, obj is |
Tim Peters | 77d8a4f | 2001-12-11 20:31:34 +0000 | [diff] [blame] | 230 | set to NULL, and the function invoking this macro returns NULL. If |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 231 | obj is not of float or int type, Py_NotImplemented is incref'ed, |
Tim Peters | 77d8a4f | 2001-12-11 20:31:34 +0000 | [diff] [blame] | 232 | stored in obj, and returned from the function invoking this macro. |
| 233 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | #define CONVERT_TO_DOUBLE(obj, dbl) \ |
| 235 | if (PyFloat_Check(obj)) \ |
| 236 | dbl = PyFloat_AS_DOUBLE(obj); \ |
| 237 | else if (convert_to_double(&(obj), &(dbl)) < 0) \ |
| 238 | return obj; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 239 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 240 | /* Methods */ |
| 241 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 242 | static int |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 243 | convert_to_double(PyObject **v, double *dbl) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 244 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 245 | PyObject *obj = *v; |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | if (PyLong_Check(obj)) { |
| 248 | *dbl = PyLong_AsDouble(obj); |
| 249 | if (*dbl == -1.0 && PyErr_Occurred()) { |
| 250 | *v = NULL; |
| 251 | return -1; |
| 252 | } |
| 253 | } |
| 254 | else { |
| 255 | Py_INCREF(Py_NotImplemented); |
| 256 | *v = Py_NotImplemented; |
| 257 | return -1; |
| 258 | } |
| 259 | return 0; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 262 | static PyObject * |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 263 | float_repr(PyFloatObject *v) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 264 | { |
| 265 | PyObject *result; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 266 | char *buf; |
| 267 | |
| 268 | buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), |
| 269 | 'r', 0, |
| 270 | Py_DTSF_ADD_DOT_0, |
| 271 | NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 272 | if (!buf) |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 273 | return PyErr_NoMemory(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 274 | result = _PyUnicode_FromASCII(buf, strlen(buf)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 275 | PyMem_Free(buf); |
| 276 | return result; |
| 277 | } |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 278 | |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 279 | /* Comparison is pretty much a nightmare. When comparing float to float, |
| 280 | * we do it as straightforwardly (and long-windedly) as conceivable, so |
| 281 | * that, e.g., Python x == y delivers the same result as the platform |
| 282 | * C x == y when x and/or y is a NaN. |
| 283 | * When mixing float with an integer type, there's no good *uniform* approach. |
| 284 | * Converting the double to an integer obviously doesn't work, since we |
| 285 | * may lose info from fractional bits. Converting the integer to a double |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 286 | * also has two failure modes: (1) an int may trigger overflow (too |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 287 | * large to fit in the dynamic range of a C double); (2) even a C long may have |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 288 | * more bits than fit in a C double (e.g., on a 64-bit box long may have |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 289 | * 63 bits of precision, but a C double probably has only 53), and then |
| 290 | * we can falsely claim equality when low-order integer bits are lost by |
| 291 | * coercion to double. So this part is painful too. |
| 292 | */ |
| 293 | |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 294 | static PyObject* |
| 295 | float_richcompare(PyObject *v, PyObject *w, int op) |
| 296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | double i, j; |
| 298 | int r = 0; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | assert(PyFloat_Check(v)); |
| 301 | i = PyFloat_AS_DOUBLE(v); |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | /* Switch on the type of w. Set i and j to doubles to be compared, |
| 304 | * and op to the richcomp to use. |
| 305 | */ |
| 306 | if (PyFloat_Check(w)) |
| 307 | j = PyFloat_AS_DOUBLE(w); |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | else if (!Py_IS_FINITE(i)) { |
| 310 | if (PyLong_Check(w)) |
| 311 | /* If i is an infinity, its magnitude exceeds any |
| 312 | * finite integer, so it doesn't matter which int we |
| 313 | * compare i with. If i is a NaN, similarly. |
| 314 | */ |
| 315 | j = 0.0; |
| 316 | else |
| 317 | goto Unimplemented; |
| 318 | } |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | else if (PyLong_Check(w)) { |
| 321 | int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1; |
| 322 | int wsign = _PyLong_Sign(w); |
| 323 | size_t nbits; |
| 324 | int exponent; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | if (vsign != wsign) { |
| 327 | /* Magnitudes are irrelevant -- the signs alone |
| 328 | * determine the outcome. |
| 329 | */ |
| 330 | i = (double)vsign; |
| 331 | j = (double)wsign; |
| 332 | goto Compare; |
| 333 | } |
| 334 | /* The signs are the same. */ |
| 335 | /* Convert w to a double if it fits. In particular, 0 fits. */ |
| 336 | nbits = _PyLong_NumBits(w); |
| 337 | if (nbits == (size_t)-1 && PyErr_Occurred()) { |
| 338 | /* This long is so large that size_t isn't big enough |
| 339 | * to hold the # of bits. Replace with little doubles |
| 340 | * that give the same outcome -- w is so large that |
| 341 | * its magnitude must exceed the magnitude of any |
| 342 | * finite float. |
| 343 | */ |
| 344 | PyErr_Clear(); |
| 345 | i = (double)vsign; |
| 346 | assert(wsign != 0); |
| 347 | j = wsign * 2.0; |
| 348 | goto Compare; |
| 349 | } |
| 350 | if (nbits <= 48) { |
| 351 | j = PyLong_AsDouble(w); |
| 352 | /* It's impossible that <= 48 bits overflowed. */ |
| 353 | assert(j != -1.0 || ! PyErr_Occurred()); |
| 354 | goto Compare; |
| 355 | } |
| 356 | assert(wsign != 0); /* else nbits was 0 */ |
| 357 | assert(vsign != 0); /* if vsign were 0, then since wsign is |
| 358 | * not 0, we would have taken the |
| 359 | * vsign != wsign branch at the start */ |
| 360 | /* We want to work with non-negative numbers. */ |
| 361 | if (vsign < 0) { |
| 362 | /* "Multiply both sides" by -1; this also swaps the |
| 363 | * comparator. |
| 364 | */ |
| 365 | i = -i; |
| 366 | op = _Py_SwappedOp[op]; |
| 367 | } |
| 368 | assert(i > 0.0); |
| 369 | (void) frexp(i, &exponent); |
| 370 | /* exponent is the # of bits in v before the radix point; |
| 371 | * we know that nbits (the # of bits in w) > 48 at this point |
| 372 | */ |
| 373 | if (exponent < 0 || (size_t)exponent < nbits) { |
| 374 | i = 1.0; |
| 375 | j = 2.0; |
| 376 | goto Compare; |
| 377 | } |
| 378 | if ((size_t)exponent > nbits) { |
| 379 | i = 2.0; |
| 380 | j = 1.0; |
| 381 | goto Compare; |
| 382 | } |
| 383 | /* v and w have the same number of bits before the radix |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 384 | * point. Construct two ints that have the same comparison |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | * outcome. |
| 386 | */ |
| 387 | { |
| 388 | double fracpart; |
| 389 | double intpart; |
| 390 | PyObject *result = NULL; |
| 391 | PyObject *one = NULL; |
| 392 | PyObject *vv = NULL; |
| 393 | PyObject *ww = w; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | if (wsign < 0) { |
| 396 | ww = PyNumber_Negative(w); |
| 397 | if (ww == NULL) |
| 398 | goto Error; |
| 399 | } |
| 400 | else |
| 401 | Py_INCREF(ww); |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | fracpart = modf(i, &intpart); |
| 404 | vv = PyLong_FromDouble(intpart); |
| 405 | if (vv == NULL) |
| 406 | goto Error; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | if (fracpart != 0.0) { |
| 409 | /* Shift left, and or a 1 bit into vv |
| 410 | * to represent the lost fraction. |
| 411 | */ |
| 412 | PyObject *temp; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | one = PyLong_FromLong(1); |
| 415 | if (one == NULL) |
| 416 | goto Error; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | temp = PyNumber_Lshift(ww, one); |
| 419 | if (temp == NULL) |
| 420 | goto Error; |
| 421 | Py_DECREF(ww); |
| 422 | ww = temp; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | temp = PyNumber_Lshift(vv, one); |
| 425 | if (temp == NULL) |
| 426 | goto Error; |
| 427 | Py_DECREF(vv); |
| 428 | vv = temp; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | temp = PyNumber_Or(vv, one); |
| 431 | if (temp == NULL) |
| 432 | goto Error; |
| 433 | Py_DECREF(vv); |
| 434 | vv = temp; |
| 435 | } |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | r = PyObject_RichCompareBool(vv, ww, op); |
| 438 | if (r < 0) |
| 439 | goto Error; |
| 440 | result = PyBool_FromLong(r); |
| 441 | Error: |
| 442 | Py_XDECREF(vv); |
| 443 | Py_XDECREF(ww); |
| 444 | Py_XDECREF(one); |
| 445 | return result; |
| 446 | } |
| 447 | } /* else if (PyLong_Check(w)) */ |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 448 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 449 | else /* w isn't float or int */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | goto Unimplemented; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 451 | |
| 452 | Compare: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | PyFPE_START_PROTECT("richcompare", return NULL) |
| 454 | switch (op) { |
| 455 | case Py_EQ: |
| 456 | r = i == j; |
| 457 | break; |
| 458 | case Py_NE: |
| 459 | r = i != j; |
| 460 | break; |
| 461 | case Py_LE: |
| 462 | r = i <= j; |
| 463 | break; |
| 464 | case Py_GE: |
| 465 | r = i >= j; |
| 466 | break; |
| 467 | case Py_LT: |
| 468 | r = i < j; |
| 469 | break; |
| 470 | case Py_GT: |
| 471 | r = i > j; |
| 472 | break; |
| 473 | } |
| 474 | PyFPE_END_PROTECT(r) |
| 475 | return PyBool_FromLong(r); |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 476 | |
| 477 | Unimplemented: |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 478 | Py_RETURN_NOTIMPLEMENTED; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 481 | static Py_hash_t |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 482 | float_hash(PyFloatObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | return _Py_HashDouble(v->ob_fval); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 487 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 488 | float_add(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | double a,b; |
| 491 | CONVERT_TO_DOUBLE(v, a); |
| 492 | CONVERT_TO_DOUBLE(w, b); |
| 493 | PyFPE_START_PROTECT("add", return 0) |
| 494 | a = a + b; |
| 495 | PyFPE_END_PROTECT(a) |
| 496 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 499 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 500 | float_sub(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | double a,b; |
| 503 | CONVERT_TO_DOUBLE(v, a); |
| 504 | CONVERT_TO_DOUBLE(w, b); |
| 505 | PyFPE_START_PROTECT("subtract", return 0) |
| 506 | a = a - b; |
| 507 | PyFPE_END_PROTECT(a) |
| 508 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 511 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 512 | float_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | double a,b; |
| 515 | CONVERT_TO_DOUBLE(v, a); |
| 516 | CONVERT_TO_DOUBLE(w, b); |
| 517 | PyFPE_START_PROTECT("multiply", return 0) |
| 518 | a = a * b; |
| 519 | PyFPE_END_PROTECT(a) |
| 520 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 523 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 524 | float_div(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | double a,b; |
| 527 | CONVERT_TO_DOUBLE(v, a); |
| 528 | CONVERT_TO_DOUBLE(w, b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | if (b == 0.0) { |
| 530 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 531 | "float division by zero"); |
| 532 | return NULL; |
| 533 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | PyFPE_START_PROTECT("divide", return 0) |
| 535 | a = a / b; |
| 536 | PyFPE_END_PROTECT(a) |
| 537 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 541 | float_rem(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 542 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | double vx, wx; |
| 544 | double mod; |
| 545 | CONVERT_TO_DOUBLE(v, vx); |
| 546 | CONVERT_TO_DOUBLE(w, wx); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | if (wx == 0.0) { |
| 548 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 549 | "float modulo"); |
| 550 | return NULL; |
| 551 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | PyFPE_START_PROTECT("modulo", return 0) |
| 553 | mod = fmod(vx, wx); |
Mark Dickinson | d2a9b20 | 2010-12-04 12:25:30 +0000 | [diff] [blame] | 554 | if (mod) { |
| 555 | /* ensure the remainder has the same sign as the denominator */ |
| 556 | if ((wx < 0) != (mod < 0)) { |
| 557 | mod += wx; |
| 558 | } |
| 559 | } |
| 560 | else { |
| 561 | /* the remainder is zero, and in the presence of signed zeroes |
| 562 | fmod returns different results across platforms; ensure |
Mark Dickinson | 7b1bee4 | 2010-12-04 13:14:29 +0000 | [diff] [blame] | 563 | it has the same sign as the denominator. */ |
| 564 | mod = copysign(0.0, wx); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | } |
| 566 | PyFPE_END_PROTECT(mod) |
| 567 | return PyFloat_FromDouble(mod); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 571 | float_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | double vx, wx; |
| 574 | double div, mod, floordiv; |
| 575 | CONVERT_TO_DOUBLE(v, vx); |
| 576 | CONVERT_TO_DOUBLE(w, wx); |
| 577 | if (wx == 0.0) { |
| 578 | PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
| 579 | return NULL; |
| 580 | } |
| 581 | PyFPE_START_PROTECT("divmod", return 0) |
| 582 | mod = fmod(vx, wx); |
| 583 | /* fmod is typically exact, so vx-mod is *mathematically* an |
| 584 | exact multiple of wx. But this is fp arithmetic, and fp |
| 585 | vx - mod is an approximation; the result is that div may |
| 586 | not be an exact integral value after the division, although |
| 587 | it will always be very close to one. |
| 588 | */ |
| 589 | div = (vx - mod) / wx; |
| 590 | if (mod) { |
| 591 | /* ensure the remainder has the same sign as the denominator */ |
| 592 | if ((wx < 0) != (mod < 0)) { |
| 593 | mod += wx; |
| 594 | div -= 1.0; |
| 595 | } |
| 596 | } |
| 597 | else { |
| 598 | /* the remainder is zero, and in the presence of signed zeroes |
| 599 | fmod returns different results across platforms; ensure |
Mark Dickinson | 7b1bee4 | 2010-12-04 13:14:29 +0000 | [diff] [blame] | 600 | it has the same sign as the denominator. */ |
| 601 | mod = copysign(0.0, wx); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | } |
| 603 | /* snap quotient to nearest integral value */ |
| 604 | if (div) { |
| 605 | floordiv = floor(div); |
| 606 | if (div - floordiv > 0.5) |
| 607 | floordiv += 1.0; |
| 608 | } |
| 609 | else { |
| 610 | /* div is zero - get the same sign as the true quotient */ |
Mark Dickinson | 7b1bee4 | 2010-12-04 13:14:29 +0000 | [diff] [blame] | 611 | floordiv = copysign(0.0, vx / wx); /* zero w/ sign of vx/wx */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | } |
| 613 | PyFPE_END_PROTECT(floordiv) |
| 614 | return Py_BuildValue("(dd)", floordiv, mod); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 617 | static PyObject * |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 618 | float_floor_div(PyObject *v, PyObject *w) |
| 619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | PyObject *t, *r; |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | t = float_divmod(v, w); |
| 623 | if (t == NULL || t == Py_NotImplemented) |
| 624 | return t; |
| 625 | assert(PyTuple_CheckExact(t)); |
| 626 | r = PyTuple_GET_ITEM(t, 0); |
| 627 | Py_INCREF(r); |
| 628 | Py_DECREF(t); |
| 629 | return r; |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 632 | /* determine whether x is an odd integer or not; assumes that |
| 633 | x is not an infinity or nan. */ |
| 634 | #define DOUBLE_IS_ODD_INTEGER(x) (fmod(fabs(x), 2.0) == 1.0) |
| 635 | |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 636 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 637 | float_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 638 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | double iv, iw, ix; |
| 640 | int negate_result = 0; |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | if ((PyObject *)z != Py_None) { |
| 643 | PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not " |
| 644 | "allowed unless all arguments are integers"); |
| 645 | return NULL; |
| 646 | } |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | CONVERT_TO_DOUBLE(v, iv); |
| 649 | CONVERT_TO_DOUBLE(w, iw); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | /* Sort out special cases here instead of relying on pow() */ |
| 652 | if (iw == 0) { /* v**0 is 1, even 0**0 */ |
| 653 | return PyFloat_FromDouble(1.0); |
| 654 | } |
| 655 | if (Py_IS_NAN(iv)) { /* nan**w = nan, unless w == 0 */ |
| 656 | return PyFloat_FromDouble(iv); |
| 657 | } |
| 658 | if (Py_IS_NAN(iw)) { /* v**nan = nan, unless v == 1; 1**nan = 1 */ |
| 659 | return PyFloat_FromDouble(iv == 1.0 ? 1.0 : iw); |
| 660 | } |
| 661 | if (Py_IS_INFINITY(iw)) { |
| 662 | /* v**inf is: 0.0 if abs(v) < 1; 1.0 if abs(v) == 1; inf if |
| 663 | * abs(v) > 1 (including case where v infinite) |
| 664 | * |
| 665 | * v**-inf is: inf if abs(v) < 1; 1.0 if abs(v) == 1; 0.0 if |
| 666 | * abs(v) > 1 (including case where v infinite) |
| 667 | */ |
| 668 | iv = fabs(iv); |
| 669 | if (iv == 1.0) |
| 670 | return PyFloat_FromDouble(1.0); |
| 671 | else if ((iw > 0.0) == (iv > 1.0)) |
| 672 | return PyFloat_FromDouble(fabs(iw)); /* return inf */ |
| 673 | else |
| 674 | return PyFloat_FromDouble(0.0); |
| 675 | } |
| 676 | if (Py_IS_INFINITY(iv)) { |
| 677 | /* (+-inf)**w is: inf for w positive, 0 for w negative; in |
| 678 | * both cases, we need to add the appropriate sign if w is |
| 679 | * an odd integer. |
| 680 | */ |
| 681 | int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw); |
| 682 | if (iw > 0.0) |
| 683 | return PyFloat_FromDouble(iw_is_odd ? iv : fabs(iv)); |
| 684 | else |
| 685 | return PyFloat_FromDouble(iw_is_odd ? |
| 686 | copysign(0.0, iv) : 0.0); |
| 687 | } |
| 688 | if (iv == 0.0) { /* 0**w is: 0 for w positive, 1 for w zero |
| 689 | (already dealt with above), and an error |
| 690 | if w is negative. */ |
| 691 | int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw); |
| 692 | if (iw < 0.0) { |
| 693 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 694 | "0.0 cannot be raised to a " |
| 695 | "negative power"); |
| 696 | return NULL; |
| 697 | } |
| 698 | /* use correct sign if iw is odd */ |
| 699 | return PyFloat_FromDouble(iw_is_odd ? iv : 0.0); |
| 700 | } |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | if (iv < 0.0) { |
| 703 | /* Whether this is an error is a mess, and bumps into libm |
| 704 | * bugs so we have to figure it out ourselves. |
| 705 | */ |
| 706 | if (iw != floor(iw)) { |
| 707 | /* Negative numbers raised to fractional powers |
| 708 | * become complex. |
| 709 | */ |
| 710 | return PyComplex_Type.tp_as_number->nb_power(v, w, z); |
| 711 | } |
| 712 | /* iw is an exact integer, albeit perhaps a very large |
| 713 | * one. Replace iv by its absolute value and remember |
| 714 | * to negate the pow result if iw is odd. |
| 715 | */ |
| 716 | iv = -iv; |
| 717 | negate_result = DOUBLE_IS_ODD_INTEGER(iw); |
| 718 | } |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */ |
| 721 | /* (-1) ** large_integer also ends up here. Here's an |
| 722 | * extract from the comments for the previous |
| 723 | * implementation explaining why this special case is |
| 724 | * necessary: |
| 725 | * |
| 726 | * -1 raised to an exact integer should never be exceptional. |
| 727 | * Alas, some libms (chiefly glibc as of early 2003) return |
| 728 | * NaN and set EDOM on pow(-1, large_int) if the int doesn't |
| 729 | * happen to be representable in a *C* integer. That's a |
| 730 | * bug. |
| 731 | */ |
| 732 | return PyFloat_FromDouble(negate_result ? -1.0 : 1.0); |
| 733 | } |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | /* Now iv and iw are finite, iw is nonzero, and iv is |
| 736 | * positive and not equal to 1.0. We finally allow |
| 737 | * the platform pow to step in and do the rest. |
| 738 | */ |
| 739 | errno = 0; |
| 740 | PyFPE_START_PROTECT("pow", return NULL) |
| 741 | ix = pow(iv, iw); |
| 742 | PyFPE_END_PROTECT(ix) |
| 743 | Py_ADJUST_ERANGE1(ix); |
| 744 | if (negate_result) |
| 745 | ix = -ix; |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | if (errno != 0) { |
| 748 | /* We don't expect any errno value other than ERANGE, but |
| 749 | * the range of libm bugs appears unbounded. |
| 750 | */ |
| 751 | PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError : |
| 752 | PyExc_ValueError); |
| 753 | return NULL; |
| 754 | } |
| 755 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 758 | #undef DOUBLE_IS_ODD_INTEGER |
| 759 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 760 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 761 | float_neg(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | return PyFloat_FromDouble(-v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 766 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 767 | float_abs(PyFloatObject *v) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | return PyFloat_FromDouble(fabs(v->ob_fval)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 772 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 773 | float_bool(PyFloatObject *v) |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | return v->ob_fval != 0.0; |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 778 | static PyObject * |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 779 | float_is_integer(PyObject *v) |
| 780 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | double x = PyFloat_AsDouble(v); |
| 782 | PyObject *o; |
| 783 | |
| 784 | if (x == -1.0 && PyErr_Occurred()) |
| 785 | return NULL; |
| 786 | if (!Py_IS_FINITE(x)) |
| 787 | Py_RETURN_FALSE; |
| 788 | errno = 0; |
| 789 | PyFPE_START_PROTECT("is_integer", return NULL) |
| 790 | o = (floor(x) == x) ? Py_True : Py_False; |
| 791 | PyFPE_END_PROTECT(x) |
| 792 | if (errno != 0) { |
| 793 | PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError : |
| 794 | PyExc_ValueError); |
| 795 | return NULL; |
| 796 | } |
| 797 | Py_INCREF(o); |
| 798 | return o; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | #if 0 |
| 802 | static PyObject * |
| 803 | float_is_inf(PyObject *v) |
| 804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | double x = PyFloat_AsDouble(v); |
| 806 | if (x == -1.0 && PyErr_Occurred()) |
| 807 | return NULL; |
| 808 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | static PyObject * |
| 812 | float_is_nan(PyObject *v) |
| 813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | double x = PyFloat_AsDouble(v); |
| 815 | if (x == -1.0 && PyErr_Occurred()) |
| 816 | return NULL; |
| 817 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | static PyObject * |
| 821 | float_is_finite(PyObject *v) |
| 822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | double x = PyFloat_AsDouble(v); |
| 824 | if (x == -1.0 && PyErr_Occurred()) |
| 825 | return NULL; |
| 826 | return PyBool_FromLong((long)Py_IS_FINITE(x)); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 827 | } |
| 828 | #endif |
| 829 | |
| 830 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 831 | float_trunc(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | double x = PyFloat_AsDouble(v); |
| 834 | double wholepart; /* integral portion of x, rounded toward 0 */ |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | (void)modf(x, &wholepart); |
| 837 | /* Try to get out cheap if this fits in a Python int. The attempt |
| 838 | * to cast to long must be protected, as C doesn't define what |
| 839 | * happens if the double is too big to fit in a long. Some rare |
| 840 | * systems raise an exception then (RISCOS was mentioned as one, |
| 841 | * and someone using a non-default option on Sun also bumped into |
| 842 | * that). Note that checking for >= and <= LONG_{MIN,MAX} would |
| 843 | * still be vulnerable: if a long has more bits of precision than |
| 844 | * a double, casting MIN/MAX to double may yield an approximation, |
| 845 | * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would |
| 846 | * yield true from the C expression wholepart<=LONG_MAX, despite |
| 847 | * that wholepart is actually greater than LONG_MAX. |
| 848 | */ |
| 849 | if (LONG_MIN < wholepart && wholepart < LONG_MAX) { |
| 850 | const long aslong = (long)wholepart; |
| 851 | return PyLong_FromLong(aslong); |
| 852 | } |
| 853 | return PyLong_FromDouble(wholepart); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 856 | /* double_round: rounds a finite double to the closest multiple of |
| 857 | 10**-ndigits; here ndigits is within reasonable bounds (typically, -308 <= |
| 858 | ndigits <= 323). Returns a Python float, or sets a Python error and |
| 859 | returns NULL on failure (OverflowError and memory errors are possible). */ |
| 860 | |
| 861 | #ifndef PY_NO_SHORT_FLOAT_REPR |
| 862 | /* version of double_round that uses the correctly-rounded string<->double |
| 863 | conversions from Python/dtoa.c */ |
| 864 | |
| 865 | static PyObject * |
| 866 | double_round(double x, int ndigits) { |
| 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | double rounded; |
| 869 | Py_ssize_t buflen, mybuflen=100; |
| 870 | char *buf, *buf_end, shortbuf[100], *mybuf=shortbuf; |
| 871 | int decpt, sign; |
| 872 | PyObject *result = NULL; |
Mark Dickinson | 261896b | 2012-01-27 21:16:01 +0000 | [diff] [blame] | 873 | _Py_SET_53BIT_PRECISION_HEADER; |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | /* round to a decimal string */ |
Mark Dickinson | 261896b | 2012-01-27 21:16:01 +0000 | [diff] [blame] | 876 | _Py_SET_53BIT_PRECISION_START; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | buf = _Py_dg_dtoa(x, 3, ndigits, &decpt, &sign, &buf_end); |
Mark Dickinson | 261896b | 2012-01-27 21:16:01 +0000 | [diff] [blame] | 878 | _Py_SET_53BIT_PRECISION_END; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | if (buf == NULL) { |
| 880 | PyErr_NoMemory(); |
| 881 | return NULL; |
| 882 | } |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | /* Get new buffer if shortbuf is too small. Space needed <= buf_end - |
| 885 | buf + 8: (1 extra for '0', 1 for sign, 5 for exp, 1 for '\0'). */ |
| 886 | buflen = buf_end - buf; |
| 887 | if (buflen + 8 > mybuflen) { |
| 888 | mybuflen = buflen+8; |
| 889 | mybuf = (char *)PyMem_Malloc(mybuflen); |
| 890 | if (mybuf == NULL) { |
| 891 | PyErr_NoMemory(); |
| 892 | goto exit; |
| 893 | } |
| 894 | } |
| 895 | /* copy buf to mybuf, adding exponent, sign and leading 0 */ |
| 896 | PyOS_snprintf(mybuf, mybuflen, "%s0%se%d", (sign ? "-" : ""), |
| 897 | buf, decpt - (int)buflen); |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | /* and convert the resulting string back to a double */ |
| 900 | errno = 0; |
Mark Dickinson | 261896b | 2012-01-27 21:16:01 +0000 | [diff] [blame] | 901 | _Py_SET_53BIT_PRECISION_START; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | rounded = _Py_dg_strtod(mybuf, NULL); |
Mark Dickinson | 261896b | 2012-01-27 21:16:01 +0000 | [diff] [blame] | 903 | _Py_SET_53BIT_PRECISION_END; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | if (errno == ERANGE && fabs(rounded) >= 1.) |
| 905 | PyErr_SetString(PyExc_OverflowError, |
| 906 | "rounded value too large to represent"); |
| 907 | else |
| 908 | result = PyFloat_FromDouble(rounded); |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | /* done computing value; now clean up */ |
| 911 | if (mybuf != shortbuf) |
| 912 | PyMem_Free(mybuf); |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 913 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | _Py_dg_freedtoa(buf); |
| 915 | return result; |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | #else /* PY_NO_SHORT_FLOAT_REPR */ |
| 919 | |
| 920 | /* fallback version, to be used when correctly rounded binary<->decimal |
| 921 | conversions aren't available */ |
| 922 | |
| 923 | static PyObject * |
| 924 | double_round(double x, int ndigits) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | double pow1, pow2, y, z; |
| 926 | if (ndigits >= 0) { |
| 927 | if (ndigits > 22) { |
| 928 | /* pow1 and pow2 are each safe from overflow, but |
| 929 | pow1*pow2 ~= pow(10.0, ndigits) might overflow */ |
| 930 | pow1 = pow(10.0, (double)(ndigits-22)); |
| 931 | pow2 = 1e22; |
| 932 | } |
| 933 | else { |
| 934 | pow1 = pow(10.0, (double)ndigits); |
| 935 | pow2 = 1.0; |
| 936 | } |
| 937 | y = (x*pow1)*pow2; |
| 938 | /* if y overflows, then rounded value is exactly x */ |
| 939 | if (!Py_IS_FINITE(y)) |
| 940 | return PyFloat_FromDouble(x); |
| 941 | } |
| 942 | else { |
| 943 | pow1 = pow(10.0, (double)-ndigits); |
| 944 | pow2 = 1.0; /* unused; silences a gcc compiler warning */ |
| 945 | y = x / pow1; |
| 946 | } |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | z = round(y); |
| 949 | if (fabs(y-z) == 0.5) |
| 950 | /* halfway between two integers; use round-half-even */ |
| 951 | z = 2.0*round(y/2.0); |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | if (ndigits >= 0) |
| 954 | z = (z / pow2) / pow1; |
| 955 | else |
| 956 | z *= pow1; |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | /* if computation resulted in overflow, raise OverflowError */ |
| 959 | if (!Py_IS_FINITE(z)) { |
| 960 | PyErr_SetString(PyExc_OverflowError, |
| 961 | "overflow occurred during round"); |
| 962 | return NULL; |
| 963 | } |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | return PyFloat_FromDouble(z); |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | #endif /* PY_NO_SHORT_FLOAT_REPR */ |
| 969 | |
| 970 | /* round a Python float v to the closest multiple of 10**-ndigits */ |
| 971 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 972 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 973 | float_round(PyObject *v, PyObject *args) |
| 974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | double x, rounded; |
| 976 | PyObject *o_ndigits = NULL; |
| 977 | Py_ssize_t ndigits; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | x = PyFloat_AsDouble(v); |
| 980 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
| 981 | return NULL; |
| 982 | if (o_ndigits == NULL) { |
| 983 | /* single-argument round: round to nearest integer */ |
| 984 | rounded = round(x); |
| 985 | if (fabs(x-rounded) == 0.5) |
| 986 | /* halfway case: round to even */ |
| 987 | rounded = 2.0*round(x/2.0); |
| 988 | return PyLong_FromDouble(rounded); |
| 989 | } |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | /* interpret second argument as a Py_ssize_t; clips on overflow */ |
| 992 | ndigits = PyNumber_AsSsize_t(o_ndigits, NULL); |
| 993 | if (ndigits == -1 && PyErr_Occurred()) |
| 994 | return NULL; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | /* nans and infinities round to themselves */ |
| 997 | if (!Py_IS_FINITE(x)) |
| 998 | return PyFloat_FromDouble(x); |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x |
| 1001 | always rounds to itself. For ndigits < NDIGITS_MIN, x always |
| 1002 | rounds to +-0.0. Here 0.30103 is an upper bound for log10(2). */ |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 1003 | #define NDIGITS_MAX ((int)((DBL_MANT_DIG-DBL_MIN_EXP) * 0.30103)) |
| 1004 | #define NDIGITS_MIN (-(int)((DBL_MAX_EXP + 1) * 0.30103)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | if (ndigits > NDIGITS_MAX) |
| 1006 | /* return x */ |
| 1007 | return PyFloat_FromDouble(x); |
| 1008 | else if (ndigits < NDIGITS_MIN) |
| 1009 | /* return 0.0, but with sign of x */ |
| 1010 | return PyFloat_FromDouble(0.0*x); |
| 1011 | else |
| 1012 | /* finite x, and ndigits is not unreasonably large */ |
| 1013 | return double_round(x, (int)ndigits); |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 1014 | #undef NDIGITS_MAX |
| 1015 | #undef NDIGITS_MIN |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1019 | float_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1020 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | if (PyFloat_CheckExact(v)) |
| 1022 | Py_INCREF(v); |
| 1023 | else |
| 1024 | v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval); |
| 1025 | return v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1028 | /* turn ASCII hex characters into integer values and vice versa */ |
| 1029 | |
| 1030 | static char |
| 1031 | char_from_hex(int x) |
| 1032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | assert(0 <= x && x < 16); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1034 | return Py_hexdigits[x]; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static int |
| 1038 | hex_from_char(char c) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | int x; |
| 1040 | switch(c) { |
| 1041 | case '0': |
| 1042 | x = 0; |
| 1043 | break; |
| 1044 | case '1': |
| 1045 | x = 1; |
| 1046 | break; |
| 1047 | case '2': |
| 1048 | x = 2; |
| 1049 | break; |
| 1050 | case '3': |
| 1051 | x = 3; |
| 1052 | break; |
| 1053 | case '4': |
| 1054 | x = 4; |
| 1055 | break; |
| 1056 | case '5': |
| 1057 | x = 5; |
| 1058 | break; |
| 1059 | case '6': |
| 1060 | x = 6; |
| 1061 | break; |
| 1062 | case '7': |
| 1063 | x = 7; |
| 1064 | break; |
| 1065 | case '8': |
| 1066 | x = 8; |
| 1067 | break; |
| 1068 | case '9': |
| 1069 | x = 9; |
| 1070 | break; |
| 1071 | case 'a': |
| 1072 | case 'A': |
| 1073 | x = 10; |
| 1074 | break; |
| 1075 | case 'b': |
| 1076 | case 'B': |
| 1077 | x = 11; |
| 1078 | break; |
| 1079 | case 'c': |
| 1080 | case 'C': |
| 1081 | x = 12; |
| 1082 | break; |
| 1083 | case 'd': |
| 1084 | case 'D': |
| 1085 | x = 13; |
| 1086 | break; |
| 1087 | case 'e': |
| 1088 | case 'E': |
| 1089 | x = 14; |
| 1090 | break; |
| 1091 | case 'f': |
| 1092 | case 'F': |
| 1093 | x = 15; |
| 1094 | break; |
| 1095 | default: |
| 1096 | x = -1; |
| 1097 | break; |
| 1098 | } |
| 1099 | return x; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* convert a float to a hexadecimal string */ |
| 1103 | |
| 1104 | /* TOHEX_NBITS is DBL_MANT_DIG rounded up to the next integer |
| 1105 | of the form 4k+1. */ |
| 1106 | #define TOHEX_NBITS DBL_MANT_DIG + 3 - (DBL_MANT_DIG+2)%4 |
| 1107 | |
| 1108 | static PyObject * |
| 1109 | float_hex(PyObject *v) |
| 1110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | double x, m; |
| 1112 | int e, shift, i, si, esign; |
| 1113 | /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the |
| 1114 | trailing NUL byte. */ |
| 1115 | char s[(TOHEX_NBITS-1)/4+3]; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | CONVERT_TO_DOUBLE(v, x); |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 1120 | return float_repr((PyFloatObject *)v); |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | if (x == 0.0) { |
Benjamin Peterson | 5d47083 | 2010-07-02 19:45:07 +0000 | [diff] [blame] | 1123 | if (copysign(1.0, x) == -1.0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | return PyUnicode_FromString("-0x0.0p+0"); |
| 1125 | else |
| 1126 | return PyUnicode_FromString("0x0.0p+0"); |
| 1127 | } |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | m = frexp(fabs(x), &e); |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1130 | shift = 1 - Py_MAX(DBL_MIN_EXP - e, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | m = ldexp(m, shift); |
| 1132 | e -= shift; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | si = 0; |
| 1135 | s[si] = char_from_hex((int)m); |
| 1136 | si++; |
| 1137 | m -= (int)m; |
| 1138 | s[si] = '.'; |
| 1139 | si++; |
| 1140 | for (i=0; i < (TOHEX_NBITS-1)/4; i++) { |
| 1141 | m *= 16.0; |
| 1142 | s[si] = char_from_hex((int)m); |
| 1143 | si++; |
| 1144 | m -= (int)m; |
| 1145 | } |
| 1146 | s[si] = '\0'; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | if (e < 0) { |
| 1149 | esign = (int)'-'; |
| 1150 | e = -e; |
| 1151 | } |
| 1152 | else |
| 1153 | esign = (int)'+'; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | if (x < 0.0) |
| 1156 | return PyUnicode_FromFormat("-0x%sp%c%d", s, esign, e); |
| 1157 | else |
| 1158 | return PyUnicode_FromFormat("0x%sp%c%d", s, esign, e); |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | PyDoc_STRVAR(float_hex_doc, |
| 1162 | "float.hex() -> string\n\ |
| 1163 | \n\ |
| 1164 | Return a hexadecimal representation of a floating-point number.\n\ |
| 1165 | >>> (-0.1).hex()\n\ |
| 1166 | '-0x1.999999999999ap-4'\n\ |
| 1167 | >>> 3.14159.hex()\n\ |
| 1168 | '0x1.921f9f01b866ep+1'"); |
| 1169 | |
| 1170 | /* Convert a hexadecimal string to a float. */ |
| 1171 | |
| 1172 | static PyObject * |
| 1173 | float_fromhex(PyObject *cls, PyObject *arg) |
| 1174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | PyObject *result_as_float, *result; |
| 1176 | double x; |
| 1177 | long exp, top_exp, lsb, key_digit; |
| 1178 | char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end; |
| 1179 | int half_eps, digit, round_up, negate=0; |
| 1180 | Py_ssize_t length, ndigits, fdigits, i; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | /* |
| 1183 | * For the sake of simplicity and correctness, we impose an artificial |
| 1184 | * limit on ndigits, the total number of hex digits in the coefficient |
| 1185 | * The limit is chosen to ensure that, writing exp for the exponent, |
| 1186 | * |
| 1187 | * (1) if exp > LONG_MAX/2 then the value of the hex string is |
| 1188 | * guaranteed to overflow (provided it's nonzero) |
| 1189 | * |
| 1190 | * (2) if exp < LONG_MIN/2 then the value of the hex string is |
| 1191 | * guaranteed to underflow to 0. |
| 1192 | * |
| 1193 | * (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of |
| 1194 | * overflow in the calculation of exp and top_exp below. |
| 1195 | * |
| 1196 | * More specifically, ndigits is assumed to satisfy the following |
| 1197 | * inequalities: |
| 1198 | * |
| 1199 | * 4*ndigits <= DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2 |
| 1200 | * 4*ndigits <= LONG_MAX/2 + 1 - DBL_MAX_EXP |
| 1201 | * |
| 1202 | * If either of these inequalities is not satisfied, a ValueError is |
| 1203 | * raised. Otherwise, write x for the value of the hex string, and |
| 1204 | * assume x is nonzero. Then |
| 1205 | * |
| 1206 | * 2**(exp-4*ndigits) <= |x| < 2**(exp+4*ndigits). |
| 1207 | * |
| 1208 | * Now if exp > LONG_MAX/2 then: |
| 1209 | * |
| 1210 | * exp - 4*ndigits >= LONG_MAX/2 + 1 - (LONG_MAX/2 + 1 - DBL_MAX_EXP) |
| 1211 | * = DBL_MAX_EXP |
| 1212 | * |
| 1213 | * so |x| >= 2**DBL_MAX_EXP, which is too large to be stored in C |
| 1214 | * double, so overflows. If exp < LONG_MIN/2, then |
| 1215 | * |
| 1216 | * exp + 4*ndigits <= LONG_MIN/2 - 1 + ( |
| 1217 | * DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2) |
| 1218 | * = DBL_MIN_EXP - DBL_MANT_DIG - 1 |
| 1219 | * |
| 1220 | * and so |x| < 2**(DBL_MIN_EXP-DBL_MANT_DIG-1), hence underflows to 0 |
| 1221 | * when converted to a C double. |
| 1222 | * |
| 1223 | * It's easy to show that if LONG_MIN/2 <= exp <= LONG_MAX/2 then both |
| 1224 | * exp+4*ndigits and exp-4*ndigits are within the range of a long. |
| 1225 | */ |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | s = _PyUnicode_AsStringAndSize(arg, &length); |
| 1228 | if (s == NULL) |
| 1229 | return NULL; |
| 1230 | s_end = s + length; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | /******************** |
| 1233 | * Parse the string * |
| 1234 | ********************/ |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | /* leading whitespace */ |
| 1237 | while (Py_ISSPACE(*s)) |
| 1238 | s++; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | /* infinities and nans */ |
| 1241 | x = _Py_parse_inf_or_nan(s, &coeff_end); |
| 1242 | if (coeff_end != s) { |
| 1243 | s = coeff_end; |
| 1244 | goto finished; |
| 1245 | } |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | /* optional sign */ |
| 1248 | if (*s == '-') { |
| 1249 | s++; |
| 1250 | negate = 1; |
| 1251 | } |
| 1252 | else if (*s == '+') |
| 1253 | s++; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | /* [0x] */ |
| 1256 | s_store = s; |
| 1257 | if (*s == '0') { |
| 1258 | s++; |
| 1259 | if (*s == 'x' || *s == 'X') |
| 1260 | s++; |
| 1261 | else |
| 1262 | s = s_store; |
| 1263 | } |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | /* coefficient: <integer> [. <fraction>] */ |
| 1266 | coeff_start = s; |
| 1267 | while (hex_from_char(*s) >= 0) |
| 1268 | s++; |
| 1269 | s_store = s; |
| 1270 | if (*s == '.') { |
| 1271 | s++; |
| 1272 | while (hex_from_char(*s) >= 0) |
| 1273 | s++; |
| 1274 | coeff_end = s-1; |
| 1275 | } |
| 1276 | else |
| 1277 | coeff_end = s; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | /* ndigits = total # of hex digits; fdigits = # after point */ |
| 1280 | ndigits = coeff_end - coeff_start; |
| 1281 | fdigits = coeff_end - s_store; |
| 1282 | if (ndigits == 0) |
| 1283 | goto parse_error; |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1284 | if (ndigits > Py_MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2, |
| 1285 | LONG_MAX/2 + 1 - DBL_MAX_EXP)/4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | goto insane_length_error; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | /* [p <exponent>] */ |
| 1289 | if (*s == 'p' || *s == 'P') { |
| 1290 | s++; |
| 1291 | exp_start = s; |
| 1292 | if (*s == '-' || *s == '+') |
| 1293 | s++; |
| 1294 | if (!('0' <= *s && *s <= '9')) |
| 1295 | goto parse_error; |
| 1296 | s++; |
| 1297 | while ('0' <= *s && *s <= '9') |
| 1298 | s++; |
| 1299 | exp = strtol(exp_start, NULL, 10); |
| 1300 | } |
| 1301 | else |
| 1302 | exp = 0; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1303 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1304 | /* for 0 <= j < ndigits, HEX_DIGIT(j) gives the jth most significant digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | #define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ? \ |
| 1306 | coeff_end-(j) : \ |
| 1307 | coeff_end-1-(j))) |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | /******************************************* |
| 1310 | * Compute rounded value of the hex string * |
| 1311 | *******************************************/ |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | /* Discard leading zeros, and catch extreme overflow and underflow */ |
| 1314 | while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0) |
| 1315 | ndigits--; |
| 1316 | if (ndigits == 0 || exp < LONG_MIN/2) { |
| 1317 | x = 0.0; |
| 1318 | goto finished; |
| 1319 | } |
| 1320 | if (exp > LONG_MAX/2) |
| 1321 | goto overflow_error; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | /* Adjust exponent for fractional part. */ |
| 1324 | exp = exp - 4*((long)fdigits); |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | /* top_exp = 1 more than exponent of most sig. bit of coefficient */ |
| 1327 | top_exp = exp + 4*((long)ndigits - 1); |
| 1328 | for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2) |
| 1329 | top_exp++; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | /* catch almost all nonextreme cases of overflow and underflow here */ |
| 1332 | if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) { |
| 1333 | x = 0.0; |
| 1334 | goto finished; |
| 1335 | } |
| 1336 | if (top_exp > DBL_MAX_EXP) |
| 1337 | goto overflow_error; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | /* lsb = exponent of least significant bit of the *rounded* value. |
| 1340 | This is top_exp - DBL_MANT_DIG unless result is subnormal. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 1341 | lsb = Py_MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | x = 0.0; |
| 1344 | if (exp >= lsb) { |
| 1345 | /* no rounding required */ |
| 1346 | for (i = ndigits-1; i >= 0; i--) |
| 1347 | x = 16.0*x + HEX_DIGIT(i); |
| 1348 | x = ldexp(x, (int)(exp)); |
| 1349 | goto finished; |
| 1350 | } |
| 1351 | /* rounding required. key_digit is the index of the hex digit |
| 1352 | containing the first bit to be rounded away. */ |
| 1353 | half_eps = 1 << (int)((lsb - exp - 1) % 4); |
| 1354 | key_digit = (lsb - exp - 1) / 4; |
| 1355 | for (i = ndigits-1; i > key_digit; i--) |
| 1356 | x = 16.0*x + HEX_DIGIT(i); |
| 1357 | digit = HEX_DIGIT(key_digit); |
| 1358 | x = 16.0*x + (double)(digit & (16-2*half_eps)); |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | /* round-half-even: round up if bit lsb-1 is 1 and at least one of |
| 1361 | bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */ |
| 1362 | if ((digit & half_eps) != 0) { |
| 1363 | round_up = 0; |
| 1364 | if ((digit & (3*half_eps-1)) != 0 || |
| 1365 | (half_eps == 8 && (HEX_DIGIT(key_digit+1) & 1) != 0)) |
| 1366 | round_up = 1; |
| 1367 | else |
| 1368 | for (i = key_digit-1; i >= 0; i--) |
| 1369 | if (HEX_DIGIT(i) != 0) { |
| 1370 | round_up = 1; |
| 1371 | break; |
| 1372 | } |
Mark Dickinson | 21a1f73 | 2010-07-06 15:11:44 +0000 | [diff] [blame] | 1373 | if (round_up) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | x += 2*half_eps; |
| 1375 | if (top_exp == DBL_MAX_EXP && |
| 1376 | x == ldexp((double)(2*half_eps), DBL_MANT_DIG)) |
| 1377 | /* overflow corner case: pre-rounded value < |
| 1378 | 2**DBL_MAX_EXP; rounded=2**DBL_MAX_EXP. */ |
| 1379 | goto overflow_error; |
| 1380 | } |
| 1381 | } |
| 1382 | x = ldexp(x, (int)(exp+4*key_digit)); |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1383 | |
| 1384 | finished: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | /* optional trailing whitespace leading to the end of the string */ |
| 1386 | while (Py_ISSPACE(*s)) |
| 1387 | s++; |
| 1388 | if (s != s_end) |
| 1389 | goto parse_error; |
| 1390 | result_as_float = Py_BuildValue("(d)", negate ? -x : x); |
| 1391 | if (result_as_float == NULL) |
| 1392 | return NULL; |
| 1393 | result = PyObject_CallObject(cls, result_as_float); |
| 1394 | Py_DECREF(result_as_float); |
| 1395 | return result; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1396 | |
| 1397 | overflow_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | PyErr_SetString(PyExc_OverflowError, |
| 1399 | "hexadecimal value too large to represent as a float"); |
| 1400 | return NULL; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1401 | |
| 1402 | parse_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | PyErr_SetString(PyExc_ValueError, |
| 1404 | "invalid hexadecimal floating-point string"); |
| 1405 | return NULL; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1406 | |
| 1407 | insane_length_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | PyErr_SetString(PyExc_ValueError, |
| 1409 | "hexadecimal string too long to convert"); |
| 1410 | return NULL; |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | PyDoc_STRVAR(float_fromhex_doc, |
| 1414 | "float.fromhex(string) -> float\n\ |
| 1415 | \n\ |
| 1416 | Create a floating-point number from a hexadecimal string.\n\ |
| 1417 | >>> float.fromhex('0x1.ffffp10')\n\ |
| 1418 | 2047.984375\n\ |
| 1419 | >>> float.fromhex('-0x1p-1074')\n\ |
Zachary Ware | a4b7a75 | 2013-11-24 01:19:09 -0600 | [diff] [blame] | 1420 | -5e-324"); |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1421 | |
| 1422 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1423 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1424 | float_as_integer_ratio(PyObject *v, PyObject *unused) |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | double self; |
| 1427 | double float_part; |
| 1428 | int exponent; |
| 1429 | int i; |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1431 | PyObject *prev; |
| 1432 | PyObject *py_exponent = NULL; |
| 1433 | PyObject *numerator = NULL; |
| 1434 | PyObject *denominator = NULL; |
| 1435 | PyObject *result_pair = NULL; |
| 1436 | PyNumberMethods *long_methods = PyLong_Type.tp_as_number; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1437 | |
| 1438 | #define INPLACE_UPDATE(obj, call) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | prev = obj; \ |
| 1440 | obj = call; \ |
| 1441 | Py_DECREF(prev); \ |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | CONVERT_TO_DOUBLE(v, self); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | if (Py_IS_INFINITY(self)) { |
| 1446 | PyErr_SetString(PyExc_OverflowError, |
| 1447 | "Cannot pass infinity to float.as_integer_ratio."); |
| 1448 | return NULL; |
| 1449 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | if (Py_IS_NAN(self)) { |
| 1451 | PyErr_SetString(PyExc_ValueError, |
| 1452 | "Cannot pass NaN to float.as_integer_ratio."); |
| 1453 | return NULL; |
| 1454 | } |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | PyFPE_START_PROTECT("as_integer_ratio", goto error); |
| 1457 | float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */ |
| 1458 | PyFPE_END_PROTECT(float_part); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | for (i=0; i<300 && float_part != floor(float_part) ; i++) { |
| 1461 | float_part *= 2.0; |
| 1462 | exponent--; |
| 1463 | } |
| 1464 | /* self == float_part * 2**exponent exactly and float_part is integral. |
| 1465 | If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part |
| 1466 | to be truncated by PyLong_FromDouble(). */ |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | numerator = PyLong_FromDouble(float_part); |
| 1469 | if (numerator == NULL) goto error; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | /* fold in 2**exponent */ |
| 1472 | denominator = PyLong_FromLong(1); |
| 1473 | py_exponent = PyLong_FromLong(labs((long)exponent)); |
| 1474 | if (py_exponent == NULL) goto error; |
| 1475 | INPLACE_UPDATE(py_exponent, |
| 1476 | long_methods->nb_lshift(denominator, py_exponent)); |
| 1477 | if (py_exponent == NULL) goto error; |
| 1478 | if (exponent > 0) { |
| 1479 | INPLACE_UPDATE(numerator, |
| 1480 | long_methods->nb_multiply(numerator, py_exponent)); |
| 1481 | if (numerator == NULL) goto error; |
| 1482 | } |
| 1483 | else { |
| 1484 | Py_DECREF(denominator); |
| 1485 | denominator = py_exponent; |
| 1486 | py_exponent = NULL; |
| 1487 | } |
| 1488 | |
| 1489 | result_pair = PyTuple_Pack(2, numerator, denominator); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1490 | |
| 1491 | #undef INPLACE_UPDATE |
| 1492 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | Py_XDECREF(py_exponent); |
| 1494 | Py_XDECREF(denominator); |
| 1495 | Py_XDECREF(numerator); |
| 1496 | return result_pair; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | PyDoc_STRVAR(float_as_integer_ratio_doc, |
| 1500 | "float.as_integer_ratio() -> (int, int)\n" |
| 1501 | "\n" |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1502 | "Return a pair of integers, whose ratio is exactly equal to the original\n" |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1503 | "float and with a positive denominator.\n" |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1504 | "Raise OverflowError on infinities and a ValueError on NaNs.\n" |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1505 | "\n" |
| 1506 | ">>> (10.0).as_integer_ratio()\n" |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1507 | "(10, 1)\n" |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1508 | ">>> (0.0).as_integer_ratio()\n" |
| 1509 | "(0, 1)\n" |
| 1510 | ">>> (-.25).as_integer_ratio()\n" |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1511 | "(-1, 4)"); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 1512 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1513 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1514 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1515 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 1516 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1517 | static PyObject * |
| 1518 | float_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | PyObject *x = Py_False; /* Integer zero */ |
| 1521 | static char *kwlist[] = {"x", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | if (type != &PyFloat_Type) |
| 1524 | return float_subtype_new(type, args, kwds); /* Wimp out */ |
| 1525 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x)) |
| 1526 | return NULL; |
| 1527 | /* If it's a string, but not a string subclass, use |
| 1528 | PyFloat_FromString. */ |
| 1529 | if (PyUnicode_CheckExact(x)) |
| 1530 | return PyFloat_FromString(x); |
| 1531 | return PyNumber_Float(x); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1534 | /* Wimpy, slow approach to tp_new calls for subtypes of float: |
| 1535 | first create a regular float from whatever arguments we got, |
| 1536 | then allocate a subtype instance and initialize its ob_fval |
| 1537 | from the regular float. The regular float is then thrown away. |
| 1538 | */ |
| 1539 | static PyObject * |
| 1540 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1541 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | PyObject *tmp, *newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | assert(PyType_IsSubtype(type, &PyFloat_Type)); |
| 1545 | tmp = float_new(&PyFloat_Type, args, kwds); |
| 1546 | if (tmp == NULL) |
| 1547 | return NULL; |
| 1548 | assert(PyFloat_CheckExact(tmp)); |
| 1549 | newobj = type->tp_alloc(type, 0); |
| 1550 | if (newobj == NULL) { |
| 1551 | Py_DECREF(tmp); |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval; |
| 1555 | Py_DECREF(tmp); |
| 1556 | return newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1559 | static PyObject * |
| 1560 | float_getnewargs(PyFloatObject *v) |
| 1561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | return Py_BuildValue("(d)", v->ob_fval); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1565 | /* this is for the benefit of the pack/unpack routines below */ |
| 1566 | |
| 1567 | typedef enum { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | unknown_format, ieee_big_endian_format, ieee_little_endian_format |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1569 | } float_format_type; |
| 1570 | |
| 1571 | static float_format_type double_format, float_format; |
| 1572 | static float_format_type detected_double_format, detected_float_format; |
| 1573 | |
| 1574 | static PyObject * |
| 1575 | float_getformat(PyTypeObject *v, PyObject* arg) |
| 1576 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | char* s; |
| 1578 | float_format_type r; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | if (!PyUnicode_Check(arg)) { |
| 1581 | PyErr_Format(PyExc_TypeError, |
| 1582 | "__getformat__() argument must be string, not %.500s", |
| 1583 | Py_TYPE(arg)->tp_name); |
| 1584 | return NULL; |
| 1585 | } |
| 1586 | s = _PyUnicode_AsString(arg); |
| 1587 | if (s == NULL) |
| 1588 | return NULL; |
| 1589 | if (strcmp(s, "double") == 0) { |
| 1590 | r = double_format; |
| 1591 | } |
| 1592 | else if (strcmp(s, "float") == 0) { |
| 1593 | r = float_format; |
| 1594 | } |
| 1595 | else { |
| 1596 | PyErr_SetString(PyExc_ValueError, |
| 1597 | "__getformat__() argument 1 must be " |
| 1598 | "'double' or 'float'"); |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | |
| 1602 | switch (r) { |
| 1603 | case unknown_format: |
| 1604 | return PyUnicode_FromString("unknown"); |
| 1605 | case ieee_little_endian_format: |
| 1606 | return PyUnicode_FromString("IEEE, little-endian"); |
| 1607 | case ieee_big_endian_format: |
| 1608 | return PyUnicode_FromString("IEEE, big-endian"); |
| 1609 | default: |
| 1610 | Py_FatalError("insane float_format or double_format"); |
| 1611 | return NULL; |
| 1612 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | PyDoc_STRVAR(float_getformat_doc, |
| 1616 | "float.__getformat__(typestr) -> string\n" |
| 1617 | "\n" |
| 1618 | "You probably don't want to use this function. It exists mainly to be\n" |
| 1619 | "used in Python's test suite.\n" |
| 1620 | "\n" |
| 1621 | "typestr must be 'double' or 'float'. This function returns whichever of\n" |
| 1622 | "'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n" |
| 1623 | "format of floating point numbers used by the C type named by typestr."); |
| 1624 | |
| 1625 | static PyObject * |
| 1626 | float_setformat(PyTypeObject *v, PyObject* args) |
| 1627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | char* typestr; |
| 1629 | char* format; |
| 1630 | float_format_type f; |
| 1631 | float_format_type detected; |
| 1632 | float_format_type *p; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1634 | if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format)) |
| 1635 | return NULL; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | if (strcmp(typestr, "double") == 0) { |
| 1638 | p = &double_format; |
| 1639 | detected = detected_double_format; |
| 1640 | } |
| 1641 | else if (strcmp(typestr, "float") == 0) { |
| 1642 | p = &float_format; |
| 1643 | detected = detected_float_format; |
| 1644 | } |
| 1645 | else { |
| 1646 | PyErr_SetString(PyExc_ValueError, |
| 1647 | "__setformat__() argument 1 must " |
| 1648 | "be 'double' or 'float'"); |
| 1649 | return NULL; |
| 1650 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | if (strcmp(format, "unknown") == 0) { |
| 1653 | f = unknown_format; |
| 1654 | } |
| 1655 | else if (strcmp(format, "IEEE, little-endian") == 0) { |
| 1656 | f = ieee_little_endian_format; |
| 1657 | } |
| 1658 | else if (strcmp(format, "IEEE, big-endian") == 0) { |
| 1659 | f = ieee_big_endian_format; |
| 1660 | } |
| 1661 | else { |
| 1662 | PyErr_SetString(PyExc_ValueError, |
| 1663 | "__setformat__() argument 2 must be " |
| 1664 | "'unknown', 'IEEE, little-endian' or " |
| 1665 | "'IEEE, big-endian'"); |
| 1666 | return NULL; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | if (f != unknown_format && f != detected) { |
| 1671 | PyErr_Format(PyExc_ValueError, |
| 1672 | "can only set %s format to 'unknown' or the " |
| 1673 | "detected platform value", typestr); |
| 1674 | return NULL; |
| 1675 | } |
| 1676 | |
| 1677 | *p = f; |
| 1678 | Py_RETURN_NONE; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | PyDoc_STRVAR(float_setformat_doc, |
| 1682 | "float.__setformat__(typestr, fmt) -> None\n" |
| 1683 | "\n" |
| 1684 | "You probably don't want to use this function. It exists mainly to be\n" |
| 1685 | "used in Python's test suite.\n" |
| 1686 | "\n" |
| 1687 | "typestr must be 'double' or 'float'. fmt must be one of 'unknown',\n" |
| 1688 | "'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n" |
| 1689 | "one of the latter two if it appears to match the underlying C reality.\n" |
| 1690 | "\n" |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1691 | "Override the automatic determination of C-level floating point type.\n" |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1692 | "This affects how floats are converted to and from binary strings."); |
| 1693 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 1694 | static PyObject * |
| 1695 | float_getzero(PyObject *v, void *closure) |
| 1696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | return PyFloat_FromDouble(0.0); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1700 | static PyObject * |
| 1701 | float__format__(PyObject *self, PyObject *args) |
| 1702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | PyObject *format_spec; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1704 | _PyUnicodeWriter writer; |
| 1705 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 1708 | return NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1709 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 1710 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1711 | ret = _PyFloat_FormatAdvancedWriter( |
| 1712 | &writer, |
| 1713 | self, |
| 1714 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 1715 | if (ret == -1) { |
| 1716 | _PyUnicodeWriter_Dealloc(&writer); |
| 1717 | return NULL; |
| 1718 | } |
| 1719 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | PyDoc_STRVAR(float__format__doc, |
| 1723 | "float.__format__(format_spec) -> string\n" |
| 1724 | "\n" |
| 1725 | "Formats the float according to format_spec."); |
| 1726 | |
| 1727 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1728 | static PyMethodDef float_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | {"conjugate", (PyCFunction)float_float, METH_NOARGS, |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1730 | "Return self, the complex conjugate of any float."}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | {"__trunc__", (PyCFunction)float_trunc, METH_NOARGS, |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1732 | "Return the Integral closest to x between 0 and x."}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | {"__round__", (PyCFunction)float_round, METH_VARARGS, |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1734 | "Return the Integral closest to x, rounding half toward even.\n" |
| 1735 | "When an argument is passed, work like built-in round(x, ndigits)."}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, |
| 1737 | float_as_integer_ratio_doc}, |
| 1738 | {"fromhex", (PyCFunction)float_fromhex, |
| 1739 | METH_O|METH_CLASS, float_fromhex_doc}, |
| 1740 | {"hex", (PyCFunction)float_hex, |
| 1741 | METH_NOARGS, float_hex_doc}, |
| 1742 | {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1743 | "Return True if the float is an integer."}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1744 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS, |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1746 | "Return True if the float is positive or negative infinite."}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS, |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1748 | "Return True if the float is finite, neither infinite nor NaN."}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS, |
Ezio Melotti | 7760b4e | 2013-10-06 00:45:11 +0300 | [diff] [blame] | 1750 | "Return True if the float is not a number (NaN)."}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1751 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS}, |
| 1753 | {"__getformat__", (PyCFunction)float_getformat, |
| 1754 | METH_O|METH_CLASS, float_getformat_doc}, |
| 1755 | {"__setformat__", (PyCFunction)float_setformat, |
| 1756 | METH_VARARGS|METH_CLASS, float_setformat_doc}, |
| 1757 | {"__format__", (PyCFunction)float__format__, |
| 1758 | METH_VARARGS, float__format__doc}, |
| 1759 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1760 | }; |
| 1761 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 1762 | static PyGetSetDef float_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | {"real", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 1764 | (getter)float_float, (setter)NULL, |
| 1765 | "the real part of a complex number", |
| 1766 | NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | {"imag", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 1768 | (getter)float_getzero, (setter)NULL, |
| 1769 | "the imaginary part of a complex number", |
| 1770 | NULL}, |
| 1771 | {NULL} /* Sentinel */ |
| 1772 | }; |
| 1773 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1774 | PyDoc_STRVAR(float_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1775 | "float(x) -> floating point number\n\ |
| 1776 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1777 | Convert a string or number to a floating point number, if possible."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1778 | |
| 1779 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1780 | static PyNumberMethods float_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | float_add, /*nb_add*/ |
| 1782 | float_sub, /*nb_subtract*/ |
| 1783 | float_mul, /*nb_multiply*/ |
| 1784 | float_rem, /*nb_remainder*/ |
| 1785 | float_divmod, /*nb_divmod*/ |
| 1786 | float_pow, /*nb_power*/ |
| 1787 | (unaryfunc)float_neg, /*nb_negative*/ |
| 1788 | (unaryfunc)float_float, /*nb_positive*/ |
| 1789 | (unaryfunc)float_abs, /*nb_absolute*/ |
| 1790 | (inquiry)float_bool, /*nb_bool*/ |
| 1791 | 0, /*nb_invert*/ |
| 1792 | 0, /*nb_lshift*/ |
| 1793 | 0, /*nb_rshift*/ |
| 1794 | 0, /*nb_and*/ |
| 1795 | 0, /*nb_xor*/ |
| 1796 | 0, /*nb_or*/ |
| 1797 | float_trunc, /*nb_int*/ |
| 1798 | 0, /*nb_reserved*/ |
| 1799 | float_float, /*nb_float*/ |
| 1800 | 0, /* nb_inplace_add */ |
| 1801 | 0, /* nb_inplace_subtract */ |
| 1802 | 0, /* nb_inplace_multiply */ |
| 1803 | 0, /* nb_inplace_remainder */ |
| 1804 | 0, /* nb_inplace_power */ |
| 1805 | 0, /* nb_inplace_lshift */ |
| 1806 | 0, /* nb_inplace_rshift */ |
| 1807 | 0, /* nb_inplace_and */ |
| 1808 | 0, /* nb_inplace_xor */ |
| 1809 | 0, /* nb_inplace_or */ |
| 1810 | float_floor_div, /* nb_floor_divide */ |
| 1811 | float_div, /* nb_true_divide */ |
| 1812 | 0, /* nb_inplace_floor_divide */ |
| 1813 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1814 | }; |
| 1815 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1816 | PyTypeObject PyFloat_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1818 | "float", |
| 1819 | sizeof(PyFloatObject), |
| 1820 | 0, |
| 1821 | (destructor)float_dealloc, /* tp_dealloc */ |
| 1822 | 0, /* tp_print */ |
| 1823 | 0, /* tp_getattr */ |
| 1824 | 0, /* tp_setattr */ |
| 1825 | 0, /* tp_reserved */ |
| 1826 | (reprfunc)float_repr, /* tp_repr */ |
| 1827 | &float_as_number, /* tp_as_number */ |
| 1828 | 0, /* tp_as_sequence */ |
| 1829 | 0, /* tp_as_mapping */ |
| 1830 | (hashfunc)float_hash, /* tp_hash */ |
| 1831 | 0, /* tp_call */ |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 1832 | (reprfunc)float_repr, /* tp_str */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1834 | 0, /* tp_setattro */ |
| 1835 | 0, /* tp_as_buffer */ |
| 1836 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1837 | float_doc, /* tp_doc */ |
| 1838 | 0, /* tp_traverse */ |
| 1839 | 0, /* tp_clear */ |
| 1840 | float_richcompare, /* tp_richcompare */ |
| 1841 | 0, /* tp_weaklistoffset */ |
| 1842 | 0, /* tp_iter */ |
| 1843 | 0, /* tp_iternext */ |
| 1844 | float_methods, /* tp_methods */ |
| 1845 | 0, /* tp_members */ |
| 1846 | float_getset, /* tp_getset */ |
| 1847 | 0, /* tp_base */ |
| 1848 | 0, /* tp_dict */ |
| 1849 | 0, /* tp_descr_get */ |
| 1850 | 0, /* tp_descr_set */ |
| 1851 | 0, /* tp_dictoffset */ |
| 1852 | 0, /* tp_init */ |
| 1853 | 0, /* tp_alloc */ |
| 1854 | float_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1855 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1856 | |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 1857 | int |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1858 | _PyFloat_Init(void) |
| 1859 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | /* We attempt to determine if this machine is using IEEE |
| 1861 | floating point formats by peering at the bits of some |
| 1862 | carefully chosen values. If it looks like we are on an |
| 1863 | IEEE platform, the float packing/unpacking routines can |
| 1864 | just copy bits, if not they resort to arithmetic & shifts |
| 1865 | and masks. The shifts & masks approach works on all finite |
| 1866 | values, but what happens to infinities, NaNs and signed |
| 1867 | zeroes on packing is an accident, and attempting to unpack |
| 1868 | a NaN or an infinity will raise an exception. |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | Note that if we're on some whacked-out platform which uses |
| 1871 | IEEE formats but isn't strictly little-endian or big- |
| 1872 | endian, we will fall back to the portable shifts & masks |
| 1873 | method. */ |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1874 | |
| 1875 | #if SIZEOF_DOUBLE == 8 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | { |
| 1877 | double x = 9006104071832581.0; |
| 1878 | if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0) |
| 1879 | detected_double_format = ieee_big_endian_format; |
| 1880 | else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0) |
| 1881 | detected_double_format = ieee_little_endian_format; |
| 1882 | else |
| 1883 | detected_double_format = unknown_format; |
| 1884 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1885 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | detected_double_format = unknown_format; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1887 | #endif |
| 1888 | |
| 1889 | #if SIZEOF_FLOAT == 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | { |
| 1891 | float y = 16711938.0; |
| 1892 | if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0) |
| 1893 | detected_float_format = ieee_big_endian_format; |
| 1894 | else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0) |
| 1895 | detected_float_format = ieee_little_endian_format; |
| 1896 | else |
| 1897 | detected_float_format = unknown_format; |
| 1898 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1899 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | detected_float_format = unknown_format; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1901 | #endif |
| 1902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | double_format = detected_double_format; |
| 1904 | float_format = detected_float_format; |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 1905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | /* Init float info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 1907 | if (FloatInfoType.tp_name == NULL) { |
| 1908 | if (PyStructSequence_InitType2(&FloatInfoType, &floatinfo_desc) < 0) |
| 1909 | return 0; |
| 1910 | } |
| 1911 | return 1; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 1914 | int |
| 1915 | PyFloat_ClearFreeList(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1916 | { |
Kristján Valur Jónsson | daa0654 | 2012-03-30 09:18:15 +0000 | [diff] [blame] | 1917 | PyFloatObject *f = free_list, *next; |
| 1918 | int i = numfree; |
| 1919 | while (f) { |
| 1920 | next = (PyFloatObject*) Py_TYPE(f); |
| 1921 | PyObject_FREE(f); |
| 1922 | f = next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | } |
Kristján Valur Jónsson | daa0654 | 2012-03-30 09:18:15 +0000 | [diff] [blame] | 1924 | free_list = NULL; |
| 1925 | numfree = 0; |
| 1926 | return i; |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | void |
| 1930 | PyFloat_Fini(void) |
| 1931 | { |
Kristján Valur Jónsson | daa0654 | 2012-03-30 09:18:15 +0000 | [diff] [blame] | 1932 | (void)PyFloat_ClearFreeList(); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1933 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1934 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1935 | /* Print summary info about the state of the optimized allocator */ |
| 1936 | void |
| 1937 | _PyFloat_DebugMallocStats(FILE *out) |
| 1938 | { |
| 1939 | _PyDebugAllocatorStats(out, |
| 1940 | "free PyFloatObject", |
| 1941 | numfree, sizeof(PyFloatObject)); |
| 1942 | } |
| 1943 | |
| 1944 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1945 | /*---------------------------------------------------------------------------- |
| 1946 | * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h. |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1947 | */ |
| 1948 | int |
| 1949 | _PyFloat_Pack4(double x, unsigned char *p, int le) |
| 1950 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | if (float_format == unknown_format) { |
| 1952 | unsigned char sign; |
| 1953 | int e; |
| 1954 | double f; |
| 1955 | unsigned int fbits; |
| 1956 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | if (le) { |
| 1959 | p += 3; |
| 1960 | incr = -1; |
| 1961 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | if (x < 0) { |
| 1964 | sign = 1; |
| 1965 | x = -x; |
| 1966 | } |
| 1967 | else |
| 1968 | sign = 0; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | f = frexp(x, &e); |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 1973 | if (0.5 <= f && f < 1.0) { |
| 1974 | f *= 2.0; |
| 1975 | e--; |
| 1976 | } |
| 1977 | else if (f == 0.0) |
| 1978 | e = 0; |
| 1979 | else { |
| 1980 | PyErr_SetString(PyExc_SystemError, |
| 1981 | "frexp() result out of range"); |
| 1982 | return -1; |
| 1983 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1985 | if (e >= 128) |
| 1986 | goto Overflow; |
| 1987 | else if (e < -126) { |
| 1988 | /* Gradual underflow */ |
| 1989 | f = ldexp(f, 126 + e); |
| 1990 | e = 0; |
| 1991 | } |
| 1992 | else if (!(e == 0 && f == 0.0)) { |
| 1993 | e += 127; |
| 1994 | f -= 1.0; /* Get rid of leading 1 */ |
| 1995 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | f *= 8388608.0; /* 2**23 */ |
| 1998 | fbits = (unsigned int)(f + 0.5); /* Round */ |
| 1999 | assert(fbits <= 8388608); |
| 2000 | if (fbits >> 23) { |
| 2001 | /* The carry propagated out of a string of 23 1 bits. */ |
| 2002 | fbits = 0; |
| 2003 | ++e; |
| 2004 | if (e >= 255) |
| 2005 | goto Overflow; |
| 2006 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | /* First byte */ |
| 2009 | *p = (sign << 7) | (e >> 1); |
| 2010 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | /* Second byte */ |
| 2013 | *p = (char) (((e & 1) << 7) | (fbits >> 16)); |
| 2014 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | /* Third byte */ |
| 2017 | *p = (fbits >> 8) & 0xFF; |
| 2018 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | /* Fourth byte */ |
| 2021 | *p = fbits & 0xFF; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | /* Done */ |
| 2024 | return 0; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | } |
| 2027 | else { |
| 2028 | float y = (float)x; |
| 2029 | const char *s = (char*)&y; |
| 2030 | int i, incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x)) |
| 2033 | goto Overflow; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | if ((float_format == ieee_little_endian_format && !le) |
| 2036 | || (float_format == ieee_big_endian_format && le)) { |
| 2037 | p += 3; |
| 2038 | incr = -1; |
| 2039 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | for (i = 0; i < 4; i++) { |
| 2042 | *p = *s++; |
| 2043 | p += incr; |
| 2044 | } |
| 2045 | return 0; |
| 2046 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2047 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | PyErr_SetString(PyExc_OverflowError, |
| 2049 | "float too large to pack with f format"); |
| 2050 | return -1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | int |
| 2054 | _PyFloat_Pack8(double x, unsigned char *p, int le) |
| 2055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | if (double_format == unknown_format) { |
| 2057 | unsigned char sign; |
| 2058 | int e; |
| 2059 | double f; |
| 2060 | unsigned int fhi, flo; |
| 2061 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | if (le) { |
| 2064 | p += 7; |
| 2065 | incr = -1; |
| 2066 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | if (x < 0) { |
| 2069 | sign = 1; |
| 2070 | x = -x; |
| 2071 | } |
| 2072 | else |
| 2073 | sign = 0; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | f = frexp(x, &e); |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2077 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 2078 | if (0.5 <= f && f < 1.0) { |
| 2079 | f *= 2.0; |
| 2080 | e--; |
| 2081 | } |
| 2082 | else if (f == 0.0) |
| 2083 | e = 0; |
| 2084 | else { |
| 2085 | PyErr_SetString(PyExc_SystemError, |
| 2086 | "frexp() result out of range"); |
| 2087 | return -1; |
| 2088 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | if (e >= 1024) |
| 2091 | goto Overflow; |
| 2092 | else if (e < -1022) { |
| 2093 | /* Gradual underflow */ |
| 2094 | f = ldexp(f, 1022 + e); |
| 2095 | e = 0; |
| 2096 | } |
| 2097 | else if (!(e == 0 && f == 0.0)) { |
| 2098 | e += 1023; |
| 2099 | f -= 1.0; /* Get rid of leading 1 */ |
| 2100 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */ |
| 2103 | f *= 268435456.0; /* 2**28 */ |
| 2104 | fhi = (unsigned int)f; /* Truncate */ |
| 2105 | assert(fhi < 268435456); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | f -= (double)fhi; |
| 2108 | f *= 16777216.0; /* 2**24 */ |
| 2109 | flo = (unsigned int)(f + 0.5); /* Round */ |
| 2110 | assert(flo <= 16777216); |
| 2111 | if (flo >> 24) { |
| 2112 | /* The carry propagated out of a string of 24 1 bits. */ |
| 2113 | flo = 0; |
| 2114 | ++fhi; |
| 2115 | if (fhi >> 28) { |
| 2116 | /* And it also progagated out of the next 28 bits. */ |
| 2117 | fhi = 0; |
| 2118 | ++e; |
| 2119 | if (e >= 2047) |
| 2120 | goto Overflow; |
| 2121 | } |
| 2122 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | /* First byte */ |
| 2125 | *p = (sign << 7) | (e >> 4); |
| 2126 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | /* Second byte */ |
| 2129 | *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24)); |
| 2130 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | /* Third byte */ |
| 2133 | *p = (fhi >> 16) & 0xFF; |
| 2134 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | /* Fourth byte */ |
| 2137 | *p = (fhi >> 8) & 0xFF; |
| 2138 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | /* Fifth byte */ |
| 2141 | *p = fhi & 0xFF; |
| 2142 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | /* Sixth byte */ |
| 2145 | *p = (flo >> 16) & 0xFF; |
| 2146 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | /* Seventh byte */ |
| 2149 | *p = (flo >> 8) & 0xFF; |
| 2150 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | /* Eighth byte */ |
| 2153 | *p = flo & 0xFF; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 2154 | /* p += incr; */ |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | /* Done */ |
| 2157 | return 0; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | Overflow: |
| 2160 | PyErr_SetString(PyExc_OverflowError, |
| 2161 | "float too large to pack with d format"); |
| 2162 | return -1; |
| 2163 | } |
| 2164 | else { |
| 2165 | const char *s = (char*)&x; |
| 2166 | int i, incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | if ((double_format == ieee_little_endian_format && !le) |
| 2169 | || (double_format == ieee_big_endian_format && le)) { |
| 2170 | p += 7; |
| 2171 | incr = -1; |
| 2172 | } |
| 2173 | |
| 2174 | for (i = 0; i < 8; i++) { |
| 2175 | *p = *s++; |
| 2176 | p += incr; |
| 2177 | } |
| 2178 | return 0; |
| 2179 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | double |
| 2183 | _PyFloat_Unpack4(const unsigned char *p, int le) |
| 2184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | if (float_format == unknown_format) { |
| 2186 | unsigned char sign; |
| 2187 | int e; |
| 2188 | unsigned int f; |
| 2189 | double x; |
| 2190 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | if (le) { |
| 2193 | p += 3; |
| 2194 | incr = -1; |
| 2195 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | /* First byte */ |
| 2198 | sign = (*p >> 7) & 1; |
| 2199 | e = (*p & 0x7F) << 1; |
| 2200 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | /* Second byte */ |
| 2203 | e |= (*p >> 7) & 1; |
| 2204 | f = (*p & 0x7F) << 16; |
| 2205 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | if (e == 255) { |
| 2208 | PyErr_SetString( |
| 2209 | PyExc_ValueError, |
| 2210 | "can't unpack IEEE 754 special value " |
| 2211 | "on non-IEEE platform"); |
| 2212 | return -1; |
| 2213 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2215 | /* Third byte */ |
| 2216 | f |= *p << 8; |
| 2217 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | /* Fourth byte */ |
| 2220 | f |= *p; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2222 | x = (double)f / 8388608.0; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | /* XXX This sadly ignores Inf/NaN issues */ |
| 2225 | if (e == 0) |
| 2226 | e = -126; |
| 2227 | else { |
| 2228 | x += 1.0; |
| 2229 | e -= 127; |
| 2230 | } |
| 2231 | x = ldexp(x, e); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | if (sign) |
| 2234 | x = -x; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | return x; |
| 2237 | } |
| 2238 | else { |
| 2239 | float x; |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | if ((float_format == ieee_little_endian_format && !le) |
| 2242 | || (float_format == ieee_big_endian_format && le)) { |
| 2243 | char buf[4]; |
| 2244 | char *d = &buf[3]; |
| 2245 | int i; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | for (i = 0; i < 4; i++) { |
| 2248 | *d-- = *p++; |
| 2249 | } |
| 2250 | memcpy(&x, buf, 4); |
| 2251 | } |
| 2252 | else { |
| 2253 | memcpy(&x, p, 4); |
| 2254 | } |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | return x; |
| 2257 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | double |
| 2261 | _PyFloat_Unpack8(const unsigned char *p, int le) |
| 2262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | if (double_format == unknown_format) { |
| 2264 | unsigned char sign; |
| 2265 | int e; |
| 2266 | unsigned int fhi, flo; |
| 2267 | double x; |
| 2268 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | if (le) { |
| 2271 | p += 7; |
| 2272 | incr = -1; |
| 2273 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | /* First byte */ |
| 2276 | sign = (*p >> 7) & 1; |
| 2277 | e = (*p & 0x7F) << 4; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2279 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | /* Second byte */ |
| 2282 | e |= (*p >> 4) & 0xF; |
| 2283 | fhi = (*p & 0xF) << 24; |
| 2284 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | if (e == 2047) { |
| 2287 | PyErr_SetString( |
| 2288 | PyExc_ValueError, |
| 2289 | "can't unpack IEEE 754 special value " |
| 2290 | "on non-IEEE platform"); |
| 2291 | return -1.0; |
| 2292 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2294 | /* Third byte */ |
| 2295 | fhi |= *p << 16; |
| 2296 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | /* Fourth byte */ |
| 2299 | fhi |= *p << 8; |
| 2300 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | /* Fifth byte */ |
| 2303 | fhi |= *p; |
| 2304 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | /* Sixth byte */ |
| 2307 | flo = *p << 16; |
| 2308 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | /* Seventh byte */ |
| 2311 | flo |= *p << 8; |
| 2312 | p += incr; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | /* Eighth byte */ |
| 2315 | flo |= *p; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 2318 | x /= 268435456.0; /* 2**28 */ |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | if (e == 0) |
| 2321 | e = -1022; |
| 2322 | else { |
| 2323 | x += 1.0; |
| 2324 | e -= 1023; |
| 2325 | } |
| 2326 | x = ldexp(x, e); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | if (sign) |
| 2329 | x = -x; |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | return x; |
| 2332 | } |
| 2333 | else { |
| 2334 | double x; |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 | if ((double_format == ieee_little_endian_format && !le) |
| 2337 | || (double_format == ieee_big_endian_format && le)) { |
| 2338 | char buf[8]; |
| 2339 | char *d = &buf[7]; |
| 2340 | int i; |
| 2341 | |
| 2342 | for (i = 0; i < 8; i++) { |
| 2343 | *d-- = *p++; |
| 2344 | } |
| 2345 | memcpy(&x, buf, 8); |
| 2346 | } |
| 2347 | else { |
| 2348 | memcpy(&x, p, 8); |
| 2349 | } |
| 2350 | |
| 2351 | return x; |
| 2352 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2353 | } |