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