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), |
| 117 | s_buffer, |
| 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 | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 185 | op->ob_type = (struct _typeobject *)free_list; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 186 | free_list = op; |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | double |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 190 | PyFloat_AsDouble(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 191 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 192 | PyNumberMethods *nb; |
| 193 | PyFloatObject *fo; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | double val; |
| 195 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 196 | if (op && PyFloat_Check(op)) |
| 197 | return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | |
| 199 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 200 | nb->nb_float == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 201 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | return -1; |
| 203 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 205 | fo = (PyFloatObject*) (*nb->nb_float) (op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | if (fo == NULL) |
| 207 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 208 | if (!PyFloat_Check(fo)) { |
| 209 | PyErr_SetString(PyExc_TypeError, |
| 210 | "nb_float should return float object"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | return -1; |
| 212 | } |
| 213 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | val = PyFloat_AS_DOUBLE(fo); |
| 215 | Py_DECREF(fo); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 216 | |
| 217 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* Methods */ |
| 221 | |
Guido van Rossum | 27dec7e | 1991-06-04 19:42:53 +0000 | [diff] [blame] | 222 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 223 | PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | { |
| 225 | register char *cp; |
| 226 | /* Subroutine for float_repr and float_print. |
| 227 | We want float numbers to be recognizable as such, |
| 228 | i.e., they should contain a decimal point or an exponent. |
| 229 | However, %g may print the number as an integer; |
| 230 | in such cases, we append ".0" to the string. */ |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 231 | sprintf(buf, "%.*g", precision, v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 232 | cp = buf; |
| 233 | if (*cp == '-') |
| 234 | cp++; |
| 235 | for (; *cp != '\0'; cp++) { |
| 236 | /* Any non-digit means it's not an integer; |
| 237 | this takes care of NAN and INF as well. */ |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 238 | if (!isdigit(Py_CHARMASK(*cp))) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | if (*cp == '\0') { |
| 242 | *cp++ = '.'; |
| 243 | *cp++ = '0'; |
| 244 | *cp++ = '\0'; |
| 245 | } |
| 246 | } |
| 247 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 248 | /* Macro and helper that convert PyObject obj to a C double and store |
| 249 | the value in dbl; this replaces the functionality of the coercion |
| 250 | slot function */ |
| 251 | |
| 252 | #define CONVERT_TO_DOUBLE(obj, dbl) \ |
| 253 | if (PyFloat_Check(obj)) \ |
| 254 | dbl = PyFloat_AS_DOUBLE(obj); \ |
| 255 | else if (convert_to_double(&(obj), &(dbl)) < 0) \ |
| 256 | return obj; |
| 257 | |
| 258 | static int |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 259 | convert_to_double(PyObject **v, double *dbl) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 260 | { |
| 261 | register PyObject *obj = *v; |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 262 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 263 | if (PyInt_Check(obj)) { |
| 264 | *dbl = (double)PyInt_AS_LONG(obj); |
| 265 | } |
| 266 | else if (PyLong_Check(obj)) { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 267 | *dbl = PyLong_AsDouble(obj); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 268 | if (*dbl == -1.0 && PyErr_Occurred()) { |
| 269 | *v = NULL; |
| 270 | return -1; |
| 271 | } |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 272 | } |
| 273 | else { |
| 274 | Py_INCREF(Py_NotImplemented); |
| 275 | *v = Py_NotImplemented; |
| 276 | return -1; |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 281 | /* Precisions used by repr() and str(), respectively. |
| 282 | |
| 283 | The repr() precision (17 significant decimal digits) is the minimal number |
| 284 | that is guaranteed to have enough precision so that if the number is read |
| 285 | back in the exact same binary value is recreated. This is true for IEEE |
| 286 | floating point by design, and also happens to work for all other modern |
| 287 | hardware. |
| 288 | |
| 289 | The str() precision is chosen so that in most cases, the rounding noise |
| 290 | created by various operations is suppressed, while giving plenty of |
| 291 | precision for practical use. |
| 292 | |
| 293 | */ |
| 294 | |
| 295 | #define PREC_REPR 17 |
| 296 | #define PREC_STR 12 |
| 297 | |
| 298 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 299 | PyFloat_AsString(char *buf, PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 300 | { |
| 301 | PyFloat_AsStringEx(buf, v, PREC_STR); |
| 302 | } |
| 303 | |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 304 | void |
| 305 | PyFloat_AsReprString(char *buf, PyFloatObject *v) |
| 306 | { |
| 307 | PyFloat_AsStringEx(buf, v, PREC_REPR); |
| 308 | } |
| 309 | |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 310 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 311 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 312 | float_print(PyFloatObject *v, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | { |
| 314 | char buf[100]; |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 315 | 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] | 316 | fputs(buf, fp); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 317 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 321 | float_repr(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | { |
| 323 | char buf[100]; |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 324 | PyFloat_AsStringEx(buf, v, PREC_REPR); |
| 325 | return PyString_FromString(buf); |
| 326 | } |
| 327 | |
| 328 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 329 | float_str(PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 330 | { |
| 331 | char buf[100]; |
| 332 | PyFloat_AsStringEx(buf, v, PREC_STR); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 337 | float_compare(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | { |
| 339 | double i = v->ob_fval; |
| 340 | double j = w->ob_fval; |
| 341 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 342 | } |
| 343 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 344 | static long |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 345 | float_hash(PyFloatObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 346 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 347 | return _Py_HashDouble(v->ob_fval); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 350 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 351 | float_add(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 353 | double a,b; |
| 354 | CONVERT_TO_DOUBLE(v, a); |
| 355 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 356 | PyFPE_START_PROTECT("add", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 357 | a = a + b; |
| 358 | PyFPE_END_PROTECT(a) |
| 359 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 362 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 363 | float_sub(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 365 | double a,b; |
| 366 | CONVERT_TO_DOUBLE(v, a); |
| 367 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 368 | PyFPE_START_PROTECT("subtract", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 369 | a = a - b; |
| 370 | PyFPE_END_PROTECT(a) |
| 371 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 375 | float_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 377 | double a,b; |
| 378 | CONVERT_TO_DOUBLE(v, a); |
| 379 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 380 | PyFPE_START_PROTECT("multiply", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 381 | a = a * b; |
| 382 | PyFPE_END_PROTECT(a) |
| 383 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 386 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 387 | float_div(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 389 | double a,b; |
| 390 | CONVERT_TO_DOUBLE(v, a); |
| 391 | CONVERT_TO_DOUBLE(w, b); |
| 392 | if (b == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 393 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 394 | return NULL; |
| 395 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 396 | PyFPE_START_PROTECT("divide", return 0) |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 397 | a = a / b; |
| 398 | PyFPE_END_PROTECT(a) |
| 399 | return PyFloat_FromDouble(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 402 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 403 | float_classic_div(PyObject *v, PyObject *w) |
| 404 | { |
| 405 | double a,b; |
| 406 | CONVERT_TO_DOUBLE(v, a); |
| 407 | CONVERT_TO_DOUBLE(w, b); |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 408 | if (Py_DivisionWarningFlag >= 2 && |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 409 | PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0) |
| 410 | return NULL; |
| 411 | if (b == 0.0) { |
| 412 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
| 413 | return NULL; |
| 414 | } |
| 415 | PyFPE_START_PROTECT("divide", return 0) |
| 416 | a = a / b; |
| 417 | PyFPE_END_PROTECT(a) |
| 418 | return PyFloat_FromDouble(a); |
| 419 | } |
| 420 | |
| 421 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 422 | float_rem(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 423 | { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 424 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 425 | double mod; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 426 | CONVERT_TO_DOUBLE(v, vx); |
| 427 | CONVERT_TO_DOUBLE(w, wx); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 428 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 429 | PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 430 | return NULL; |
| 431 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 432 | PyFPE_START_PROTECT("modulo", return 0) |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 433 | mod = fmod(vx, wx); |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 434 | /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 435 | 0 if wx < sqrt(smallest nonzero double) */ |
| 436 | if (mod && ((wx < 0) != (mod < 0))) { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 437 | mod += wx; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 438 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 439 | PyFPE_END_PROTECT(mod) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 440 | return PyFloat_FromDouble(mod); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 444 | float_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 445 | { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 446 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 447 | double div, mod, floordiv; |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 448 | CONVERT_TO_DOUBLE(v, vx); |
| 449 | CONVERT_TO_DOUBLE(w, wx); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 450 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 452 | return NULL; |
| 453 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 454 | PyFPE_START_PROTECT("divmod", return 0) |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 455 | mod = fmod(vx, wx); |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 456 | /* fmod is typically exact, so vx-mod is *mathematically* an |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 457 | exact multiple of wx. But this is fp arithmetic, and fp |
| 458 | vx - mod is an approximation; the result is that div may |
| 459 | not be an exact integral value after the division, although |
| 460 | it will always be very close to one. |
| 461 | */ |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 462 | div = (vx - mod) / wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 463 | /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 464 | 0 if wx < sqrt(smallest nonzero double) */ |
| 465 | if (mod && ((wx < 0) != (mod < 0))) { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 466 | mod += wx; |
| 467 | div -= 1.0; |
| 468 | } |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 469 | /* snap quotient to nearest integral value */ |
| 470 | floordiv = floor(div); |
| 471 | if (div - floordiv > 0.5) |
| 472 | floordiv += 1.0; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 473 | PyFPE_END_PROTECT(div) |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 474 | return Py_BuildValue("(dd)", floordiv, mod); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 477 | static PyObject * |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 478 | float_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 479 | { |
| 480 | double iv, iw, ix; |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 481 | |
| 482 | if ((PyObject *)z != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 483 | PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not " |
Tim Peters | 97f4a33 | 2001-09-05 23:49:24 +0000 | [diff] [blame] | 484 | "allowed unless all arguments are integers"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 485 | return NULL; |
| 486 | } |
| 487 | |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 488 | CONVERT_TO_DOUBLE(v, iv); |
| 489 | CONVERT_TO_DOUBLE(w, iw); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 490 | |
| 491 | /* Sort out special cases here instead of relying on pow() */ |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 492 | if (iw == 0) { /* v**0 is 1, even 0**0 */ |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 493 | PyFPE_START_PROTECT("pow", return NULL) |
| 494 | if ((PyObject *)z != Py_None) { |
Neil Schemenauer | 32117e5 | 2001-01-04 01:44:34 +0000 | [diff] [blame] | 495 | double iz; |
Neil Schemenauer | 010b0cc | 2001-01-08 06:29:50 +0000 | [diff] [blame] | 496 | CONVERT_TO_DOUBLE(z, iz); |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 497 | ix = fmod(1.0, iz); |
| 498 | if (ix != 0 && iz < 0) |
| 499 | ix += iz; |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 500 | } |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 501 | else |
| 502 | ix = 1.0; |
| 503 | PyFPE_END_PROTECT(ix) |
| 504 | return PyFloat_FromDouble(ix); |
| 505 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 506 | 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] | 507 | if (iw < 0.0) { |
| 508 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 509 | "0.0 cannot be raised to a negative power"); |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 510 | return NULL; |
| 511 | } |
| 512 | return PyFloat_FromDouble(0.0); |
| 513 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 514 | if (iv < 0.0 && iw != floor(iw)) { |
| 515 | PyErr_SetString(PyExc_ValueError, |
| 516 | "negative number cannot be raised to a fractional power"); |
| 517 | return NULL; |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 518 | } |
Tim Peters | 96685bf | 2001-08-23 22:31:37 +0000 | [diff] [blame] | 519 | errno = 0; |
| 520 | PyFPE_START_PROTECT("pow", return NULL) |
| 521 | ix = pow(iv, iw); |
| 522 | PyFPE_END_PROTECT(ix) |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 523 | Py_SET_ERANGE_IF_OVERFLOW(ix); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 524 | if (errno != 0) { |
| 525 | /* XXX could it be another type of error? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 526 | PyErr_SetFromErrno(PyExc_OverflowError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 527 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 528 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 529 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 532 | static PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 533 | float_int_div(PyObject *v, PyObject *w) |
| 534 | { |
| 535 | PyObject *t, *r; |
| 536 | |
| 537 | t = float_divmod(v, w); |
| 538 | if (t != NULL) { |
| 539 | r = PyTuple_GET_ITEM(t, 0); |
| 540 | Py_INCREF(r); |
| 541 | Py_DECREF(t); |
| 542 | return r; |
| 543 | } |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 548 | float_neg(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 549 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 550 | return PyFloat_FromDouble(-v->ob_fval); |
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 * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 554 | float_pos(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 555 | { |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 556 | if (PyFloat_CheckExact(v)) { |
| 557 | Py_INCREF(v); |
| 558 | return (PyObject *)v; |
| 559 | } |
| 560 | else |
| 561 | return PyFloat_FromDouble(v->ob_fval); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 565 | float_abs(PyFloatObject *v) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 566 | { |
| 567 | if (v->ob_fval < 0) |
| 568 | return float_neg(v); |
| 569 | else |
| 570 | return float_pos(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 573 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 574 | float_nonzero(PyFloatObject *v) |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 575 | { |
| 576 | return v->ob_fval != 0.0; |
| 577 | } |
| 578 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 579 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 580 | float_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 581 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | if (PyInt_Check(*pw)) { |
| 583 | long x = PyInt_AsLong(*pw); |
| 584 | *pw = PyFloat_FromDouble((double)x); |
| 585 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 586 | return 0; |
| 587 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 588 | else if (PyLong_Check(*pw)) { |
| 589 | *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw)); |
| 590 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | return 1; /* Can't do it */ |
| 594 | } |
| 595 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 596 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 597 | float_int(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 598 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 599 | double x = PyFloat_AsDouble(v); |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 600 | double wholepart; /* integral portion of x, rounded toward 0 */ |
| 601 | long aslong; /* (long)wholepart */ |
| 602 | |
| 603 | (void)modf(x, &wholepart); |
| 604 | /* doubles may have more bits than longs, or vice versa; and casting |
| 605 | to long may yield gibberish in either case. What really matters |
| 606 | is whether converting back to double again reproduces what we |
| 607 | started with. */ |
| 608 | aslong = (long)wholepart; |
| 609 | if ((double)aslong == wholepart) |
| 610 | return PyInt_FromLong(aslong); |
| 611 | PyErr_SetString(PyExc_OverflowError, "float too large to convert"); |
| 612 | return NULL; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 615 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 616 | float_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 617 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 618 | double x = PyFloat_AsDouble(v); |
| 619 | return PyLong_FromDouble(x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 622 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 623 | float_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 624 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | Py_INCREF(v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 626 | return v; |
| 627 | } |
| 628 | |
| 629 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 630 | staticforward PyObject * |
| 631 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 632 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 633 | static PyObject * |
| 634 | float_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 635 | { |
| 636 | PyObject *x = Py_False; /* Integer zero */ |
| 637 | static char *kwlist[] = {"x", 0}; |
| 638 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 639 | if (type != &PyFloat_Type) |
| 640 | return float_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 641 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x)) |
| 642 | return NULL; |
| 643 | if (PyString_Check(x)) |
| 644 | return PyFloat_FromString(x, NULL); |
| 645 | return PyNumber_Float(x); |
| 646 | } |
| 647 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 648 | /* Wimpy, slow approach to tp_new calls for subtypes of float: |
| 649 | first create a regular float from whatever arguments we got, |
| 650 | then allocate a subtype instance and initialize its ob_fval |
| 651 | from the regular float. The regular float is then thrown away. |
| 652 | */ |
| 653 | static PyObject * |
| 654 | float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 655 | { |
| 656 | PyObject *tmp, *new; |
| 657 | |
| 658 | assert(PyType_IsSubtype(type, &PyFloat_Type)); |
| 659 | tmp = float_new(&PyFloat_Type, args, kwds); |
| 660 | if (tmp == NULL) |
| 661 | return NULL; |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame^] | 662 | assert(PyFloat_CheckExact(tmp)); |
Guido van Rossum | d93dce1 | 2001-08-30 03:09:31 +0000 | [diff] [blame] | 663 | new = type->tp_alloc(type, 0); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 664 | if (new == NULL) |
| 665 | return NULL; |
| 666 | ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval; |
| 667 | Py_DECREF(tmp); |
| 668 | return new; |
| 669 | } |
| 670 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 671 | static char float_doc[] = |
| 672 | "float(x) -> floating point number\n\ |
| 673 | \n\ |
| 674 | Convert a string or number to a floating point number, if possible."; |
| 675 | |
| 676 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 677 | static PyNumberMethods float_as_number = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 678 | (binaryfunc)float_add, /*nb_add*/ |
| 679 | (binaryfunc)float_sub, /*nb_subtract*/ |
| 680 | (binaryfunc)float_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 681 | (binaryfunc)float_classic_div, /*nb_divide*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 682 | (binaryfunc)float_rem, /*nb_remainder*/ |
| 683 | (binaryfunc)float_divmod, /*nb_divmod*/ |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 684 | (ternaryfunc)float_pow, /*nb_power*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 685 | (unaryfunc)float_neg, /*nb_negative*/ |
| 686 | (unaryfunc)float_pos, /*nb_positive*/ |
| 687 | (unaryfunc)float_abs, /*nb_absolute*/ |
| 688 | (inquiry)float_nonzero, /*nb_nonzero*/ |
Guido van Rossum | 27acb33 | 1991-10-24 14:55:28 +0000 | [diff] [blame] | 689 | 0, /*nb_invert*/ |
| 690 | 0, /*nb_lshift*/ |
| 691 | 0, /*nb_rshift*/ |
| 692 | 0, /*nb_and*/ |
| 693 | 0, /*nb_xor*/ |
| 694 | 0, /*nb_or*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 695 | (coercion)float_coerce, /*nb_coerce*/ |
| 696 | (unaryfunc)float_int, /*nb_int*/ |
| 697 | (unaryfunc)float_long, /*nb_long*/ |
| 698 | (unaryfunc)float_float, /*nb_float*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 699 | 0, /* nb_oct */ |
| 700 | 0, /* nb_hex */ |
| 701 | 0, /* nb_inplace_add */ |
| 702 | 0, /* nb_inplace_subtract */ |
| 703 | 0, /* nb_inplace_multiply */ |
| 704 | 0, /* nb_inplace_divide */ |
| 705 | 0, /* nb_inplace_remainder */ |
| 706 | 0, /* nb_inplace_power */ |
| 707 | 0, /* nb_inplace_lshift */ |
| 708 | 0, /* nb_inplace_rshift */ |
| 709 | 0, /* nb_inplace_and */ |
| 710 | 0, /* nb_inplace_xor */ |
| 711 | 0, /* nb_inplace_or */ |
| 712 | float_int_div, /* nb_floor_divide */ |
| 713 | float_div, /* nb_true_divide */ |
| 714 | 0, /* nb_inplace_floor_divide */ |
| 715 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 716 | }; |
| 717 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 718 | PyTypeObject PyFloat_Type = { |
| 719 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 720 | 0, |
| 721 | "float", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 722 | sizeof(PyFloatObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 723 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 724 | (destructor)float_dealloc, /* tp_dealloc */ |
| 725 | (printfunc)float_print, /* tp_print */ |
| 726 | 0, /* tp_getattr */ |
| 727 | 0, /* tp_setattr */ |
| 728 | (cmpfunc)float_compare, /* tp_compare */ |
| 729 | (reprfunc)float_repr, /* tp_repr */ |
| 730 | &float_as_number, /* tp_as_number */ |
| 731 | 0, /* tp_as_sequence */ |
| 732 | 0, /* tp_as_mapping */ |
| 733 | (hashfunc)float_hash, /* tp_hash */ |
| 734 | 0, /* tp_call */ |
| 735 | (reprfunc)float_str, /* tp_str */ |
| 736 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 737 | 0, /* tp_setattro */ |
| 738 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 739 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 740 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 741 | float_doc, /* tp_doc */ |
| 742 | 0, /* tp_traverse */ |
| 743 | 0, /* tp_clear */ |
| 744 | 0, /* tp_richcompare */ |
| 745 | 0, /* tp_weaklistoffset */ |
| 746 | 0, /* tp_iter */ |
| 747 | 0, /* tp_iternext */ |
| 748 | 0, /* tp_methods */ |
| 749 | 0, /* tp_members */ |
| 750 | 0, /* tp_getset */ |
| 751 | 0, /* tp_base */ |
| 752 | 0, /* tp_dict */ |
| 753 | 0, /* tp_descr_get */ |
| 754 | 0, /* tp_descr_set */ |
| 755 | 0, /* tp_dictoffset */ |
| 756 | 0, /* tp_init */ |
| 757 | 0, /* tp_alloc */ |
| 758 | float_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 759 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 760 | |
| 761 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 762 | PyFloat_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 763 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 764 | PyFloatObject *p; |
| 765 | PyFloatBlock *list, *next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 766 | int i; |
| 767 | int bc, bf; /* block count, number of freed blocks */ |
| 768 | int frem, fsum; /* remaining unfreed floats per block, total */ |
| 769 | |
| 770 | bc = 0; |
| 771 | bf = 0; |
| 772 | fsum = 0; |
| 773 | list = block_list; |
| 774 | block_list = NULL; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 775 | free_list = NULL; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 776 | while (list != NULL) { |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 777 | bc++; |
| 778 | frem = 0; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 779 | for (i = 0, p = &list->objects[0]; |
| 780 | i < N_FLOATOBJECTS; |
| 781 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 782 | if (PyFloat_CheckExact(p) && p->ob_refcnt != 0) |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 783 | frem++; |
| 784 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 785 | next = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 786 | if (frem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 787 | list->next = block_list; |
| 788 | block_list = list; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 789 | for (i = 0, p = &list->objects[0]; |
| 790 | i < N_FLOATOBJECTS; |
| 791 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 792 | if (!PyFloat_CheckExact(p) || |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 793 | p->ob_refcnt == 0) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 794 | p->ob_type = (struct _typeobject *) |
| 795 | free_list; |
| 796 | free_list = p; |
| 797 | } |
| 798 | } |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 799 | } |
| 800 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 801 | PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 802 | bf++; |
| 803 | } |
| 804 | fsum += frem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 805 | list = next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 806 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 807 | if (!Py_VerboseFlag) |
| 808 | return; |
| 809 | fprintf(stderr, "# cleanup floats"); |
| 810 | if (!fsum) { |
| 811 | fprintf(stderr, "\n"); |
| 812 | } |
| 813 | else { |
| 814 | fprintf(stderr, |
| 815 | ": %d unfreed float%s in %d out of %d block%s\n", |
| 816 | fsum, fsum == 1 ? "" : "s", |
| 817 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 818 | } |
| 819 | if (Py_VerboseFlag > 1) { |
| 820 | list = block_list; |
| 821 | while (list != NULL) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 822 | for (i = 0, p = &list->objects[0]; |
| 823 | i < N_FLOATOBJECTS; |
| 824 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 825 | if (PyFloat_CheckExact(p) && |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 826 | p->ob_refcnt != 0) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 827 | char buf[100]; |
| 828 | PyFloat_AsString(buf, p); |
| 829 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 830 | "# <float at %p, refcnt=%d, val=%s>\n", |
| 831 | p, p->ob_refcnt, buf); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | list = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 837 | } |