Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Float object implementation */ |
| 12 | |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 13 | /* XXX There should be overflow checks here, but it's hard to check |
| 14 | for any kind of float exception without losing portability. */ |
| 15 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 16 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 20 | #ifdef i860 |
| 21 | /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
| 22 | #undef HUGE_VAL |
| 23 | #endif |
| 24 | |
Guido van Rossum | 9d81b55 | 1996-06-26 18:27:19 +0000 | [diff] [blame] | 25 | #if defined(HUGE_VAL) && !defined(CHECK) |
Guido van Rossum | 7fa52f8 | 1991-12-16 15:43:14 +0000 | [diff] [blame] | 26 | #define CHECK(x) if (errno != 0) ; \ |
| 27 | else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
| 28 | else errno = ERANGE |
Guido van Rossum | 9d81b55 | 1996-06-26 18:27:19 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | #ifndef CHECK |
Guido van Rossum | 7fa52f8 | 1991-12-16 15:43:14 +0000 | [diff] [blame] | 32 | #define CHECK(x) /* Don't know how to check */ |
| 33 | #endif |
| 34 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 35 | #ifdef HAVE_LIMITS_H |
| 36 | #include <limits.h> |
| 37 | #endif |
| 38 | |
| 39 | #ifndef LONG_MAX |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 40 | #if SIZEOF_LONG == 4 |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 41 | #define LONG_MAX 0X7FFFFFFFL |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 42 | #elif SIZEOF_LONG == 8 |
| 43 | #define LONG_MAX 0X7FFFFFFFFFFFFFFFL |
| 44 | #else |
| 45 | #error "could not set LONG_MAX" |
| 46 | #endif |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #ifndef LONG_MIN |
| 50 | #define LONG_MIN (-LONG_MAX-1) |
| 51 | #endif |
| 52 | |
Guido van Rossum | 67ca701 | 1995-02-13 16:38:41 +0000 | [diff] [blame] | 53 | #ifdef __NeXT__ |
| 54 | #ifdef __sparc__ |
| 55 | /* |
| 56 | * This works around a bug in the NS/Sparc 3.3 pre-release |
| 57 | * limits.h header file. |
| 58 | * 10-Feb-1995 bwarsaw@cnri.reston.va.us |
| 59 | */ |
| 60 | #undef LONG_MIN |
| 61 | #define LONG_MIN (-LONG_MAX-1) |
| 62 | #endif |
| 63 | #endif |
| 64 | |
Guido van Rossum | 07e3a7e | 1995-02-27 10:13:37 +0000 | [diff] [blame] | 65 | #if !defined(__STDC__) && !defined(macintosh) |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 66 | extern double fmod(double, double); |
| 67 | extern double pow(double, double); |
Guido van Rossum | 6923e13 | 1990-11-02 17:50:43 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Guido van Rossum | 3c03fa8 | 1997-10-31 17:00:30 +0000 | [diff] [blame] | 70 | #ifdef sun |
| 71 | /* On SunOS4.1 only libm.a exists. Make sure that references to all |
| 72 | needed math functions exist in the executable, so that dynamic |
| 73 | loading of mathmodule does not fail. */ |
| 74 | double (*_Py_math_funcs_hack[])() = { |
| 75 | acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, |
| 76 | fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh |
| 77 | }; |
| 78 | #endif |
| 79 | |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 80 | /* Special free list -- see comments for same code in intobject.c. */ |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 81 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 82 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 83 | #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject)) |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 85 | struct _floatblock { |
| 86 | struct _floatblock *next; |
| 87 | PyFloatObject objects[N_FLOATOBJECTS]; |
| 88 | }; |
| 89 | |
| 90 | typedef struct _floatblock PyFloatBlock; |
| 91 | |
| 92 | static PyFloatBlock *block_list = NULL; |
| 93 | static PyFloatObject *free_list = NULL; |
| 94 | |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 95 | static PyFloatObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 96 | fill_free_list(void) |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 97 | { |
| 98 | PyFloatObject *p, *q; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 99 | /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */ |
| 100 | p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock)); |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 101 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 102 | return (PyFloatObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 103 | ((PyFloatBlock *)p)->next = block_list; |
| 104 | block_list = (PyFloatBlock *)p; |
| 105 | p = &((PyFloatBlock *)p)->objects[0]; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 106 | q = p + N_FLOATOBJECTS; |
| 107 | while (--q > p) |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 108 | q->ob_type = (struct _typeobject *)(q-1); |
| 109 | q->ob_type = NULL; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 110 | return p + N_FLOATOBJECTS - 1; |
| 111 | } |
| 112 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 114 | PyFloat_FromDouble(double fval) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | { |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 116 | register PyFloatObject *op; |
| 117 | if (free_list == NULL) { |
| 118 | if ((free_list = fill_free_list()) == NULL) |
| 119 | return NULL; |
| 120 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 121 | /* PyObject_New is inlined */ |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 122 | op = free_list; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 123 | free_list = (PyFloatObject *)op->ob_type; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 124 | PyObject_INIT(op, &PyFloat_Type); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 125 | op->ob_fval = fval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 129 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 130 | PyFloat_FromString(PyObject *v, char **pend) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 131 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 132 | extern double strtod(const char *, char **); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 133 | const char *s, *last, *end; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 134 | double x; |
| 135 | char buffer[256]; /* For errors */ |
Barry Warsaw | 67c1a04 | 2000-08-18 05:00:03 +0000 | [diff] [blame] | 136 | char s_buffer[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 137 | int len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 139 | if (PyString_Check(v)) { |
| 140 | s = PyString_AS_STRING(v); |
| 141 | len = PyString_GET_SIZE(v); |
| 142 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 143 | else if (PyUnicode_Check(v)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 144 | if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) { |
| 145 | PyErr_SetString(PyExc_ValueError, |
| 146 | "float() literal too large to convert"); |
| 147 | return NULL; |
| 148 | } |
| 149 | if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
| 150 | PyUnicode_GET_SIZE(v), |
| 151 | s_buffer, |
| 152 | NULL)) |
| 153 | return NULL; |
| 154 | s = s_buffer; |
Trent Mick | a248fb6 | 2000-08-12 21:37:39 +0000 | [diff] [blame] | 155 | len = (int)strlen(s); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 156 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 157 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
| 158 | PyErr_SetString(PyExc_TypeError, |
| 159 | "float() needs a string argument"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 160 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 161 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 163 | last = s + len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 164 | while (*s && isspace(Py_CHARMASK(*s))) |
| 165 | s++; |
| 166 | if (s[0] == '\0') { |
| 167 | PyErr_SetString(PyExc_ValueError, "empty string for float()"); |
| 168 | return NULL; |
| 169 | } |
| 170 | errno = 0; |
| 171 | PyFPE_START_PROTECT("PyFloat_FromString", return 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 172 | x = strtod((char *)s, (char **)&end); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 173 | PyFPE_END_PROTECT(x) |
| 174 | /* Believe it or not, Solaris 2.6 can move end *beyond* the null |
| 175 | byte at the end of the string, when the input is inf(inity) */ |
| 176 | if (end > last) |
| 177 | end = last; |
| 178 | while (*end && isspace(Py_CHARMASK(*end))) |
| 179 | end++; |
| 180 | if (*end != '\0') { |
| 181 | sprintf(buffer, "invalid literal for float(): %.200s", s); |
| 182 | PyErr_SetString(PyExc_ValueError, buffer); |
| 183 | return NULL; |
| 184 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 185 | else if (end != last) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 186 | PyErr_SetString(PyExc_ValueError, |
| 187 | "null byte in argument for float()"); |
| 188 | return NULL; |
| 189 | } |
| 190 | else if (errno != 0) { |
| 191 | sprintf(buffer, "float() literal too large: %.200s", s); |
| 192 | PyErr_SetString(PyExc_ValueError, buffer); |
| 193 | return NULL; |
| 194 | } |
| 195 | if (pend) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 196 | *pend = (char *)end; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 197 | return PyFloat_FromDouble(x); |
| 198 | } |
| 199 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 200 | static void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 201 | float_dealloc(PyFloatObject *op) |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 202 | { |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 203 | op->ob_type = (struct _typeobject *)free_list; |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 204 | free_list = op; |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 207 | double |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 208 | PyFloat_AsDouble(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 210 | PyNumberMethods *nb; |
| 211 | PyFloatObject *fo; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | double val; |
| 213 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | if (op && PyFloat_Check(op)) |
| 215 | return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 216 | |
| 217 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 218 | nb->nb_float == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 219 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | return -1; |
| 221 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 223 | fo = (PyFloatObject*) (*nb->nb_float) (op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | if (fo == NULL) |
| 225 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 226 | if (!PyFloat_Check(fo)) { |
| 227 | PyErr_SetString(PyExc_TypeError, |
| 228 | "nb_float should return float object"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | return -1; |
| 230 | } |
| 231 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 232 | val = PyFloat_AS_DOUBLE(fo); |
| 233 | Py_DECREF(fo); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | |
| 235 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* Methods */ |
| 239 | |
Guido van Rossum | 27dec7e | 1991-06-04 19:42:53 +0000 | [diff] [blame] | 240 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 241 | PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 242 | { |
| 243 | register char *cp; |
| 244 | /* Subroutine for float_repr and float_print. |
| 245 | We want float numbers to be recognizable as such, |
| 246 | i.e., they should contain a decimal point or an exponent. |
| 247 | However, %g may print the number as an integer; |
| 248 | in such cases, we append ".0" to the string. */ |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 249 | sprintf(buf, "%.*g", precision, v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | cp = buf; |
| 251 | if (*cp == '-') |
| 252 | cp++; |
| 253 | for (; *cp != '\0'; cp++) { |
| 254 | /* Any non-digit means it's not an integer; |
| 255 | this takes care of NAN and INF as well. */ |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 256 | if (!isdigit(Py_CHARMASK(*cp))) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | if (*cp == '\0') { |
| 260 | *cp++ = '.'; |
| 261 | *cp++ = '0'; |
| 262 | *cp++ = '\0'; |
| 263 | } |
| 264 | } |
| 265 | |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 266 | /* Precisions used by repr() and str(), respectively. |
| 267 | |
| 268 | The repr() precision (17 significant decimal digits) is the minimal number |
| 269 | that is guaranteed to have enough precision so that if the number is read |
| 270 | back in the exact same binary value is recreated. This is true for IEEE |
| 271 | floating point by design, and also happens to work for all other modern |
| 272 | hardware. |
| 273 | |
| 274 | The str() precision is chosen so that in most cases, the rounding noise |
| 275 | created by various operations is suppressed, while giving plenty of |
| 276 | precision for practical use. |
| 277 | |
| 278 | */ |
| 279 | |
| 280 | #define PREC_REPR 17 |
| 281 | #define PREC_STR 12 |
| 282 | |
| 283 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 284 | PyFloat_AsString(char *buf, PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 285 | { |
| 286 | PyFloat_AsStringEx(buf, v, PREC_STR); |
| 287 | } |
| 288 | |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 289 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 290 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 291 | float_print(PyFloatObject *v, FILE *fp, int flags) |
| 292 | /* flags -- not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 293 | { |
| 294 | char buf[100]; |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 295 | 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] | 296 | fputs(buf, fp); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 297 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 300 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 301 | float_repr(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 302 | { |
| 303 | char buf[100]; |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 304 | PyFloat_AsStringEx(buf, v, PREC_REPR); |
| 305 | return PyString_FromString(buf); |
| 306 | } |
| 307 | |
| 308 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 309 | float_str(PyFloatObject *v) |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 310 | { |
| 311 | char buf[100]; |
| 312 | PyFloat_AsStringEx(buf, v, PREC_STR); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 317 | float_compare(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | { |
| 319 | double i = v->ob_fval; |
| 320 | double j = w->ob_fval; |
| 321 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 322 | } |
| 323 | |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 325 | static long |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 326 | float_hash(PyFloatObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 327 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 328 | return _Py_HashDouble(v->ob_fval); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 331 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 332 | float_add(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 334 | double result; |
| 335 | PyFPE_START_PROTECT("add", return 0) |
| 336 | result = v->ob_fval + w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 337 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 338 | return PyFloat_FromDouble(result); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 342 | float_sub(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 344 | double result; |
| 345 | PyFPE_START_PROTECT("subtract", return 0) |
| 346 | result = v->ob_fval - w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 347 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 348 | return PyFloat_FromDouble(result); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 352 | float_mul(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 353 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 354 | double result; |
| 355 | |
| 356 | PyFPE_START_PROTECT("multiply", return 0) |
| 357 | result = v->ob_fval * w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 358 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 359 | return PyFloat_FromDouble(result); |
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 * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 363 | float_div(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 365 | double result; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 366 | if (w->ob_fval == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 367 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 368 | return NULL; |
| 369 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 370 | PyFPE_START_PROTECT("divide", return 0) |
| 371 | result = v->ob_fval / w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 372 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 373 | return PyFloat_FromDouble(result); |
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 * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 377 | float_rem(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 378 | { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 379 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 380 | double mod; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 381 | wx = w->ob_fval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 382 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 383 | PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 384 | return NULL; |
| 385 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 386 | PyFPE_START_PROTECT("modulo", return 0) |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 387 | vx = v->ob_fval; |
| 388 | mod = fmod(vx, wx); |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 389 | /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 390 | 0 if wx < sqrt(smallest nonzero double) */ |
| 391 | if (mod && ((wx < 0) != (mod < 0))) { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 392 | mod += wx; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 393 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 394 | PyFPE_END_PROTECT(mod) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | return PyFloat_FromDouble(mod); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 398 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 399 | float_divmod(PyFloatObject *v, PyFloatObject *w) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 400 | { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 401 | double vx, wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 402 | double div, mod, floordiv; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 403 | wx = w->ob_fval; |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 404 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 405 | PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 406 | return NULL; |
| 407 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 408 | PyFPE_START_PROTECT("divmod", return 0) |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 409 | vx = v->ob_fval; |
| 410 | mod = fmod(vx, wx); |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 411 | /* fmod is typically exact, so vx-mod is *mathemtically* an |
| 412 | exact multiple of wx. But this is fp arithmetic, and fp |
| 413 | vx - mod is an approximation; the result is that div may |
| 414 | not be an exact integral value after the division, although |
| 415 | it will always be very close to one. |
| 416 | */ |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 417 | div = (vx - mod) / wx; |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 418 | /* note: checking mod*wx < 0 is incorrect -- underflows to |
| 419 | 0 if wx < sqrt(smallest nonzero double) */ |
| 420 | if (mod && ((wx < 0) != (mod < 0))) { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 421 | mod += wx; |
| 422 | div -= 1.0; |
| 423 | } |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 424 | /* snap quotient to nearest integral value */ |
| 425 | floordiv = floor(div); |
| 426 | if (div - floordiv > 0.5) |
| 427 | floordiv += 1.0; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 428 | PyFPE_END_PROTECT(div) |
Guido van Rossum | 9263e78 | 1999-05-06 14:26:34 +0000 | [diff] [blame] | 429 | return Py_BuildValue("(dd)", floordiv, mod); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 432 | static double powu(double x, long n) |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 433 | { |
| 434 | double r = 1.; |
| 435 | double p = x; |
| 436 | long mask = 1; |
| 437 | while (mask > 0 && n >= mask) { |
| 438 | if (n & mask) |
| 439 | r *= p; |
| 440 | mask <<= 1; |
| 441 | p *= p; |
| 442 | } |
| 443 | return r; |
| 444 | } |
| 445 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 446 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 447 | float_pow(PyFloatObject *v, PyObject *w, PyFloatObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 448 | { |
| 449 | double iv, iw, ix; |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 450 | long intw; |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 451 | /* XXX Doesn't handle overflows if z!=None yet; it may never do so :( |
| 452 | * The z parameter is really only going to be useful for integers and |
| 453 | * long integers. Maybe something clever with logarithms could be done. |
| 454 | * [AMK] |
| 455 | */ |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 456 | iv = v->ob_fval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 457 | iw = ((PyFloatObject *)w)->ob_fval; |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 458 | intw = (long)iw; |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 459 | if (iw == intw && -10000 < intw && intw < 10000) { |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 460 | /* Sort out special cases here instead of relying on pow() */ |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 461 | if (intw == 0) { /* x**0 is 1, even 0**0 */ |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 462 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 463 | if ((PyObject *)z!=Py_None) { |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 464 | ix=fmod(1.0, z->ob_fval); |
| 465 | if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval; |
| 466 | } |
| 467 | else ix=1.0; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 468 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 469 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 470 | } |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 471 | errno = 0; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 472 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 473 | if (intw > 0) |
| 474 | ix = powu(iv, intw); |
| 475 | else |
| 476 | ix = 1./powu(iv, -intw); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 477 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 478 | } |
| 479 | else { |
| 480 | /* Sort out special cases here instead of relying on pow() */ |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 481 | if (iv == 0.0) { |
| 482 | if (iw < 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 483 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 484 | "0.0 to a negative power"); |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 485 | return NULL; |
| 486 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 487 | return PyFloat_FromDouble(0.0); |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 488 | } |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 489 | if (iv < 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 490 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 491 | "negative number to a float power"); |
| 492 | return NULL; |
| 493 | } |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 494 | errno = 0; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 495 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 496 | ix = pow(iv, iw); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 497 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 498 | } |
Guido van Rossum | 7fa52f8 | 1991-12-16 15:43:14 +0000 | [diff] [blame] | 499 | CHECK(ix); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 500 | if (errno != 0) { |
| 501 | /* XXX could it be another type of error? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 502 | PyErr_SetFromErrno(PyExc_OverflowError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 503 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 504 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 505 | if ((PyObject *)z!=Py_None) { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 506 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 507 | ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */ |
| 508 | if ( ix!=0 && |
| 509 | ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) { |
| 510 | ix+=z->ob_fval; |
| 511 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 512 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 513 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 514 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 517 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 518 | float_neg(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 519 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 520 | return PyFloat_FromDouble(-v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 523 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 524 | float_pos(PyFloatObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 526 | Py_INCREF(v); |
| 527 | return (PyObject *)v; |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 530 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 531 | float_abs(PyFloatObject *v) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 532 | { |
| 533 | if (v->ob_fval < 0) |
| 534 | return float_neg(v); |
| 535 | else |
| 536 | return float_pos(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 539 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 540 | float_nonzero(PyFloatObject *v) |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 541 | { |
| 542 | return v->ob_fval != 0.0; |
| 543 | } |
| 544 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 545 | static int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 546 | float_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 547 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | if (PyInt_Check(*pw)) { |
| 549 | long x = PyInt_AsLong(*pw); |
| 550 | *pw = PyFloat_FromDouble((double)x); |
| 551 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 552 | return 0; |
| 553 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 554 | else if (PyLong_Check(*pw)) { |
| 555 | *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw)); |
| 556 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | return 1; /* Can't do it */ |
| 560 | } |
| 561 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 562 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 563 | float_int(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 564 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 565 | double x = PyFloat_AsDouble(v); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 566 | if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN |
| 567 | : (x = floor(x)) > (double)LONG_MAX) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | PyErr_SetString(PyExc_OverflowError, |
| 569 | "float too large to convert"); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 570 | return NULL; |
| 571 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 572 | return PyInt_FromLong((long)x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 575 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 576 | float_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 577 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 578 | double x = PyFloat_AsDouble(v); |
| 579 | return PyLong_FromDouble(x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 583 | float_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 584 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 585 | Py_INCREF(v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 586 | return v; |
| 587 | } |
| 588 | |
| 589 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 590 | static PyNumberMethods float_as_number = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 591 | (binaryfunc)float_add, /*nb_add*/ |
| 592 | (binaryfunc)float_sub, /*nb_subtract*/ |
| 593 | (binaryfunc)float_mul, /*nb_multiply*/ |
| 594 | (binaryfunc)float_div, /*nb_divide*/ |
| 595 | (binaryfunc)float_rem, /*nb_remainder*/ |
| 596 | (binaryfunc)float_divmod, /*nb_divmod*/ |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 597 | (ternaryfunc)float_pow, /*nb_power*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 598 | (unaryfunc)float_neg, /*nb_negative*/ |
| 599 | (unaryfunc)float_pos, /*nb_positive*/ |
| 600 | (unaryfunc)float_abs, /*nb_absolute*/ |
| 601 | (inquiry)float_nonzero, /*nb_nonzero*/ |
Guido van Rossum | 27acb33 | 1991-10-24 14:55:28 +0000 | [diff] [blame] | 602 | 0, /*nb_invert*/ |
| 603 | 0, /*nb_lshift*/ |
| 604 | 0, /*nb_rshift*/ |
| 605 | 0, /*nb_and*/ |
| 606 | 0, /*nb_xor*/ |
| 607 | 0, /*nb_or*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 608 | (coercion)float_coerce, /*nb_coerce*/ |
| 609 | (unaryfunc)float_int, /*nb_int*/ |
| 610 | (unaryfunc)float_long, /*nb_long*/ |
| 611 | (unaryfunc)float_float, /*nb_float*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 612 | 0, /*nb_oct*/ |
| 613 | 0, /*nb_hex*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 614 | }; |
| 615 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 616 | PyTypeObject PyFloat_Type = { |
| 617 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 618 | 0, |
| 619 | "float", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | sizeof(PyFloatObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 621 | 0, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 622 | (destructor)float_dealloc, /*tp_dealloc*/ |
| 623 | (printfunc)float_print, /*tp_print*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 624 | 0, /*tp_getattr*/ |
| 625 | 0, /*tp_setattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 626 | (cmpfunc)float_compare, /*tp_compare*/ |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 627 | (reprfunc)float_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | &float_as_number, /*tp_as_number*/ |
| 629 | 0, /*tp_as_sequence*/ |
| 630 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 57072eb | 1999-12-23 19:00:28 +0000 | [diff] [blame] | 631 | (hashfunc)float_hash, /*tp_hash*/ |
| 632 | 0, /*tp_call*/ |
| 633 | (reprfunc)float_str, /*tp_str*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 634 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 635 | |
| 636 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 637 | PyFloat_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 638 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 639 | PyFloatObject *p; |
| 640 | PyFloatBlock *list, *next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 641 | int i; |
| 642 | int bc, bf; /* block count, number of freed blocks */ |
| 643 | int frem, fsum; /* remaining unfreed floats per block, total */ |
| 644 | |
| 645 | bc = 0; |
| 646 | bf = 0; |
| 647 | fsum = 0; |
| 648 | list = block_list; |
| 649 | block_list = NULL; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 650 | free_list = NULL; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 651 | while (list != NULL) { |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 652 | bc++; |
| 653 | frem = 0; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 654 | for (i = 0, p = &list->objects[0]; |
| 655 | i < N_FLOATOBJECTS; |
| 656 | i++, p++) { |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 657 | if (PyFloat_Check(p) && p->ob_refcnt != 0) |
| 658 | frem++; |
| 659 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 660 | next = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 661 | if (frem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 662 | list->next = block_list; |
| 663 | block_list = list; |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 664 | for (i = 0, p = &list->objects[0]; |
| 665 | i < N_FLOATOBJECTS; |
| 666 | i++, p++) { |
| 667 | if (!PyFloat_Check(p) || p->ob_refcnt == 0) { |
| 668 | p->ob_type = (struct _typeobject *) |
| 669 | free_list; |
| 670 | free_list = p; |
| 671 | } |
| 672 | } |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 673 | } |
| 674 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 675 | PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 676 | bf++; |
| 677 | } |
| 678 | fsum += frem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 679 | list = next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 680 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 681 | if (!Py_VerboseFlag) |
| 682 | return; |
| 683 | fprintf(stderr, "# cleanup floats"); |
| 684 | if (!fsum) { |
| 685 | fprintf(stderr, "\n"); |
| 686 | } |
| 687 | else { |
| 688 | fprintf(stderr, |
| 689 | ": %d unfreed float%s in %d out of %d block%s\n", |
| 690 | fsum, fsum == 1 ? "" : "s", |
| 691 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 692 | } |
| 693 | if (Py_VerboseFlag > 1) { |
| 694 | list = block_list; |
| 695 | while (list != NULL) { |
Guido van Rossum | d7b5fb8 | 1999-03-19 20:59:40 +0000 | [diff] [blame] | 696 | for (i = 0, p = &list->objects[0]; |
| 697 | i < N_FLOATOBJECTS; |
| 698 | i++, p++) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 699 | if (PyFloat_Check(p) && p->ob_refcnt != 0) { |
| 700 | char buf[100]; |
| 701 | PyFloat_AsString(buf, p); |
| 702 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 703 | "# <float at %p, refcnt=%d, val=%s>\n", |
| 704 | p, p->ob_refcnt, buf); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | list = list->next; |
Guido van Rossum | f61bbc8 | 1999-03-12 00:12:21 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 710 | } |