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