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