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