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. */ |
| 97 | static PyFloatObject *free_list = NULL; |
| 98 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
| 99 | #define N_FLOATOBJECTS (BLOCK_SIZE / sizeof(PyFloatObject)) |
| 100 | |
| 101 | static PyFloatObject * |
| 102 | fill_free_list() |
| 103 | { |
| 104 | PyFloatObject *p, *q; |
| 105 | p = PyMem_NEW(PyFloatObject, N_FLOATOBJECTS); |
| 106 | if (p == NULL) |
| 107 | return (PyFloatObject *)PyErr_NoMemory(); |
| 108 | q = p + N_FLOATOBJECTS; |
| 109 | while (--q > p) |
| 110 | *(PyFloatObject **)q = q-1; |
| 111 | *(PyFloatObject **)q = NULL; |
| 112 | return p + N_FLOATOBJECTS - 1; |
| 113 | } |
| 114 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | PyObject * |
Guido van Rossum | 07e3a7e | 1995-02-27 10:13:37 +0000 | [diff] [blame] | 116 | #ifdef __SC__ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 117 | PyFloat_FromDouble(double fval) |
Guido van Rossum | 07e3a7e | 1995-02-27 10:13:37 +0000 | [diff] [blame] | 118 | #else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 119 | PyFloat_FromDouble(fval) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | double fval; |
Guido van Rossum | 07e3a7e | 1995-02-27 10:13:37 +0000 | [diff] [blame] | 121 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 122 | { |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 123 | register PyFloatObject *op; |
| 124 | if (free_list == NULL) { |
| 125 | if ((free_list = fill_free_list()) == NULL) |
| 126 | return NULL; |
| 127 | } |
| 128 | op = free_list; |
| 129 | free_list = *(PyFloatObject **)free_list; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 130 | op->ob_type = &PyFloat_Type; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 131 | op->ob_fval = fval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | _Py_NewReference(op); |
| 133 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 136 | static void |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 137 | float_dealloc(op) |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 138 | PyFloatObject *op; |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 139 | { |
Guido van Rossum | 93ad0df | 1997-05-13 21:00:42 +0000 | [diff] [blame] | 140 | *(PyFloatObject **)op = free_list; |
| 141 | free_list = op; |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | double |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | PyFloat_AsDouble(op) |
| 146 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | PyNumberMethods *nb; |
| 149 | PyFloatObject *fo; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 150 | double val; |
| 151 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 152 | if (op && PyFloat_Check(op)) |
| 153 | return PyFloat_AS_DOUBLE((PyFloatObject*) op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 154 | |
| 155 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 156 | nb->nb_float == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | return -1; |
| 159 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 161 | fo = (PyFloatObject*) (*nb->nb_float) (op); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 162 | if (fo == NULL) |
| 163 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 164 | if (!PyFloat_Check(fo)) { |
| 165 | PyErr_SetString(PyExc_TypeError, |
| 166 | "nb_float should return float object"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 170 | val = PyFloat_AS_DOUBLE(fo); |
| 171 | Py_DECREF(fo); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | |
| 173 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* Methods */ |
| 177 | |
Guido van Rossum | 27dec7e | 1991-06-04 19:42:53 +0000 | [diff] [blame] | 178 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 179 | PyFloat_AsString(buf, v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | char *buf; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 181 | PyFloatObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | { |
| 183 | register char *cp; |
| 184 | /* Subroutine for float_repr and float_print. |
| 185 | We want float numbers to be recognizable as such, |
| 186 | i.e., they should contain a decimal point or an exponent. |
| 187 | However, %g may print the number as an integer; |
| 188 | in such cases, we append ".0" to the string. */ |
| 189 | sprintf(buf, "%.12g", v->ob_fval); |
| 190 | cp = buf; |
| 191 | if (*cp == '-') |
| 192 | cp++; |
| 193 | for (; *cp != '\0'; cp++) { |
| 194 | /* Any non-digit means it's not an integer; |
| 195 | this takes care of NAN and INF as well. */ |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 196 | if (!isdigit(Py_CHARMASK(*cp))) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | if (*cp == '\0') { |
| 200 | *cp++ = '.'; |
| 201 | *cp++ = '0'; |
| 202 | *cp++ = '\0'; |
| 203 | } |
| 204 | } |
| 205 | |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 206 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 207 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | float_print(v, fp, flags) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 209 | PyFloatObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | FILE *fp; |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 211 | int flags; /* Not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | { |
| 213 | char buf[100]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | PyFloat_AsString(buf, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 215 | fputs(buf, fp); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 216 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 219 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | float_repr(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 221 | PyFloatObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | { |
| 223 | char buf[100]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | PyFloat_AsString(buf, v); |
| 225 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static int |
| 229 | float_compare(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | PyFloatObject *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | { |
| 232 | double i = v->ob_fval; |
| 233 | double j = w->ob_fval; |
| 234 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 235 | } |
| 236 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 237 | static long |
| 238 | float_hash(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 239 | PyFloatObject *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 240 | { |
| 241 | double intpart, fractpart; |
| 242 | int expo; |
| 243 | long x; |
| 244 | /* This is designed so that Python numbers with the same |
| 245 | value hash to the same value, otherwise comparisons |
| 246 | of mapping keys will turn out weird */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
| 248 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 249 | { |
| 250 | extended e; |
| 251 | fractpart = modf(v->ob_fval, &e); |
| 252 | intpart = e; |
| 253 | } |
| 254 | #else |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 255 | fractpart = modf(v->ob_fval, &intpart); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #endif |
| 257 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 258 | if (fractpart == 0.0) { |
| 259 | if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) { |
| 260 | /* Convert to long int and use its hash... */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 261 | PyObject *w = PyLong_FromDouble(v->ob_fval); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 262 | if (w == NULL) |
| 263 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 264 | x = PyObject_Hash(w); |
| 265 | Py_DECREF(w); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 266 | return x; |
| 267 | } |
| 268 | x = (long)intpart; |
| 269 | } |
| 270 | else { |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 271 | /* Note -- if you change this code, also change the copy |
| 272 | in complexobject.c */ |
| 273 | long hipart; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 274 | fractpart = frexp(fractpart, &expo); |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 275 | fractpart = fractpart * 2147483648.0; /* 2**31 */ |
| 276 | hipart = (long)fractpart; /* Take the top 32 bits */ |
| 277 | fractpart = (fractpart - (double)hipart) * 2147483648.0; |
| 278 | /* Get the next 32 bits */ |
| 279 | x = hipart + (long)fractpart + (long)intpart + (expo << 15); |
| 280 | /* Combine everything */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 281 | } |
| 282 | if (x == -1) |
| 283 | x = -2; |
| 284 | return x; |
| 285 | } |
| 286 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 287 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 288 | float_add(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 289 | PyFloatObject *v; |
| 290 | PyFloatObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 291 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 292 | double result; |
| 293 | PyFPE_START_PROTECT("add", return 0) |
| 294 | result = v->ob_fval + w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 295 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 296 | return PyFloat_FromDouble(result); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | float_sub(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 301 | PyFloatObject *v; |
| 302 | PyFloatObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 304 | double result; |
| 305 | PyFPE_START_PROTECT("subtract", return 0) |
| 306 | result = v->ob_fval - w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 307 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | return PyFloat_FromDouble(result); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 311 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 312 | float_mul(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | PyFloatObject *v; |
| 314 | PyFloatObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 316 | double result; |
| 317 | |
| 318 | PyFPE_START_PROTECT("multiply", return 0) |
| 319 | result = v->ob_fval * w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 320 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 321 | return PyFloat_FromDouble(result); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | float_div(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 326 | PyFloatObject *v; |
| 327 | PyFloatObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 329 | double result; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 330 | if (w->ob_fval == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 331 | PyErr_SetString(PyExc_ZeroDivisionError, "float division"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 332 | return NULL; |
| 333 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 334 | PyFPE_START_PROTECT("divide", return 0) |
| 335 | result = v->ob_fval / w->ob_fval; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 336 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | return PyFloat_FromDouble(result); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | float_rem(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | PyFloatObject *v; |
| 343 | PyFloatObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 344 | { |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 345 | double vx, wx; |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 346 | double /* div, */ mod; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 347 | wx = w->ob_fval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 349 | PyErr_SetString(PyExc_ZeroDivisionError, "float modulo"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 352 | PyFPE_START_PROTECT("modulo", return 0) |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 353 | vx = v->ob_fval; |
| 354 | mod = fmod(vx, wx); |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 355 | /* div = (vx - mod) / wx; */ |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 356 | if (wx*mod < 0) { |
| 357 | mod += wx; |
Guido van Rossum | 3132a5a | 1992-03-27 17:28:44 +0000 | [diff] [blame] | 358 | /* div -= 1.0; */ |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 359 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 360 | PyFPE_END_PROTECT(mod) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 361 | return PyFloat_FromDouble(mod); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | static PyObject * |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 365 | float_divmod(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 366 | PyFloatObject *v; |
| 367 | PyFloatObject *w; |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 368 | { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 369 | double vx, wx; |
| 370 | double div, mod; |
Guido van Rossum | 56cd67a | 1992-01-26 18:16:35 +0000 | [diff] [blame] | 371 | wx = w->ob_fval; |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 372 | if (wx == 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 373 | PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()"); |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 376 | PyFPE_START_PROTECT("divmod", return 0) |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 377 | vx = v->ob_fval; |
| 378 | mod = fmod(vx, wx); |
| 379 | div = (vx - mod) / wx; |
| 380 | if (wx*mod < 0) { |
| 381 | mod += wx; |
| 382 | div -= 1.0; |
| 383 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 384 | PyFPE_END_PROTECT(div) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 385 | return Py_BuildValue("(dd)", div, mod); |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Guido van Rossum | 363078a | 1996-05-24 20:45:01 +0000 | [diff] [blame] | 388 | static double powu(x, n) |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 389 | double x; |
| 390 | long n; |
| 391 | { |
| 392 | double r = 1.; |
| 393 | double p = x; |
| 394 | long mask = 1; |
| 395 | while (mask > 0 && n >= mask) { |
| 396 | if (n & mask) |
| 397 | r *= p; |
| 398 | mask <<= 1; |
| 399 | p *= p; |
| 400 | } |
| 401 | return r; |
| 402 | } |
| 403 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 404 | static PyObject * |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 405 | float_pow(v, w, z) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 406 | PyFloatObject *v; |
| 407 | PyObject *w; |
| 408 | PyFloatObject *z; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 409 | { |
| 410 | double iv, iw, ix; |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 411 | long intw; |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 412 | /* XXX Doesn't handle overflows if z!=None yet; it may never do so :( |
| 413 | * The z parameter is really only going to be useful for integers and |
| 414 | * long integers. Maybe something clever with logarithms could be done. |
| 415 | * [AMK] |
| 416 | */ |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 417 | iv = v->ob_fval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 418 | iw = ((PyFloatObject *)w)->ob_fval; |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 419 | intw = (long)iw; |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 420 | if (iw == intw && -10000 < intw && intw < 10000) { |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 421 | /* Sort out special cases here instead of relying on pow() */ |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 422 | if (intw == 0) { /* x**0 is 1, even 0**0 */ |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 423 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 424 | if ((PyObject *)z!=Py_None) { |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 425 | ix=fmod(1.0, z->ob_fval); |
| 426 | if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval; |
| 427 | } |
| 428 | else ix=1.0; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 429 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 430 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 431 | } |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 432 | errno = 0; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 433 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 434 | if (intw > 0) |
| 435 | ix = powu(iv, intw); |
| 436 | else |
| 437 | ix = 1./powu(iv, -intw); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 438 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | 86c04c2 | 1996-08-09 20:50:14 +0000 | [diff] [blame] | 439 | } |
| 440 | else { |
| 441 | /* Sort out special cases here instead of relying on pow() */ |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 442 | if (iv == 0.0) { |
| 443 | if (iw < 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 444 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 445 | "0.0 to a negative power"); |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 446 | return NULL; |
| 447 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 448 | return PyFloat_FromDouble(0.0); |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 449 | } |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 450 | if (iv < 0.0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | c13bcca | 1996-08-16 20:42:57 +0000 | [diff] [blame] | 452 | "negative number to a float power"); |
| 453 | return NULL; |
| 454 | } |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 455 | errno = 0; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 456 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | 39739ea | 1996-01-12 01:22:56 +0000 | [diff] [blame] | 457 | ix = pow(iv, iw); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 458 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 459 | } |
Guido van Rossum | 7fa52f8 | 1991-12-16 15:43:14 +0000 | [diff] [blame] | 460 | CHECK(ix); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 461 | if (errno != 0) { |
| 462 | /* XXX could it be another type of error? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 463 | PyErr_SetFromErrno(PyExc_OverflowError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 464 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 465 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 466 | if ((PyObject *)z!=Py_None) { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 467 | PyFPE_START_PROTECT("pow", return 0) |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 468 | ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */ |
| 469 | if ( ix!=0 && |
| 470 | ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) { |
| 471 | ix+=z->ob_fval; |
| 472 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 473 | PyFPE_END_PROTECT(ix) |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 474 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 475 | return PyFloat_FromDouble(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 478 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 479 | float_neg(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | PyFloatObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 481 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 482 | return PyFloat_FromDouble(-v->ob_fval); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 485 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 486 | float_pos(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 487 | PyFloatObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 488 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 489 | Py_INCREF(v); |
| 490 | return (PyObject *)v; |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | static PyObject * |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 494 | float_abs(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 495 | PyFloatObject *v; |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 496 | { |
| 497 | if (v->ob_fval < 0) |
| 498 | return float_neg(v); |
| 499 | else |
| 500 | return float_pos(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 503 | static int |
| 504 | float_nonzero(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 505 | PyFloatObject *v; |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 506 | { |
| 507 | return v->ob_fval != 0.0; |
| 508 | } |
| 509 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 510 | static int |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 511 | float_coerce(pv, pw) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 512 | PyObject **pv; |
| 513 | PyObject **pw; |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 514 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 515 | if (PyInt_Check(*pw)) { |
| 516 | long x = PyInt_AsLong(*pw); |
| 517 | *pw = PyFloat_FromDouble((double)x); |
| 518 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 521 | else if (PyLong_Check(*pw)) { |
| 522 | *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw)); |
| 523 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | return 1; /* Can't do it */ |
| 527 | } |
| 528 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 529 | static PyObject * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 530 | float_int(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 531 | PyObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 532 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 533 | double x = PyFloat_AsDouble(v); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 534 | if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN |
| 535 | : (x = floor(x)) > (double)LONG_MAX) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 536 | PyErr_SetString(PyExc_OverflowError, |
| 537 | "float too large to convert"); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 538 | return NULL; |
| 539 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | return PyInt_FromLong((long)x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 543 | static PyObject * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 544 | float_long(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 545 | PyObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 546 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 547 | double x = PyFloat_AsDouble(v); |
| 548 | return PyLong_FromDouble(x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 551 | static PyObject * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 552 | float_float(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 553 | PyObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 554 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 555 | Py_INCREF(v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 556 | return v; |
| 557 | } |
| 558 | |
| 559 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 560 | static PyNumberMethods float_as_number = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 561 | (binaryfunc)float_add, /*nb_add*/ |
| 562 | (binaryfunc)float_sub, /*nb_subtract*/ |
| 563 | (binaryfunc)float_mul, /*nb_multiply*/ |
| 564 | (binaryfunc)float_div, /*nb_divide*/ |
| 565 | (binaryfunc)float_rem, /*nb_remainder*/ |
| 566 | (binaryfunc)float_divmod, /*nb_divmod*/ |
Guido van Rossum | 0b7d02a | 1994-08-12 12:52:35 +0000 | [diff] [blame] | 567 | (ternaryfunc)float_pow, /*nb_power*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 568 | (unaryfunc)float_neg, /*nb_negative*/ |
| 569 | (unaryfunc)float_pos, /*nb_positive*/ |
| 570 | (unaryfunc)float_abs, /*nb_absolute*/ |
| 571 | (inquiry)float_nonzero, /*nb_nonzero*/ |
Guido van Rossum | 27acb33 | 1991-10-24 14:55:28 +0000 | [diff] [blame] | 572 | 0, /*nb_invert*/ |
| 573 | 0, /*nb_lshift*/ |
| 574 | 0, /*nb_rshift*/ |
| 575 | 0, /*nb_and*/ |
| 576 | 0, /*nb_xor*/ |
| 577 | 0, /*nb_or*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 578 | (coercion)float_coerce, /*nb_coerce*/ |
| 579 | (unaryfunc)float_int, /*nb_int*/ |
| 580 | (unaryfunc)float_long, /*nb_long*/ |
| 581 | (unaryfunc)float_float, /*nb_float*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 582 | 0, /*nb_oct*/ |
| 583 | 0, /*nb_hex*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 584 | }; |
| 585 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 586 | PyTypeObject PyFloat_Type = { |
| 587 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 588 | 0, |
| 589 | "float", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 590 | sizeof(PyFloatObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 591 | 0, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 592 | (destructor)float_dealloc, /*tp_dealloc*/ |
| 593 | (printfunc)float_print, /*tp_print*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 594 | 0, /*tp_getattr*/ |
| 595 | 0, /*tp_setattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 596 | (cmpfunc)float_compare, /*tp_compare*/ |
| 597 | (reprfunc)float_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 598 | &float_as_number, /*tp_as_number*/ |
| 599 | 0, /*tp_as_sequence*/ |
| 600 | 0, /*tp_as_mapping*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 601 | (hashfunc)float_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 602 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 603 | |
| 604 | void |
| 605 | PyFloat_Fini() |
| 606 | { |
| 607 | /* XXX Alas, the free list is not easily and safely freeable */ |
| 608 | } |