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