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