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