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