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