blob: 5650b41302e59dc11d4d84292a4cf5f67a6b34e0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Float object implementation */
12
Guido van Rossum2a9096b1990-10-21 22:15:08 +000013/* XXX There should be overflow checks here, but it's hard to check
14 for any kind of float exception without losing portability. */
15
Guido van Rossumc0b618a1997-05-02 03:12:38 +000016#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017
Guido van Rossum3f5da241990-12-20 15:06:42 +000018#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019
Guido van Rossum9575a441993-04-07 14:06:14 +000020#ifdef i860
21/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
22#undef HUGE_VAL
23#endif
24
Guido van Rossum9d81b551996-06-26 18:27:19 +000025#if defined(HUGE_VAL) && !defined(CHECK)
Guido van Rossum7fa52f81991-12-16 15:43:14 +000026#define CHECK(x) if (errno != 0) ; \
27 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
28 else errno = ERANGE
Guido van Rossum9d81b551996-06-26 18:27:19 +000029#endif
30
31#ifndef CHECK
Guido van Rossum7fa52f81991-12-16 15:43:14 +000032#define CHECK(x) /* Don't know how to check */
33#endif
34
Guido van Rossum03093a21994-09-28 15:51:32 +000035#ifdef HAVE_LIMITS_H
36#include <limits.h>
37#endif
38
39#ifndef LONG_MAX
Fred Drake13634cf2000-06-29 19:17:04 +000040#if SIZEOF_LONG == 4
Guido van Rossum03093a21994-09-28 15:51:32 +000041#define LONG_MAX 0X7FFFFFFFL
Fred Drake13634cf2000-06-29 19:17:04 +000042#elif SIZEOF_LONG == 8
43#define LONG_MAX 0X7FFFFFFFFFFFFFFFL
44#else
45#error "could not set LONG_MAX"
46#endif
Guido van Rossum03093a21994-09-28 15:51:32 +000047#endif
48
49#ifndef LONG_MIN
50#define LONG_MIN (-LONG_MAX-1)
51#endif
52
Guido van Rossum67ca7011995-02-13 16:38:41 +000053#ifdef __NeXT__
54#ifdef __sparc__
55/*
56 * This works around a bug in the NS/Sparc 3.3 pre-release
57 * limits.h header file.
58 * 10-Feb-1995 bwarsaw@cnri.reston.va.us
59 */
60#undef LONG_MIN
61#define LONG_MIN (-LONG_MAX-1)
62#endif
63#endif
64
Guido van Rossum07e3a7e1995-02-27 10:13:37 +000065#if !defined(__STDC__) && !defined(macintosh)
Tim Petersdbd9ba62000-07-09 03:09:57 +000066extern double fmod(double, double);
67extern double pow(double, double);
Guido van Rossum6923e131990-11-02 17:50:43 +000068#endif
69
Guido van Rossum3c03fa81997-10-31 17:00:30 +000070#ifdef sun
71/* On SunOS4.1 only libm.a exists. Make sure that references to all
72 needed math functions exist in the executable, so that dynamic
73 loading of mathmodule does not fail. */
74double (*_Py_math_funcs_hack[])() = {
75 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
76 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
77};
78#endif
79
Guido van Rossum93ad0df1997-05-13 21:00:42 +000080/* Special free list -- see comments for same code in intobject.c. */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000081#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000082#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
Guido van Rossumf61bbc81999-03-12 00:12:21 +000083#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
Guido van Rossum3fce8831999-03-12 19:43:17 +000084
Guido van Rossum3fce8831999-03-12 19:43:17 +000085struct _floatblock {
86 struct _floatblock *next;
87 PyFloatObject objects[N_FLOATOBJECTS];
88};
89
90typedef struct _floatblock PyFloatBlock;
91
92static PyFloatBlock *block_list = NULL;
93static PyFloatObject *free_list = NULL;
94
Guido van Rossum93ad0df1997-05-13 21:00:42 +000095static PyFloatObject *
Fred Drakefd99de62000-07-09 05:02:18 +000096fill_free_list(void)
Guido van Rossum93ad0df1997-05-13 21:00:42 +000097{
98 PyFloatObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000099 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
100 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000101 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000102 return (PyFloatObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +0000103 ((PyFloatBlock *)p)->next = block_list;
104 block_list = (PyFloatBlock *)p;
105 p = &((PyFloatBlock *)p)->objects[0];
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000106 q = p + N_FLOATOBJECTS;
107 while (--q > p)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000108 q->ob_type = (struct _typeobject *)(q-1);
109 q->ob_type = NULL;
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000110 return p + N_FLOATOBJECTS - 1;
111}
112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114PyFloat_FromDouble(double fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115{
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000116 register PyFloatObject *op;
117 if (free_list == NULL) {
118 if ((free_list = fill_free_list()) == NULL)
119 return NULL;
120 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121 /* PyObject_New is inlined */
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000122 op = free_list;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000123 free_list = (PyFloatObject *)op->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000124 PyObject_INIT(op, &PyFloat_Type);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000125 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127}
128
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000129PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000130PyFloat_FromString(PyObject *v, char **pend)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000131{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000132 extern double strtod(const char *, char **);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000133 const char *s, *last, *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000134 double x;
135 char buffer[256]; /* For errors */
Barry Warsaw67c1a042000-08-18 05:00:03 +0000136 char s_buffer[256];
Guido van Rossum4c08d552000-03-10 22:55:18 +0000137 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138
Guido van Rossum4c08d552000-03-10 22:55:18 +0000139 if (PyString_Check(v)) {
140 s = PyString_AS_STRING(v);
141 len = PyString_GET_SIZE(v);
142 }
Guido van Rossum9e896b32000-04-05 20:11:21 +0000143 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000144 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
145 PyErr_SetString(PyExc_ValueError,
146 "float() literal too large to convert");
147 return NULL;
148 }
149 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
150 PyUnicode_GET_SIZE(v),
151 s_buffer,
152 NULL))
153 return NULL;
154 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000155 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000156 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000157 else if (PyObject_AsCharBuffer(v, &s, &len)) {
158 PyErr_SetString(PyExc_TypeError,
159 "float() needs a string argument");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000160 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000161 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000162
Guido van Rossum4c08d552000-03-10 22:55:18 +0000163 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000164 while (*s && isspace(Py_CHARMASK(*s)))
165 s++;
166 if (s[0] == '\0') {
167 PyErr_SetString(PyExc_ValueError, "empty string for float()");
168 return NULL;
169 }
170 errno = 0;
171 PyFPE_START_PROTECT("PyFloat_FromString", return 0)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000172 x = strtod((char *)s, (char **)&end);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000173 PyFPE_END_PROTECT(x)
174 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
175 byte at the end of the string, when the input is inf(inity) */
176 if (end > last)
177 end = last;
178 while (*end && isspace(Py_CHARMASK(*end)))
179 end++;
180 if (*end != '\0') {
181 sprintf(buffer, "invalid literal for float(): %.200s", s);
182 PyErr_SetString(PyExc_ValueError, buffer);
183 return NULL;
184 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000185 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000186 PyErr_SetString(PyExc_ValueError,
187 "null byte in argument for float()");
188 return NULL;
189 }
190 else if (errno != 0) {
191 sprintf(buffer, "float() literal too large: %.200s", s);
192 PyErr_SetString(PyExc_ValueError, buffer);
193 return NULL;
194 }
195 if (pend)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000196 *pend = (char *)end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000197 return PyFloat_FromDouble(x);
198}
199
Guido van Rossum234f9421993-06-17 12:35:49 +0000200static void
Fred Drakefd99de62000-07-09 05:02:18 +0000201float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000202{
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000203 op->ob_type = (struct _typeobject *)free_list;
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000204 free_list = op;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000205}
206
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207double
Fred Drakefd99de62000-07-09 05:02:18 +0000208PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210 PyNumberMethods *nb;
211 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212 double val;
213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 if (op && PyFloat_Check(op))
215 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
217 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
218 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220 return -1;
221 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224 if (fo == NULL)
225 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 if (!PyFloat_Check(fo)) {
227 PyErr_SetString(PyExc_TypeError,
228 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229 return -1;
230 }
231
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000232 val = PyFloat_AS_DOUBLE(fo);
233 Py_DECREF(fo);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
235 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236}
237
238/* Methods */
239
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000240void
Fred Drakefd99de62000-07-09 05:02:18 +0000241PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242{
243 register char *cp;
244 /* Subroutine for float_repr and float_print.
245 We want float numbers to be recognizable as such,
246 i.e., they should contain a decimal point or an exponent.
247 However, %g may print the number as an integer;
248 in such cases, we append ".0" to the string. */
Guido van Rossum57072eb1999-12-23 19:00:28 +0000249 sprintf(buf, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 cp = buf;
251 if (*cp == '-')
252 cp++;
253 for (; *cp != '\0'; cp++) {
254 /* Any non-digit means it's not an integer;
255 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000256 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 break;
258 }
259 if (*cp == '\0') {
260 *cp++ = '.';
261 *cp++ = '0';
262 *cp++ = '\0';
263 }
264}
265
Guido van Rossum57072eb1999-12-23 19:00:28 +0000266/* Precisions used by repr() and str(), respectively.
267
268 The repr() precision (17 significant decimal digits) is the minimal number
269 that is guaranteed to have enough precision so that if the number is read
270 back in the exact same binary value is recreated. This is true for IEEE
271 floating point by design, and also happens to work for all other modern
272 hardware.
273
274 The str() precision is chosen so that in most cases, the rounding noise
275 created by various operations is suppressed, while giving plenty of
276 precision for practical use.
277
278*/
279
280#define PREC_REPR 17
281#define PREC_STR 12
282
283void
Fred Drakefd99de62000-07-09 05:02:18 +0000284PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000285{
286 PyFloat_AsStringEx(buf, v, PREC_STR);
287}
288
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000289/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000290static int
Fred Drakefd99de62000-07-09 05:02:18 +0000291float_print(PyFloatObject *v, FILE *fp, int flags)
292 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293{
294 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000295 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000297 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298}
299
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000301float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302{
303 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000304 PyFloat_AsStringEx(buf, v, PREC_REPR);
305 return PyString_FromString(buf);
306}
307
308static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000309float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000310{
311 char buf[100];
312 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314}
315
316static int
Fred Drakefd99de62000-07-09 05:02:18 +0000317float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318{
319 double i = v->ob_fval;
320 double j = w->ob_fval;
321 return (i < j) ? -1 : (i > j) ? 1 : 0;
322}
323
Fred Drake13634cf2000-06-29 19:17:04 +0000324
Guido van Rossum9bfef441993-03-29 10:43:31 +0000325static long
Fred Drakefd99de62000-07-09 05:02:18 +0000326float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000327{
Tim Peters39dce292000-08-15 03:34:48 +0000328 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000329}
330
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000332float_add(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000334 double result;
335 PyFPE_START_PROTECT("add", return 0)
336 result = v->ob_fval + w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000337 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339}
340
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000342float_sub(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000344 double result;
345 PyFPE_START_PROTECT("subtract", return 0)
346 result = v->ob_fval - w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000347 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349}
350
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000352float_mul(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000354 double result;
355
356 PyFPE_START_PROTECT("multiply", return 0)
357 result = v->ob_fval * w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000358 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360}
361
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000363float_div(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000365 double result;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000366 if (w->ob_fval == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368 return NULL;
369 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000370 PyFPE_START_PROTECT("divide", return 0)
371 result = v->ob_fval / w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000372 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374}
375
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000377float_rem(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000379 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000380 double mod;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000381 wx = w->ob_fval;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384 return NULL;
385 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000386 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000387 vx = v->ob_fval;
388 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000389 /* note: checking mod*wx < 0 is incorrect -- underflows to
390 0 if wx < sqrt(smallest nonzero double) */
391 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000392 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000393 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000394 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396}
397
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000399float_divmod(PyFloatObject *v, PyFloatObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000400{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000401 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000402 double div, mod, floordiv;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000403 wx = w->ob_fval;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000404 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000406 return NULL;
407 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000408 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000409 vx = v->ob_fval;
410 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000411 /* fmod is typically exact, so vx-mod is *mathemtically* an
412 exact multiple of wx. But this is fp arithmetic, and fp
413 vx - mod is an approximation; the result is that div may
414 not be an exact integral value after the division, although
415 it will always be very close to one.
416 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000417 div = (vx - mod) / wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000418 /* note: checking mod*wx < 0 is incorrect -- underflows to
419 0 if wx < sqrt(smallest nonzero double) */
420 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum15ecff41991-10-20 20:16:45 +0000421 mod += wx;
422 div -= 1.0;
423 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000424 /* snap quotient to nearest integral value */
425 floordiv = floor(div);
426 if (div - floordiv > 0.5)
427 floordiv += 1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000428 PyFPE_END_PROTECT(div)
Guido van Rossum9263e781999-05-06 14:26:34 +0000429 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000430}
431
Fred Drakefd99de62000-07-09 05:02:18 +0000432static double powu(double x, long n)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000433{
434 double r = 1.;
435 double p = x;
436 long mask = 1;
437 while (mask > 0 && n >= mask) {
438 if (n & mask)
439 r *= p;
440 mask <<= 1;
441 p *= p;
442 }
443 return r;
444}
445
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000447float_pow(PyFloatObject *v, PyObject *w, PyFloatObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448{
449 double iv, iw, ix;
Guido van Rossum39739ea1996-01-12 01:22:56 +0000450 long intw;
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000451 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
452 * The z parameter is really only going to be useful for integers and
453 * long integers. Maybe something clever with logarithms could be done.
454 * [AMK]
455 */
Guido van Rossum39739ea1996-01-12 01:22:56 +0000456 iv = v->ob_fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 iw = ((PyFloatObject *)w)->ob_fval;
Guido van Rossum39739ea1996-01-12 01:22:56 +0000458 intw = (long)iw;
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000459 if (iw == intw && -10000 < intw && intw < 10000) {
Guido van Rossum39739ea1996-01-12 01:22:56 +0000460 /* Sort out special cases here instead of relying on pow() */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000461 if (intw == 0) { /* x**0 is 1, even 0**0 */
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000462 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463 if ((PyObject *)z!=Py_None) {
Guido van Rossum39739ea1996-01-12 01:22:56 +0000464 ix=fmod(1.0, z->ob_fval);
465 if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
466 }
467 else ix=1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000468 PyFPE_END_PROTECT(ix)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469 return PyFloat_FromDouble(ix);
Guido van Rossum70d93461991-05-28 21:57:39 +0000470 }
Guido van Rossum86c04c21996-08-09 20:50:14 +0000471 errno = 0;
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000472 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000473 if (intw > 0)
474 ix = powu(iv, intw);
475 else
476 ix = 1./powu(iv, -intw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000477 PyFPE_END_PROTECT(ix)
Guido van Rossum86c04c21996-08-09 20:50:14 +0000478 }
479 else {
480 /* Sort out special cases here instead of relying on pow() */
Guido van Rossum39739ea1996-01-12 01:22:56 +0000481 if (iv == 0.0) {
482 if (iw < 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 PyErr_SetString(PyExc_ValueError,
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000484 "0.0 to a negative power");
Guido van Rossum39739ea1996-01-12 01:22:56 +0000485 return NULL;
486 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487 return PyFloat_FromDouble(0.0);
Guido van Rossum39739ea1996-01-12 01:22:56 +0000488 }
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000489 if (iv < 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 PyErr_SetString(PyExc_ValueError,
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000491 "negative number to a float power");
492 return NULL;
493 }
Guido van Rossum39739ea1996-01-12 01:22:56 +0000494 errno = 0;
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000495 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000496 ix = pow(iv, iw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000497 PyFPE_END_PROTECT(ix)
Guido van Rossum70d93461991-05-28 21:57:39 +0000498 }
Guido van Rossum7fa52f81991-12-16 15:43:14 +0000499 CHECK(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000500 if (errno != 0) {
501 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000503 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000504 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505 if ((PyObject *)z!=Py_None) {
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000506 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000507 ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */
508 if ( ix!=0 &&
509 ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
510 ix+=z->ob_fval;
511 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000512 PyFPE_END_PROTECT(ix)
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000513 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515}
516
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000518float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521}
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000524float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 Py_INCREF(v);
527 return (PyObject *)v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000528}
529
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000531float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000532{
533 if (v->ob_fval < 0)
534 return float_neg(v);
535 else
536 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537}
538
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000539static int
Fred Drakefd99de62000-07-09 05:02:18 +0000540float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000541{
542 return v->ob_fval != 0.0;
543}
544
Guido van Rossum234f9421993-06-17 12:35:49 +0000545static int
Fred Drakefd99de62000-07-09 05:02:18 +0000546float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000547{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 if (PyInt_Check(*pw)) {
549 long x = PyInt_AsLong(*pw);
550 *pw = PyFloat_FromDouble((double)x);
551 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000552 return 0;
553 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 else if (PyLong_Check(*pw)) {
555 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
556 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000557 return 0;
558 }
559 return 1; /* Can't do it */
560}
561
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000563float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000564{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565 double x = PyFloat_AsDouble(v);
Guido van Rossum03093a21994-09-28 15:51:32 +0000566 if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
567 : (x = floor(x)) > (double)LONG_MAX) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyErr_SetString(PyExc_OverflowError,
569 "float too large to convert");
Guido van Rossum03093a21994-09-28 15:51:32 +0000570 return NULL;
571 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 return PyInt_FromLong((long)x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000573}
574
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000576float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000577{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 double x = PyFloat_AsDouble(v);
579 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000580}
581
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000583float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000584{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000586 return v;
587}
588
589
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000591 (binaryfunc)float_add, /*nb_add*/
592 (binaryfunc)float_sub, /*nb_subtract*/
593 (binaryfunc)float_mul, /*nb_multiply*/
594 (binaryfunc)float_div, /*nb_divide*/
595 (binaryfunc)float_rem, /*nb_remainder*/
596 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000597 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000598 (unaryfunc)float_neg, /*nb_negative*/
599 (unaryfunc)float_pos, /*nb_positive*/
600 (unaryfunc)float_abs, /*nb_absolute*/
601 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000602 0, /*nb_invert*/
603 0, /*nb_lshift*/
604 0, /*nb_rshift*/
605 0, /*nb_and*/
606 0, /*nb_xor*/
607 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000608 (coercion)float_coerce, /*nb_coerce*/
609 (unaryfunc)float_int, /*nb_int*/
610 (unaryfunc)float_long, /*nb_long*/
611 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000612 0, /*nb_oct*/
613 0, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614};
615
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616PyTypeObject PyFloat_Type = {
617 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618 0,
619 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +0000622 (destructor)float_dealloc, /*tp_dealloc*/
623 (printfunc)float_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624 0, /*tp_getattr*/
625 0, /*tp_setattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000626 (cmpfunc)float_compare, /*tp_compare*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000627 (reprfunc)float_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628 &float_as_number, /*tp_as_number*/
629 0, /*tp_as_sequence*/
630 0, /*tp_as_mapping*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000631 (hashfunc)float_hash, /*tp_hash*/
632 0, /*tp_call*/
633 (reprfunc)float_str, /*tp_str*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000634};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000635
636void
Fred Drakefd99de62000-07-09 05:02:18 +0000637PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000638{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000639 PyFloatObject *p;
640 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000641 int i;
642 int bc, bf; /* block count, number of freed blocks */
643 int frem, fsum; /* remaining unfreed floats per block, total */
644
645 bc = 0;
646 bf = 0;
647 fsum = 0;
648 list = block_list;
649 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000650 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000651 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000652 bc++;
653 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000654 for (i = 0, p = &list->objects[0];
655 i < N_FLOATOBJECTS;
656 i++, p++) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000657 if (PyFloat_Check(p) && p->ob_refcnt != 0)
658 frem++;
659 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000660 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000661 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000662 list->next = block_list;
663 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000664 for (i = 0, p = &list->objects[0];
665 i < N_FLOATOBJECTS;
666 i++, p++) {
667 if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
668 p->ob_type = (struct _typeobject *)
669 free_list;
670 free_list = p;
671 }
672 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000673 }
674 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000675 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000676 bf++;
677 }
678 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000679 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000680 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000681 if (!Py_VerboseFlag)
682 return;
683 fprintf(stderr, "# cleanup floats");
684 if (!fsum) {
685 fprintf(stderr, "\n");
686 }
687 else {
688 fprintf(stderr,
689 ": %d unfreed float%s in %d out of %d block%s\n",
690 fsum, fsum == 1 ? "" : "s",
691 bc - bf, bc, bc == 1 ? "" : "s");
692 }
693 if (Py_VerboseFlag > 1) {
694 list = block_list;
695 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000696 for (i = 0, p = &list->objects[0];
697 i < N_FLOATOBJECTS;
698 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000699 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
700 char buf[100];
701 PyFloat_AsString(buf, p);
702 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000703 "# <float at %p, refcnt=%d, val=%s>\n",
704 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000705 }
706 }
707 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000708 }
709 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000710}