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