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