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