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