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 | |
Guido van Rossum | 07e3a7e | 1995-02-27 10:13:37 +0000 | [diff] [blame] | 11 | #if !defined(__STDC__) && !defined(macintosh) |
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 | |
Martin v. Löwis | 01c6526 | 2001-03-06 12:14:54 +0000 | [diff] [blame] | 16 | #if defined(sun) && !defined(__SVR4) |
Guido van Rossum | 3c03fa8 | 1997-10-31 17:00:30 +0000 | [diff] [blame] | 17 | /* On SunOS4.1 only libm.a exists. Make sure that references to all |
| 18 | needed math functions exist in the executable, so that dynamic |
| 19 | loading of mathmodule does not fail. */ |
| 20 | double (*_Py_math_funcs_hack[])() = { |
| 21 | acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, |
| 22 | fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh |
| 23 | }; |
| 24 | #endif |
| 25 | |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 26 | /* Special free list -- see comments for same code in intobject.c. */ |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 27 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 28 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 29 | #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject)) |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 31 | struct _floatblock { |
| 32 | struct _floatblock *next; |
| 33 | PyFloatObject objects[N_FLOATOBJECTS]; |
| 34 | }; |
| 35 | |
| 36 | typedef struct _floatblock PyFloatBlock; |
| 37 | |
| 38 | static PyFloatBlock *block_list = NULL; |
| 39 | static PyFloatObject *free_list = NULL; |
| 40 | |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 41 | static PyFloatObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 42 | fill_free_list(void) |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 43 | { |
| 44 | PyFloatObject *p, *q; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 45 | /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */ |
| 46 | p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock)); |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 47 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 48 | return (PyFloatObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 49 | ((PyFloatBlock *)p)->next = block_list; |
| 50 | block_list = (PyFloatBlock *)p; |
| 51 | p = &((PyFloatBlock *)p)->objects[0]; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 52 | q = p + N_FLOATOBJECTS; |
| 53 | while (--q > p) |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 54 | q->ob_type = (struct _typeobject *)(q-1); |
| 55 | q->ob_type = NULL; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 56 | return p + N_FLOATOBJECTS - 1; |
| 57 | } |
| 58 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 60 | PyFloat_FromDouble(double fval) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | { |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 62 | register PyFloatObject *op; |
| 63 | if (free_list == NULL) { |
| 64 | if ((free_list = fill_free_list()) == NULL) |
| 65 | return NULL; |
| 66 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 67 | /* PyObject_New is inlined */ |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 68 | op = free_list; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 69 | free_list = (PyFloatObject *)op->ob_type; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 70 | PyObject_INIT(op, &PyFloat_Type); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 71 | op->ob_fval = fval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 72 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 75 | /************************************************************************** |
| 76 | RED_FLAG 22-Sep-2000 tim |
| 77 | PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG, |
| 78 | |
| 79 | 1. If v was a regular string, *pend was set to point to its terminating |
| 80 | null byte. That's useless (the caller can find that without any |
| 81 | help from this function!). |
| 82 | |
| 83 | 2. If v was a Unicode string, or an object convertible to a character |
| 84 | buffer, *pend was set to point into stack trash (the auto temp |
| 85 | vector holding the character buffer). That was downright dangerous. |
| 86 | |
| 87 | Since we can't change the interface of a public API function, pend is |
| 88 | still supported but now *officially* useless: if pend is not NULL, |
| 89 | *pend is set to NULL. |
| 90 | **************************************************************************/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 91 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 92 | PyFloat_FromString(PyObject *v, char **pend) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 93 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 94 | const char *s, *last, *end; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 95 | double x; |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 96 | char buffer[256]; /* for errors */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 97 | #ifdef Py_USING_UNICODE |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 98 | 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] | 99 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 100 | int len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 101 | |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 102 | if (pend) |
| 103 | *pend = NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 104 | if (PyString_Check(v)) { |
| 105 | s = PyString_AS_STRING(v); |
| 106 | len = PyString_GET_SIZE(v); |
| 107 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 108 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 109 | else if (PyUnicode_Check(v)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 110 | if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) { |
| 111 | PyErr_SetString(PyExc_ValueError, |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 112 | "Unicode float() literal too long to convert"); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 115 | if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 116 | PyUnicode_GET_SIZE(v), |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 117 | s_buffer, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 118 | NULL)) |
| 119 | return NULL; |
| 120 | s = s_buffer; |
Trent Mick | a248fb6 | 2000-08-12 21:37:39 +0000 | [diff] [blame] | 121 | len = (int)strlen(s); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 122 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 123 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 124 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
| 125 | PyErr_SetString(PyExc_TypeError, |
| 126 | "float() needs a string argument"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 127 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 128 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 130 | last = s + len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 131 | while (*s && isspace(Py_CHARMASK(*s))) |
| 132 | s++; |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 133 | if (*s == '\0') { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 134 | PyErr_SetString(PyExc_ValueError, "empty string for float()"); |
| 135 | return NULL; |
| 136 | } |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 137 | /* We don't care about overflow or underflow. If the platform supports |
| 138 | * them, infinities and signed zeroes (on underflow) are fine. |
| 139 | * However, strtod can return 0 for denormalized numbers, where atof |
| 140 | * does not. So (alas!) we special-case a zero result. Note that |
| 141 | * whether strtod sets errno on underflow is not defined, so we can't |
| 142 | * key off errno. |
| 143 | */ |
Tim Peters | 858346e | 2000-09-25 21:01:28 +0000 | [diff] [blame] | 144 | PyFPE_START_PROTECT("strtod", return NULL) |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 145 | x = strtod(s, (char **)&end); |
Tim Peters | 858346e | 2000-09-25 21:01:28 +0000 | [diff] [blame] | 146 | PyFPE_END_PROTECT(x) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 147 | errno = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 148 | /* 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] | 149 | 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] | 150 | if (end > last) |
| 151 | end = last; |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 152 | if (end == s) { |
| 153 | sprintf(buffer, "invalid literal for float(): %.200s", s); |
| 154 | PyErr_SetString(PyExc_ValueError, buffer); |
| 155 | return NULL; |
| 156 | } |
| 157 | /* Since end != s, the platform made *some* kind of sense out |
| 158 | of the input. Trust it. */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 159 | while (*end && isspace(Py_CHARMASK(*end))) |
| 160 | end++; |
| 161 | if (*end != '\0') { |
| 162 | sprintf(buffer, "invalid literal for float(): %.200s", s); |
| 163 | PyErr_SetString(PyExc_ValueError, buffer); |
| 164 | return NULL; |
| 165 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 166 | else if (end != last) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 167 | PyErr_SetString(PyExc_ValueError, |
| 168 | "null byte in argument for float()"); |
| 169 | return NULL; |
| 170 | } |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 171 | if (x == 0.0) { |
| 172 | /* See above -- may have been strtod being anal |
| 173 | about denorms. */ |
Tim Peters | 858346e | 2000-09-25 21:01:28 +0000 | [diff] [blame] | 174 | PyFPE_START_PROTECT("atof", return NULL) |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 175 | x = atof(s); |
Tim Peters | 858346e | 2000-09-25 21:01:28 +0000 | [diff] [blame] | 176 | PyFPE_END_PROTECT(x) |
Tim Peters | ef14d73 | 2000-09-23 03:39:17 +0000 | [diff] [blame] | 177 | errno = 0; /* whether atof ever set errno is undefined */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 178 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 179 | return PyFloat_FromDouble(x); |
| 180 | } |
| 181 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 182 | static void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 183 | float_dealloc(PyFloatObject *op) |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 184 | { |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 185 | if (PyFloat_CheckExact(op)) { |
| 186 | op->ob_type = (struct _typeobject *)free_list; |
| 187 | free_list = op; |
| 188 | } |
| 189 | else |
| 190 | op->ob_type->tp_free((PyObject *)op); |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | double |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 194 | PyFloat_AsDouble(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 196 | PyNumberMethods *nb; |
| 197 | PyFloatObject *fo; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | double val; |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 200 | if (op && PyFloat_Check(op)) |
| 201 | return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 204 | nb->nb_float == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 205 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | return -1; |
| 207 | } |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 209 | fo = (PyFloatObject*) (*nb->nb_float) (op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 210 | if (fo == NULL) |
| 211 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | if (!PyFloat_Check(fo)) { |
| 213 | PyErr_SetString(PyExc_TypeError, |
| 214 | "nb_float should return float object"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | return -1; |
| 216 | } |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 218 | val = PyFloat_AS_DOUBLE(fo); |
| 219 | Py_DECREF(fo); |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* Methods */ |
| 225 | |
Guido van Rossum | 27dec7e | 1991-06-04 19:42:53 +0000 | [diff] [blame] | 226 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 227 | PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | { |
| 229 | register char *cp; |
| 230 | /* Subroutine for float_repr and float_print. |
| 231 | We want float numbers to be recognizable as such, |
| 232 | i.e., they should contain a decimal point or an exponent. |
| 233 | However, %g may print the number as an integer; |
| 234 | in such cases, we append ".0" to the string. */ |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 235 | sprintf(buf, "%.*g", precision, v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | cp = buf; |
| 237 | if (*cp == '-') |
| 238 | cp++; |
| 239 | for (; *cp != '\0'; cp++) { |
| 240 | /* Any non-digit means it's not an integer; |
| 241 | this takes care of NAN and INF as well. */ |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 242 | if (!isdigit(Py_CHARMASK(*cp))) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | if (*cp == '\0') { |
| 246 | *cp++ = '.'; |
| 247 | *cp++ = '0'; |
| 248 | *cp++ = '\0'; |
| 249 | } |
| 250 | } |
| 251 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 252 | /* Macro and helper that convert PyObject obj to a C double and store |
| 253 | the value in dbl; this replaces the functionality of the coercion |
| 254 | slot function */ |
| 255 | |
| 256 | #define CONVERT_TO_DOUBLE(obj, dbl) \ |
| 257 | if (PyFloat_Check(obj)) \ |
| 258 | dbl = PyFloat_AS_DOUBLE(obj); \ |
| 259 | else if (convert_to_double(&(obj), &(dbl)) < 0) \ |
| 260 | return obj; |
| 261 | |
| 262 | static int |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 263 | convert_to_double(PyObject **v, double *dbl) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 264 | { |
| 265 | register PyObject *obj = *v; |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 266 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 267 | if (PyInt_Check(obj)) { |
| 268 | *dbl = (double)PyInt_AS_LONG(obj); |
| 269 | } |
| 270 | else if (PyLong_Check(obj)) { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 271 | *dbl = PyLong_AsDouble(obj); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 272 | if (*dbl == -1.0 && PyErr_Occurred()) { |
| 273 | *v = NULL; |
| 274 | return -1; |
| 275 | } |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 276 | } |
| 277 | else { |
| 278 | Py_INCREF(Py_NotImplemented); |
| 279 | *v = Py_NotImplemented; |
| 280 | return -1; |
| 281 | } |
| 282 | return 0; |
| 283 | } |
| 284 | |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 285 | /* Precisions used by repr() and str(), respectively. |
| 286 | |
| 287 | The repr() precision (17 significant decimal digits) is the minimal number |
| 288 | that is guaranteed to have enough precision so that if the number is read |
| 289 | back in the exact same binary value is recreated. This is true for IEEE |
| 290 | floating point by design, and also happens to work for all other modern |
| 291 | hardware. |
| 292 | |
| 293 | The str() precision is chosen so that in most cases, the rounding noise |
| 294 | created by various operations is suppressed, while giving plenty of |
| 295 | precision for practical use. |
| 296 | |
| 297 | */ |
| 298 | |
| 299 | #define PREC_REPR 17 |
| 300 | #define PREC_STR 12 |
| 301 | |
| 302 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 303 | PyFloat_AsString(char *buf, PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 304 | { |
| 305 | PyFloat_AsStringEx(buf, v, PREC_STR); |
| 306 | } |
| 307 | |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 308 | void |
| 309 | PyFloat_AsReprString(char *buf, PyFloatObject *v) |
| 310 | { |
| 311 | PyFloat_AsStringEx(buf, v, PREC_REPR); |
| 312 | } |
| 313 | |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 314 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 315 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 316 | float_print(PyFloatObject *v, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 317 | { |
| 318 | char buf[100]; |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 319 | PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | fputs(buf, fp); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 321 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 325 | float_repr(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | { |
| 327 | char buf[100]; |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 328 | PyFloat_AsStringEx(buf, v, PREC_REPR); |
| 329 | return PyString_FromString(buf); |
| 330 | } |
| 331 | |
| 332 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 333 | float_str(PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 334 | { |
| 335 | char buf[100]; |
| 336 | PyFloat_AsStringEx(buf, v, PREC_STR); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 341 | float_compare(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | { |
| 343 | double i = v->ob_fval; |
| 344 | double j = w->ob_fval; |
| 345 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 346 | } |
| 347 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 348 | static long |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 349 | float_hash(PyFloatObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 350 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 351 | return _Py_HashDouble(v->ob_fval); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 355 | float_add(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 356 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 357 | double a,b; |
| 358 | CONVERT_TO_DOUBLE(v, a); |
| 359 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 360 | PyFPE_START_PROTECT("add", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 361 | a = a + b; |
| 362 | PyFPE_END_PROTECT(a) |
| 363 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 366 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 367 | float_sub(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 368 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 369 | double a,b; |
| 370 | CONVERT_TO_DOUBLE(v, a); |
| 371 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 372 | PyFPE_START_PROTECT("subtract", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 373 | a = a - b; |
| 374 | PyFPE_END_PROTECT(a) |
| 375 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 378 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 379 | float_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 380 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 381 | double a,b; |
| 382 | CONVERT_TO_DOUBLE(v, a); |
| 383 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 384 | PyFPE_START_PROTECT("multiply", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 385 | a = a * b; |
| 386 | PyFPE_END_PROTECT(a) |
| 387 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 390 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 391 | float_div(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 393 | double a,b; |
| 394 | CONVERT_TO_DOUBLE(v, a); |
| 395 | CONVERT_TO_DOUBLE(w, b); |
| 396 | if (b == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 397 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 400 | PyFPE_START_PROTECT("divide", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 401 | a = a / b; |
| 402 | PyFPE_END_PROTECT(a) |
| 403 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 406 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 407 | float_classic_div(PyObject *v, PyObject *w) |
| 408 | { |
| 409 | double a,b; |
| 410 | CONVERT_TO_DOUBLE(v, a); |
| 411 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 412 | if (Py_DivisionWarningFlag >= 2 && |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 413 | PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0) |
| 414 | return NULL; |
| 415 | if (b == 0.0) { |
| 416 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
| 417 | return NULL; |
| 418 | } |
| 419 | PyFPE_START_PROTECT("divide", return 0) |
| 420 | a = a / b; |
| 421 | PyFPE_END_PROTECT(a) |
| 422 | return PyFloat_FromDouble(a); |
| 423 | } |
| 424 | |
| 425 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 426 | float_rem(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 427 | { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 428 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 429 | double mod; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 430 | CONVERT_TO_DOUBLE(v, vx); |
| 431 | CONVERT_TO_DOUBLE(w, wx); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 432 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 433 | PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 436 | PyFPE_START_PROTECT("modulo", return 0) |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 437 | mod = fmod(vx, wx); |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 438 | /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 439 | 0 if wx < sqrt(smallest nonzero double) */ |
| 440 | if (mod && ((wx < 0) != (mod < 0))) { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 441 | mod += wx; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 442 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 443 | PyFPE_END_PROTECT(mod) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 444 | return PyFloat_FromDouble(mod); |
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 * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 448 | float_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 449 | { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 450 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 451 | double div, mod, floordiv; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 452 | CONVERT_TO_DOUBLE(v, vx); |
| 453 | CONVERT_TO_DOUBLE(w, wx); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 454 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 455 | PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 458 | PyFPE_START_PROTECT("divmod", return 0) |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 459 | mod = fmod(vx, wx); |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 460 | /* fmod is typically exact, so vx-mod is *mathematically* an |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 461 | exact multiple of wx. But this is fp arithmetic, and fp |
| 462 | vx - mod is an approximation; the result is that div may |
| 463 | not be an exact integral value after the division, although |
| 464 | it will always be very close to one. |
| 465 | */ |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 466 | div = (vx - mod) / wx; |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 467 | if (mod) { |
| 468 | /* ensure the remainder has the same sign as the denominator */ |
| 469 | if ((wx < 0) != (mod < 0)) { |
| 470 | mod += wx; |
| 471 | div -= 1.0; |
| 472 | } |
| 473 | } |
| 474 | else { |
| 475 | /* the remainder is zero, and in the presence of signed zeroes |
| 476 | fmod returns different results across platforms; ensure |
| 477 | it has the same sign as the denominator; we'd like to do |
| 478 | "mod = wx * 0.0", but that may get optimized away */ |
Tim Peters | 4e8ab5d | 2001-11-01 23:59:56 +0000 | [diff] [blame] | 479 | mod *= mod; /* hide "mod = +0" from optimizer */ |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 480 | if (wx < 0.0) |
| 481 | mod = -mod; |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 482 | } |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 483 | /* snap quotient to nearest integral value */ |
Tim Peters | d2e40d6 | 2001-11-01 23:12:27 +0000 | [diff] [blame] | 484 | if (div) { |
| 485 | floordiv = floor(div); |
| 486 | if (div - floordiv > 0.5) |
| 487 | floordiv += 1.0; |
| 488 | } |
| 489 | else { |
| 490 | /* div is zero - get the same sign as the true quotient */ |
| 491 | div *= div; /* hide "div = +0" from optimizers */ |
| 492 | floordiv = div * vx / wx; /* zero w/ sign of vx/wx */ |
| 493 | } |
| 494 | PyFPE_END_PROTECT(floordiv) |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 495 | return Py_BuildValue("(dd)", floordiv, mod); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 498 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 499 | float_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | { |
| 501 | double iv, iw, ix; |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 502 | |
| 503 | if ((PyObject *)z != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 504 | PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not " |
Tim Peters | 97f4a33 | 2001-09-05 23:49:24 +0000 | [diff] [blame] | 505 | "allowed unless all arguments are integers"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 506 | return NULL; |
| 507 | } |
| 508 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 509 | CONVERT_TO_DOUBLE(v, iv); |
| 510 | CONVERT_TO_DOUBLE(w, iw); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 511 | |
| 512 | /* Sort out special cases here instead of relying on pow() */ |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 513 | if (iw == 0) { /* v**0 is 1, even 0**0 */ |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 514 | PyFPE_START_PROTECT("pow", return NULL) |
| 515 | if ((PyObject *)z != Py_None) { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 516 | double iz; |
Neil Schemenauer | 010b0cc | 2001-01-08 06:29:50 +0000 | [diff] [blame] | 517 | CONVERT_TO_DOUBLE(z, iz); |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 518 | ix = fmod(1.0, iz); |
| 519 | if (ix != 0 && iz < 0) |
| 520 | ix += iz; |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 521 | } |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 522 | else |
| 523 | ix = 1.0; |
| 524 | PyFPE_END_PROTECT(ix) |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 525 | return PyFloat_FromDouble(ix); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 526 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 527 | 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] | 528 | if (iw < 0.0) { |
| 529 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 530 | "0.0 cannot be raised to a negative power"); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 531 | return NULL; |
| 532 | } |
| 533 | return PyFloat_FromDouble(0.0); |
| 534 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 535 | if (iv < 0.0 && iw != floor(iw)) { |
| 536 | PyErr_SetString(PyExc_ValueError, |
| 537 | "negative number cannot be raised to a fractional power"); |
| 538 | return NULL; |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 539 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 540 | errno = 0; |
| 541 | PyFPE_START_PROTECT("pow", return NULL) |
| 542 | ix = pow(iv, iw); |
| 543 | PyFPE_END_PROTECT(ix) |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 544 | Py_SET_ERANGE_IF_OVERFLOW(ix); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 545 | if (errno != 0) { |
| 546 | /* XXX could it be another type of error? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 547 | PyErr_SetFromErrno(PyExc_OverflowError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 548 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 549 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 550 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 553 | static PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 554 | float_int_div(PyObject *v, PyObject *w) |
| 555 | { |
| 556 | PyObject *t, *r; |
Tim Peters | d2364e8 | 2001-11-01 20:09:42 +0000 | [diff] [blame] | 557 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 558 | t = float_divmod(v, w); |
| 559 | if (t != NULL) { |
| 560 | r = PyTuple_GET_ITEM(t, 0); |
| 561 | Py_INCREF(r); |
| 562 | Py_DECREF(t); |
| 563 | return r; |
| 564 | } |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 569 | float_neg(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 570 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 571 | return PyFloat_FromDouble(-v->ob_fval); |
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 * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 575 | float_pos(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 576 | { |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 577 | if (PyFloat_CheckExact(v)) { |
| 578 | Py_INCREF(v); |
| 579 | return (PyObject *)v; |
| 580 | } |
| 581 | else |
| 582 | return PyFloat_FromDouble(v->ob_fval); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 585 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 586 | float_abs(PyFloatObject *v) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 587 | { |
Tim Peters | faf0cd2 | 2001-11-01 21:51:15 +0000 | [diff] [blame] | 588 | return PyFloat_FromDouble(fabs(v->ob_fval)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 591 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 592 | float_nonzero(PyFloatObject *v) |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 593 | { |
| 594 | return v->ob_fval != 0.0; |
| 595 | } |
| 596 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 597 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 598 | float_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 599 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 600 | if (PyInt_Check(*pw)) { |
| 601 | long x = PyInt_AsLong(*pw); |
| 602 | *pw = PyFloat_FromDouble((double)x); |
| 603 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 604 | return 0; |
| 605 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | else if (PyLong_Check(*pw)) { |
| 607 | *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw)); |
| 608 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 609 | return 0; |
| 610 | } |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 611 | else if (PyFloat_Check(*pw)) { |
| 612 | Py_INCREF(*pv); |
| 613 | Py_INCREF(*pw); |
| 614 | return 0; |
| 615 | } |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 616 | return 1; /* Can't do it */ |
| 617 | } |
| 618 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 619 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 620 | float_int(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 621 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 622 | double x = PyFloat_AsDouble(v); |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 623 | double wholepart; /* integral portion of x, rounded toward 0 */ |
| 624 | long aslong; /* (long)wholepart */ |
| 625 | |
| 626 | (void)modf(x, &wholepart); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 627 | #ifdef RISCOS |
| 628 | /* conversion from floating to integral type would raise exception */ |
| 629 | if (wholepart>LONG_MAX || wholepart<LONG_MIN) { |
| 630 | PyErr_SetString(PyExc_OverflowError, "float too large to convert"); |
| 631 | return NULL; |
| 632 | } |
| 633 | #endif |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 634 | /* doubles may have more bits than longs, or vice versa; and casting |
| 635 | to long may yield gibberish in either case. What really matters |
| 636 | is whether converting back to double again reproduces what we |
| 637 | started with. */ |
| 638 | aslong = (long)wholepart; |
| 639 | if ((double)aslong == wholepart) |
| 640 | return PyInt_FromLong(aslong); |
| 641 | PyErr_SetString(PyExc_OverflowError, "float too large to convert"); |
| 642 | return NULL; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 645 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 646 | float_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 647 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 648 | double x = PyFloat_AsDouble(v); |
| 649 | return PyLong_FromDouble(x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 652 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 653 | float_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 654 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 655 | Py_INCREF(v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 656 | return v; |
| 657 | } |
| 658 | |
| 659 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 660 | staticforward PyObject * |
| 661 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 662 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 663 | static PyObject * |
| 664 | float_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 665 | { |
| 666 | PyObject *x = Py_False; /* Integer zero */ |
| 667 | static char *kwlist[] = {"x", 0}; |
| 668 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 669 | if (type != &PyFloat_Type) |
| 670 | return float_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 671 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x)) |
| 672 | return NULL; |
| 673 | if (PyString_Check(x)) |
| 674 | return PyFloat_FromString(x, NULL); |
| 675 | return PyNumber_Float(x); |
| 676 | } |
| 677 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 678 | /* Wimpy, slow approach to tp_new calls for subtypes of float: |
| 679 | first create a regular float from whatever arguments we got, |
| 680 | then allocate a subtype instance and initialize its ob_fval |
| 681 | from the regular float. The regular float is then thrown away. |
| 682 | */ |
| 683 | static PyObject * |
| 684 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 685 | { |
| 686 | PyObject *tmp, *new; |
| 687 | |
| 688 | assert(PyType_IsSubtype(type, &PyFloat_Type)); |
| 689 | tmp = float_new(&PyFloat_Type, args, kwds); |
| 690 | if (tmp == NULL) |
| 691 | return NULL; |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 692 | assert(PyFloat_CheckExact(tmp)); |
Guido van Rossum | d93dce1 | 2001-08-30 03:09:31 +0000 | [diff] [blame] | 693 | new = type->tp_alloc(type, 0); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 694 | if (new == NULL) |
| 695 | return NULL; |
| 696 | ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval; |
| 697 | Py_DECREF(tmp); |
| 698 | return new; |
| 699 | } |
| 700 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 701 | static char float_doc[] = |
| 702 | "float(x) -> floating point number\n\ |
| 703 | \n\ |
| 704 | Convert a string or number to a floating point number, if possible."; |
| 705 | |
| 706 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 707 | static PyNumberMethods float_as_number = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 708 | (binaryfunc)float_add, /*nb_add*/ |
| 709 | (binaryfunc)float_sub, /*nb_subtract*/ |
| 710 | (binaryfunc)float_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 711 | (binaryfunc)float_classic_div, /*nb_divide*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 712 | (binaryfunc)float_rem, /*nb_remainder*/ |
| 713 | (binaryfunc)float_divmod, /*nb_divmod*/ |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 714 | (ternaryfunc)float_pow, /*nb_power*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 715 | (unaryfunc)float_neg, /*nb_negative*/ |
| 716 | (unaryfunc)float_pos, /*nb_positive*/ |
| 717 | (unaryfunc)float_abs, /*nb_absolute*/ |
| 718 | (inquiry)float_nonzero, /*nb_nonzero*/ |
Guido van Rossum | 27acb33 | 1991-10-24 14:55:28 +0000 | [diff] [blame] | 719 | 0, /*nb_invert*/ |
| 720 | 0, /*nb_lshift*/ |
| 721 | 0, /*nb_rshift*/ |
| 722 | 0, /*nb_and*/ |
| 723 | 0, /*nb_xor*/ |
| 724 | 0, /*nb_or*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 725 | (coercion)float_coerce, /*nb_coerce*/ |
| 726 | (unaryfunc)float_int, /*nb_int*/ |
| 727 | (unaryfunc)float_long, /*nb_long*/ |
| 728 | (unaryfunc)float_float, /*nb_float*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 729 | 0, /* nb_oct */ |
| 730 | 0, /* nb_hex */ |
| 731 | 0, /* nb_inplace_add */ |
| 732 | 0, /* nb_inplace_subtract */ |
| 733 | 0, /* nb_inplace_multiply */ |
| 734 | 0, /* nb_inplace_divide */ |
| 735 | 0, /* nb_inplace_remainder */ |
| 736 | 0, /* nb_inplace_power */ |
| 737 | 0, /* nb_inplace_lshift */ |
| 738 | 0, /* nb_inplace_rshift */ |
| 739 | 0, /* nb_inplace_and */ |
| 740 | 0, /* nb_inplace_xor */ |
| 741 | 0, /* nb_inplace_or */ |
| 742 | float_int_div, /* nb_floor_divide */ |
| 743 | float_div, /* nb_true_divide */ |
| 744 | 0, /* nb_inplace_floor_divide */ |
| 745 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 746 | }; |
| 747 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 748 | PyTypeObject PyFloat_Type = { |
| 749 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 750 | 0, |
| 751 | "float", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 752 | sizeof(PyFloatObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 753 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 754 | (destructor)float_dealloc, /* tp_dealloc */ |
| 755 | (printfunc)float_print, /* tp_print */ |
| 756 | 0, /* tp_getattr */ |
| 757 | 0, /* tp_setattr */ |
| 758 | (cmpfunc)float_compare, /* tp_compare */ |
| 759 | (reprfunc)float_repr, /* tp_repr */ |
| 760 | &float_as_number, /* tp_as_number */ |
| 761 | 0, /* tp_as_sequence */ |
| 762 | 0, /* tp_as_mapping */ |
| 763 | (hashfunc)float_hash, /* tp_hash */ |
| 764 | 0, /* tp_call */ |
| 765 | (reprfunc)float_str, /* tp_str */ |
| 766 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 767 | 0, /* tp_setattro */ |
| 768 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 769 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 770 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 771 | float_doc, /* tp_doc */ |
| 772 | 0, /* tp_traverse */ |
| 773 | 0, /* tp_clear */ |
| 774 | 0, /* tp_richcompare */ |
| 775 | 0, /* tp_weaklistoffset */ |
| 776 | 0, /* tp_iter */ |
| 777 | 0, /* tp_iternext */ |
| 778 | 0, /* tp_methods */ |
| 779 | 0, /* tp_members */ |
| 780 | 0, /* tp_getset */ |
| 781 | 0, /* tp_base */ |
| 782 | 0, /* tp_dict */ |
| 783 | 0, /* tp_descr_get */ |
| 784 | 0, /* tp_descr_set */ |
| 785 | 0, /* tp_dictoffset */ |
| 786 | 0, /* tp_init */ |
| 787 | 0, /* tp_alloc */ |
| 788 | float_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 789 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 790 | |
| 791 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 792 | PyFloat_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 793 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 794 | PyFloatObject *p; |
| 795 | PyFloatBlock *list, *next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 796 | int i; |
| 797 | int bc, bf; /* block count, number of freed blocks */ |
| 798 | int frem, fsum; /* remaining unfreed floats per block, total */ |
| 799 | |
| 800 | bc = 0; |
| 801 | bf = 0; |
| 802 | fsum = 0; |
| 803 | list = block_list; |
| 804 | block_list = NULL; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 805 | free_list = NULL; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 806 | while (list != NULL) { |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 807 | bc++; |
| 808 | frem = 0; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 809 | for (i = 0, p = &list->objects[0]; |
| 810 | i < N_FLOATOBJECTS; |
| 811 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 812 | if (PyFloat_CheckExact(p) && p->ob_refcnt != 0) |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 813 | frem++; |
| 814 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 815 | next = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 816 | if (frem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 817 | list->next = block_list; |
| 818 | block_list = list; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 819 | for (i = 0, p = &list->objects[0]; |
| 820 | i < N_FLOATOBJECTS; |
| 821 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 822 | if (!PyFloat_CheckExact(p) || |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 823 | p->ob_refcnt == 0) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 824 | p->ob_type = (struct _typeobject *) |
| 825 | free_list; |
| 826 | free_list = p; |
| 827 | } |
| 828 | } |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 829 | } |
| 830 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 831 | PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 832 | bf++; |
| 833 | } |
| 834 | fsum += frem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 835 | list = next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 836 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 837 | if (!Py_VerboseFlag) |
| 838 | return; |
| 839 | fprintf(stderr, "# cleanup floats"); |
| 840 | if (!fsum) { |
| 841 | fprintf(stderr, "\n"); |
| 842 | } |
| 843 | else { |
| 844 | fprintf(stderr, |
| 845 | ": %d unfreed float%s in %d out of %d block%s\n", |
| 846 | fsum, fsum == 1 ? "" : "s", |
| 847 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 848 | } |
| 849 | if (Py_VerboseFlag > 1) { |
| 850 | list = block_list; |
| 851 | while (list != NULL) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 852 | for (i = 0, p = &list->objects[0]; |
| 853 | i < N_FLOATOBJECTS; |
| 854 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 855 | if (PyFloat_CheckExact(p) && |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 856 | p->ob_refcnt != 0) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 857 | char buf[100]; |
| 858 | PyFloat_AsString(buf, p); |
| 859 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 860 | "# <float at %p, refcnt=%d, val=%s>\n", |
| 861 | p, p->ob_refcnt, buf); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | list = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 867 | } |