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 | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 8 | #include "structseq.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 11 | #include <float.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 13 | #undef MAX |
| 14 | #undef MIN |
| 15 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 16 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 17 | |
Neal Norwitz | 9fdfaaf | 2008-03-28 05:34:59 +0000 | [diff] [blame] | 18 | #ifdef HAVE_IEEEFP_H |
| 19 | #include <ieeefp.h> |
| 20 | #endif |
| 21 | |
Neal Norwitz | 5f95a79 | 2008-01-25 08:04:16 +0000 | [diff] [blame] | 22 | #ifdef _OSF_SOURCE |
| 23 | /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
| 24 | extern int finite(double); |
| 25 | #endif |
| 26 | |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 27 | /* Special free list -- see comments for same code in intobject.c. */ |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 28 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 29 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 30 | #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject)) |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 32 | struct _floatblock { |
| 33 | struct _floatblock *next; |
| 34 | PyFloatObject objects[N_FLOATOBJECTS]; |
| 35 | }; |
| 36 | |
| 37 | typedef struct _floatblock PyFloatBlock; |
| 38 | |
| 39 | static PyFloatBlock *block_list = NULL; |
| 40 | static PyFloatObject *free_list = NULL; |
| 41 | |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 42 | static PyFloatObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 43 | fill_free_list(void) |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 44 | { |
| 45 | PyFloatObject *p, *q; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 46 | /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */ |
| 47 | p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock)); |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 48 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 49 | return (PyFloatObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 50 | ((PyFloatBlock *)p)->next = block_list; |
| 51 | block_list = (PyFloatBlock *)p; |
| 52 | p = &((PyFloatBlock *)p)->objects[0]; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 53 | q = p + N_FLOATOBJECTS; |
| 54 | while (--q > p) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 55 | Py_TYPE(q) = (struct _typeobject *)(q-1); |
| 56 | Py_TYPE(q) = NULL; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 57 | return p + N_FLOATOBJECTS - 1; |
| 58 | } |
| 59 | |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 60 | double |
| 61 | PyFloat_GetMax(void) |
| 62 | { |
| 63 | return DBL_MAX; |
| 64 | } |
| 65 | |
| 66 | double |
| 67 | PyFloat_GetMin(void) |
| 68 | { |
| 69 | return DBL_MIN; |
| 70 | } |
| 71 | |
Christian Heimes | 796fc31 | 2008-01-30 18:58:29 +0000 | [diff] [blame] | 72 | static PyTypeObject FloatInfoType = {0, 0, 0, 0, 0, 0}; |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 73 | |
| 74 | PyDoc_STRVAR(floatinfo__doc__, |
Benjamin Peterson | bf9ec9b | 2009-06-16 23:13:09 +0000 | [diff] [blame] | 75 | "sys.float_info\n\ |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 76 | \n\ |
| 77 | A structseq holding information about the float type. It contains low level\n\ |
| 78 | information about the precision and internal representation. Please study\n\ |
| 79 | your system's :file:`float.h` for more information."); |
| 80 | |
| 81 | static PyStructSequence_Field floatinfo_fields[] = { |
| 82 | {"max", "DBL_MAX -- maximum representable finite float"}, |
| 83 | {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) " |
| 84 | "is representable"}, |
| 85 | {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e " |
| 86 | "is representable"}, |
| 87 | {"min", "DBL_MIN -- Minimum positive normalizer float"}, |
| 88 | {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) " |
| 89 | "is a normalized float"}, |
| 90 | {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is " |
| 91 | "a normalized"}, |
| 92 | {"dig", "DBL_DIG -- digits"}, |
| 93 | {"mant_dig", "DBL_MANT_DIG -- mantissa digits"}, |
| 94 | {"epsilon", "DBL_EPSILON -- Difference between 1 and the next " |
| 95 | "representable float"}, |
| 96 | {"radix", "FLT_RADIX -- radix of exponent"}, |
| 97 | {"rounds", "FLT_ROUNDS -- addition rounds"}, |
| 98 | {0} |
| 99 | }; |
| 100 | |
| 101 | static PyStructSequence_Desc floatinfo_desc = { |
Benjamin Peterson | bf9ec9b | 2009-06-16 23:13:09 +0000 | [diff] [blame] | 102 | "sys.float_info", /* name */ |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 103 | floatinfo__doc__, /* doc */ |
| 104 | floatinfo_fields, /* fields */ |
| 105 | 11 |
| 106 | }; |
| 107 | |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 108 | PyObject * |
| 109 | PyFloat_GetInfo(void) |
| 110 | { |
Christian Heimes | 796fc31 | 2008-01-30 18:58:29 +0000 | [diff] [blame] | 111 | PyObject* floatinfo; |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 112 | int pos = 0; |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 113 | |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 114 | floatinfo = PyStructSequence_New(&FloatInfoType); |
| 115 | if (floatinfo == NULL) { |
| 116 | return NULL; |
| 117 | } |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 118 | |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 119 | #define SetIntFlag(flag) \ |
| 120 | PyStructSequence_SET_ITEM(floatinfo, pos++, PyInt_FromLong(flag)) |
| 121 | #define SetDblFlag(flag) \ |
| 122 | PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag)) |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 123 | |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 124 | SetDblFlag(DBL_MAX); |
| 125 | SetIntFlag(DBL_MAX_EXP); |
| 126 | SetIntFlag(DBL_MAX_10_EXP); |
| 127 | SetDblFlag(DBL_MIN); |
| 128 | SetIntFlag(DBL_MIN_EXP); |
| 129 | SetIntFlag(DBL_MIN_10_EXP); |
| 130 | SetIntFlag(DBL_DIG); |
| 131 | SetIntFlag(DBL_MANT_DIG); |
| 132 | SetDblFlag(DBL_EPSILON); |
| 133 | SetIntFlag(FLT_RADIX); |
| 134 | SetIntFlag(FLT_ROUNDS); |
| 135 | #undef SetIntFlag |
| 136 | #undef SetDblFlag |
| 137 | |
| 138 | if (PyErr_Occurred()) { |
| 139 | Py_CLEAR(floatinfo); |
| 140 | return NULL; |
| 141 | } |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 142 | return floatinfo; |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | PyFloat_FromDouble(double fval) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | { |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 148 | register PyFloatObject *op; |
| 149 | if (free_list == NULL) { |
| 150 | if ((free_list = fill_free_list()) == NULL) |
| 151 | return NULL; |
| 152 | } |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 153 | /* Inline PyObject_New */ |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 154 | op = free_list; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 155 | free_list = (PyFloatObject *)Py_TYPE(op); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 156 | PyObject_INIT(op, &PyFloat_Type); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 157 | op->ob_fval = fval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 158 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 161 | /************************************************************************** |
| 162 | RED_FLAG 22-Sep-2000 tim |
| 163 | PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG, |
| 164 | |
| 165 | 1. If v was a regular string, *pend was set to point to its terminating |
| 166 | null byte. That's useless (the caller can find that without any |
| 167 | help from this function!). |
| 168 | |
| 169 | 2. If v was a Unicode string, or an object convertible to a character |
| 170 | buffer, *pend was set to point into stack trash (the auto temp |
| 171 | vector holding the character buffer). That was downright dangerous. |
| 172 | |
| 173 | Since we can't change the interface of a public API function, pend is |
| 174 | still supported but now *officially* useless: if pend is not NULL, |
| 175 | *pend is set to NULL. |
| 176 | **************************************************************************/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 177 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 178 | PyFloat_FromString(PyObject *v, char **pend) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 179 | { |
Mark Dickinson | 6d6b220 | 2009-04-26 16:04:05 +0000 | [diff] [blame] | 180 | const char *s, *last, *end; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 181 | double x; |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 182 | char buffer[256]; /* for errors */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 183 | #ifdef Py_USING_UNICODE |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 184 | char *s_buffer = NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 185 | #endif |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 186 | Py_ssize_t len; |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 187 | PyObject *result = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 188 | |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 189 | if (pend) |
| 190 | *pend = NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 191 | if (PyString_Check(v)) { |
| 192 | s = PyString_AS_STRING(v); |
| 193 | len = PyString_GET_SIZE(v); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 194 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 195 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 196 | else if (PyUnicode_Check(v)) { |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 197 | s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1); |
| 198 | if (s_buffer == NULL) |
| 199 | return PyErr_NoMemory(); |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 200 | if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 201 | PyUnicode_GET_SIZE(v), |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 202 | s_buffer, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 203 | NULL)) |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 204 | goto error; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 205 | s = s_buffer; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 206 | len = strlen(s); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 207 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 208 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 209 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
| 210 | PyErr_SetString(PyExc_TypeError, |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 211 | "float() argument must be a string or a number"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 212 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 213 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 214 | last = s + len; |
Mark Dickinson | 6d6b220 | 2009-04-26 16:04:05 +0000 | [diff] [blame] | 215 | |
Mark Dickinson | 777e4ff | 2009-05-03 20:59:48 +0000 | [diff] [blame] | 216 | while (Py_ISSPACE(*s)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 217 | s++; |
Mark Dickinson | 6d6b220 | 2009-04-26 16:04:05 +0000 | [diff] [blame] | 218 | /* We don't care about overflow or underflow. If the platform |
| 219 | * supports them, infinities and signed zeroes (on underflow) are |
| 220 | * fine. */ |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 221 | x = PyOS_string_to_double(s, (char **)&end, NULL); |
| 222 | if (x == -1.0 && PyErr_Occurred()) |
| 223 | goto error; |
Mark Dickinson | 777e4ff | 2009-05-03 20:59:48 +0000 | [diff] [blame] | 224 | while (Py_ISSPACE(*end)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 225 | end++; |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 226 | if (end == last) |
| 227 | result = PyFloat_FromDouble(x); |
| 228 | else { |
| 229 | PyOS_snprintf(buffer, sizeof(buffer), |
| 230 | "invalid literal for float(): %.200s", s); |
| 231 | PyErr_SetString(PyExc_ValueError, buffer); |
| 232 | result = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 233 | } |
Mark Dickinson | 8568b19 | 2009-10-26 21:11:20 +0000 | [diff] [blame] | 234 | |
| 235 | error: |
| 236 | #ifdef Py_USING_UNICODE |
| 237 | if (s_buffer) |
| 238 | PyMem_FREE(s_buffer); |
| 239 | #endif |
| 240 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 243 | static void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 244 | float_dealloc(PyFloatObject *op) |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 246 | if (PyFloat_CheckExact(op)) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 247 | Py_TYPE(op) = (struct _typeobject *)free_list; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 248 | free_list = op; |
| 249 | } |
| 250 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 251 | Py_TYPE(op)->tp_free((PyObject *)op); |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 254 | double |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 255 | PyFloat_AsDouble(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 256 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 257 | PyNumberMethods *nb; |
| 258 | PyFloatObject *fo; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | double val; |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 261 | if (op && PyFloat_Check(op)) |
| 262 | return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 263 | |
Neil Schemenauer | 2c77e90 | 2002-11-18 16:06:21 +0000 | [diff] [blame] | 264 | if (op == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 265 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 266 | return -1; |
| 267 | } |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 268 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 269 | 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] | 270 | PyErr_SetString(PyExc_TypeError, "a float is required"); |
| 271 | return -1; |
| 272 | } |
| 273 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 274 | fo = (PyFloatObject*) (*nb->nb_float) (op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | if (fo == NULL) |
| 276 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 277 | if (!PyFloat_Check(fo)) { |
| 278 | PyErr_SetString(PyExc_TypeError, |
| 279 | "nb_float should return float object"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | return -1; |
| 281 | } |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 283 | val = PyFloat_AS_DOUBLE(fo); |
| 284 | Py_DECREF(fo); |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* Methods */ |
| 290 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 291 | /* Macro and helper that convert PyObject obj to a C double and store |
| 292 | the value in dbl; this replaces the functionality of the coercion |
Tim Peters | 77d8a4f | 2001-12-11 20:31:34 +0000 | [diff] [blame] | 293 | slot function. If conversion to double raises an exception, obj is |
| 294 | set to NULL, and the function invoking this macro returns NULL. If |
| 295 | obj is not of float, int or long type, Py_NotImplemented is incref'ed, |
| 296 | stored in obj, and returned from the function invoking this macro. |
| 297 | */ |
Raymond Hettinger | 9c437af | 2008-06-24 22:46:07 +0000 | [diff] [blame] | 298 | #define CONVERT_TO_DOUBLE(obj, dbl) \ |
| 299 | if (PyFloat_Check(obj)) \ |
| 300 | dbl = PyFloat_AS_DOUBLE(obj); \ |
| 301 | else if (convert_to_double(&(obj), &(dbl)) < 0) \ |
| 302 | return obj; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 303 | |
| 304 | static int |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 305 | convert_to_double(PyObject **v, double *dbl) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 306 | { |
| 307 | register PyObject *obj = *v; |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 308 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 309 | if (PyInt_Check(obj)) { |
| 310 | *dbl = (double)PyInt_AS_LONG(obj); |
| 311 | } |
| 312 | else if (PyLong_Check(obj)) { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 313 | *dbl = PyLong_AsDouble(obj); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 314 | if (*dbl == -1.0 && PyErr_Occurred()) { |
| 315 | *v = NULL; |
| 316 | return -1; |
| 317 | } |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 318 | } |
| 319 | else { |
| 320 | Py_INCREF(Py_NotImplemented); |
| 321 | *v = Py_NotImplemented; |
| 322 | return -1; |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 327 | /* XXX PyFloat_AsString and PyFloat_AsReprString are deprecated: |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 328 | XXX they pass a char buffer without passing a length. |
| 329 | */ |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 330 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 331 | PyFloat_AsString(char *buf, PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 332 | { |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 333 | char *tmp = PyOS_double_to_string(v->ob_fval, 'g', |
| 334 | PyFloat_STR_PRECISION, |
| 335 | Py_DTSF_ADD_DOT_0, NULL); |
| 336 | strcpy(buf, tmp); |
| 337 | PyMem_Free(tmp); |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 340 | void |
| 341 | PyFloat_AsReprString(char *buf, PyFloatObject *v) |
| 342 | { |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 343 | char * tmp = PyOS_double_to_string(v->ob_fval, 'r', 0, |
| 344 | Py_DTSF_ADD_DOT_0, NULL); |
| 345 | strcpy(buf, tmp); |
| 346 | PyMem_Free(tmp); |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 349 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 350 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 351 | float_print(PyFloatObject *v, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | { |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 353 | char *buf; |
| 354 | if (flags & Py_PRINT_RAW) |
| 355 | buf = PyOS_double_to_string(v->ob_fval, |
| 356 | 'g', PyFloat_STR_PRECISION, |
| 357 | Py_DTSF_ADD_DOT_0, NULL); |
| 358 | else |
| 359 | buf = PyOS_double_to_string(v->ob_fval, |
| 360 | 'r', 0, Py_DTSF_ADD_DOT_0, NULL); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 361 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | fputs(buf, fp); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 363 | Py_END_ALLOW_THREADS |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 364 | PyMem_Free(buf); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 365 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 368 | static PyObject * |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 369 | float_str_or_repr(PyFloatObject *v, int precision, char format_code) |
| 370 | { |
| 371 | PyObject *result; |
| 372 | char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), |
| 373 | format_code, precision, |
| 374 | Py_DTSF_ADD_DOT_0, |
| 375 | NULL); |
| 376 | if (!buf) |
| 377 | return PyErr_NoMemory(); |
| 378 | result = PyString_FromString(buf); |
| 379 | PyMem_Free(buf); |
| 380 | return result; |
| 381 | } |
| 382 | |
| 383 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 384 | float_repr(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 385 | { |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 386 | return float_str_or_repr(v, 0, 'r'); |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 390 | float_str(PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 391 | { |
Eric Smith | cfaf79c | 2009-10-26 14:48:55 +0000 | [diff] [blame] | 392 | return float_str_or_repr(v, PyFloat_STR_PRECISION, 'g'); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 395 | /* Comparison is pretty much a nightmare. When comparing float to float, |
| 396 | * we do it as straightforwardly (and long-windedly) as conceivable, so |
| 397 | * that, e.g., Python x == y delivers the same result as the platform |
| 398 | * C x == y when x and/or y is a NaN. |
| 399 | * When mixing float with an integer type, there's no good *uniform* approach. |
| 400 | * Converting the double to an integer obviously doesn't work, since we |
| 401 | * may lose info from fractional bits. Converting the integer to a double |
| 402 | * also has two failure modes: (1) a long int may trigger overflow (too |
| 403 | * large to fit in the dynamic range of a C double); (2) even a C long may have |
| 404 | * more bits than fit in a C double (e.g., on a a 64-bit box long may have |
| 405 | * 63 bits of precision, but a C double probably has only 53), and then |
| 406 | * we can falsely claim equality when low-order integer bits are lost by |
| 407 | * coercion to double. So this part is painful too. |
| 408 | */ |
| 409 | |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 410 | static PyObject* |
| 411 | float_richcompare(PyObject *v, PyObject *w, int op) |
| 412 | { |
| 413 | double i, j; |
| 414 | int r = 0; |
| 415 | |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 416 | assert(PyFloat_Check(v)); |
| 417 | i = PyFloat_AS_DOUBLE(v); |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 418 | |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 419 | /* Switch on the type of w. Set i and j to doubles to be compared, |
| 420 | * and op to the richcomp to use. |
| 421 | */ |
| 422 | if (PyFloat_Check(w)) |
| 423 | j = PyFloat_AS_DOUBLE(w); |
| 424 | |
Kristján Valur Jónsson | f94323f | 2006-05-25 15:53:30 +0000 | [diff] [blame] | 425 | else if (!Py_IS_FINITE(i)) { |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 426 | if (PyInt_Check(w) || PyLong_Check(w)) |
Tim Peters | e1c69b3 | 2004-09-23 19:22:41 +0000 | [diff] [blame] | 427 | /* If i is an infinity, its magnitude exceeds any |
| 428 | * finite integer, so it doesn't matter which int we |
| 429 | * compare i with. If i is a NaN, similarly. |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 430 | */ |
| 431 | j = 0.0; |
| 432 | else |
| 433 | goto Unimplemented; |
| 434 | } |
| 435 | |
| 436 | else if (PyInt_Check(w)) { |
| 437 | long jj = PyInt_AS_LONG(w); |
| 438 | /* In the worst realistic case I can imagine, C double is a |
| 439 | * Cray single with 48 bits of precision, and long has 64 |
| 440 | * bits. |
| 441 | */ |
Tim Peters | e1c69b3 | 2004-09-23 19:22:41 +0000 | [diff] [blame] | 442 | #if SIZEOF_LONG > 6 |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 443 | unsigned long abs = (unsigned long)(jj < 0 ? -jj : jj); |
| 444 | if (abs >> 48) { |
| 445 | /* Needs more than 48 bits. Make it take the |
| 446 | * PyLong path. |
| 447 | */ |
| 448 | PyObject *result; |
| 449 | PyObject *ww = PyLong_FromLong(jj); |
| 450 | |
| 451 | if (ww == NULL) |
| 452 | return NULL; |
| 453 | result = float_richcompare(v, ww, op); |
| 454 | Py_DECREF(ww); |
| 455 | return result; |
| 456 | } |
| 457 | #endif |
| 458 | j = (double)jj; |
| 459 | assert((long)j == jj); |
| 460 | } |
| 461 | |
| 462 | else if (PyLong_Check(w)) { |
| 463 | int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1; |
| 464 | int wsign = _PyLong_Sign(w); |
| 465 | size_t nbits; |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 466 | int exponent; |
| 467 | |
| 468 | if (vsign != wsign) { |
| 469 | /* Magnitudes are irrelevant -- the signs alone |
| 470 | * determine the outcome. |
| 471 | */ |
| 472 | i = (double)vsign; |
| 473 | j = (double)wsign; |
| 474 | goto Compare; |
| 475 | } |
| 476 | /* The signs are the same. */ |
| 477 | /* Convert w to a double if it fits. In particular, 0 fits. */ |
| 478 | nbits = _PyLong_NumBits(w); |
| 479 | if (nbits == (size_t)-1 && PyErr_Occurred()) { |
| 480 | /* This long is so large that size_t isn't big enough |
Tim Peters | e1c69b3 | 2004-09-23 19:22:41 +0000 | [diff] [blame] | 481 | * to hold the # of bits. Replace with little doubles |
| 482 | * that give the same outcome -- w is so large that |
| 483 | * its magnitude must exceed the magnitude of any |
| 484 | * finite float. |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 485 | */ |
| 486 | PyErr_Clear(); |
| 487 | i = (double)vsign; |
| 488 | assert(wsign != 0); |
| 489 | j = wsign * 2.0; |
| 490 | goto Compare; |
| 491 | } |
| 492 | if (nbits <= 48) { |
| 493 | j = PyLong_AsDouble(w); |
| 494 | /* It's impossible that <= 48 bits overflowed. */ |
| 495 | assert(j != -1.0 || ! PyErr_Occurred()); |
| 496 | goto Compare; |
| 497 | } |
| 498 | assert(wsign != 0); /* else nbits was 0 */ |
| 499 | assert(vsign != 0); /* if vsign were 0, then since wsign is |
| 500 | * not 0, we would have taken the |
| 501 | * vsign != wsign branch at the start */ |
| 502 | /* We want to work with non-negative numbers. */ |
| 503 | if (vsign < 0) { |
| 504 | /* "Multiply both sides" by -1; this also swaps the |
| 505 | * comparator. |
| 506 | */ |
| 507 | i = -i; |
| 508 | op = _Py_SwappedOp[op]; |
| 509 | } |
| 510 | assert(i > 0.0); |
Neal Norwitz | b2da01b | 2006-01-08 01:11:25 +0000 | [diff] [blame] | 511 | (void) frexp(i, &exponent); |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 512 | /* exponent is the # of bits in v before the radix point; |
| 513 | * we know that nbits (the # of bits in w) > 48 at this point |
| 514 | */ |
| 515 | if (exponent < 0 || (size_t)exponent < nbits) { |
| 516 | i = 1.0; |
| 517 | j = 2.0; |
| 518 | goto Compare; |
| 519 | } |
| 520 | if ((size_t)exponent > nbits) { |
| 521 | i = 2.0; |
| 522 | j = 1.0; |
| 523 | goto Compare; |
| 524 | } |
| 525 | /* v and w have the same number of bits before the radix |
| 526 | * point. Construct two longs that have the same comparison |
| 527 | * outcome. |
| 528 | */ |
| 529 | { |
| 530 | double fracpart; |
| 531 | double intpart; |
| 532 | PyObject *result = NULL; |
| 533 | PyObject *one = NULL; |
| 534 | PyObject *vv = NULL; |
| 535 | PyObject *ww = w; |
| 536 | |
| 537 | if (wsign < 0) { |
| 538 | ww = PyNumber_Negative(w); |
| 539 | if (ww == NULL) |
| 540 | goto Error; |
| 541 | } |
| 542 | else |
| 543 | Py_INCREF(ww); |
| 544 | |
| 545 | fracpart = modf(i, &intpart); |
| 546 | vv = PyLong_FromDouble(intpart); |
| 547 | if (vv == NULL) |
| 548 | goto Error; |
| 549 | |
| 550 | if (fracpart != 0.0) { |
| 551 | /* Shift left, and or a 1 bit into vv |
| 552 | * to represent the lost fraction. |
| 553 | */ |
| 554 | PyObject *temp; |
| 555 | |
| 556 | one = PyInt_FromLong(1); |
| 557 | if (one == NULL) |
| 558 | goto Error; |
| 559 | |
| 560 | temp = PyNumber_Lshift(ww, one); |
| 561 | if (temp == NULL) |
| 562 | goto Error; |
| 563 | Py_DECREF(ww); |
| 564 | ww = temp; |
| 565 | |
| 566 | temp = PyNumber_Lshift(vv, one); |
| 567 | if (temp == NULL) |
| 568 | goto Error; |
| 569 | Py_DECREF(vv); |
| 570 | vv = temp; |
| 571 | |
| 572 | temp = PyNumber_Or(vv, one); |
| 573 | if (temp == NULL) |
| 574 | goto Error; |
| 575 | Py_DECREF(vv); |
| 576 | vv = temp; |
| 577 | } |
| 578 | |
| 579 | r = PyObject_RichCompareBool(vv, ww, op); |
| 580 | if (r < 0) |
| 581 | goto Error; |
| 582 | result = PyBool_FromLong(r); |
| 583 | Error: |
| 584 | Py_XDECREF(vv); |
| 585 | Py_XDECREF(ww); |
| 586 | Py_XDECREF(one); |
| 587 | return result; |
| 588 | } |
| 589 | } /* else if (PyLong_Check(w)) */ |
| 590 | |
| 591 | else /* w isn't float, int, or long */ |
| 592 | goto Unimplemented; |
| 593 | |
| 594 | Compare: |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 595 | PyFPE_START_PROTECT("richcompare", return NULL) |
| 596 | switch (op) { |
| 597 | case Py_EQ: |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 598 | r = i == j; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 599 | break; |
| 600 | case Py_NE: |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 601 | r = i != j; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 602 | break; |
| 603 | case Py_LE: |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 604 | r = i <= j; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 605 | break; |
| 606 | case Py_GE: |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 607 | r = i >= j; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 608 | break; |
| 609 | case Py_LT: |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 610 | r = i < j; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 611 | break; |
| 612 | case Py_GT: |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 613 | r = i > j; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 614 | break; |
| 615 | } |
Michael W. Hudson | 957f977 | 2004-02-26 12:33:09 +0000 | [diff] [blame] | 616 | PyFPE_END_PROTECT(r) |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 617 | return PyBool_FromLong(r); |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 618 | |
| 619 | Unimplemented: |
| 620 | Py_INCREF(Py_NotImplemented); |
| 621 | return Py_NotImplemented; |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 624 | static long |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 625 | float_hash(PyFloatObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 626 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 627 | return _Py_HashDouble(v->ob_fval); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 630 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 631 | float_add(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 632 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 633 | double a,b; |
| 634 | CONVERT_TO_DOUBLE(v, a); |
| 635 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 636 | PyFPE_START_PROTECT("add", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 637 | a = a + b; |
| 638 | PyFPE_END_PROTECT(a) |
| 639 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 642 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 643 | float_sub(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 644 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 645 | double a,b; |
| 646 | CONVERT_TO_DOUBLE(v, a); |
| 647 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 648 | PyFPE_START_PROTECT("subtract", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 649 | a = a - b; |
| 650 | PyFPE_END_PROTECT(a) |
| 651 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 654 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 655 | float_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 656 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 657 | double a,b; |
| 658 | CONVERT_TO_DOUBLE(v, a); |
| 659 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 660 | PyFPE_START_PROTECT("multiply", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 661 | a = a * b; |
| 662 | PyFPE_END_PROTECT(a) |
| 663 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 666 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 667 | float_div(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 668 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 669 | double a,b; |
| 670 | CONVERT_TO_DOUBLE(v, a); |
| 671 | CONVERT_TO_DOUBLE(w, b); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 672 | #ifdef Py_NAN |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 673 | if (b == 0.0) { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 674 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 675 | "float division"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 676 | return NULL; |
| 677 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 678 | #endif |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 679 | PyFPE_START_PROTECT("divide", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 680 | a = a / b; |
| 681 | PyFPE_END_PROTECT(a) |
| 682 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 685 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 686 | float_classic_div(PyObject *v, PyObject *w) |
| 687 | { |
| 688 | double a,b; |
| 689 | CONVERT_TO_DOUBLE(v, a); |
| 690 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 691 | if (Py_DivisionWarningFlag >= 2 && |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 692 | PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0) |
| 693 | return NULL; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 694 | #ifdef Py_NAN |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 695 | if (b == 0.0) { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 696 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 697 | "float division"); |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 698 | return NULL; |
| 699 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 700 | #endif |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 701 | PyFPE_START_PROTECT("divide", return 0) |
| 702 | a = a / b; |
| 703 | PyFPE_END_PROTECT(a) |
| 704 | return PyFloat_FromDouble(a); |
| 705 | } |
| 706 | |
| 707 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 708 | float_rem(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 709 | { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 710 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 711 | double mod; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 712 | CONVERT_TO_DOUBLE(v, vx); |
| 713 | CONVERT_TO_DOUBLE(w, wx); |
| 714 | #ifdef Py_NAN |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 715 | if (wx == 0.0) { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 716 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 717 | "float modulo"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 718 | return NULL; |
| 719 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 720 | #endif |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 721 | PyFPE_START_PROTECT("modulo", return 0) |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 722 | mod = fmod(vx, wx); |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 723 | /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 724 | 0 if wx < sqrt(smallest nonzero double) */ |
| 725 | if (mod && ((wx < 0) != (mod < 0))) { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 726 | mod += wx; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 727 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 728 | PyFPE_END_PROTECT(mod) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 729 | return PyFloat_FromDouble(mod); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 732 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 733 | float_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 734 | { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 735 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 736 | double div, mod, floordiv; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 737 | CONVERT_TO_DOUBLE(v, vx); |
| 738 | CONVERT_TO_DOUBLE(w, wx); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 739 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 740 | PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 741 | return NULL; |
| 742 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 743 | PyFPE_START_PROTECT("divmod", return 0) |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 744 | mod = fmod(vx, wx); |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 745 | /* fmod is typically exact, so vx-mod is *mathematically* an |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 746 | exact multiple of wx. But this is fp arithmetic, and fp |
| 747 | vx - mod is an approximation; the result is that div may |
| 748 | not be an exact integral value after the division, although |
| 749 | it will always be very close to one. |
| 750 | */ |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 751 | div = (vx - mod) / wx; |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 752 | if (mod) { |
| 753 | /* ensure the remainder has the same sign as the denominator */ |
| 754 | if ((wx < 0) != (mod < 0)) { |
| 755 | mod += wx; |
| 756 | div -= 1.0; |
| 757 | } |
| 758 | } |
| 759 | else { |
| 760 | /* the remainder is zero, and in the presence of signed zeroes |
| 761 | fmod returns different results across platforms; ensure |
| 762 | it has the same sign as the denominator; we'd like to do |
| 763 | "mod = wx * 0.0", but that may get optimized away */ |
Tim Peters | 4e8ab5d | 2001-11-01 23:59:56 +0000 | [diff] [blame] | 764 | mod *= mod; /* hide "mod = +0" from optimizer */ |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 765 | if (wx < 0.0) |
| 766 | mod = -mod; |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 767 | } |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 768 | /* snap quotient to nearest integral value */ |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 769 | if (div) { |
| 770 | floordiv = floor(div); |
| 771 | if (div - floordiv > 0.5) |
| 772 | floordiv += 1.0; |
| 773 | } |
| 774 | else { |
| 775 | /* div is zero - get the same sign as the true quotient */ |
| 776 | div *= div; /* hide "div = +0" from optimizers */ |
| 777 | floordiv = div * vx / wx; /* zero w/ sign of vx/wx */ |
| 778 | } |
| 779 | PyFPE_END_PROTECT(floordiv) |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 780 | return Py_BuildValue("(dd)", floordiv, mod); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 783 | static PyObject * |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 784 | float_floor_div(PyObject *v, PyObject *w) |
| 785 | { |
| 786 | PyObject *t, *r; |
| 787 | |
| 788 | t = float_divmod(v, w); |
Tim Peters | 77d8a4f | 2001-12-11 20:31:34 +0000 | [diff] [blame] | 789 | if (t == NULL || t == Py_NotImplemented) |
| 790 | return t; |
| 791 | assert(PyTuple_CheckExact(t)); |
| 792 | r = PyTuple_GET_ITEM(t, 0); |
| 793 | Py_INCREF(r); |
| 794 | Py_DECREF(t); |
| 795 | return r; |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 799 | float_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 800 | { |
| 801 | double iv, iw, ix; |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 802 | |
| 803 | if ((PyObject *)z != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 804 | PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not " |
Tim Peters | 97f4a33 | 2001-09-05 23:49:24 +0000 | [diff] [blame] | 805 | "allowed unless all arguments are integers"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 806 | return NULL; |
| 807 | } |
| 808 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 809 | CONVERT_TO_DOUBLE(v, iv); |
| 810 | CONVERT_TO_DOUBLE(w, iw); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 811 | |
| 812 | /* Sort out special cases here instead of relying on pow() */ |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 813 | if (iw == 0) { /* v**0 is 1, even 0**0 */ |
Neal Norwitz | 8b267b5 | 2007-05-03 07:20:57 +0000 | [diff] [blame] | 814 | return PyFloat_FromDouble(1.0); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 815 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 816 | if (iv == 0.0) { /* 0**w is error if w<0, else 1 */ |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 817 | if (iw < 0.0) { |
| 818 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 819 | "0.0 cannot be raised to a negative power"); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 820 | return NULL; |
| 821 | } |
| 822 | return PyFloat_FromDouble(0.0); |
| 823 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 824 | if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */ |
| 825 | return PyFloat_FromDouble(1.0); |
| 826 | } |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 827 | if (iv < 0.0) { |
| 828 | /* Whether this is an error is a mess, and bumps into libm |
| 829 | * bugs so we have to figure it out ourselves. |
| 830 | */ |
| 831 | if (iw != floor(iw)) { |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 832 | PyErr_SetString(PyExc_ValueError, "negative number " |
| 833 | "cannot be raised to a fractional power"); |
| 834 | return NULL; |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 835 | } |
| 836 | /* iw is an exact integer, albeit perhaps a very large one. |
| 837 | * -1 raised to an exact integer should never be exceptional. |
| 838 | * Alas, some libms (chiefly glibc as of early 2003) return |
| 839 | * NaN and set EDOM on pow(-1, large_int) if the int doesn't |
| 840 | * happen to be representable in a *C* integer. That's a |
| 841 | * bug; we let that slide in math.pow() (which currently |
| 842 | * reflects all platform accidents), but not for Python's **. |
| 843 | */ |
Kristján Valur Jónsson | f94323f | 2006-05-25 15:53:30 +0000 | [diff] [blame] | 844 | if (iv == -1.0 && Py_IS_FINITE(iw)) { |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 845 | /* Return 1 if iw is even, -1 if iw is odd; there's |
| 846 | * no guarantee that any C integral type is big |
| 847 | * enough to hold iw, so we have to check this |
| 848 | * indirectly. |
| 849 | */ |
| 850 | ix = floor(iw * 0.5) * 2.0; |
| 851 | return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0); |
| 852 | } |
| 853 | /* Else iv != -1.0, and overflow or underflow are possible. |
| 854 | * Unless we're to write pow() ourselves, we have to trust |
| 855 | * the platform to do this correctly. |
| 856 | */ |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 857 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 858 | errno = 0; |
| 859 | PyFPE_START_PROTECT("pow", return NULL) |
| 860 | ix = pow(iv, iw); |
| 861 | PyFPE_END_PROTECT(ix) |
Tim Peters | dc5a508 | 2002-03-09 04:58:24 +0000 | [diff] [blame] | 862 | Py_ADJUST_ERANGE1(ix); |
Alex Martelli | 348dc88 | 2006-08-23 22:17:59 +0000 | [diff] [blame] | 863 | if (errno != 0) { |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 864 | /* We don't expect any errno value other than ERANGE, but |
| 865 | * the range of libm bugs appears unbounded. |
| 866 | */ |
Alex Martelli | 348dc88 | 2006-08-23 22:17:59 +0000 | [diff] [blame] | 867 | PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError : |
| 868 | PyExc_ValueError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 869 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 870 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 871 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 874 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 875 | float_neg(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 876 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 877 | return PyFloat_FromDouble(-v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 880 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 881 | float_abs(PyFloatObject *v) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 882 | { |
Tim Peters | faf0cd2 | 2001-11-01 21:51:15 +0000 | [diff] [blame] | 883 | return PyFloat_FromDouble(fabs(v->ob_fval)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 886 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 887 | float_nonzero(PyFloatObject *v) |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 888 | { |
| 889 | return v->ob_fval != 0.0; |
| 890 | } |
| 891 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 892 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 893 | float_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 894 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 895 | if (PyInt_Check(*pw)) { |
| 896 | long x = PyInt_AsLong(*pw); |
| 897 | *pw = PyFloat_FromDouble((double)x); |
| 898 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 899 | return 0; |
| 900 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 901 | else if (PyLong_Check(*pw)) { |
Neal Norwitz | abcb0c0 | 2003-01-28 19:21:24 +0000 | [diff] [blame] | 902 | double x = PyLong_AsDouble(*pw); |
| 903 | if (x == -1.0 && PyErr_Occurred()) |
| 904 | return -1; |
| 905 | *pw = PyFloat_FromDouble(x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 906 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 907 | return 0; |
| 908 | } |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 909 | else if (PyFloat_Check(*pw)) { |
| 910 | Py_INCREF(*pv); |
| 911 | Py_INCREF(*pw); |
| 912 | return 0; |
| 913 | } |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 914 | return 1; /* Can't do it */ |
| 915 | } |
| 916 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 917 | static PyObject * |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 918 | float_is_integer(PyObject *v) |
| 919 | { |
| 920 | double x = PyFloat_AsDouble(v); |
| 921 | PyObject *o; |
| 922 | |
| 923 | if (x == -1.0 && PyErr_Occurred()) |
| 924 | return NULL; |
| 925 | if (!Py_IS_FINITE(x)) |
| 926 | Py_RETURN_FALSE; |
Mark Dickinson | e81c376 | 2008-05-09 16:14:15 +0000 | [diff] [blame] | 927 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 928 | PyFPE_START_PROTECT("is_integer", return NULL) |
| 929 | o = (floor(x) == x) ? Py_True : Py_False; |
| 930 | PyFPE_END_PROTECT(x) |
| 931 | if (errno != 0) { |
| 932 | PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError : |
| 933 | PyExc_ValueError); |
| 934 | return NULL; |
| 935 | } |
| 936 | Py_INCREF(o); |
| 937 | return o; |
| 938 | } |
| 939 | |
| 940 | #if 0 |
| 941 | static PyObject * |
| 942 | float_is_inf(PyObject *v) |
| 943 | { |
| 944 | double x = PyFloat_AsDouble(v); |
| 945 | if (x == -1.0 && PyErr_Occurred()) |
| 946 | return NULL; |
| 947 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 948 | } |
| 949 | |
| 950 | static PyObject * |
| 951 | float_is_nan(PyObject *v) |
| 952 | { |
| 953 | double x = PyFloat_AsDouble(v); |
| 954 | if (x == -1.0 && PyErr_Occurred()) |
| 955 | return NULL; |
| 956 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 957 | } |
| 958 | |
| 959 | static PyObject * |
| 960 | float_is_finite(PyObject *v) |
| 961 | { |
| 962 | double x = PyFloat_AsDouble(v); |
| 963 | if (x == -1.0 && PyErr_Occurred()) |
| 964 | return NULL; |
| 965 | return PyBool_FromLong((long)Py_IS_FINITE(x)); |
| 966 | } |
| 967 | #endif |
| 968 | |
| 969 | static PyObject * |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 970 | float_trunc(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 971 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 972 | double x = PyFloat_AsDouble(v); |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 973 | double wholepart; /* integral portion of x, rounded toward 0 */ |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 974 | |
| 975 | (void)modf(x, &wholepart); |
Tim Peters | 7d79124 | 2002-11-21 22:26:37 +0000 | [diff] [blame] | 976 | /* Try to get out cheap if this fits in a Python int. The attempt |
| 977 | * to cast to long must be protected, as C doesn't define what |
| 978 | * happens if the double is too big to fit in a long. Some rare |
| 979 | * systems raise an exception then (RISCOS was mentioned as one, |
| 980 | * and someone using a non-default option on Sun also bumped into |
| 981 | * that). Note that checking for >= and <= LONG_{MIN,MAX} would |
| 982 | * still be vulnerable: if a long has more bits of precision than |
| 983 | * a double, casting MIN/MAX to double may yield an approximation, |
| 984 | * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would |
| 985 | * yield true from the C expression wholepart<=LONG_MAX, despite |
| 986 | * that wholepart is actually greater than LONG_MAX. |
| 987 | */ |
| 988 | if (LONG_MIN < wholepart && wholepart < LONG_MAX) { |
| 989 | const long aslong = (long)wholepart; |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 990 | return PyInt_FromLong(aslong); |
Tim Peters | 7d79124 | 2002-11-21 22:26:37 +0000 | [diff] [blame] | 991 | } |
| 992 | return PyLong_FromDouble(wholepart); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 995 | static PyObject * |
Amaury Forgeot d'Arc | d3ffb89 | 2008-09-09 07:24:30 +0000 | [diff] [blame] | 996 | float_long(PyObject *v) |
| 997 | { |
| 998 | double x = PyFloat_AsDouble(v); |
| 999 | return PyLong_FromDouble(x); |
| 1000 | } |
| 1001 | |
Mark Dickinson | bd15a06 | 2009-11-18 19:33:35 +0000 | [diff] [blame] | 1002 | /* _Py_double_round: rounds a finite nonzero double to the closest multiple of |
| 1003 | 10**-ndigits; here ndigits is within reasonable bounds (typically, -308 <= |
| 1004 | ndigits <= 323). Returns a Python float, or sets a Python error and |
| 1005 | returns NULL on failure (OverflowError and memory errors are possible). */ |
| 1006 | |
| 1007 | #ifndef PY_NO_SHORT_FLOAT_REPR |
| 1008 | /* version of _Py_double_round that uses the correctly-rounded string<->double |
| 1009 | conversions from Python/dtoa.c */ |
| 1010 | |
| 1011 | /* FIVE_POW_LIMIT is the largest k such that 5**k is exactly representable as |
| 1012 | a double. Since we're using the code in Python/dtoa.c, it should be safe |
| 1013 | to assume that C doubles are IEEE 754 binary64 format. To be on the safe |
| 1014 | side, we check this. */ |
| 1015 | #if DBL_MANT_DIG == 53 |
| 1016 | #define FIVE_POW_LIMIT 22 |
| 1017 | #else |
| 1018 | #error "C doubles do not appear to be IEEE 754 binary64 format" |
| 1019 | #endif |
| 1020 | |
| 1021 | PyObject * |
| 1022 | _Py_double_round(double x, int ndigits) { |
| 1023 | |
| 1024 | double rounded, m; |
| 1025 | Py_ssize_t buflen, mybuflen=100; |
| 1026 | char *buf, *buf_end, shortbuf[100], *mybuf=shortbuf; |
| 1027 | int decpt, sign, val, halfway_case; |
| 1028 | PyObject *result = NULL; |
| 1029 | |
| 1030 | /* The basic idea is very simple: convert and round the double to a |
| 1031 | decimal string using _Py_dg_dtoa, then convert that decimal string |
| 1032 | back to a double with _Py_dg_strtod. There's one minor difficulty: |
| 1033 | Python 2.x expects round to do round-half-away-from-zero, while |
| 1034 | _Py_dg_dtoa does round-half-to-even. So we need some way to detect |
| 1035 | and correct the halfway cases. |
| 1036 | |
| 1037 | Detection: a halfway value has the form k * 0.5 * 10**-ndigits for |
| 1038 | some odd integer k. Or in other words, a rational number x is |
| 1039 | exactly halfway between two multiples of 10**-ndigits if its |
| 1040 | 2-valuation is exactly -ndigits-1 and its 5-valuation is at least |
| 1041 | -ndigits. For ndigits >= 0 the latter condition is automatically |
| 1042 | satisfied for a binary float x, since any such float has |
| 1043 | nonnegative 5-valuation. For 0 > ndigits >= -22, x needs to be an |
| 1044 | integral multiple of 5**-ndigits; we can check this using fmod. |
| 1045 | For -22 > ndigits, there are no halfway cases: 5**23 takes 54 bits |
| 1046 | to represent exactly, so any odd multiple of 0.5 * 10**n for n >= |
| 1047 | 23 takes at least 54 bits of precision to represent exactly. |
| 1048 | |
| 1049 | Correction: a simple strategy for dealing with halfway cases is to |
| 1050 | (for the halfway cases only) call _Py_dg_dtoa with an argument of |
| 1051 | ndigits+1 instead of ndigits (thus doing an exact conversion to |
| 1052 | decimal), round the resulting string manually, and then convert |
| 1053 | back using _Py_dg_strtod. |
| 1054 | */ |
| 1055 | |
| 1056 | /* nans, infinities and zeros should have already been dealt |
| 1057 | with by the caller (in this case, builtin_round) */ |
| 1058 | assert(Py_IS_FINITE(x) && x != 0.0); |
| 1059 | |
| 1060 | /* find 2-valuation val of x */ |
| 1061 | m = frexp(x, &val); |
| 1062 | while (m != floor(m)) { |
| 1063 | m *= 2.0; |
| 1064 | val--; |
| 1065 | } |
| 1066 | |
| 1067 | /* determine whether this is a halfway case */ |
| 1068 | if (val == -ndigits-1) { |
| 1069 | if (ndigits >= 0) |
| 1070 | halfway_case = 1; |
| 1071 | else if (ndigits >= -FIVE_POW_LIMIT) { |
| 1072 | double five_pow = 1.0; |
| 1073 | int i; |
| 1074 | for (i=0; i < -ndigits; i++) |
| 1075 | five_pow *= 5.0; |
| 1076 | halfway_case = fmod(x, five_pow) == 0.0; |
| 1077 | } |
| 1078 | else |
| 1079 | halfway_case = 0; |
| 1080 | } |
| 1081 | else |
| 1082 | halfway_case = 0; |
| 1083 | |
| 1084 | /* round to a decimal string; use an extra place for halfway case */ |
| 1085 | buf = _Py_dg_dtoa(x, 3, ndigits+halfway_case, &decpt, &sign, &buf_end); |
| 1086 | if (buf == NULL) { |
| 1087 | PyErr_NoMemory(); |
| 1088 | return NULL; |
| 1089 | } |
| 1090 | buflen = buf_end - buf; |
| 1091 | |
| 1092 | /* in halfway case, do the round-half-away-from-zero manually */ |
| 1093 | if (halfway_case) { |
| 1094 | int i, carry; |
| 1095 | /* sanity check: _Py_dg_dtoa should not have stripped |
| 1096 | any zeros from the result: there should be exactly |
| 1097 | ndigits+1 places following the decimal point, and |
| 1098 | the last digit in the buffer should be a '5'.*/ |
| 1099 | assert(buflen - decpt == ndigits+1); |
| 1100 | assert(buf[buflen-1] == '5'); |
| 1101 | |
| 1102 | /* increment and shift right at the same time. */ |
| 1103 | decpt += 1; |
| 1104 | carry = 1; |
| 1105 | for (i=buflen-1; i-- > 0;) { |
| 1106 | carry += buf[i] - '0'; |
| 1107 | buf[i+1] = carry % 10 + '0'; |
| 1108 | carry /= 10; |
| 1109 | } |
| 1110 | buf[0] = carry + '0'; |
| 1111 | } |
| 1112 | |
| 1113 | /* Get new buffer if shortbuf is too small. Space needed <= buf_end - |
| 1114 | buf + 8: (1 extra for '0', 1 for sign, 5 for exp, 1 for '\0'). */ |
| 1115 | if (buflen + 8 > mybuflen) { |
| 1116 | mybuflen = buflen+8; |
| 1117 | mybuf = (char *)PyMem_Malloc(mybuflen); |
| 1118 | if (mybuf == NULL) { |
| 1119 | PyErr_NoMemory(); |
| 1120 | goto exit; |
| 1121 | } |
| 1122 | } |
| 1123 | /* copy buf to mybuf, adding exponent, sign and leading 0 */ |
| 1124 | PyOS_snprintf(mybuf, mybuflen, "%s0%se%d", (sign ? "-" : ""), |
| 1125 | buf, decpt - (int)buflen); |
| 1126 | |
| 1127 | /* and convert the resulting string back to a double */ |
| 1128 | errno = 0; |
| 1129 | rounded = _Py_dg_strtod(mybuf, NULL); |
| 1130 | if (errno == ERANGE && fabs(rounded) >= 1.) |
| 1131 | PyErr_SetString(PyExc_OverflowError, |
| 1132 | "rounded value too large to represent"); |
| 1133 | else |
| 1134 | result = PyFloat_FromDouble(rounded); |
| 1135 | |
| 1136 | /* done computing value; now clean up */ |
| 1137 | if (mybuf != shortbuf) |
| 1138 | PyMem_Free(mybuf); |
| 1139 | exit: |
| 1140 | _Py_dg_freedtoa(buf); |
| 1141 | return result; |
| 1142 | } |
| 1143 | |
| 1144 | #undef FIVE_POW_LIMIT |
| 1145 | |
| 1146 | #else /* PY_NO_SHORT_FLOAT_REPR */ |
| 1147 | |
| 1148 | /* fallback version, to be used when correctly rounded binary<->decimal |
| 1149 | conversions aren't available */ |
| 1150 | |
| 1151 | PyObject * |
| 1152 | _Py_double_round(double x, int ndigits) { |
| 1153 | double pow1, pow2, y, z; |
| 1154 | if (ndigits >= 0) { |
| 1155 | if (ndigits > 22) { |
| 1156 | /* pow1 and pow2 are each safe from overflow, but |
| 1157 | pow1*pow2 ~= pow(10.0, ndigits) might overflow */ |
| 1158 | pow1 = pow(10.0, (double)(ndigits-22)); |
| 1159 | pow2 = 1e22; |
| 1160 | } |
| 1161 | else { |
| 1162 | pow1 = pow(10.0, (double)ndigits); |
| 1163 | pow2 = 1.0; |
| 1164 | } |
| 1165 | y = (x*pow1)*pow2; |
| 1166 | /* if y overflows, then rounded value is exactly x */ |
| 1167 | if (!Py_IS_FINITE(y)) |
| 1168 | return PyFloat_FromDouble(x); |
| 1169 | } |
| 1170 | else { |
| 1171 | pow1 = pow(10.0, (double)-ndigits); |
| 1172 | pow2 = 1.0; /* unused; silences a gcc compiler warning */ |
| 1173 | y = x / pow1; |
| 1174 | } |
| 1175 | |
| 1176 | z = round(y); |
| 1177 | if (fabs(y-z) == 0.5) |
| 1178 | /* halfway between two integers; use round-away-from-zero */ |
| 1179 | z = y + copysign(0.5, y); |
| 1180 | |
| 1181 | if (ndigits >= 0) |
| 1182 | z = (z / pow2) / pow1; |
| 1183 | else |
| 1184 | z *= pow1; |
| 1185 | |
| 1186 | /* if computation resulted in overflow, raise OverflowError */ |
| 1187 | if (!Py_IS_FINITE(z)) { |
| 1188 | PyErr_SetString(PyExc_OverflowError, |
| 1189 | "overflow occurred during round"); |
| 1190 | return NULL; |
| 1191 | } |
| 1192 | |
| 1193 | return PyFloat_FromDouble(z); |
| 1194 | } |
| 1195 | |
| 1196 | #endif /* PY_NO_SHORT_FLOAT_REPR */ |
| 1197 | |
Amaury Forgeot d'Arc | d3ffb89 | 2008-09-09 07:24:30 +0000 | [diff] [blame] | 1198 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1199 | float_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1200 | { |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1201 | if (PyFloat_CheckExact(v)) |
| 1202 | Py_INCREF(v); |
| 1203 | else |
| 1204 | v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1205 | return v; |
| 1206 | } |
| 1207 | |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1208 | /* turn ASCII hex characters into integer values and vice versa */ |
| 1209 | |
| 1210 | static char |
| 1211 | char_from_hex(int x) |
| 1212 | { |
| 1213 | assert(0 <= x && x < 16); |
| 1214 | return "0123456789abcdef"[x]; |
| 1215 | } |
| 1216 | |
| 1217 | static int |
| 1218 | hex_from_char(char c) { |
| 1219 | int x; |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1220 | switch(c) { |
| 1221 | case '0': |
| 1222 | x = 0; |
| 1223 | break; |
| 1224 | case '1': |
| 1225 | x = 1; |
| 1226 | break; |
| 1227 | case '2': |
| 1228 | x = 2; |
| 1229 | break; |
| 1230 | case '3': |
| 1231 | x = 3; |
| 1232 | break; |
| 1233 | case '4': |
| 1234 | x = 4; |
| 1235 | break; |
| 1236 | case '5': |
| 1237 | x = 5; |
| 1238 | break; |
| 1239 | case '6': |
| 1240 | x = 6; |
| 1241 | break; |
| 1242 | case '7': |
| 1243 | x = 7; |
| 1244 | break; |
| 1245 | case '8': |
| 1246 | x = 8; |
| 1247 | break; |
| 1248 | case '9': |
| 1249 | x = 9; |
| 1250 | break; |
| 1251 | case 'a': |
| 1252 | case 'A': |
| 1253 | x = 10; |
| 1254 | break; |
| 1255 | case 'b': |
| 1256 | case 'B': |
| 1257 | x = 11; |
| 1258 | break; |
| 1259 | case 'c': |
| 1260 | case 'C': |
| 1261 | x = 12; |
| 1262 | break; |
| 1263 | case 'd': |
| 1264 | case 'D': |
| 1265 | x = 13; |
| 1266 | break; |
| 1267 | case 'e': |
| 1268 | case 'E': |
| 1269 | x = 14; |
| 1270 | break; |
| 1271 | case 'f': |
| 1272 | case 'F': |
| 1273 | x = 15; |
| 1274 | break; |
| 1275 | default: |
| 1276 | x = -1; |
| 1277 | break; |
| 1278 | } |
| 1279 | return x; |
| 1280 | } |
| 1281 | |
| 1282 | /* convert a float to a hexadecimal string */ |
| 1283 | |
| 1284 | /* TOHEX_NBITS is DBL_MANT_DIG rounded up to the next integer |
| 1285 | of the form 4k+1. */ |
| 1286 | #define TOHEX_NBITS DBL_MANT_DIG + 3 - (DBL_MANT_DIG+2)%4 |
| 1287 | |
| 1288 | static PyObject * |
| 1289 | float_hex(PyObject *v) |
| 1290 | { |
| 1291 | double x, m; |
| 1292 | int e, shift, i, si, esign; |
| 1293 | /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the |
| 1294 | trailing NUL byte. */ |
| 1295 | char s[(TOHEX_NBITS-1)/4+3]; |
| 1296 | |
| 1297 | CONVERT_TO_DOUBLE(v, x); |
| 1298 | |
| 1299 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) |
| 1300 | return float_str((PyFloatObject *)v); |
| 1301 | |
| 1302 | if (x == 0.0) { |
| 1303 | if(copysign(1.0, x) == -1.0) |
| 1304 | return PyString_FromString("-0x0.0p+0"); |
| 1305 | else |
| 1306 | return PyString_FromString("0x0.0p+0"); |
| 1307 | } |
| 1308 | |
| 1309 | m = frexp(fabs(x), &e); |
| 1310 | shift = 1 - MAX(DBL_MIN_EXP - e, 0); |
| 1311 | m = ldexp(m, shift); |
| 1312 | e -= shift; |
| 1313 | |
| 1314 | si = 0; |
| 1315 | s[si] = char_from_hex((int)m); |
| 1316 | si++; |
| 1317 | m -= (int)m; |
| 1318 | s[si] = '.'; |
| 1319 | si++; |
| 1320 | for (i=0; i < (TOHEX_NBITS-1)/4; i++) { |
| 1321 | m *= 16.0; |
| 1322 | s[si] = char_from_hex((int)m); |
| 1323 | si++; |
| 1324 | m -= (int)m; |
| 1325 | } |
| 1326 | s[si] = '\0'; |
| 1327 | |
| 1328 | if (e < 0) { |
| 1329 | esign = (int)'-'; |
| 1330 | e = -e; |
| 1331 | } |
| 1332 | else |
| 1333 | esign = (int)'+'; |
| 1334 | |
| 1335 | if (x < 0.0) |
| 1336 | return PyString_FromFormat("-0x%sp%c%d", s, esign, e); |
| 1337 | else |
| 1338 | return PyString_FromFormat("0x%sp%c%d", s, esign, e); |
| 1339 | } |
| 1340 | |
| 1341 | PyDoc_STRVAR(float_hex_doc, |
| 1342 | "float.hex() -> string\n\ |
| 1343 | \n\ |
| 1344 | Return a hexadecimal representation of a floating-point number.\n\ |
| 1345 | >>> (-0.1).hex()\n\ |
| 1346 | '-0x1.999999999999ap-4'\n\ |
| 1347 | >>> 3.14159.hex()\n\ |
| 1348 | '0x1.921f9f01b866ep+1'"); |
| 1349 | |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1350 | /* Case-insensitive locale-independent string match used for nan and inf |
| 1351 | detection. t should be lower-case and null-terminated. Return a nonzero |
| 1352 | result if the first strlen(t) characters of s match t and 0 otherwise. */ |
| 1353 | |
| 1354 | static int |
| 1355 | case_insensitive_match(const char *s, const char *t) |
| 1356 | { |
| 1357 | while(*t && Py_TOLOWER(*s) == *t) { |
| 1358 | s++; |
| 1359 | t++; |
| 1360 | } |
| 1361 | return *t ? 0 : 1; |
| 1362 | } |
| 1363 | |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1364 | /* Convert a hexadecimal string to a float. */ |
| 1365 | |
| 1366 | static PyObject * |
| 1367 | float_fromhex(PyObject *cls, PyObject *arg) |
| 1368 | { |
| 1369 | PyObject *result_as_float, *result; |
| 1370 | double x; |
| 1371 | long exp, top_exp, lsb, key_digit; |
| 1372 | char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end; |
| 1373 | int half_eps, digit, round_up, sign=1; |
| 1374 | Py_ssize_t length, ndigits, fdigits, i; |
| 1375 | |
| 1376 | /* |
| 1377 | * For the sake of simplicity and correctness, we impose an artificial |
| 1378 | * limit on ndigits, the total number of hex digits in the coefficient |
| 1379 | * The limit is chosen to ensure that, writing exp for the exponent, |
| 1380 | * |
| 1381 | * (1) if exp > LONG_MAX/2 then the value of the hex string is |
| 1382 | * guaranteed to overflow (provided it's nonzero) |
| 1383 | * |
| 1384 | * (2) if exp < LONG_MIN/2 then the value of the hex string is |
| 1385 | * guaranteed to underflow to 0. |
| 1386 | * |
| 1387 | * (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of |
| 1388 | * overflow in the calculation of exp and top_exp below. |
| 1389 | * |
| 1390 | * More specifically, ndigits is assumed to satisfy the following |
| 1391 | * inequalities: |
| 1392 | * |
| 1393 | * 4*ndigits <= DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2 |
| 1394 | * 4*ndigits <= LONG_MAX/2 + 1 - DBL_MAX_EXP |
| 1395 | * |
| 1396 | * If either of these inequalities is not satisfied, a ValueError is |
| 1397 | * raised. Otherwise, write x for the value of the hex string, and |
| 1398 | * assume x is nonzero. Then |
| 1399 | * |
| 1400 | * 2**(exp-4*ndigits) <= |x| < 2**(exp+4*ndigits). |
| 1401 | * |
| 1402 | * Now if exp > LONG_MAX/2 then: |
| 1403 | * |
| 1404 | * exp - 4*ndigits >= LONG_MAX/2 + 1 - (LONG_MAX/2 + 1 - DBL_MAX_EXP) |
| 1405 | * = DBL_MAX_EXP |
| 1406 | * |
| 1407 | * so |x| >= 2**DBL_MAX_EXP, which is too large to be stored in C |
| 1408 | * double, so overflows. If exp < LONG_MIN/2, then |
| 1409 | * |
| 1410 | * exp + 4*ndigits <= LONG_MIN/2 - 1 + ( |
| 1411 | * DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2) |
| 1412 | * = DBL_MIN_EXP - DBL_MANT_DIG - 1 |
| 1413 | * |
| 1414 | * and so |x| < 2**(DBL_MIN_EXP-DBL_MANT_DIG-1), hence underflows to 0 |
| 1415 | * when converted to a C double. |
| 1416 | * |
| 1417 | * It's easy to show that if LONG_MIN/2 <= exp <= LONG_MAX/2 then both |
| 1418 | * exp+4*ndigits and exp-4*ndigits are within the range of a long. |
| 1419 | */ |
| 1420 | |
| 1421 | if (PyString_AsStringAndSize(arg, &s, &length)) |
| 1422 | return NULL; |
| 1423 | s_end = s + length; |
| 1424 | |
| 1425 | /******************** |
| 1426 | * Parse the string * |
| 1427 | ********************/ |
| 1428 | |
| 1429 | /* leading whitespace and optional sign */ |
Mark Dickinson | 777e4ff | 2009-05-03 20:59:48 +0000 | [diff] [blame] | 1430 | while (Py_ISSPACE(*s)) |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1431 | s++; |
| 1432 | if (*s == '-') { |
| 1433 | s++; |
| 1434 | sign = -1; |
| 1435 | } |
| 1436 | else if (*s == '+') |
| 1437 | s++; |
| 1438 | |
| 1439 | /* infinities and nans */ |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1440 | if (*s == 'i' || *s == 'I') { |
| 1441 | if (!case_insensitive_match(s+1, "nf")) |
| 1442 | goto parse_error; |
| 1443 | s += 3; |
| 1444 | x = Py_HUGE_VAL; |
| 1445 | if (case_insensitive_match(s, "inity")) |
| 1446 | s += 5; |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1447 | goto finished; |
| 1448 | } |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1449 | if (*s == 'n' || *s == 'N') { |
| 1450 | if (!case_insensitive_match(s+1, "an")) |
| 1451 | goto parse_error; |
| 1452 | s += 3; |
| 1453 | x = Py_NAN; |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1454 | goto finished; |
| 1455 | } |
| 1456 | |
| 1457 | /* [0x] */ |
| 1458 | s_store = s; |
| 1459 | if (*s == '0') { |
| 1460 | s++; |
Mark Dickinson | 777e4ff | 2009-05-03 20:59:48 +0000 | [diff] [blame] | 1461 | if (*s == 'x' || *s == 'X') |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1462 | s++; |
| 1463 | else |
| 1464 | s = s_store; |
| 1465 | } |
| 1466 | |
| 1467 | /* coefficient: <integer> [. <fraction>] */ |
| 1468 | coeff_start = s; |
Mark Dickinson | 5c2bb1a | 2008-08-21 21:38:38 +0000 | [diff] [blame] | 1469 | while (hex_from_char(*s) >= 0) |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1470 | s++; |
| 1471 | s_store = s; |
| 1472 | if (*s == '.') { |
| 1473 | s++; |
Mark Dickinson | 5c2bb1a | 2008-08-21 21:38:38 +0000 | [diff] [blame] | 1474 | while (hex_from_char(*s) >= 0) |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1475 | s++; |
| 1476 | coeff_end = s-1; |
| 1477 | } |
| 1478 | else |
| 1479 | coeff_end = s; |
| 1480 | |
| 1481 | /* ndigits = total # of hex digits; fdigits = # after point */ |
| 1482 | ndigits = coeff_end - coeff_start; |
| 1483 | fdigits = coeff_end - s_store; |
| 1484 | if (ndigits == 0) |
| 1485 | goto parse_error; |
| 1486 | if (ndigits > MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2, |
| 1487 | LONG_MAX/2 + 1 - DBL_MAX_EXP)/4) |
| 1488 | goto insane_length_error; |
| 1489 | |
| 1490 | /* [p <exponent>] */ |
Mark Dickinson | 777e4ff | 2009-05-03 20:59:48 +0000 | [diff] [blame] | 1491 | if (*s == 'p' || *s == 'P') { |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1492 | s++; |
| 1493 | exp_start = s; |
| 1494 | if (*s == '-' || *s == '+') |
| 1495 | s++; |
Mark Dickinson | 5c2bb1a | 2008-08-21 21:38:38 +0000 | [diff] [blame] | 1496 | if (!('0' <= *s && *s <= '9')) |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1497 | goto parse_error; |
| 1498 | s++; |
Mark Dickinson | 5c2bb1a | 2008-08-21 21:38:38 +0000 | [diff] [blame] | 1499 | while ('0' <= *s && *s <= '9') |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1500 | s++; |
| 1501 | exp = strtol(exp_start, NULL, 10); |
| 1502 | } |
| 1503 | else |
| 1504 | exp = 0; |
| 1505 | |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1506 | /* for 0 <= j < ndigits, HEX_DIGIT(j) gives the jth most significant digit */ |
| 1507 | #define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ? \ |
| 1508 | coeff_end-(j) : \ |
| 1509 | coeff_end-1-(j))) |
| 1510 | |
| 1511 | /******************************************* |
| 1512 | * Compute rounded value of the hex string * |
| 1513 | *******************************************/ |
| 1514 | |
| 1515 | /* Discard leading zeros, and catch extreme overflow and underflow */ |
| 1516 | while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0) |
| 1517 | ndigits--; |
| 1518 | if (ndigits == 0 || exp < LONG_MIN/2) { |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1519 | x = 0.0; |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1520 | goto finished; |
| 1521 | } |
| 1522 | if (exp > LONG_MAX/2) |
| 1523 | goto overflow_error; |
| 1524 | |
| 1525 | /* Adjust exponent for fractional part. */ |
| 1526 | exp = exp - 4*((long)fdigits); |
| 1527 | |
| 1528 | /* top_exp = 1 more than exponent of most sig. bit of coefficient */ |
| 1529 | top_exp = exp + 4*((long)ndigits - 1); |
| 1530 | for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2) |
| 1531 | top_exp++; |
| 1532 | |
| 1533 | /* catch almost all nonextreme cases of overflow and underflow here */ |
| 1534 | if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) { |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1535 | x = 0.0; |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1536 | goto finished; |
| 1537 | } |
| 1538 | if (top_exp > DBL_MAX_EXP) |
| 1539 | goto overflow_error; |
| 1540 | |
| 1541 | /* lsb = exponent of least significant bit of the *rounded* value. |
| 1542 | This is top_exp - DBL_MANT_DIG unless result is subnormal. */ |
| 1543 | lsb = MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG; |
| 1544 | |
| 1545 | x = 0.0; |
| 1546 | if (exp >= lsb) { |
| 1547 | /* no rounding required */ |
| 1548 | for (i = ndigits-1; i >= 0; i--) |
| 1549 | x = 16.0*x + HEX_DIGIT(i); |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1550 | x = ldexp(x, (int)(exp)); |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1551 | goto finished; |
| 1552 | } |
| 1553 | /* rounding required. key_digit is the index of the hex digit |
| 1554 | containing the first bit to be rounded away. */ |
| 1555 | half_eps = 1 << (int)((lsb - exp - 1) % 4); |
| 1556 | key_digit = (lsb - exp - 1) / 4; |
| 1557 | for (i = ndigits-1; i > key_digit; i--) |
| 1558 | x = 16.0*x + HEX_DIGIT(i); |
| 1559 | digit = HEX_DIGIT(key_digit); |
| 1560 | x = 16.0*x + (double)(digit & (16-2*half_eps)); |
| 1561 | |
| 1562 | /* round-half-even: round up if bit lsb-1 is 1 and at least one of |
| 1563 | bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */ |
| 1564 | if ((digit & half_eps) != 0) { |
| 1565 | round_up = 0; |
| 1566 | if ((digit & (3*half_eps-1)) != 0 || |
| 1567 | (half_eps == 8 && (HEX_DIGIT(key_digit+1) & 1) != 0)) |
| 1568 | round_up = 1; |
| 1569 | else |
| 1570 | for (i = key_digit-1; i >= 0; i--) |
| 1571 | if (HEX_DIGIT(i) != 0) { |
| 1572 | round_up = 1; |
| 1573 | break; |
| 1574 | } |
| 1575 | if (round_up == 1) { |
| 1576 | x += 2*half_eps; |
| 1577 | if (top_exp == DBL_MAX_EXP && |
| 1578 | x == ldexp((double)(2*half_eps), DBL_MANT_DIG)) |
| 1579 | /* overflow corner case: pre-rounded value < |
| 1580 | 2**DBL_MAX_EXP; rounded=2**DBL_MAX_EXP. */ |
| 1581 | goto overflow_error; |
| 1582 | } |
| 1583 | } |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1584 | x = ldexp(x, (int)(exp+4*key_digit)); |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1585 | |
| 1586 | finished: |
Mark Dickinson | b1d4585 | 2009-05-11 15:33:08 +0000 | [diff] [blame] | 1587 | /* optional trailing whitespace leading to the end of the string */ |
| 1588 | while (Py_ISSPACE(*s)) |
| 1589 | s++; |
| 1590 | if (s != s_end) |
| 1591 | goto parse_error; |
| 1592 | result_as_float = Py_BuildValue("(d)", sign * x); |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1593 | if (result_as_float == NULL) |
| 1594 | return NULL; |
| 1595 | result = PyObject_CallObject(cls, result_as_float); |
| 1596 | Py_DECREF(result_as_float); |
| 1597 | return result; |
| 1598 | |
| 1599 | overflow_error: |
| 1600 | PyErr_SetString(PyExc_OverflowError, |
| 1601 | "hexadecimal value too large to represent as a float"); |
| 1602 | return NULL; |
| 1603 | |
| 1604 | parse_error: |
| 1605 | PyErr_SetString(PyExc_ValueError, |
| 1606 | "invalid hexadecimal floating-point string"); |
| 1607 | return NULL; |
| 1608 | |
| 1609 | insane_length_error: |
| 1610 | PyErr_SetString(PyExc_ValueError, |
| 1611 | "hexadecimal string too long to convert"); |
| 1612 | return NULL; |
| 1613 | } |
| 1614 | |
| 1615 | PyDoc_STRVAR(float_fromhex_doc, |
| 1616 | "float.fromhex(string) -> float\n\ |
| 1617 | \n\ |
| 1618 | Create a floating-point number from a hexadecimal string.\n\ |
| 1619 | >>> float.fromhex('0x1.ffffp10')\n\ |
| 1620 | 2047.984375\n\ |
| 1621 | >>> float.fromhex('-0x1p-1074')\n\ |
| 1622 | -4.9406564584124654e-324"); |
| 1623 | |
| 1624 | |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1625 | static PyObject * |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1626 | float_as_integer_ratio(PyObject *v, PyObject *unused) |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1627 | { |
| 1628 | double self; |
| 1629 | double float_part; |
Raymond Hettinger | 1bcb99a | 2008-02-01 23:12:19 +0000 | [diff] [blame] | 1630 | int exponent; |
Raymond Hettinger | f985903 | 2008-02-01 23:45:44 +0000 | [diff] [blame] | 1631 | int i; |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1632 | |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1633 | PyObject *prev; |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1634 | PyObject *py_exponent = NULL; |
| 1635 | PyObject *numerator = NULL; |
| 1636 | PyObject *denominator = NULL; |
| 1637 | PyObject *result_pair = NULL; |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1638 | PyNumberMethods *long_methods = PyLong_Type.tp_as_number; |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1639 | |
| 1640 | #define INPLACE_UPDATE(obj, call) \ |
| 1641 | prev = obj; \ |
| 1642 | obj = call; \ |
| 1643 | Py_DECREF(prev); \ |
| 1644 | |
| 1645 | CONVERT_TO_DOUBLE(v, self); |
| 1646 | |
| 1647 | if (Py_IS_INFINITY(self)) { |
| 1648 | PyErr_SetString(PyExc_OverflowError, |
| 1649 | "Cannot pass infinity to float.as_integer_ratio."); |
| 1650 | return NULL; |
| 1651 | } |
| 1652 | #ifdef Py_NAN |
| 1653 | if (Py_IS_NAN(self)) { |
| 1654 | PyErr_SetString(PyExc_ValueError, |
Andrew M. Kuchling | bd7c4ca | 2008-10-04 01:02:29 +0000 | [diff] [blame] | 1655 | "Cannot pass NaN to float.as_integer_ratio."); |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1656 | return NULL; |
| 1657 | } |
| 1658 | #endif |
| 1659 | |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1660 | PyFPE_START_PROTECT("as_integer_ratio", goto error); |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1661 | float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */ |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1662 | PyFPE_END_PROTECT(float_part); |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1663 | |
Raymond Hettinger | f985903 | 2008-02-01 23:45:44 +0000 | [diff] [blame] | 1664 | for (i=0; i<300 && float_part != floor(float_part) ; i++) { |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1665 | float_part *= 2.0; |
| 1666 | exponent--; |
Raymond Hettinger | f985903 | 2008-02-01 23:45:44 +0000 | [diff] [blame] | 1667 | } |
Raymond Hettinger | 2d1aa33 | 2008-02-02 05:11:40 +0000 | [diff] [blame] | 1668 | /* self == float_part * 2**exponent exactly and float_part is integral. |
| 1669 | If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part |
| 1670 | to be truncated by PyLong_FromDouble(). */ |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1671 | |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1672 | numerator = PyLong_FromDouble(float_part); |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1673 | if (numerator == NULL) goto error; |
| 1674 | |
Raymond Hettinger | f985903 | 2008-02-01 23:45:44 +0000 | [diff] [blame] | 1675 | /* fold in 2**exponent */ |
Raymond Hettinger | c9e928a | 2008-02-01 22:15:52 +0000 | [diff] [blame] | 1676 | denominator = PyLong_FromLong(1); |
Raymond Hettinger | 1bcb99a | 2008-02-01 23:12:19 +0000 | [diff] [blame] | 1677 | py_exponent = PyLong_FromLong(labs((long)exponent)); |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1678 | if (py_exponent == NULL) goto error; |
| 1679 | INPLACE_UPDATE(py_exponent, |
| 1680 | long_methods->nb_lshift(denominator, py_exponent)); |
| 1681 | if (py_exponent == NULL) goto error; |
| 1682 | if (exponent > 0) { |
| 1683 | INPLACE_UPDATE(numerator, |
Raymond Hettinger | f985903 | 2008-02-01 23:45:44 +0000 | [diff] [blame] | 1684 | long_methods->nb_multiply(numerator, py_exponent)); |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1685 | if (numerator == NULL) goto error; |
| 1686 | } |
| 1687 | else { |
| 1688 | Py_DECREF(denominator); |
| 1689 | denominator = py_exponent; |
| 1690 | py_exponent = NULL; |
| 1691 | } |
| 1692 | |
Raymond Hettinger | c9e928a | 2008-02-01 22:15:52 +0000 | [diff] [blame] | 1693 | /* Returns ints instead of longs where possible */ |
| 1694 | INPLACE_UPDATE(numerator, PyNumber_Int(numerator)); |
| 1695 | if (numerator == NULL) goto error; |
| 1696 | INPLACE_UPDATE(denominator, PyNumber_Int(denominator)); |
| 1697 | if (denominator == NULL) goto error; |
| 1698 | |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1699 | result_pair = PyTuple_Pack(2, numerator, denominator); |
| 1700 | |
| 1701 | #undef INPLACE_UPDATE |
| 1702 | error: |
| 1703 | Py_XDECREF(py_exponent); |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1704 | Py_XDECREF(denominator); |
| 1705 | Py_XDECREF(numerator); |
| 1706 | return result_pair; |
| 1707 | } |
| 1708 | |
| 1709 | PyDoc_STRVAR(float_as_integer_ratio_doc, |
| 1710 | "float.as_integer_ratio() -> (int, int)\n" |
| 1711 | "\n" |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1712 | "Returns a pair of integers, whose ratio is exactly equal to the original\n" |
| 1713 | "float and with a positive denominator.\n" |
Andrew M. Kuchling | bd7c4ca | 2008-10-04 01:02:29 +0000 | [diff] [blame] | 1714 | "Raises OverflowError on infinities and a ValueError on NaNs.\n" |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1715 | "\n" |
| 1716 | ">>> (10.0).as_integer_ratio()\n" |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1717 | "(10, 1)\n" |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1718 | ">>> (0.0).as_integer_ratio()\n" |
| 1719 | "(0, 1)\n" |
| 1720 | ">>> (-.25).as_integer_ratio()\n" |
Raymond Hettinger | 04c96d5 | 2008-02-01 21:30:23 +0000 | [diff] [blame] | 1721 | "(-1, 4)"); |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1723 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1724 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1725 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 1726 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1727 | static PyObject * |
| 1728 | float_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1729 | { |
| 1730 | PyObject *x = Py_False; /* Integer zero */ |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1731 | static char *kwlist[] = {"x", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1732 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1733 | if (type != &PyFloat_Type) |
| 1734 | return float_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1735 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x)) |
| 1736 | return NULL; |
Benjamin Peterson | 99d36f1 | 2009-04-15 21:26:36 +0000 | [diff] [blame] | 1737 | /* If it's a string, but not a string subclass, use |
| 1738 | PyFloat_FromString. */ |
| 1739 | if (PyString_CheckExact(x)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1740 | return PyFloat_FromString(x, NULL); |
| 1741 | return PyNumber_Float(x); |
| 1742 | } |
| 1743 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1744 | /* Wimpy, slow approach to tp_new calls for subtypes of float: |
| 1745 | first create a regular float from whatever arguments we got, |
| 1746 | then allocate a subtype instance and initialize its ob_fval |
| 1747 | from the regular float. The regular float is then thrown away. |
| 1748 | */ |
| 1749 | static PyObject * |
| 1750 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1751 | { |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1752 | PyObject *tmp, *newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1753 | |
| 1754 | assert(PyType_IsSubtype(type, &PyFloat_Type)); |
| 1755 | tmp = float_new(&PyFloat_Type, args, kwds); |
| 1756 | if (tmp == NULL) |
| 1757 | return NULL; |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1758 | assert(PyFloat_CheckExact(tmp)); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1759 | newobj = type->tp_alloc(type, 0); |
| 1760 | if (newobj == NULL) { |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1761 | Py_DECREF(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1762 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1763 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1764 | ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1765 | Py_DECREF(tmp); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1766 | return newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1769 | static PyObject * |
| 1770 | float_getnewargs(PyFloatObject *v) |
| 1771 | { |
| 1772 | return Py_BuildValue("(d)", v->ob_fval); |
| 1773 | } |
| 1774 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1775 | /* this is for the benefit of the pack/unpack routines below */ |
| 1776 | |
| 1777 | typedef enum { |
| 1778 | unknown_format, ieee_big_endian_format, ieee_little_endian_format |
| 1779 | } float_format_type; |
| 1780 | |
| 1781 | static float_format_type double_format, float_format; |
| 1782 | static float_format_type detected_double_format, detected_float_format; |
| 1783 | |
| 1784 | static PyObject * |
| 1785 | float_getformat(PyTypeObject *v, PyObject* arg) |
| 1786 | { |
| 1787 | char* s; |
| 1788 | float_format_type r; |
| 1789 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1790 | if (!PyString_Check(arg)) { |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1791 | PyErr_Format(PyExc_TypeError, |
| 1792 | "__getformat__() argument must be string, not %.500s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1793 | Py_TYPE(arg)->tp_name); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1794 | return NULL; |
| 1795 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1796 | s = PyString_AS_STRING(arg); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1797 | if (strcmp(s, "double") == 0) { |
| 1798 | r = double_format; |
| 1799 | } |
| 1800 | else if (strcmp(s, "float") == 0) { |
| 1801 | r = float_format; |
| 1802 | } |
| 1803 | else { |
| 1804 | PyErr_SetString(PyExc_ValueError, |
| 1805 | "__getformat__() argument 1 must be " |
| 1806 | "'double' or 'float'"); |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | |
| 1810 | switch (r) { |
| 1811 | case unknown_format: |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1812 | return PyString_FromString("unknown"); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1813 | case ieee_little_endian_format: |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1814 | return PyString_FromString("IEEE, little-endian"); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1815 | case ieee_big_endian_format: |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1816 | return PyString_FromString("IEEE, big-endian"); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1817 | default: |
| 1818 | Py_FatalError("insane float_format or double_format"); |
| 1819 | return NULL; |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | PyDoc_STRVAR(float_getformat_doc, |
| 1824 | "float.__getformat__(typestr) -> string\n" |
| 1825 | "\n" |
| 1826 | "You probably don't want to use this function. It exists mainly to be\n" |
| 1827 | "used in Python's test suite.\n" |
| 1828 | "\n" |
| 1829 | "typestr must be 'double' or 'float'. This function returns whichever of\n" |
| 1830 | "'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n" |
| 1831 | "format of floating point numbers used by the C type named by typestr."); |
| 1832 | |
| 1833 | static PyObject * |
| 1834 | float_setformat(PyTypeObject *v, PyObject* args) |
| 1835 | { |
| 1836 | char* typestr; |
| 1837 | char* format; |
| 1838 | float_format_type f; |
| 1839 | float_format_type detected; |
| 1840 | float_format_type *p; |
| 1841 | |
| 1842 | if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format)) |
| 1843 | return NULL; |
| 1844 | |
| 1845 | if (strcmp(typestr, "double") == 0) { |
| 1846 | p = &double_format; |
| 1847 | detected = detected_double_format; |
| 1848 | } |
| 1849 | else if (strcmp(typestr, "float") == 0) { |
| 1850 | p = &float_format; |
| 1851 | detected = detected_float_format; |
| 1852 | } |
| 1853 | else { |
| 1854 | PyErr_SetString(PyExc_ValueError, |
| 1855 | "__setformat__() argument 1 must " |
| 1856 | "be 'double' or 'float'"); |
| 1857 | return NULL; |
| 1858 | } |
| 1859 | |
| 1860 | if (strcmp(format, "unknown") == 0) { |
| 1861 | f = unknown_format; |
| 1862 | } |
| 1863 | else if (strcmp(format, "IEEE, little-endian") == 0) { |
| 1864 | f = ieee_little_endian_format; |
| 1865 | } |
| 1866 | else if (strcmp(format, "IEEE, big-endian") == 0) { |
| 1867 | f = ieee_big_endian_format; |
| 1868 | } |
| 1869 | else { |
| 1870 | PyErr_SetString(PyExc_ValueError, |
| 1871 | "__setformat__() argument 2 must be " |
| 1872 | "'unknown', 'IEEE, little-endian' or " |
| 1873 | "'IEEE, big-endian'"); |
| 1874 | return NULL; |
| 1875 | |
| 1876 | } |
| 1877 | |
| 1878 | if (f != unknown_format && f != detected) { |
| 1879 | PyErr_Format(PyExc_ValueError, |
| 1880 | "can only set %s format to 'unknown' or the " |
| 1881 | "detected platform value", typestr); |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | |
| 1885 | *p = f; |
| 1886 | Py_RETURN_NONE; |
| 1887 | } |
| 1888 | |
| 1889 | PyDoc_STRVAR(float_setformat_doc, |
| 1890 | "float.__setformat__(typestr, fmt) -> None\n" |
| 1891 | "\n" |
| 1892 | "You probably don't want to use this function. It exists mainly to be\n" |
| 1893 | "used in Python's test suite.\n" |
| 1894 | "\n" |
| 1895 | "typestr must be 'double' or 'float'. fmt must be one of 'unknown',\n" |
| 1896 | "'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n" |
| 1897 | "one of the latter two if it appears to match the underlying C reality.\n" |
| 1898 | "\n" |
| 1899 | "Overrides the automatic determination of C-level floating point type.\n" |
| 1900 | "This affects how floats are converted to and from binary strings."); |
| 1901 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1902 | static PyObject * |
| 1903 | float_getzero(PyObject *v, void *closure) |
| 1904 | { |
| 1905 | return PyFloat_FromDouble(0.0); |
| 1906 | } |
| 1907 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1908 | static PyObject * |
| 1909 | float__format__(PyObject *self, PyObject *args) |
| 1910 | { |
| 1911 | PyObject *format_spec; |
| 1912 | |
| 1913 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 1914 | return NULL; |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 1915 | if (PyBytes_Check(format_spec)) |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1916 | return _PyFloat_FormatAdvanced(self, |
| 1917 | PyBytes_AS_STRING(format_spec), |
| 1918 | PyBytes_GET_SIZE(format_spec)); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1919 | if (PyUnicode_Check(format_spec)) { |
| 1920 | /* Convert format_spec to a str */ |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1921 | PyObject *result; |
| 1922 | PyObject *str_spec = PyObject_Str(format_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1923 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1924 | if (str_spec == NULL) |
| 1925 | return NULL; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1926 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1927 | result = _PyFloat_FormatAdvanced(self, |
| 1928 | PyBytes_AS_STRING(str_spec), |
| 1929 | PyBytes_GET_SIZE(str_spec)); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1930 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1931 | Py_DECREF(str_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1932 | return result; |
| 1933 | } |
| 1934 | PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode"); |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | |
| 1938 | PyDoc_STRVAR(float__format__doc, |
| 1939 | "float.__format__(format_spec) -> string\n" |
| 1940 | "\n" |
| 1941 | "Formats the float according to format_spec."); |
| 1942 | |
| 1943 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1944 | static PyMethodDef float_methods[] = { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1945 | {"conjugate", (PyCFunction)float_float, METH_NOARGS, |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1946 | "Returns self, the complex conjugate of any float."}, |
| 1947 | {"__trunc__", (PyCFunction)float_trunc, METH_NOARGS, |
| 1948 | "Returns the Integral closest to x between 0 and x."}, |
Jeffrey Yasskin | 3ea7b41 | 2008-01-27 23:08:46 +0000 | [diff] [blame] | 1949 | {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, |
| 1950 | float_as_integer_ratio_doc}, |
Mark Dickinson | 7103aa4 | 2008-07-15 19:08:33 +0000 | [diff] [blame] | 1951 | {"fromhex", (PyCFunction)float_fromhex, |
| 1952 | METH_O|METH_CLASS, float_fromhex_doc}, |
| 1953 | {"hex", (PyCFunction)float_hex, |
| 1954 | METH_NOARGS, float_hex_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1955 | {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, |
| 1956 | "Returns True if the float is an integer."}, |
| 1957 | #if 0 |
| 1958 | {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS, |
| 1959 | "Returns True if the float is positive or negative infinite."}, |
| 1960 | {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS, |
| 1961 | "Returns True if the float is finite, neither infinite nor NaN."}, |
| 1962 | {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS, |
| 1963 | "Returns True if the float is not a number (NaN)."}, |
| 1964 | #endif |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1965 | {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS}, |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1966 | {"__getformat__", (PyCFunction)float_getformat, |
| 1967 | METH_O|METH_CLASS, float_getformat_doc}, |
| 1968 | {"__setformat__", (PyCFunction)float_setformat, |
| 1969 | METH_VARARGS|METH_CLASS, float_setformat_doc}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1970 | {"__format__", (PyCFunction)float__format__, |
| 1971 | METH_VARARGS, float__format__doc}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1972 | {NULL, NULL} /* sentinel */ |
| 1973 | }; |
| 1974 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1975 | static PyGetSetDef float_getset[] = { |
| 1976 | {"real", |
| 1977 | (getter)float_float, (setter)NULL, |
| 1978 | "the real part of a complex number", |
| 1979 | NULL}, |
| 1980 | {"imag", |
| 1981 | (getter)float_getzero, (setter)NULL, |
| 1982 | "the imaginary part of a complex number", |
| 1983 | NULL}, |
| 1984 | {NULL} /* Sentinel */ |
| 1985 | }; |
| 1986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1987 | PyDoc_STRVAR(float_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1988 | "float(x) -> floating point number\n\ |
| 1989 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1990 | Convert a string or number to a floating point number, if possible."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1991 | |
| 1992 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1993 | static PyNumberMethods float_as_number = { |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 1994 | float_add, /*nb_add*/ |
| 1995 | float_sub, /*nb_subtract*/ |
| 1996 | float_mul, /*nb_multiply*/ |
| 1997 | float_classic_div, /*nb_divide*/ |
| 1998 | float_rem, /*nb_remainder*/ |
| 1999 | float_divmod, /*nb_divmod*/ |
| 2000 | float_pow, /*nb_power*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2001 | (unaryfunc)float_neg, /*nb_negative*/ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 2002 | (unaryfunc)float_float, /*nb_positive*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2003 | (unaryfunc)float_abs, /*nb_absolute*/ |
| 2004 | (inquiry)float_nonzero, /*nb_nonzero*/ |
Guido van Rossum | 27acb33 | 1991-10-24 14:55:28 +0000 | [diff] [blame] | 2005 | 0, /*nb_invert*/ |
| 2006 | 0, /*nb_lshift*/ |
| 2007 | 0, /*nb_rshift*/ |
| 2008 | 0, /*nb_and*/ |
| 2009 | 0, /*nb_xor*/ |
| 2010 | 0, /*nb_or*/ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2011 | float_coerce, /*nb_coerce*/ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 2012 | float_trunc, /*nb_int*/ |
Amaury Forgeot d'Arc | d3ffb89 | 2008-09-09 07:24:30 +0000 | [diff] [blame] | 2013 | float_long, /*nb_long*/ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2014 | float_float, /*nb_float*/ |
Raymond Hettinger | 9c437af | 2008-06-24 22:46:07 +0000 | [diff] [blame] | 2015 | 0, /* nb_oct */ |
| 2016 | 0, /* nb_hex */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2017 | 0, /* nb_inplace_add */ |
| 2018 | 0, /* nb_inplace_subtract */ |
| 2019 | 0, /* nb_inplace_multiply */ |
| 2020 | 0, /* nb_inplace_divide */ |
| 2021 | 0, /* nb_inplace_remainder */ |
| 2022 | 0, /* nb_inplace_power */ |
| 2023 | 0, /* nb_inplace_lshift */ |
| 2024 | 0, /* nb_inplace_rshift */ |
| 2025 | 0, /* nb_inplace_and */ |
| 2026 | 0, /* nb_inplace_xor */ |
| 2027 | 0, /* nb_inplace_or */ |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 2028 | float_floor_div, /* nb_floor_divide */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2029 | float_div, /* nb_true_divide */ |
| 2030 | 0, /* nb_inplace_floor_divide */ |
| 2031 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2032 | }; |
| 2033 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2034 | PyTypeObject PyFloat_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2035 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2036 | "float", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2037 | sizeof(PyFloatObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2038 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2039 | (destructor)float_dealloc, /* tp_dealloc */ |
| 2040 | (printfunc)float_print, /* tp_print */ |
| 2041 | 0, /* tp_getattr */ |
| 2042 | 0, /* tp_setattr */ |
Michael W. Hudson | 08678a1 | 2004-05-26 17:36:12 +0000 | [diff] [blame] | 2043 | 0, /* tp_compare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2044 | (reprfunc)float_repr, /* tp_repr */ |
| 2045 | &float_as_number, /* tp_as_number */ |
| 2046 | 0, /* tp_as_sequence */ |
| 2047 | 0, /* tp_as_mapping */ |
| 2048 | (hashfunc)float_hash, /* tp_hash */ |
| 2049 | 0, /* tp_call */ |
| 2050 | (reprfunc)float_str, /* tp_str */ |
| 2051 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2052 | 0, /* tp_setattro */ |
| 2053 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 2054 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 2055 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2056 | float_doc, /* tp_doc */ |
| 2057 | 0, /* tp_traverse */ |
| 2058 | 0, /* tp_clear */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2059 | float_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2060 | 0, /* tp_weaklistoffset */ |
| 2061 | 0, /* tp_iter */ |
| 2062 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 2063 | float_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2064 | 0, /* tp_members */ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 2065 | float_getset, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2066 | 0, /* tp_base */ |
| 2067 | 0, /* tp_dict */ |
| 2068 | 0, /* tp_descr_get */ |
| 2069 | 0, /* tp_descr_set */ |
| 2070 | 0, /* tp_dictoffset */ |
| 2071 | 0, /* tp_init */ |
| 2072 | 0, /* tp_alloc */ |
| 2073 | float_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2074 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 2075 | |
| 2076 | void |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2077 | _PyFloat_Init(void) |
| 2078 | { |
| 2079 | /* We attempt to determine if this machine is using IEEE |
| 2080 | floating point formats by peering at the bits of some |
| 2081 | carefully chosen values. If it looks like we are on an |
| 2082 | IEEE platform, the float packing/unpacking routines can |
| 2083 | just copy bits, if not they resort to arithmetic & shifts |
| 2084 | and masks. The shifts & masks approach works on all finite |
| 2085 | values, but what happens to infinities, NaNs and signed |
| 2086 | zeroes on packing is an accident, and attempting to unpack |
| 2087 | a NaN or an infinity will raise an exception. |
| 2088 | |
| 2089 | Note that if we're on some whacked-out platform which uses |
| 2090 | IEEE formats but isn't strictly little-endian or big- |
| 2091 | endian, we will fall back to the portable shifts & masks |
| 2092 | method. */ |
| 2093 | |
| 2094 | #if SIZEOF_DOUBLE == 8 |
| 2095 | { |
| 2096 | double x = 9006104071832581.0; |
| 2097 | if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0) |
| 2098 | detected_double_format = ieee_big_endian_format; |
| 2099 | else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0) |
| 2100 | detected_double_format = ieee_little_endian_format; |
| 2101 | else |
| 2102 | detected_double_format = unknown_format; |
| 2103 | } |
| 2104 | #else |
| 2105 | detected_double_format = unknown_format; |
| 2106 | #endif |
| 2107 | |
| 2108 | #if SIZEOF_FLOAT == 4 |
| 2109 | { |
| 2110 | float y = 16711938.0; |
| 2111 | if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0) |
| 2112 | detected_float_format = ieee_big_endian_format; |
| 2113 | else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0) |
| 2114 | detected_float_format = ieee_little_endian_format; |
| 2115 | else |
| 2116 | detected_float_format = unknown_format; |
| 2117 | } |
| 2118 | #else |
| 2119 | detected_float_format = unknown_format; |
| 2120 | #endif |
| 2121 | |
| 2122 | double_format = detected_double_format; |
| 2123 | float_format = detected_float_format; |
Christian Heimes | f15c66e | 2007-12-11 00:54:34 +0000 | [diff] [blame] | 2124 | |
Christian Heimes | 796fc31 | 2008-01-30 18:58:29 +0000 | [diff] [blame] | 2125 | /* Init float info */ |
| 2126 | if (FloatInfoType.tp_name == 0) |
| 2127 | PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2130 | int |
| 2131 | PyFloat_ClearFreeList(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 2132 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2133 | PyFloatObject *p; |
| 2134 | PyFloatBlock *list, *next; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2135 | int i; |
| 2136 | int u; /* remaining unfreed ints per block */ |
| 2137 | int freelist_size = 0; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2138 | |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2139 | list = block_list; |
| 2140 | block_list = NULL; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 2141 | free_list = NULL; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2142 | while (list != NULL) { |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2143 | u = 0; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 2144 | for (i = 0, p = &list->objects[0]; |
| 2145 | i < N_FLOATOBJECTS; |
| 2146 | i++, p++) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2147 | if (PyFloat_CheckExact(p) && Py_REFCNT(p) != 0) |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2148 | u++; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2149 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2150 | next = list->next; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2151 | if (u) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2152 | list->next = block_list; |
| 2153 | block_list = list; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 2154 | for (i = 0, p = &list->objects[0]; |
| 2155 | i < N_FLOATOBJECTS; |
| 2156 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 2157 | if (!PyFloat_CheckExact(p) || |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2158 | Py_REFCNT(p) == 0) { |
| 2159 | Py_TYPE(p) = (struct _typeobject *) |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 2160 | free_list; |
| 2161 | free_list = p; |
| 2162 | } |
| 2163 | } |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2164 | } |
| 2165 | else { |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2166 | PyMem_FREE(list); |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2167 | } |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2168 | freelist_size += u; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2169 | list = next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2170 | } |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2171 | return freelist_size; |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | void |
| 2175 | PyFloat_Fini(void) |
| 2176 | { |
| 2177 | PyFloatObject *p; |
| 2178 | PyFloatBlock *list; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2179 | int i; |
| 2180 | int u; /* total unfreed floats per block */ |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 2181 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2182 | u = PyFloat_ClearFreeList(); |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 2183 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2184 | if (!Py_VerboseFlag) |
| 2185 | return; |
| 2186 | fprintf(stderr, "# cleanup floats"); |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2187 | if (!u) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2188 | fprintf(stderr, "\n"); |
| 2189 | } |
| 2190 | else { |
| 2191 | fprintf(stderr, |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 2192 | ": %d unfreed float%s\n", |
| 2193 | u, u == 1 ? "" : "s"); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2194 | } |
| 2195 | if (Py_VerboseFlag > 1) { |
| 2196 | list = block_list; |
| 2197 | while (list != NULL) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 2198 | for (i = 0, p = &list->objects[0]; |
| 2199 | i < N_FLOATOBJECTS; |
| 2200 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 2201 | if (PyFloat_CheckExact(p) && |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2202 | Py_REFCNT(p) != 0) { |
Eric Smith | b327258 | 2009-10-16 14:26:36 +0000 | [diff] [blame] | 2203 | char *buf = PyOS_double_to_string( |
| 2204 | PyFloat_AS_DOUBLE(p), 'r', |
| 2205 | 0, 0, NULL); |
| 2206 | if (buf) { |
| 2207 | /* XXX(twouters) cast |
| 2208 | refcount to long |
| 2209 | until %zd is |
| 2210 | universally |
| 2211 | available |
| 2212 | */ |
| 2213 | fprintf(stderr, |
Thomas Wouters | 8b87a0b | 2006-03-01 05:41:20 +0000 | [diff] [blame] | 2214 | "# <float at %p, refcnt=%ld, val=%s>\n", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2215 | p, (long)Py_REFCNT(p), buf); |
Eric Smith | b327258 | 2009-10-16 14:26:36 +0000 | [diff] [blame] | 2216 | PyMem_Free(buf); |
| 2217 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | list = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 2221 | } |
| 2222 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 2223 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2224 | |
| 2225 | /*---------------------------------------------------------------------------- |
| 2226 | * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h. |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2227 | */ |
| 2228 | int |
| 2229 | _PyFloat_Pack4(double x, unsigned char *p, int le) |
| 2230 | { |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2231 | if (float_format == unknown_format) { |
| 2232 | unsigned char sign; |
| 2233 | int e; |
| 2234 | double f; |
| 2235 | unsigned int fbits; |
| 2236 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2237 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2238 | if (le) { |
| 2239 | p += 3; |
| 2240 | incr = -1; |
| 2241 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2242 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2243 | if (x < 0) { |
| 2244 | sign = 1; |
| 2245 | x = -x; |
| 2246 | } |
| 2247 | else |
| 2248 | sign = 0; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2249 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2250 | f = frexp(x, &e); |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2251 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2252 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 2253 | if (0.5 <= f && f < 1.0) { |
| 2254 | f *= 2.0; |
| 2255 | e--; |
| 2256 | } |
| 2257 | else if (f == 0.0) |
| 2258 | e = 0; |
| 2259 | else { |
| 2260 | PyErr_SetString(PyExc_SystemError, |
| 2261 | "frexp() result out of range"); |
| 2262 | return -1; |
| 2263 | } |
| 2264 | |
| 2265 | if (e >= 128) |
| 2266 | goto Overflow; |
| 2267 | else if (e < -126) { |
| 2268 | /* Gradual underflow */ |
| 2269 | f = ldexp(f, 126 + e); |
| 2270 | e = 0; |
| 2271 | } |
| 2272 | else if (!(e == 0 && f == 0.0)) { |
| 2273 | e += 127; |
| 2274 | f -= 1.0; /* Get rid of leading 1 */ |
| 2275 | } |
| 2276 | |
| 2277 | f *= 8388608.0; /* 2**23 */ |
| 2278 | fbits = (unsigned int)(f + 0.5); /* Round */ |
| 2279 | assert(fbits <= 8388608); |
| 2280 | if (fbits >> 23) { |
| 2281 | /* The carry propagated out of a string of 23 1 bits. */ |
| 2282 | fbits = 0; |
| 2283 | ++e; |
| 2284 | if (e >= 255) |
| 2285 | goto Overflow; |
| 2286 | } |
| 2287 | |
| 2288 | /* First byte */ |
| 2289 | *p = (sign << 7) | (e >> 1); |
| 2290 | p += incr; |
| 2291 | |
| 2292 | /* Second byte */ |
| 2293 | *p = (char) (((e & 1) << 7) | (fbits >> 16)); |
| 2294 | p += incr; |
| 2295 | |
| 2296 | /* Third byte */ |
| 2297 | *p = (fbits >> 8) & 0xFF; |
| 2298 | p += incr; |
| 2299 | |
| 2300 | /* Fourth byte */ |
| 2301 | *p = fbits & 0xFF; |
| 2302 | |
| 2303 | /* Done */ |
| 2304 | return 0; |
| 2305 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2306 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2307 | else { |
Michael W. Hudson | 3095ad0 | 2005-06-30 00:02:26 +0000 | [diff] [blame] | 2308 | float y = (float)x; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2309 | const char *s = (char*)&y; |
| 2310 | int i, incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2311 | |
Mark Dickinson | c23b8a7 | 2008-03-14 14:23:37 +0000 | [diff] [blame] | 2312 | if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x)) |
| 2313 | goto Overflow; |
| 2314 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2315 | if ((float_format == ieee_little_endian_format && !le) |
| 2316 | || (float_format == ieee_big_endian_format && le)) { |
| 2317 | p += 3; |
| 2318 | incr = -1; |
| 2319 | } |
Mark Dickinson | c23b8a7 | 2008-03-14 14:23:37 +0000 | [diff] [blame] | 2320 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2321 | for (i = 0; i < 4; i++) { |
| 2322 | *p = *s++; |
| 2323 | p += incr; |
| 2324 | } |
| 2325 | return 0; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2326 | } |
Mark Dickinson | c23b8a7 | 2008-03-14 14:23:37 +0000 | [diff] [blame] | 2327 | Overflow: |
| 2328 | PyErr_SetString(PyExc_OverflowError, |
| 2329 | "float too large to pack with f format"); |
| 2330 | return -1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | int |
| 2334 | _PyFloat_Pack8(double x, unsigned char *p, int le) |
| 2335 | { |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2336 | if (double_format == unknown_format) { |
| 2337 | unsigned char sign; |
| 2338 | int e; |
| 2339 | double f; |
| 2340 | unsigned int fhi, flo; |
| 2341 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2342 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2343 | if (le) { |
| 2344 | p += 7; |
| 2345 | incr = -1; |
| 2346 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2347 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2348 | if (x < 0) { |
| 2349 | sign = 1; |
| 2350 | x = -x; |
| 2351 | } |
| 2352 | else |
| 2353 | sign = 0; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2354 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2355 | f = frexp(x, &e); |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2356 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2357 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 2358 | if (0.5 <= f && f < 1.0) { |
| 2359 | f *= 2.0; |
| 2360 | e--; |
| 2361 | } |
| 2362 | else if (f == 0.0) |
| 2363 | e = 0; |
| 2364 | else { |
| 2365 | PyErr_SetString(PyExc_SystemError, |
| 2366 | "frexp() result out of range"); |
| 2367 | return -1; |
| 2368 | } |
| 2369 | |
| 2370 | if (e >= 1024) |
| 2371 | goto Overflow; |
| 2372 | else if (e < -1022) { |
| 2373 | /* Gradual underflow */ |
| 2374 | f = ldexp(f, 1022 + e); |
| 2375 | e = 0; |
| 2376 | } |
| 2377 | else if (!(e == 0 && f == 0.0)) { |
| 2378 | e += 1023; |
| 2379 | f -= 1.0; /* Get rid of leading 1 */ |
| 2380 | } |
| 2381 | |
| 2382 | /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */ |
| 2383 | f *= 268435456.0; /* 2**28 */ |
| 2384 | fhi = (unsigned int)f; /* Truncate */ |
| 2385 | assert(fhi < 268435456); |
| 2386 | |
| 2387 | f -= (double)fhi; |
| 2388 | f *= 16777216.0; /* 2**24 */ |
| 2389 | flo = (unsigned int)(f + 0.5); /* Round */ |
| 2390 | assert(flo <= 16777216); |
| 2391 | if (flo >> 24) { |
| 2392 | /* The carry propagated out of a string of 24 1 bits. */ |
| 2393 | flo = 0; |
| 2394 | ++fhi; |
| 2395 | if (fhi >> 28) { |
| 2396 | /* And it also progagated out of the next 28 bits. */ |
| 2397 | fhi = 0; |
| 2398 | ++e; |
| 2399 | if (e >= 2047) |
| 2400 | goto Overflow; |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | /* First byte */ |
| 2405 | *p = (sign << 7) | (e >> 4); |
| 2406 | p += incr; |
| 2407 | |
| 2408 | /* Second byte */ |
| 2409 | *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24)); |
| 2410 | p += incr; |
| 2411 | |
| 2412 | /* Third byte */ |
| 2413 | *p = (fhi >> 16) & 0xFF; |
| 2414 | p += incr; |
| 2415 | |
| 2416 | /* Fourth byte */ |
| 2417 | *p = (fhi >> 8) & 0xFF; |
| 2418 | p += incr; |
| 2419 | |
| 2420 | /* Fifth byte */ |
| 2421 | *p = fhi & 0xFF; |
| 2422 | p += incr; |
| 2423 | |
| 2424 | /* Sixth byte */ |
| 2425 | *p = (flo >> 16) & 0xFF; |
| 2426 | p += incr; |
| 2427 | |
| 2428 | /* Seventh byte */ |
| 2429 | *p = (flo >> 8) & 0xFF; |
| 2430 | p += incr; |
| 2431 | |
| 2432 | /* Eighth byte */ |
| 2433 | *p = flo & 0xFF; |
| 2434 | p += incr; |
| 2435 | |
| 2436 | /* Done */ |
| 2437 | return 0; |
| 2438 | |
| 2439 | Overflow: |
| 2440 | PyErr_SetString(PyExc_OverflowError, |
| 2441 | "float too large to pack with d format"); |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2442 | return -1; |
| 2443 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2444 | else { |
| 2445 | const char *s = (char*)&x; |
| 2446 | int i, incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2447 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2448 | if ((double_format == ieee_little_endian_format && !le) |
| 2449 | || (double_format == ieee_big_endian_format && le)) { |
| 2450 | p += 7; |
| 2451 | incr = -1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2452 | } |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2453 | |
| 2454 | for (i = 0; i < 8; i++) { |
| 2455 | *p = *s++; |
| 2456 | p += incr; |
| 2457 | } |
| 2458 | return 0; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2459 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | double |
| 2463 | _PyFloat_Unpack4(const unsigned char *p, int le) |
| 2464 | { |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2465 | if (float_format == unknown_format) { |
| 2466 | unsigned char sign; |
| 2467 | int e; |
| 2468 | unsigned int f; |
| 2469 | double x; |
| 2470 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2471 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2472 | if (le) { |
| 2473 | p += 3; |
| 2474 | incr = -1; |
| 2475 | } |
| 2476 | |
| 2477 | /* First byte */ |
| 2478 | sign = (*p >> 7) & 1; |
| 2479 | e = (*p & 0x7F) << 1; |
| 2480 | p += incr; |
| 2481 | |
| 2482 | /* Second byte */ |
| 2483 | e |= (*p >> 7) & 1; |
| 2484 | f = (*p & 0x7F) << 16; |
| 2485 | p += incr; |
| 2486 | |
| 2487 | if (e == 255) { |
| 2488 | PyErr_SetString( |
| 2489 | PyExc_ValueError, |
| 2490 | "can't unpack IEEE 754 special value " |
| 2491 | "on non-IEEE platform"); |
| 2492 | return -1; |
| 2493 | } |
| 2494 | |
| 2495 | /* Third byte */ |
| 2496 | f |= *p << 8; |
| 2497 | p += incr; |
| 2498 | |
| 2499 | /* Fourth byte */ |
| 2500 | f |= *p; |
| 2501 | |
| 2502 | x = (double)f / 8388608.0; |
| 2503 | |
| 2504 | /* XXX This sadly ignores Inf/NaN issues */ |
| 2505 | if (e == 0) |
| 2506 | e = -126; |
| 2507 | else { |
| 2508 | x += 1.0; |
| 2509 | e -= 127; |
| 2510 | } |
| 2511 | x = ldexp(x, e); |
| 2512 | |
| 2513 | if (sign) |
| 2514 | x = -x; |
| 2515 | |
| 2516 | return x; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2517 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2518 | else { |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2519 | float x; |
| 2520 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2521 | if ((float_format == ieee_little_endian_format && !le) |
| 2522 | || (float_format == ieee_big_endian_format && le)) { |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2523 | char buf[4]; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2524 | char *d = &buf[3]; |
| 2525 | int i; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2526 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2527 | for (i = 0; i < 4; i++) { |
| 2528 | *d-- = *p++; |
| 2529 | } |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2530 | memcpy(&x, buf, 4); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2531 | } |
| 2532 | else { |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2533 | memcpy(&x, p, 4); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2534 | } |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2535 | |
| 2536 | return x; |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2537 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | double |
| 2541 | _PyFloat_Unpack8(const unsigned char *p, int le) |
| 2542 | { |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2543 | if (double_format == unknown_format) { |
| 2544 | unsigned char sign; |
| 2545 | int e; |
| 2546 | unsigned int fhi, flo; |
| 2547 | double x; |
| 2548 | int incr = 1; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2549 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2550 | if (le) { |
| 2551 | p += 7; |
| 2552 | incr = -1; |
| 2553 | } |
| 2554 | |
| 2555 | /* First byte */ |
| 2556 | sign = (*p >> 7) & 1; |
| 2557 | e = (*p & 0x7F) << 4; |
| 2558 | |
| 2559 | p += incr; |
| 2560 | |
| 2561 | /* Second byte */ |
| 2562 | e |= (*p >> 4) & 0xF; |
| 2563 | fhi = (*p & 0xF) << 24; |
| 2564 | p += incr; |
| 2565 | |
| 2566 | if (e == 2047) { |
| 2567 | PyErr_SetString( |
| 2568 | PyExc_ValueError, |
| 2569 | "can't unpack IEEE 754 special value " |
| 2570 | "on non-IEEE platform"); |
| 2571 | return -1.0; |
| 2572 | } |
| 2573 | |
| 2574 | /* Third byte */ |
| 2575 | fhi |= *p << 16; |
| 2576 | p += incr; |
| 2577 | |
| 2578 | /* Fourth byte */ |
| 2579 | fhi |= *p << 8; |
| 2580 | p += incr; |
| 2581 | |
| 2582 | /* Fifth byte */ |
| 2583 | fhi |= *p; |
| 2584 | p += incr; |
| 2585 | |
| 2586 | /* Sixth byte */ |
| 2587 | flo = *p << 16; |
| 2588 | p += incr; |
| 2589 | |
| 2590 | /* Seventh byte */ |
| 2591 | flo |= *p << 8; |
| 2592 | p += incr; |
| 2593 | |
| 2594 | /* Eighth byte */ |
| 2595 | flo |= *p; |
| 2596 | |
| 2597 | x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 2598 | x /= 268435456.0; /* 2**28 */ |
| 2599 | |
| 2600 | if (e == 0) |
| 2601 | e = -1022; |
| 2602 | else { |
| 2603 | x += 1.0; |
| 2604 | e -= 1023; |
| 2605 | } |
| 2606 | x = ldexp(x, e); |
| 2607 | |
| 2608 | if (sign) |
| 2609 | x = -x; |
| 2610 | |
| 2611 | return x; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2612 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2613 | else { |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2614 | double x; |
| 2615 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2616 | if ((double_format == ieee_little_endian_format && !le) |
| 2617 | || (double_format == ieee_big_endian_format && le)) { |
| 2618 | char buf[8]; |
| 2619 | char *d = &buf[7]; |
| 2620 | int i; |
| 2621 | |
| 2622 | for (i = 0; i < 8; i++) { |
| 2623 | *d-- = *p++; |
| 2624 | } |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2625 | memcpy(&x, buf, 8); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2626 | } |
| 2627 | else { |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2628 | memcpy(&x, p, 8); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 2629 | } |
Michael W. Hudson | b78a5fc | 2005-12-05 00:27:49 +0000 | [diff] [blame] | 2630 | |
| 2631 | return x; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2632 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 2633 | } |