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