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