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