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