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