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 |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 90 | int 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)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 100 | if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) { |
| 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; |
Trent Mick | a248fb6 | 2000-08-12 21:37:39 +0000 | [diff] [blame] | 111 | len = (int)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) |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 135 | x = 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) |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 167 | x = 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; |
| 226 | /* Subroutine for float_repr and float_print. |
| 227 | We want float numbers to be recognizable as such, |
| 228 | i.e., they should contain a decimal point or an exponent. |
| 229 | However, %g may print the number as an integer; |
| 230 | in such cases, we append ".0" to the string. */ |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 231 | |
| 232 | assert(PyFloat_Check(v)); |
| 233 | PyOS_snprintf(buf, buflen, "%.*g", precision, v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | cp = buf; |
| 235 | if (*cp == '-') |
| 236 | cp++; |
| 237 | for (; *cp != '\0'; cp++) { |
| 238 | /* Any non-digit means it's not an integer; |
| 239 | this takes care of NAN and INF as well. */ |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 240 | if (!isdigit(Py_CHARMASK(*cp))) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | if (*cp == '\0') { |
| 244 | *cp++ = '.'; |
| 245 | *cp++ = '0'; |
| 246 | *cp++ = '\0'; |
| 247 | } |
| 248 | } |
| 249 | |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 250 | /* XXX PyFloat_AsStringEx should not be a public API function (for one |
| 251 | XXX thing, its signature passes a buffer without a length; for another, |
| 252 | XXX it isn't useful outside this file). |
| 253 | */ |
| 254 | void |
| 255 | PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision) |
| 256 | { |
| 257 | format_float(buf, 100, v, precision); |
| 258 | } |
| 259 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 260 | /* Macro and helper that convert PyObject obj to a C double and store |
| 261 | the value in dbl; this replaces the functionality of the coercion |
Tim Peters | 77d8a4f | 2001-12-11 20:31:34 +0000 | [diff] [blame] | 262 | slot function. If conversion to double raises an exception, obj is |
| 263 | set to NULL, and the function invoking this macro returns NULL. If |
| 264 | obj is not of float, int or long type, Py_NotImplemented is incref'ed, |
| 265 | stored in obj, and returned from the function invoking this macro. |
| 266 | */ |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 267 | #define CONVERT_TO_DOUBLE(obj, dbl) \ |
| 268 | if (PyFloat_Check(obj)) \ |
| 269 | dbl = PyFloat_AS_DOUBLE(obj); \ |
| 270 | else if (convert_to_double(&(obj), &(dbl)) < 0) \ |
| 271 | return obj; |
| 272 | |
| 273 | static int |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 274 | convert_to_double(PyObject **v, double *dbl) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 275 | { |
| 276 | register PyObject *obj = *v; |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 277 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 278 | if (PyInt_Check(obj)) { |
| 279 | *dbl = (double)PyInt_AS_LONG(obj); |
| 280 | } |
| 281 | else if (PyLong_Check(obj)) { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 282 | *dbl = PyLong_AsDouble(obj); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 283 | if (*dbl == -1.0 && PyErr_Occurred()) { |
| 284 | *v = NULL; |
| 285 | return -1; |
| 286 | } |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 287 | } |
| 288 | else { |
| 289 | Py_INCREF(Py_NotImplemented); |
| 290 | *v = Py_NotImplemented; |
| 291 | return -1; |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 296 | /* Precisions used by repr() and str(), respectively. |
| 297 | |
| 298 | The repr() precision (17 significant decimal digits) is the minimal number |
| 299 | that is guaranteed to have enough precision so that if the number is read |
| 300 | back in the exact same binary value is recreated. This is true for IEEE |
| 301 | floating point by design, and also happens to work for all other modern |
| 302 | hardware. |
| 303 | |
| 304 | The str() precision is chosen so that in most cases, the rounding noise |
| 305 | created by various operations is suppressed, while giving plenty of |
| 306 | precision for practical use. |
| 307 | |
| 308 | */ |
| 309 | |
| 310 | #define PREC_REPR 17 |
| 311 | #define PREC_STR 12 |
| 312 | |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 313 | /* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated: |
| 314 | XXX they pass a char buffer without passing a length. |
| 315 | */ |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 316 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 317 | PyFloat_AsString(char *buf, PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 318 | { |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 319 | format_float(buf, 100, v, PREC_STR); |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 322 | void |
| 323 | PyFloat_AsReprString(char *buf, PyFloatObject *v) |
| 324 | { |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 325 | format_float(buf, 100, v, PREC_REPR); |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 328 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 329 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 330 | float_print(PyFloatObject *v, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | { |
| 332 | char buf[100]; |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 333 | format_float(buf, sizeof(buf), v, |
| 334 | (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | fputs(buf, fp); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 336 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 340 | float_repr(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | { |
| 342 | char buf[100]; |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 343 | format_float(buf, sizeof(buf), v, PREC_REPR); |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 344 | return PyString_FromString(buf); |
| 345 | } |
| 346 | |
| 347 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 348 | float_str(PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 349 | { |
| 350 | char buf[100]; |
Tim Peters | 97019e4 | 2001-11-28 22:43:45 +0000 | [diff] [blame] | 351 | format_float(buf, sizeof(buf), v, PREC_STR); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 352 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 355 | static PyObject* |
| 356 | float_richcompare(PyObject *v, PyObject *w, int op) |
| 357 | { |
| 358 | double i, j; |
| 359 | int r = 0; |
| 360 | |
| 361 | CONVERT_TO_DOUBLE(v, i); |
| 362 | CONVERT_TO_DOUBLE(w, j); |
| 363 | |
| 364 | PyFPE_START_PROTECT("richcompare", return NULL) |
| 365 | switch (op) { |
| 366 | case Py_EQ: |
| 367 | r = i==j; |
| 368 | break; |
| 369 | case Py_NE: |
| 370 | r = i!=j; |
| 371 | break; |
| 372 | case Py_LE: |
| 373 | r = i<=j; |
| 374 | break; |
| 375 | case Py_GE: |
| 376 | r = i>=j; |
| 377 | break; |
| 378 | case Py_LT: |
| 379 | r = i<j; |
| 380 | break; |
| 381 | case Py_GT: |
| 382 | r = i>j; |
| 383 | break; |
| 384 | } |
Michael W. Hudson | 957f977 | 2004-02-26 12:33:09 +0000 | [diff] [blame] | 385 | PyFPE_END_PROTECT(r) |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 386 | return PyBool_FromLong(r); |
| 387 | } |
| 388 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 389 | static long |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 390 | float_hash(PyFloatObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 391 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 392 | return _Py_HashDouble(v->ob_fval); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 396 | float_add(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 398 | double a,b; |
| 399 | CONVERT_TO_DOUBLE(v, a); |
| 400 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 401 | PyFPE_START_PROTECT("add", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 402 | a = a + b; |
| 403 | PyFPE_END_PROTECT(a) |
| 404 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 407 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 408 | float_sub(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 409 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 410 | double a,b; |
| 411 | CONVERT_TO_DOUBLE(v, a); |
| 412 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 413 | PyFPE_START_PROTECT("subtract", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 414 | a = a - b; |
| 415 | PyFPE_END_PROTECT(a) |
| 416 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 419 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 420 | float_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 421 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 422 | double a,b; |
| 423 | CONVERT_TO_DOUBLE(v, a); |
| 424 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 425 | PyFPE_START_PROTECT("multiply", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 426 | a = a * b; |
| 427 | PyFPE_END_PROTECT(a) |
| 428 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 431 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 432 | float_div(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 433 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 434 | double a,b; |
| 435 | CONVERT_TO_DOUBLE(v, a); |
| 436 | CONVERT_TO_DOUBLE(w, b); |
| 437 | if (b == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 438 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 441 | PyFPE_START_PROTECT("divide", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 442 | a = a / b; |
| 443 | PyFPE_END_PROTECT(a) |
| 444 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 447 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 448 | float_classic_div(PyObject *v, PyObject *w) |
| 449 | { |
| 450 | double a,b; |
| 451 | CONVERT_TO_DOUBLE(v, a); |
| 452 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 453 | if (Py_DivisionWarningFlag >= 2 && |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 454 | PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0) |
| 455 | return NULL; |
| 456 | if (b == 0.0) { |
| 457 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
| 458 | return NULL; |
| 459 | } |
| 460 | PyFPE_START_PROTECT("divide", return 0) |
| 461 | a = a / b; |
| 462 | PyFPE_END_PROTECT(a) |
| 463 | return PyFloat_FromDouble(a); |
| 464 | } |
| 465 | |
| 466 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 467 | float_rem(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 468 | { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 469 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 470 | double mod; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 471 | CONVERT_TO_DOUBLE(v, vx); |
| 472 | CONVERT_TO_DOUBLE(w, wx); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 473 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 474 | PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 475 | return NULL; |
| 476 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 477 | PyFPE_START_PROTECT("modulo", return 0) |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 478 | mod = fmod(vx, wx); |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 479 | /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 480 | 0 if wx < sqrt(smallest nonzero double) */ |
| 481 | if (mod && ((wx < 0) != (mod < 0))) { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 482 | mod += wx; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 484 | PyFPE_END_PROTECT(mod) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 485 | return PyFloat_FromDouble(mod); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 488 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 489 | float_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 490 | { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 491 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 492 | double div, mod, floordiv; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 493 | CONVERT_TO_DOUBLE(v, vx); |
| 494 | CONVERT_TO_DOUBLE(w, wx); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 495 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 496 | PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 497 | return NULL; |
| 498 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 499 | PyFPE_START_PROTECT("divmod", return 0) |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 500 | mod = fmod(vx, wx); |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 501 | /* fmod is typically exact, so vx-mod is *mathematically* an |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 502 | exact multiple of wx. But this is fp arithmetic, and fp |
| 503 | vx - mod is an approximation; the result is that div may |
| 504 | not be an exact integral value after the division, although |
| 505 | it will always be very close to one. |
| 506 | */ |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 507 | div = (vx - mod) / wx; |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 508 | if (mod) { |
| 509 | /* ensure the remainder has the same sign as the denominator */ |
| 510 | if ((wx < 0) != (mod < 0)) { |
| 511 | mod += wx; |
| 512 | div -= 1.0; |
| 513 | } |
| 514 | } |
| 515 | else { |
| 516 | /* the remainder is zero, and in the presence of signed zeroes |
| 517 | fmod returns different results across platforms; ensure |
| 518 | it has the same sign as the denominator; we'd like to do |
| 519 | "mod = wx * 0.0", but that may get optimized away */ |
Tim Peters | 4e8ab5d | 2001-11-01 23:59:56 +0000 | [diff] [blame] | 520 | mod *= mod; /* hide "mod = +0" from optimizer */ |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 521 | if (wx < 0.0) |
| 522 | mod = -mod; |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 523 | } |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 524 | /* snap quotient to nearest integral value */ |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 525 | if (div) { |
| 526 | floordiv = floor(div); |
| 527 | if (div - floordiv > 0.5) |
| 528 | floordiv += 1.0; |
| 529 | } |
| 530 | else { |
| 531 | /* div is zero - get the same sign as the true quotient */ |
| 532 | div *= div; /* hide "div = +0" from optimizers */ |
| 533 | floordiv = div * vx / wx; /* zero w/ sign of vx/wx */ |
| 534 | } |
| 535 | PyFPE_END_PROTECT(floordiv) |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 536 | return Py_BuildValue("(dd)", floordiv, mod); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 539 | static PyObject * |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 540 | float_floor_div(PyObject *v, PyObject *w) |
| 541 | { |
| 542 | PyObject *t, *r; |
| 543 | |
| 544 | t = float_divmod(v, w); |
Tim Peters | 77d8a4f | 2001-12-11 20:31:34 +0000 | [diff] [blame] | 545 | if (t == NULL || t == Py_NotImplemented) |
| 546 | return t; |
| 547 | assert(PyTuple_CheckExact(t)); |
| 548 | r = PyTuple_GET_ITEM(t, 0); |
| 549 | Py_INCREF(r); |
| 550 | Py_DECREF(t); |
| 551 | return r; |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 555 | float_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 556 | { |
| 557 | double iv, iw, ix; |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 558 | |
| 559 | if ((PyObject *)z != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 560 | PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not " |
Tim Peters | 97f4a33 | 2001-09-05 23:49:24 +0000 | [diff] [blame] | 561 | "allowed unless all arguments are integers"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
| 564 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 565 | CONVERT_TO_DOUBLE(v, iv); |
| 566 | CONVERT_TO_DOUBLE(w, iw); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 567 | |
| 568 | /* Sort out special cases here instead of relying on pow() */ |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 569 | if (iw == 0) { /* v**0 is 1, even 0**0 */ |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 570 | PyFPE_START_PROTECT("pow", return NULL) |
| 571 | if ((PyObject *)z != Py_None) { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 572 | double iz; |
Neil Schemenauer | 010b0cc | 2001-01-08 06:29:50 +0000 | [diff] [blame] | 573 | CONVERT_TO_DOUBLE(z, iz); |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 574 | ix = fmod(1.0, iz); |
| 575 | if (ix != 0 && iz < 0) |
| 576 | ix += iz; |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 577 | } |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 578 | else |
| 579 | ix = 1.0; |
| 580 | PyFPE_END_PROTECT(ix) |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 581 | return PyFloat_FromDouble(ix); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 582 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 583 | 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] | 584 | if (iw < 0.0) { |
| 585 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 586 | "0.0 cannot be raised to a negative power"); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 587 | return NULL; |
| 588 | } |
| 589 | return PyFloat_FromDouble(0.0); |
| 590 | } |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 591 | if (iv < 0.0) { |
| 592 | /* Whether this is an error is a mess, and bumps into libm |
| 593 | * bugs so we have to figure it out ourselves. |
| 594 | */ |
| 595 | if (iw != floor(iw)) { |
| 596 | PyErr_SetString(PyExc_ValueError, "negative number " |
| 597 | "cannot be raised to a fractional power"); |
| 598 | return NULL; |
| 599 | } |
| 600 | /* iw is an exact integer, albeit perhaps a very large one. |
| 601 | * -1 raised to an exact integer should never be exceptional. |
| 602 | * Alas, some libms (chiefly glibc as of early 2003) return |
| 603 | * NaN and set EDOM on pow(-1, large_int) if the int doesn't |
| 604 | * happen to be representable in a *C* integer. That's a |
| 605 | * bug; we let that slide in math.pow() (which currently |
| 606 | * reflects all platform accidents), but not for Python's **. |
| 607 | */ |
| 608 | if (iv == -1.0 && !Py_IS_INFINITY(iw) && iw == iw) { |
| 609 | /* XXX the "iw == iw" was to weed out NaNs. This |
| 610 | * XXX doesn't actually work on all platforms. |
| 611 | */ |
| 612 | /* Return 1 if iw is even, -1 if iw is odd; there's |
| 613 | * no guarantee that any C integral type is big |
| 614 | * enough to hold iw, so we have to check this |
| 615 | * indirectly. |
| 616 | */ |
| 617 | ix = floor(iw * 0.5) * 2.0; |
| 618 | return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0); |
| 619 | } |
| 620 | /* Else iv != -1.0, and overflow or underflow are possible. |
| 621 | * Unless we're to write pow() ourselves, we have to trust |
| 622 | * the platform to do this correctly. |
| 623 | */ |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 624 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 625 | errno = 0; |
| 626 | PyFPE_START_PROTECT("pow", return NULL) |
| 627 | ix = pow(iv, iw); |
| 628 | PyFPE_END_PROTECT(ix) |
Tim Peters | dc5a508 | 2002-03-09 04:58:24 +0000 | [diff] [blame] | 629 | Py_ADJUST_ERANGE1(ix); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 630 | if (errno != 0) { |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 631 | /* We don't expect any errno value other than ERANGE, but |
| 632 | * the range of libm bugs appears unbounded. |
| 633 | */ |
| 634 | PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError : |
| 635 | PyExc_ValueError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 636 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 637 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 638 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 641 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 642 | float_neg(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 643 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 644 | return PyFloat_FromDouble(-v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 647 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 648 | float_pos(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 649 | { |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 650 | if (PyFloat_CheckExact(v)) { |
| 651 | Py_INCREF(v); |
| 652 | return (PyObject *)v; |
| 653 | } |
| 654 | else |
| 655 | return PyFloat_FromDouble(v->ob_fval); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 658 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 659 | float_abs(PyFloatObject *v) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 660 | { |
Tim Peters | faf0cd2 | 2001-11-01 21:51:15 +0000 | [diff] [blame] | 661 | return PyFloat_FromDouble(fabs(v->ob_fval)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 664 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 665 | float_nonzero(PyFloatObject *v) |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 666 | { |
| 667 | return v->ob_fval != 0.0; |
| 668 | } |
| 669 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 670 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 671 | float_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 672 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 673 | if (PyInt_Check(*pw)) { |
| 674 | long x = PyInt_AsLong(*pw); |
| 675 | *pw = PyFloat_FromDouble((double)x); |
| 676 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 677 | return 0; |
| 678 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 679 | else if (PyLong_Check(*pw)) { |
Neal Norwitz | abcb0c0 | 2003-01-28 19:21:24 +0000 | [diff] [blame] | 680 | double x = PyLong_AsDouble(*pw); |
| 681 | if (x == -1.0 && PyErr_Occurred()) |
| 682 | return -1; |
| 683 | *pw = PyFloat_FromDouble(x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 684 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 685 | return 0; |
| 686 | } |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 687 | else if (PyFloat_Check(*pw)) { |
| 688 | Py_INCREF(*pv); |
| 689 | Py_INCREF(*pw); |
| 690 | return 0; |
| 691 | } |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 692 | return 1; /* Can't do it */ |
| 693 | } |
| 694 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 695 | static PyObject * |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 696 | float_long(PyObject *v) |
| 697 | { |
| 698 | double x = PyFloat_AsDouble(v); |
| 699 | return PyLong_FromDouble(x); |
| 700 | } |
| 701 | |
| 702 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 703 | float_int(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 704 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 705 | double x = PyFloat_AsDouble(v); |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 706 | double wholepart; /* integral portion of x, rounded toward 0 */ |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 707 | |
| 708 | (void)modf(x, &wholepart); |
Tim Peters | 7d79124 | 2002-11-21 22:26:37 +0000 | [diff] [blame] | 709 | /* Try to get out cheap if this fits in a Python int. The attempt |
| 710 | * to cast to long must be protected, as C doesn't define what |
| 711 | * happens if the double is too big to fit in a long. Some rare |
| 712 | * systems raise an exception then (RISCOS was mentioned as one, |
| 713 | * and someone using a non-default option on Sun also bumped into |
| 714 | * that). Note that checking for >= and <= LONG_{MIN,MAX} would |
| 715 | * still be vulnerable: if a long has more bits of precision than |
| 716 | * a double, casting MIN/MAX to double may yield an approximation, |
| 717 | * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would |
| 718 | * yield true from the C expression wholepart<=LONG_MAX, despite |
| 719 | * that wholepart is actually greater than LONG_MAX. |
| 720 | */ |
| 721 | if (LONG_MIN < wholepart && wholepart < LONG_MAX) { |
| 722 | const long aslong = (long)wholepart; |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 723 | return PyInt_FromLong(aslong); |
Tim Peters | 7d79124 | 2002-11-21 22:26:37 +0000 | [diff] [blame] | 724 | } |
| 725 | return PyLong_FromDouble(wholepart); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 728 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 729 | float_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 730 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 731 | Py_INCREF(v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 732 | return v; |
| 733 | } |
| 734 | |
| 735 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 736 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 737 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 738 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 739 | static PyObject * |
| 740 | float_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 741 | { |
| 742 | PyObject *x = Py_False; /* Integer zero */ |
| 743 | static char *kwlist[] = {"x", 0}; |
| 744 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 745 | if (type != &PyFloat_Type) |
| 746 | return float_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 747 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x)) |
| 748 | return NULL; |
| 749 | if (PyString_Check(x)) |
| 750 | return PyFloat_FromString(x, NULL); |
| 751 | return PyNumber_Float(x); |
| 752 | } |
| 753 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 754 | /* Wimpy, slow approach to tp_new calls for subtypes of float: |
| 755 | first create a regular float from whatever arguments we got, |
| 756 | then allocate a subtype instance and initialize its ob_fval |
| 757 | from the regular float. The regular float is then thrown away. |
| 758 | */ |
| 759 | static PyObject * |
| 760 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 761 | { |
| 762 | PyObject *tmp, *new; |
| 763 | |
| 764 | assert(PyType_IsSubtype(type, &PyFloat_Type)); |
| 765 | tmp = float_new(&PyFloat_Type, args, kwds); |
| 766 | if (tmp == NULL) |
| 767 | return NULL; |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 768 | assert(PyFloat_CheckExact(tmp)); |
Guido van Rossum | d93dce1 | 2001-08-30 03:09:31 +0000 | [diff] [blame] | 769 | new = type->tp_alloc(type, 0); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 770 | if (new == NULL) { |
| 771 | Py_DECREF(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 772 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 773 | } |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 774 | ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval; |
| 775 | Py_DECREF(tmp); |
| 776 | return new; |
| 777 | } |
| 778 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 779 | static PyObject * |
| 780 | float_getnewargs(PyFloatObject *v) |
| 781 | { |
| 782 | return Py_BuildValue("(d)", v->ob_fval); |
| 783 | } |
| 784 | |
| 785 | static PyMethodDef float_methods[] = { |
| 786 | {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS}, |
| 787 | {NULL, NULL} /* sentinel */ |
| 788 | }; |
| 789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 790 | PyDoc_STRVAR(float_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 791 | "float(x) -> floating point number\n\ |
| 792 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 793 | Convert a string or number to a floating point number, if possible."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 794 | |
| 795 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 796 | static PyNumberMethods float_as_number = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 797 | (binaryfunc)float_add, /*nb_add*/ |
| 798 | (binaryfunc)float_sub, /*nb_subtract*/ |
| 799 | (binaryfunc)float_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 800 | (binaryfunc)float_classic_div, /*nb_divide*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 801 | (binaryfunc)float_rem, /*nb_remainder*/ |
| 802 | (binaryfunc)float_divmod, /*nb_divmod*/ |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 803 | (ternaryfunc)float_pow, /*nb_power*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 804 | (unaryfunc)float_neg, /*nb_negative*/ |
| 805 | (unaryfunc)float_pos, /*nb_positive*/ |
| 806 | (unaryfunc)float_abs, /*nb_absolute*/ |
| 807 | (inquiry)float_nonzero, /*nb_nonzero*/ |
Guido van Rossum | 27acb33 | 1991-10-24 14:55:28 +0000 | [diff] [blame] | 808 | 0, /*nb_invert*/ |
| 809 | 0, /*nb_lshift*/ |
| 810 | 0, /*nb_rshift*/ |
| 811 | 0, /*nb_and*/ |
| 812 | 0, /*nb_xor*/ |
| 813 | 0, /*nb_or*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 814 | (coercion)float_coerce, /*nb_coerce*/ |
| 815 | (unaryfunc)float_int, /*nb_int*/ |
| 816 | (unaryfunc)float_long, /*nb_long*/ |
| 817 | (unaryfunc)float_float, /*nb_float*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 818 | 0, /* nb_oct */ |
| 819 | 0, /* nb_hex */ |
| 820 | 0, /* nb_inplace_add */ |
| 821 | 0, /* nb_inplace_subtract */ |
| 822 | 0, /* nb_inplace_multiply */ |
| 823 | 0, /* nb_inplace_divide */ |
| 824 | 0, /* nb_inplace_remainder */ |
| 825 | 0, /* nb_inplace_power */ |
| 826 | 0, /* nb_inplace_lshift */ |
| 827 | 0, /* nb_inplace_rshift */ |
| 828 | 0, /* nb_inplace_and */ |
| 829 | 0, /* nb_inplace_xor */ |
| 830 | 0, /* nb_inplace_or */ |
Tim Peters | 63a3571 | 2001-12-11 19:57:24 +0000 | [diff] [blame] | 831 | float_floor_div, /* nb_floor_divide */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 832 | float_div, /* nb_true_divide */ |
| 833 | 0, /* nb_inplace_floor_divide */ |
| 834 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 835 | }; |
| 836 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 837 | PyTypeObject PyFloat_Type = { |
| 838 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 839 | 0, |
| 840 | "float", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 841 | sizeof(PyFloatObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 842 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 843 | (destructor)float_dealloc, /* tp_dealloc */ |
| 844 | (printfunc)float_print, /* tp_print */ |
| 845 | 0, /* tp_getattr */ |
| 846 | 0, /* tp_setattr */ |
Michael W. Hudson | 08678a1 | 2004-05-26 17:36:12 +0000 | [diff] [blame] | 847 | 0, /* tp_compare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 848 | (reprfunc)float_repr, /* tp_repr */ |
| 849 | &float_as_number, /* tp_as_number */ |
| 850 | 0, /* tp_as_sequence */ |
| 851 | 0, /* tp_as_mapping */ |
| 852 | (hashfunc)float_hash, /* tp_hash */ |
| 853 | 0, /* tp_call */ |
| 854 | (reprfunc)float_str, /* tp_str */ |
| 855 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 856 | 0, /* tp_setattro */ |
| 857 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 858 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 859 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 860 | float_doc, /* tp_doc */ |
| 861 | 0, /* tp_traverse */ |
| 862 | 0, /* tp_clear */ |
Michael W. Hudson | d3b33b5 | 2004-02-19 19:35:22 +0000 | [diff] [blame] | 863 | (richcmpfunc)float_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 864 | 0, /* tp_weaklistoffset */ |
| 865 | 0, /* tp_iter */ |
| 866 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 867 | float_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 868 | 0, /* tp_members */ |
| 869 | 0, /* tp_getset */ |
| 870 | 0, /* tp_base */ |
| 871 | 0, /* tp_dict */ |
| 872 | 0, /* tp_descr_get */ |
| 873 | 0, /* tp_descr_set */ |
| 874 | 0, /* tp_dictoffset */ |
| 875 | 0, /* tp_init */ |
| 876 | 0, /* tp_alloc */ |
| 877 | float_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 878 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 879 | |
| 880 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 881 | PyFloat_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 882 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 883 | PyFloatObject *p; |
| 884 | PyFloatBlock *list, *next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 885 | int i; |
| 886 | int bc, bf; /* block count, number of freed blocks */ |
| 887 | int frem, fsum; /* remaining unfreed floats per block, total */ |
| 888 | |
| 889 | bc = 0; |
| 890 | bf = 0; |
| 891 | fsum = 0; |
| 892 | list = block_list; |
| 893 | block_list = NULL; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 894 | free_list = NULL; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 895 | while (list != NULL) { |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 896 | bc++; |
| 897 | frem = 0; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 898 | for (i = 0, p = &list->objects[0]; |
| 899 | i < N_FLOATOBJECTS; |
| 900 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 901 | if (PyFloat_CheckExact(p) && p->ob_refcnt != 0) |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 902 | frem++; |
| 903 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 904 | next = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 905 | if (frem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 906 | list->next = block_list; |
| 907 | block_list = list; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 908 | for (i = 0, p = &list->objects[0]; |
| 909 | i < N_FLOATOBJECTS; |
| 910 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 911 | if (!PyFloat_CheckExact(p) || |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 912 | p->ob_refcnt == 0) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 913 | p->ob_type = (struct _typeobject *) |
| 914 | free_list; |
| 915 | free_list = p; |
| 916 | } |
| 917 | } |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 918 | } |
| 919 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 920 | PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 921 | bf++; |
| 922 | } |
| 923 | fsum += frem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 924 | list = next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 925 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 926 | if (!Py_VerboseFlag) |
| 927 | return; |
| 928 | fprintf(stderr, "# cleanup floats"); |
| 929 | if (!fsum) { |
| 930 | fprintf(stderr, "\n"); |
| 931 | } |
| 932 | else { |
| 933 | fprintf(stderr, |
| 934 | ": %d unfreed float%s in %d out of %d block%s\n", |
| 935 | fsum, fsum == 1 ? "" : "s", |
| 936 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 937 | } |
| 938 | if (Py_VerboseFlag > 1) { |
| 939 | list = block_list; |
| 940 | while (list != NULL) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 941 | for (i = 0, p = &list->objects[0]; |
| 942 | i < N_FLOATOBJECTS; |
| 943 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 944 | if (PyFloat_CheckExact(p) && |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 945 | p->ob_refcnt != 0) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 946 | char buf[100]; |
| 947 | PyFloat_AsString(buf, p); |
| 948 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 949 | "# <float at %p, refcnt=%d, val=%s>\n", |
| 950 | p, p->ob_refcnt, buf); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | list = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 956 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 957 | |
| 958 | /*---------------------------------------------------------------------------- |
| 959 | * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h. |
| 960 | * |
| 961 | * TODO: On platforms that use the standard IEEE-754 single and double |
| 962 | * formats natively, these routines could simply copy the bytes. |
| 963 | */ |
| 964 | int |
| 965 | _PyFloat_Pack4(double x, unsigned char *p, int le) |
| 966 | { |
| 967 | unsigned char sign; |
| 968 | int e; |
| 969 | double f; |
| 970 | unsigned int fbits; |
| 971 | int incr = 1; |
| 972 | |
| 973 | if (le) { |
| 974 | p += 3; |
| 975 | incr = -1; |
| 976 | } |
| 977 | |
| 978 | if (x < 0) { |
| 979 | sign = 1; |
| 980 | x = -x; |
| 981 | } |
| 982 | else |
| 983 | sign = 0; |
| 984 | |
| 985 | f = frexp(x, &e); |
| 986 | |
| 987 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 988 | if (0.5 <= f && f < 1.0) { |
| 989 | f *= 2.0; |
| 990 | e--; |
| 991 | } |
| 992 | else if (f == 0.0) |
| 993 | e = 0; |
| 994 | else { |
| 995 | PyErr_SetString(PyExc_SystemError, |
| 996 | "frexp() result out of range"); |
| 997 | return -1; |
| 998 | } |
| 999 | |
| 1000 | if (e >= 128) |
| 1001 | goto Overflow; |
| 1002 | else if (e < -126) { |
| 1003 | /* Gradual underflow */ |
| 1004 | f = ldexp(f, 126 + e); |
| 1005 | e = 0; |
| 1006 | } |
| 1007 | else if (!(e == 0 && f == 0.0)) { |
| 1008 | e += 127; |
| 1009 | f -= 1.0; /* Get rid of leading 1 */ |
| 1010 | } |
| 1011 | |
| 1012 | f *= 8388608.0; /* 2**23 */ |
Tim Peters | f1ed934 | 2003-03-21 17:10:03 +0000 | [diff] [blame] | 1013 | fbits = (unsigned int)(f + 0.5); /* Round */ |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1014 | assert(fbits <= 8388608); |
| 1015 | if (fbits >> 23) { |
| 1016 | /* The carry propagated out of a string of 23 1 bits. */ |
| 1017 | fbits = 0; |
| 1018 | ++e; |
| 1019 | if (e >= 255) |
| 1020 | goto Overflow; |
| 1021 | } |
| 1022 | |
| 1023 | /* First byte */ |
| 1024 | *p = (sign << 7) | (e >> 1); |
| 1025 | p += incr; |
| 1026 | |
| 1027 | /* Second byte */ |
| 1028 | *p = (char) (((e & 1) << 7) | (fbits >> 16)); |
| 1029 | p += incr; |
| 1030 | |
| 1031 | /* Third byte */ |
| 1032 | *p = (fbits >> 8) & 0xFF; |
| 1033 | p += incr; |
| 1034 | |
| 1035 | /* Fourth byte */ |
| 1036 | *p = fbits & 0xFF; |
| 1037 | |
| 1038 | /* Done */ |
| 1039 | return 0; |
| 1040 | |
| 1041 | Overflow: |
| 1042 | PyErr_SetString(PyExc_OverflowError, |
| 1043 | "float too large to pack with f format"); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | int |
| 1048 | _PyFloat_Pack8(double x, unsigned char *p, int le) |
| 1049 | { |
| 1050 | unsigned char sign; |
| 1051 | int e; |
| 1052 | double f; |
| 1053 | unsigned int fhi, flo; |
| 1054 | int incr = 1; |
| 1055 | |
| 1056 | if (le) { |
| 1057 | p += 7; |
| 1058 | incr = -1; |
| 1059 | } |
| 1060 | |
| 1061 | if (x < 0) { |
| 1062 | sign = 1; |
| 1063 | x = -x; |
| 1064 | } |
| 1065 | else |
| 1066 | sign = 0; |
| 1067 | |
| 1068 | f = frexp(x, &e); |
| 1069 | |
| 1070 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 1071 | if (0.5 <= f && f < 1.0) { |
| 1072 | f *= 2.0; |
| 1073 | e--; |
| 1074 | } |
| 1075 | else if (f == 0.0) |
| 1076 | e = 0; |
| 1077 | else { |
| 1078 | PyErr_SetString(PyExc_SystemError, |
| 1079 | "frexp() result out of range"); |
| 1080 | return -1; |
| 1081 | } |
| 1082 | |
| 1083 | if (e >= 1024) |
| 1084 | goto Overflow; |
| 1085 | else if (e < -1022) { |
| 1086 | /* Gradual underflow */ |
| 1087 | f = ldexp(f, 1022 + e); |
| 1088 | e = 0; |
| 1089 | } |
| 1090 | else if (!(e == 0 && f == 0.0)) { |
| 1091 | e += 1023; |
| 1092 | f -= 1.0; /* Get rid of leading 1 */ |
| 1093 | } |
| 1094 | |
| 1095 | /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */ |
| 1096 | f *= 268435456.0; /* 2**28 */ |
| 1097 | fhi = (unsigned int)f; /* Truncate */ |
| 1098 | assert(fhi < 268435456); |
| 1099 | |
| 1100 | f -= (double)fhi; |
| 1101 | f *= 16777216.0; /* 2**24 */ |
| 1102 | flo = (unsigned int)(f + 0.5); /* Round */ |
| 1103 | assert(flo <= 16777216); |
| 1104 | if (flo >> 24) { |
| 1105 | /* The carry propagated out of a string of 24 1 bits. */ |
| 1106 | flo = 0; |
| 1107 | ++fhi; |
| 1108 | if (fhi >> 28) { |
| 1109 | /* And it also progagated out of the next 28 bits. */ |
| 1110 | fhi = 0; |
| 1111 | ++e; |
| 1112 | if (e >= 2047) |
| 1113 | goto Overflow; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /* First byte */ |
| 1118 | *p = (sign << 7) | (e >> 4); |
| 1119 | p += incr; |
| 1120 | |
| 1121 | /* Second byte */ |
| 1122 | *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24)); |
| 1123 | p += incr; |
| 1124 | |
| 1125 | /* Third byte */ |
| 1126 | *p = (fhi >> 16) & 0xFF; |
| 1127 | p += incr; |
| 1128 | |
| 1129 | /* Fourth byte */ |
| 1130 | *p = (fhi >> 8) & 0xFF; |
| 1131 | p += incr; |
| 1132 | |
| 1133 | /* Fifth byte */ |
| 1134 | *p = fhi & 0xFF; |
| 1135 | p += incr; |
| 1136 | |
| 1137 | /* Sixth byte */ |
| 1138 | *p = (flo >> 16) & 0xFF; |
| 1139 | p += incr; |
| 1140 | |
| 1141 | /* Seventh byte */ |
| 1142 | *p = (flo >> 8) & 0xFF; |
| 1143 | p += incr; |
| 1144 | |
| 1145 | /* Eighth byte */ |
| 1146 | *p = flo & 0xFF; |
| 1147 | p += incr; |
| 1148 | |
| 1149 | /* Done */ |
| 1150 | return 0; |
| 1151 | |
| 1152 | Overflow: |
| 1153 | PyErr_SetString(PyExc_OverflowError, |
| 1154 | "float too large to pack with d format"); |
| 1155 | return -1; |
| 1156 | } |
| 1157 | |
| 1158 | double |
| 1159 | _PyFloat_Unpack4(const unsigned char *p, int le) |
| 1160 | { |
| 1161 | unsigned char sign; |
| 1162 | int e; |
| 1163 | unsigned int f; |
| 1164 | double x; |
| 1165 | int incr = 1; |
| 1166 | |
| 1167 | if (le) { |
| 1168 | p += 3; |
| 1169 | incr = -1; |
| 1170 | } |
| 1171 | |
| 1172 | /* First byte */ |
| 1173 | sign = (*p >> 7) & 1; |
| 1174 | e = (*p & 0x7F) << 1; |
| 1175 | p += incr; |
| 1176 | |
| 1177 | /* Second byte */ |
| 1178 | e |= (*p >> 7) & 1; |
| 1179 | f = (*p & 0x7F) << 16; |
| 1180 | p += incr; |
| 1181 | |
| 1182 | /* Third byte */ |
| 1183 | f |= *p << 8; |
| 1184 | p += incr; |
| 1185 | |
| 1186 | /* Fourth byte */ |
| 1187 | f |= *p; |
| 1188 | |
| 1189 | x = (double)f / 8388608.0; |
| 1190 | |
| 1191 | /* XXX This sadly ignores Inf/NaN issues */ |
| 1192 | if (e == 0) |
| 1193 | e = -126; |
| 1194 | else { |
| 1195 | x += 1.0; |
| 1196 | e -= 127; |
| 1197 | } |
| 1198 | x = ldexp(x, e); |
| 1199 | |
| 1200 | if (sign) |
| 1201 | x = -x; |
| 1202 | |
| 1203 | return x; |
| 1204 | } |
| 1205 | |
| 1206 | double |
| 1207 | _PyFloat_Unpack8(const unsigned char *p, int le) |
| 1208 | { |
| 1209 | unsigned char sign; |
| 1210 | int e; |
| 1211 | unsigned int fhi, flo; |
| 1212 | double x; |
| 1213 | int incr = 1; |
| 1214 | |
| 1215 | if (le) { |
| 1216 | p += 7; |
| 1217 | incr = -1; |
| 1218 | } |
| 1219 | |
| 1220 | /* First byte */ |
| 1221 | sign = (*p >> 7) & 1; |
| 1222 | e = (*p & 0x7F) << 4; |
| 1223 | p += incr; |
| 1224 | |
| 1225 | /* Second byte */ |
| 1226 | e |= (*p >> 4) & 0xF; |
| 1227 | fhi = (*p & 0xF) << 24; |
| 1228 | p += incr; |
| 1229 | |
| 1230 | /* Third byte */ |
| 1231 | fhi |= *p << 16; |
| 1232 | p += incr; |
| 1233 | |
| 1234 | /* Fourth byte */ |
| 1235 | fhi |= *p << 8; |
| 1236 | p += incr; |
| 1237 | |
| 1238 | /* Fifth byte */ |
| 1239 | fhi |= *p; |
| 1240 | p += incr; |
| 1241 | |
| 1242 | /* Sixth byte */ |
| 1243 | flo = *p << 16; |
| 1244 | p += incr; |
| 1245 | |
| 1246 | /* Seventh byte */ |
| 1247 | flo |= *p << 8; |
| 1248 | p += incr; |
| 1249 | |
| 1250 | /* Eighth byte */ |
| 1251 | flo |= *p; |
| 1252 | |
| 1253 | x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 1254 | x /= 268435456.0; /* 2**28 */ |
| 1255 | |
| 1256 | /* XXX This sadly ignores Inf/NaN */ |
| 1257 | if (e == 0) |
| 1258 | e = -1022; |
| 1259 | else { |
| 1260 | x += 1.0; |
| 1261 | e -= 1023; |
| 1262 | } |
| 1263 | x = ldexp(x, e); |
| 1264 | |
| 1265 | if (sign) |
| 1266 | x = -x; |
| 1267 | |
| 1268 | return x; |
| 1269 | } |