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