blob: c8414234220b02acf90f47305fe2dd8492397bd9 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Float object implementation */
3
Guido van Rossum2a9096b1990-10-21 22:15:08 +00004/* XXX There should be overflow checks here, but it's hard to check
5 for any kind of float exception without losing portability. */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossum3f5da241990-12-20 15:06:42 +00009#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Guido van Rossum9575a441993-04-07 14:06:14 +000011#ifdef i860
12/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
13#undef HUGE_VAL
14#endif
15
Guido van Rossum9d81b551996-06-26 18:27:19 +000016#if defined(HUGE_VAL) && !defined(CHECK)
Guido van Rossum7fa52f81991-12-16 15:43:14 +000017#define CHECK(x) if (errno != 0) ; \
18 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
19 else errno = ERANGE
Guido van Rossum9d81b551996-06-26 18:27:19 +000020#endif
21
22#ifndef CHECK
Guido van Rossum7fa52f81991-12-16 15:43:14 +000023#define CHECK(x) /* Don't know how to check */
24#endif
25
Guido van Rossum07e3a7e1995-02-27 10:13:37 +000026#if !defined(__STDC__) && !defined(macintosh)
Tim Petersdbd9ba62000-07-09 03:09:57 +000027extern double fmod(double, double);
28extern double pow(double, double);
Guido van Rossum6923e131990-11-02 17:50:43 +000029#endif
30
Guido van Rossum3c03fa81997-10-31 17:00:30 +000031#ifdef sun
32/* On SunOS4.1 only libm.a exists. Make sure that references to all
33 needed math functions exist in the executable, so that dynamic
34 loading of mathmodule does not fail. */
35double (*_Py_math_funcs_hack[])() = {
36 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
37 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
38};
39#endif
40
Guido van Rossum93ad0df1997-05-13 21:00:42 +000041/* Special free list -- see comments for same code in intobject.c. */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000042#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000043#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
Guido van Rossumf61bbc81999-03-12 00:12:21 +000044#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
Guido van Rossum3fce8831999-03-12 19:43:17 +000045
Guido van Rossum3fce8831999-03-12 19:43:17 +000046struct _floatblock {
47 struct _floatblock *next;
48 PyFloatObject objects[N_FLOATOBJECTS];
49};
50
51typedef struct _floatblock PyFloatBlock;
52
53static PyFloatBlock *block_list = NULL;
54static PyFloatObject *free_list = NULL;
55
Guido van Rossum93ad0df1997-05-13 21:00:42 +000056static PyFloatObject *
Fred Drakefd99de62000-07-09 05:02:18 +000057fill_free_list(void)
Guido van Rossum93ad0df1997-05-13 21:00:42 +000058{
59 PyFloatObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000060 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
61 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
Guido van Rossum93ad0df1997-05-13 21:00:42 +000062 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000063 return (PyFloatObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000064 ((PyFloatBlock *)p)->next = block_list;
65 block_list = (PyFloatBlock *)p;
66 p = &((PyFloatBlock *)p)->objects[0];
Guido van Rossum93ad0df1997-05-13 21:00:42 +000067 q = p + N_FLOATOBJECTS;
68 while (--q > p)
Guido van Rossumf61bbc81999-03-12 00:12:21 +000069 q->ob_type = (struct _typeobject *)(q-1);
70 q->ob_type = NULL;
Guido van Rossum93ad0df1997-05-13 21:00:42 +000071 return p + N_FLOATOBJECTS - 1;
72}
73
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyFloat_FromDouble(double fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076{
Guido van Rossum93ad0df1997-05-13 21:00:42 +000077 register PyFloatObject *op;
78 if (free_list == NULL) {
79 if ((free_list = fill_free_list()) == NULL)
80 return NULL;
81 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000082 /* PyObject_New is inlined */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000083 op = free_list;
Guido van Rossumf61bbc81999-03-12 00:12:21 +000084 free_list = (PyFloatObject *)op->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +000085 PyObject_INIT(op, &PyFloat_Type);
Guido van Rossum2a9096b1990-10-21 22:15:08 +000086 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088}
89
Tim Petersef14d732000-09-23 03:39:17 +000090/**************************************************************************
91RED_FLAG 22-Sep-2000 tim
92PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
93
941. If v was a regular string, *pend was set to point to its terminating
95 null byte. That's useless (the caller can find that without any
96 help from this function!).
97
982. If v was a Unicode string, or an object convertible to a character
99 buffer, *pend was set to point into stack trash (the auto temp
100 vector holding the character buffer). That was downright dangerous.
101
102Since we can't change the interface of a public API function, pend is
103still supported but now *officially* useless: if pend is not NULL,
104*pend is set to NULL.
105**************************************************************************/
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000106PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000107PyFloat_FromString(PyObject *v, char **pend)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000108{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000109 const char *s, *last, *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000110 double x;
Tim Petersef14d732000-09-23 03:39:17 +0000111 char buffer[256]; /* for errors */
112 char s_buffer[256]; /* for objects convertible to a char buffer */
Guido van Rossum4c08d552000-03-10 22:55:18 +0000113 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000114
Tim Petersef14d732000-09-23 03:39:17 +0000115 if (pend)
116 *pend = NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000117 if (PyString_Check(v)) {
118 s = PyString_AS_STRING(v);
119 len = PyString_GET_SIZE(v);
120 }
Guido van Rossum9e896b32000-04-05 20:11:21 +0000121 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000122 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
123 PyErr_SetString(PyExc_ValueError,
Tim Petersef14d732000-09-23 03:39:17 +0000124 "Unicode float() literal too long to convert");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000125 return NULL;
126 }
Tim Petersef14d732000-09-23 03:39:17 +0000127 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000128 PyUnicode_GET_SIZE(v),
129 s_buffer,
130 NULL))
131 return NULL;
132 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000133 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000134 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000135 else if (PyObject_AsCharBuffer(v, &s, &len)) {
136 PyErr_SetString(PyExc_TypeError,
137 "float() needs a string argument");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000139 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000140
Guido van Rossum4c08d552000-03-10 22:55:18 +0000141 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000142 while (*s && isspace(Py_CHARMASK(*s)))
143 s++;
Tim Petersef14d732000-09-23 03:39:17 +0000144 if (*s == '\0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000145 PyErr_SetString(PyExc_ValueError, "empty string for float()");
146 return NULL;
147 }
Tim Petersef14d732000-09-23 03:39:17 +0000148 /* We don't care about overflow or underflow. If the platform supports
149 * them, infinities and signed zeroes (on underflow) are fine.
150 * However, strtod can return 0 for denormalized numbers, where atof
151 * does not. So (alas!) we special-case a zero result. Note that
152 * whether strtod sets errno on underflow is not defined, so we can't
153 * key off errno.
154 */
Tim Peters858346e2000-09-25 21:01:28 +0000155 PyFPE_START_PROTECT("strtod", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000156 x = strtod(s, (char **)&end);
Tim Peters858346e2000-09-25 21:01:28 +0000157 PyFPE_END_PROTECT(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000158 errno = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000159 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
Tim Petersef14d732000-09-23 03:39:17 +0000160 byte at the end of the string, when the input is inf(inity). */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000161 if (end > last)
162 end = last;
Tim Petersef14d732000-09-23 03:39:17 +0000163 if (end == s) {
164 sprintf(buffer, "invalid literal for float(): %.200s", s);
165 PyErr_SetString(PyExc_ValueError, buffer);
166 return NULL;
167 }
168 /* Since end != s, the platform made *some* kind of sense out
169 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000170 while (*end && isspace(Py_CHARMASK(*end)))
171 end++;
172 if (*end != '\0') {
173 sprintf(buffer, "invalid literal for float(): %.200s", s);
174 PyErr_SetString(PyExc_ValueError, buffer);
175 return NULL;
176 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000177 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178 PyErr_SetString(PyExc_ValueError,
179 "null byte in argument for float()");
180 return NULL;
181 }
Tim Petersef14d732000-09-23 03:39:17 +0000182 if (x == 0.0) {
183 /* See above -- may have been strtod being anal
184 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000185 PyFPE_START_PROTECT("atof", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000186 x = atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000187 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000188 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000189 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000190 return PyFloat_FromDouble(x);
191}
192
Guido van Rossum234f9421993-06-17 12:35:49 +0000193static void
Fred Drakefd99de62000-07-09 05:02:18 +0000194float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000195{
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000196 op->ob_type = (struct _typeobject *)free_list;
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000197 free_list = op;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000198}
199
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200double
Fred Drakefd99de62000-07-09 05:02:18 +0000201PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 PyNumberMethods *nb;
204 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205 double val;
206
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 if (op && PyFloat_Check(op))
208 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
210 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
211 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 return -1;
214 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217 if (fo == NULL)
218 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 if (!PyFloat_Check(fo)) {
220 PyErr_SetString(PyExc_TypeError,
221 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222 return -1;
223 }
224
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 val = PyFloat_AS_DOUBLE(fo);
226 Py_DECREF(fo);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227
228 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
231/* Methods */
232
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000233void
Fred Drakefd99de62000-07-09 05:02:18 +0000234PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235{
236 register char *cp;
237 /* Subroutine for float_repr and float_print.
238 We want float numbers to be recognizable as such,
239 i.e., they should contain a decimal point or an exponent.
240 However, %g may print the number as an integer;
241 in such cases, we append ".0" to the string. */
Guido van Rossum57072eb1999-12-23 19:00:28 +0000242 sprintf(buf, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 cp = buf;
244 if (*cp == '-')
245 cp++;
246 for (; *cp != '\0'; cp++) {
247 /* Any non-digit means it's not an integer;
248 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000249 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 break;
251 }
252 if (*cp == '\0') {
253 *cp++ = '.';
254 *cp++ = '0';
255 *cp++ = '\0';
256 }
257}
258
Neil Schemenauer32117e52001-01-04 01:44:34 +0000259/* Macro and helper that convert PyObject obj to a C double and store
260 the value in dbl; this replaces the functionality of the coercion
261 slot function */
262
263#define CONVERT_TO_DOUBLE(obj, dbl) \
264 if (PyFloat_Check(obj)) \
265 dbl = PyFloat_AS_DOUBLE(obj); \
266 else if (convert_to_double(&(obj), &(dbl)) < 0) \
267 return obj;
268
269static int
270convert_to_double(PyObject **v,
271 double *dbl)
272{
273 register PyObject *obj = *v;
274
275 if (PyInt_Check(obj)) {
276 *dbl = (double)PyInt_AS_LONG(obj);
277 }
278 else if (PyLong_Check(obj)) {
279 PyFPE_START_PROTECT("convert_to_double", {*v=NULL;return -1;})
280 *dbl = PyLong_AsDouble(obj);
281 PyFPE_END_PROTECT(result)
282 }
283 else {
284 Py_INCREF(Py_NotImplemented);
285 *v = Py_NotImplemented;
286 return -1;
287 }
288 return 0;
289}
290
Guido van Rossum57072eb1999-12-23 19:00:28 +0000291/* Precisions used by repr() and str(), respectively.
292
293 The repr() precision (17 significant decimal digits) is the minimal number
294 that is guaranteed to have enough precision so that if the number is read
295 back in the exact same binary value is recreated. This is true for IEEE
296 floating point by design, and also happens to work for all other modern
297 hardware.
298
299 The str() precision is chosen so that in most cases, the rounding noise
300 created by various operations is suppressed, while giving plenty of
301 precision for practical use.
302
303*/
304
305#define PREC_REPR 17
306#define PREC_STR 12
307
308void
Fred Drakefd99de62000-07-09 05:02:18 +0000309PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000310{
311 PyFloat_AsStringEx(buf, v, PREC_STR);
312}
313
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000314/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000315static int
Fred Drakefd99de62000-07-09 05:02:18 +0000316float_print(PyFloatObject *v, FILE *fp, int flags)
317 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318{
319 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000320 PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000322 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323}
324
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000326float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327{
328 char buf[100];
Guido van Rossum57072eb1999-12-23 19:00:28 +0000329 PyFloat_AsStringEx(buf, v, PREC_REPR);
330 return PyString_FromString(buf);
331}
332
333static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000334float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000335{
336 char buf[100];
337 PyFloat_AsStringEx(buf, v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339}
340
341static int
Fred Drakefd99de62000-07-09 05:02:18 +0000342float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
344 double i = v->ob_fval;
345 double j = w->ob_fval;
346 return (i < j) ? -1 : (i > j) ? 1 : 0;
347}
348
Neil Schemenauer32117e52001-01-04 01:44:34 +0000349/* Needed for the new style number compare slots */
350static PyObject *
351float_cmp(PyObject *v, PyObject *w)
352{
353 double a,b;
354 CONVERT_TO_DOUBLE(v, a);
355 CONVERT_TO_DOUBLE(w, b);
356 return PyInt_FromLong((a < b) ? -1 : (a > b) ? 1 : 0);
357}
Fred Drake13634cf2000-06-29 19:17:04 +0000358
Guido van Rossum9bfef441993-03-29 10:43:31 +0000359static long
Fred Drakefd99de62000-07-09 05:02:18 +0000360float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000361{
Tim Peters39dce292000-08-15 03:34:48 +0000362 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000363}
364
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000366float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000368 double a,b;
369 CONVERT_TO_DOUBLE(v, a);
370 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000371 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000372 a = a + b;
373 PyFPE_END_PROTECT(a)
374 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
376
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000377static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000378float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000380 double a,b;
381 CONVERT_TO_DOUBLE(v, a);
382 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000383 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000384 a = a - b;
385 PyFPE_END_PROTECT(a)
386 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387}
388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000390float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000392 double a,b;
393 CONVERT_TO_DOUBLE(v, a);
394 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000395 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000396 a = a * b;
397 PyFPE_END_PROTECT(a)
398 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399}
400
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000402float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000404 double a,b;
405 CONVERT_TO_DOUBLE(v, a);
406 CONVERT_TO_DOUBLE(w, b);
407 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 return NULL;
410 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000411 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000412 a = a / b;
413 PyFPE_END_PROTECT(a)
414 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415}
416
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000418float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000420 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000421 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000422 CONVERT_TO_DOUBLE(v, vx);
423 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426 return NULL;
427 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000428 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000429 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000430 /* note: checking mod*wx < 0 is incorrect -- underflows to
431 0 if wx < sqrt(smallest nonzero double) */
432 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000433 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000434 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000435 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000440float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000441{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000442 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000443 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000444 CONVERT_TO_DOUBLE(v, vx);
445 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000446 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000448 return NULL;
449 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000450 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000451 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000452 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000453 exact multiple of wx. But this is fp arithmetic, and fp
454 vx - mod is an approximation; the result is that div may
455 not be an exact integral value after the division, although
456 it will always be very close to one.
457 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000458 div = (vx - mod) / wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000459 /* note: checking mod*wx < 0 is incorrect -- underflows to
460 0 if wx < sqrt(smallest nonzero double) */
461 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum15ecff41991-10-20 20:16:45 +0000462 mod += wx;
463 div -= 1.0;
464 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000465 /* snap quotient to nearest integral value */
466 floordiv = floor(div);
467 if (div - floordiv > 0.5)
468 floordiv += 1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000469 PyFPE_END_PROTECT(div)
Guido van Rossum9263e781999-05-06 14:26:34 +0000470 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000471}
472
Fred Drakefd99de62000-07-09 05:02:18 +0000473static double powu(double x, long n)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000474{
475 double r = 1.;
476 double p = x;
477 long mask = 1;
478 while (mask > 0 && n >= mask) {
479 if (n & mask)
480 r *= p;
481 mask <<= 1;
482 p *= p;
483 }
484 return r;
485}
486
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000488float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489{
490 double iv, iw, ix;
Guido van Rossum39739ea1996-01-12 01:22:56 +0000491 long intw;
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000492 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
493 * The z parameter is really only going to be useful for integers and
494 * long integers. Maybe something clever with logarithms could be done.
495 * [AMK]
496 */
Neil Schemenauer32117e52001-01-04 01:44:34 +0000497 CONVERT_TO_DOUBLE(v, iv);
498 CONVERT_TO_DOUBLE(w, iw);
Guido van Rossum39739ea1996-01-12 01:22:56 +0000499 intw = (long)iw;
Tim Petersc54d1902000-10-06 00:36:09 +0000500
501 /* Sort out special cases here instead of relying on pow() */
502 if (iw == 0) { /* x**0 is 1, even 0**0 */
503 PyFPE_START_PROTECT("pow", return NULL)
504 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000505 double iz;
506 CONVERT_TO_DOUBLE(w, iz);
507 ix=fmod(1.0, iz);
508 if (ix!=0 && iz<0) ix+=iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000509 }
Tim Petersc54d1902000-10-06 00:36:09 +0000510 else
511 ix = 1.0;
512 PyFPE_END_PROTECT(ix)
513 return PyFloat_FromDouble(ix);
514 }
515 if (iv == 0.0) {
516 if (iw < 0.0) {
517 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000518 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000519 return NULL;
520 }
521 return PyFloat_FromDouble(0.0);
522 }
523
524 if (iw == intw && intw > LONG_MIN) {
525 /* ruled out LONG_MIN because -LONG_MIN isn't representable */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000526 errno = 0;
Tim Petersc54d1902000-10-06 00:36:09 +0000527 PyFPE_START_PROTECT("pow", return NULL)
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000528 if (intw > 0)
529 ix = powu(iv, intw);
530 else
531 ix = 1./powu(iv, -intw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000532 PyFPE_END_PROTECT(ix)
Guido van Rossum86c04c21996-08-09 20:50:14 +0000533 }
534 else {
535 /* Sort out special cases here instead of relying on pow() */
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000536 if (iv < 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000538 "negative number cannot be raised to a fractional power");
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000539 return NULL;
540 }
Guido van Rossum39739ea1996-01-12 01:22:56 +0000541 errno = 0;
Tim Petersc54d1902000-10-06 00:36:09 +0000542 PyFPE_START_PROTECT("pow", return NULL)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000543 ix = pow(iv, iw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000544 PyFPE_END_PROTECT(ix)
Guido van Rossum70d93461991-05-28 21:57:39 +0000545 }
Guido van Rossum7fa52f81991-12-16 15:43:14 +0000546 CHECK(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000547 if (errno != 0) {
548 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000551 }
Tim Petersc54d1902000-10-06 00:36:09 +0000552 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000553 double iz;
554 CONVERT_TO_DOUBLE(z, iz);
555 PyFPE_START_PROTECT("pow", return 0)
556 ix=fmod(ix, iz); /* XXX To Be Rewritten */
557 if (ix!=0 && ((iv<0 && iz>0) || (iv>0 && iz<0) )) {
558 ix+=iz;
Tim Petersc54d1902000-10-06 00:36:09 +0000559 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000560 PyFPE_END_PROTECT(ix)
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000561 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563}
564
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000566float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569}
570
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000572float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574 Py_INCREF(v);
575 return (PyObject *)v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000576}
577
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000579float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000580{
581 if (v->ob_fval < 0)
582 return float_neg(v);
583 else
584 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585}
586
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000587static int
Fred Drakefd99de62000-07-09 05:02:18 +0000588float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000589{
590 return v->ob_fval != 0.0;
591}
592
Guido van Rossum234f9421993-06-17 12:35:49 +0000593static int
Fred Drakefd99de62000-07-09 05:02:18 +0000594float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000595{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 if (PyInt_Check(*pw)) {
597 long x = PyInt_AsLong(*pw);
598 *pw = PyFloat_FromDouble((double)x);
599 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000600 return 0;
601 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 else if (PyLong_Check(*pw)) {
603 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
604 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000605 return 0;
606 }
607 return 1; /* Can't do it */
608}
609
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000611float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000612{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 double x = PyFloat_AsDouble(v);
Guido van Rossum03093a21994-09-28 15:51:32 +0000614 if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
615 : (x = floor(x)) > (double)LONG_MAX) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 PyErr_SetString(PyExc_OverflowError,
617 "float too large to convert");
Guido van Rossum03093a21994-09-28 15:51:32 +0000618 return NULL;
619 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 return PyInt_FromLong((long)x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000621}
622
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000624float_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000625{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626 double x = PyFloat_AsDouble(v);
627 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000628}
629
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000631float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000632{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000634 return v;
635}
636
637
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000639 (binaryfunc)float_add, /*nb_add*/
640 (binaryfunc)float_sub, /*nb_subtract*/
641 (binaryfunc)float_mul, /*nb_multiply*/
642 (binaryfunc)float_div, /*nb_divide*/
643 (binaryfunc)float_rem, /*nb_remainder*/
644 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000645 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000646 (unaryfunc)float_neg, /*nb_negative*/
647 (unaryfunc)float_pos, /*nb_positive*/
648 (unaryfunc)float_abs, /*nb_absolute*/
649 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000650 0, /*nb_invert*/
651 0, /*nb_lshift*/
652 0, /*nb_rshift*/
653 0, /*nb_and*/
654 0, /*nb_xor*/
655 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000656 (coercion)float_coerce, /*nb_coerce*/
657 (unaryfunc)float_int, /*nb_int*/
658 (unaryfunc)float_long, /*nb_long*/
659 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000660 0, /*nb_oct*/
661 0, /*nb_hex*/
Neil Schemenauer32117e52001-01-04 01:44:34 +0000662 0, /*nb_inplace_add*/
663 0, /*nb_inplace_subtract*/
664 0, /*nb_inplace_multiply*/
665 0, /*nb_inplace_divide*/
666 0, /*nb_inplace_remainder*/
667 0, /*nb_inplace_power*/
668 0, /*nb_inplace_lshift*/
669 0, /*nb_inplace_rshift*/
670 0, /*nb_inplace_and*/
671 0, /*nb_inplace_xor*/
672 0, /*nb_inplace_or*/
673
674 /* New style slots: */
675 (binaryfunc)float_cmp, /*nb_cmp*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676};
677
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678PyTypeObject PyFloat_Type = {
679 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000680 0,
681 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000683 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +0000684 (destructor)float_dealloc, /*tp_dealloc*/
685 (printfunc)float_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686 0, /*tp_getattr*/
687 0, /*tp_setattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000688 (cmpfunc)float_compare, /*tp_compare*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000689 (reprfunc)float_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000690 &float_as_number, /*tp_as_number*/
691 0, /*tp_as_sequence*/
692 0, /*tp_as_mapping*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000693 (hashfunc)float_hash, /*tp_hash*/
694 0, /*tp_call*/
695 (reprfunc)float_str, /*tp_str*/
Neil Schemenauer32117e52001-01-04 01:44:34 +0000696 0, /*tp_getattro*/
697 0, /*tp_setattro*/
698 0, /*tp_as_buffer*/
699 Py_TPFLAGS_NEWSTYLENUMBER /*tp_flags*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000701
702void
Fred Drakefd99de62000-07-09 05:02:18 +0000703PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000704{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000705 PyFloatObject *p;
706 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000707 int i;
708 int bc, bf; /* block count, number of freed blocks */
709 int frem, fsum; /* remaining unfreed floats per block, total */
710
711 bc = 0;
712 bf = 0;
713 fsum = 0;
714 list = block_list;
715 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000716 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000717 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000718 bc++;
719 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000720 for (i = 0, p = &list->objects[0];
721 i < N_FLOATOBJECTS;
722 i++, p++) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000723 if (PyFloat_Check(p) && p->ob_refcnt != 0)
724 frem++;
725 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000726 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000727 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000728 list->next = block_list;
729 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000730 for (i = 0, p = &list->objects[0];
731 i < N_FLOATOBJECTS;
732 i++, p++) {
733 if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
734 p->ob_type = (struct _typeobject *)
735 free_list;
736 free_list = p;
737 }
738 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000739 }
740 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000741 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000742 bf++;
743 }
744 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000745 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000746 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000747 if (!Py_VerboseFlag)
748 return;
749 fprintf(stderr, "# cleanup floats");
750 if (!fsum) {
751 fprintf(stderr, "\n");
752 }
753 else {
754 fprintf(stderr,
755 ": %d unfreed float%s in %d out of %d block%s\n",
756 fsum, fsum == 1 ? "" : "s",
757 bc - bf, bc, bc == 1 ? "" : "s");
758 }
759 if (Py_VerboseFlag > 1) {
760 list = block_list;
761 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000762 for (i = 0, p = &list->objects[0];
763 i < N_FLOATOBJECTS;
764 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000765 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
766 char buf[100];
767 PyFloat_AsString(buf, p);
768 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000769 "# <float at %p, refcnt=%d, val=%s>\n",
770 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000771 }
772 }
773 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000774 }
775 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000776}