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