blob: 7476ac7ca8dd88ecd7684a6ca89f2269cee740f7 [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
Jack Janseneddc1442003-11-20 01:44:59 +000011#if !defined(__STDC__)
Tim Petersdbd9ba62000-07-09 03:09:57 +000012extern double fmod(double, double);
13extern double pow(double, double);
Guido van Rossum6923e131990-11-02 17:50:43 +000014#endif
15
Guido van Rossum93ad0df1997-05-13 21:00:42 +000016/* Special free list -- see comments for same code in intobject.c. */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000017#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000018#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
Guido van Rossumf61bbc81999-03-12 00:12:21 +000019#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
Guido van Rossum3fce8831999-03-12 19:43:17 +000020
Guido van Rossum3fce8831999-03-12 19:43:17 +000021struct _floatblock {
22 struct _floatblock *next;
23 PyFloatObject objects[N_FLOATOBJECTS];
24};
25
26typedef struct _floatblock PyFloatBlock;
27
28static PyFloatBlock *block_list = NULL;
29static PyFloatObject *free_list = NULL;
30
Guido van Rossum93ad0df1997-05-13 21:00:42 +000031static PyFloatObject *
Fred Drakefd99de62000-07-09 05:02:18 +000032fill_free_list(void)
Guido van Rossum93ad0df1997-05-13 21:00:42 +000033{
34 PyFloatObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000035 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
36 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
Guido van Rossum93ad0df1997-05-13 21:00:42 +000037 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000038 return (PyFloatObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000039 ((PyFloatBlock *)p)->next = block_list;
40 block_list = (PyFloatBlock *)p;
41 p = &((PyFloatBlock *)p)->objects[0];
Guido van Rossum93ad0df1997-05-13 21:00:42 +000042 q = p + N_FLOATOBJECTS;
43 while (--q > p)
Guido van Rossumf61bbc81999-03-12 00:12:21 +000044 q->ob_type = (struct _typeobject *)(q-1);
45 q->ob_type = NULL;
Guido van Rossum93ad0df1997-05-13 21:00:42 +000046 return p + N_FLOATOBJECTS - 1;
47}
48
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050PyFloat_FromDouble(double fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Guido van Rossum93ad0df1997-05-13 21:00:42 +000052 register PyFloatObject *op;
53 if (free_list == NULL) {
54 if ((free_list = fill_free_list()) == NULL)
55 return NULL;
56 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +000057 /* Inline PyObject_New */
Guido van Rossum93ad0df1997-05-13 21:00:42 +000058 op = free_list;
Guido van Rossumf61bbc81999-03-12 00:12:21 +000059 free_list = (PyFloatObject *)op->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +000060 PyObject_INIT(op, &PyFloat_Type);
Guido van Rossum2a9096b1990-10-21 22:15:08 +000061 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063}
64
Tim Petersef14d732000-09-23 03:39:17 +000065/**************************************************************************
66RED_FLAG 22-Sep-2000 tim
67PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
68
691. If v was a regular string, *pend was set to point to its terminating
70 null byte. That's useless (the caller can find that without any
71 help from this function!).
72
732. If v was a Unicode string, or an object convertible to a character
74 buffer, *pend was set to point into stack trash (the auto temp
75 vector holding the character buffer). That was downright dangerous.
76
77Since we can't change the interface of a public API function, pend is
78still supported but now *officially* useless: if pend is not NULL,
79*pend is set to NULL.
80**************************************************************************/
Barry Warsaw226ae6c1999-10-12 19:54:53 +000081PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000082PyFloat_FromString(PyObject *v, char **pend)
Barry Warsaw226ae6c1999-10-12 19:54:53 +000083{
Guido van Rossum4c08d552000-03-10 22:55:18 +000084 const char *s, *last, *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +000085 double x;
Tim Petersef14d732000-09-23 03:39:17 +000086 char buffer[256]; /* for errors */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000087#ifdef Py_USING_UNICODE
Tim Petersef14d732000-09-23 03:39:17 +000088 char s_buffer[256]; /* for objects convertible to a char buffer */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000089#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +000090 int len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091
Tim Petersef14d732000-09-23 03:39:17 +000092 if (pend)
93 *pend = NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +000094 if (PyString_Check(v)) {
95 s = PyString_AS_STRING(v);
96 len = PyString_GET_SIZE(v);
97 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +000098#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +000099 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000100 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
101 PyErr_SetString(PyExc_ValueError,
Tim Petersef14d732000-09-23 03:39:17 +0000102 "Unicode float() literal too long to convert");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000103 return NULL;
104 }
Tim Petersef14d732000-09-23 03:39:17 +0000105 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000106 PyUnicode_GET_SIZE(v),
Tim Petersd2364e82001-11-01 20:09:42 +0000107 s_buffer,
Guido van Rossum9e896b32000-04-05 20:11:21 +0000108 NULL))
109 return NULL;
110 s = s_buffer;
Trent Micka248fb62000-08-12 21:37:39 +0000111 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000112 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000113#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000114 else if (PyObject_AsCharBuffer(v, &s, &len)) {
115 PyErr_SetString(PyExc_TypeError,
Skip Montanaro71390a92002-05-02 13:03:22 +0000116 "float() argument must be a string or a number");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000117 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000118 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000119
Guido van Rossum4c08d552000-03-10 22:55:18 +0000120 last = s + len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000121 while (*s && isspace(Py_CHARMASK(*s)))
122 s++;
Tim Petersef14d732000-09-23 03:39:17 +0000123 if (*s == '\0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000124 PyErr_SetString(PyExc_ValueError, "empty string for float()");
125 return NULL;
126 }
Tim Petersef14d732000-09-23 03:39:17 +0000127 /* We don't care about overflow or underflow. If the platform supports
128 * them, infinities and signed zeroes (on underflow) are fine.
129 * However, strtod can return 0 for denormalized numbers, where atof
130 * does not. So (alas!) we special-case a zero result. Note that
131 * whether strtod sets errno on underflow is not defined, so we can't
132 * key off errno.
133 */
Tim Peters858346e2000-09-25 21:01:28 +0000134 PyFPE_START_PROTECT("strtod", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000135 x = strtod(s, (char **)&end);
Tim Peters858346e2000-09-25 21:01:28 +0000136 PyFPE_END_PROTECT(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000137 errno = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
Tim Petersef14d732000-09-23 03:39:17 +0000139 byte at the end of the string, when the input is inf(inity). */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000140 if (end > last)
141 end = last;
Tim Petersef14d732000-09-23 03:39:17 +0000142 if (end == s) {
Barry Warsawaf8aef92001-11-28 20:52:21 +0000143 PyOS_snprintf(buffer, sizeof(buffer),
144 "invalid literal for float(): %.200s", s);
Tim Petersef14d732000-09-23 03:39:17 +0000145 PyErr_SetString(PyExc_ValueError, buffer);
146 return NULL;
147 }
148 /* Since end != s, the platform made *some* kind of sense out
149 of the input. Trust it. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000150 while (*end && isspace(Py_CHARMASK(*end)))
151 end++;
152 if (*end != '\0') {
Barry Warsawaf8aef92001-11-28 20:52:21 +0000153 PyOS_snprintf(buffer, sizeof(buffer),
154 "invalid literal for float(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000155 PyErr_SetString(PyExc_ValueError, buffer);
156 return NULL;
157 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000158 else if (end != last) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000159 PyErr_SetString(PyExc_ValueError,
160 "null byte in argument for float()");
161 return NULL;
162 }
Tim Petersef14d732000-09-23 03:39:17 +0000163 if (x == 0.0) {
164 /* See above -- may have been strtod being anal
165 about denorms. */
Tim Peters858346e2000-09-25 21:01:28 +0000166 PyFPE_START_PROTECT("atof", return NULL)
Tim Petersef14d732000-09-23 03:39:17 +0000167 x = atof(s);
Tim Peters858346e2000-09-25 21:01:28 +0000168 PyFPE_END_PROTECT(x)
Tim Petersef14d732000-09-23 03:39:17 +0000169 errno = 0; /* whether atof ever set errno is undefined */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000170 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000171 return PyFloat_FromDouble(x);
172}
173
Guido van Rossum234f9421993-06-17 12:35:49 +0000174static void
Fred Drakefd99de62000-07-09 05:02:18 +0000175float_dealloc(PyFloatObject *op)
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000176{
Guido van Rossum9475a232001-10-05 20:51:39 +0000177 if (PyFloat_CheckExact(op)) {
178 op->ob_type = (struct _typeobject *)free_list;
179 free_list = op;
180 }
181 else
182 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000183}
184
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185double
Fred Drakefd99de62000-07-09 05:02:18 +0000186PyFloat_AsDouble(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 PyNumberMethods *nb;
189 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190 double val;
Tim Petersd2364e82001-11-01 20:09:42 +0000191
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 if (op && PyFloat_Check(op))
193 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Tim Petersd2364e82001-11-01 20:09:42 +0000194
Neil Schemenauer2c77e902002-11-18 16:06:21 +0000195 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 return -1;
198 }
Tim Petersd2364e82001-11-01 20:09:42 +0000199
Neil Schemenauer2c77e902002-11-18 16:06:21 +0000200 if ((nb = op->ob_type->tp_as_number) == NULL || nb->nb_float == NULL) {
201 PyErr_SetString(PyExc_TypeError, "a float is required");
202 return -1;
203 }
204
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206 if (fo == NULL)
207 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 if (!PyFloat_Check(fo)) {
209 PyErr_SetString(PyExc_TypeError,
210 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211 return -1;
212 }
Tim Petersd2364e82001-11-01 20:09:42 +0000213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 val = PyFloat_AS_DOUBLE(fo);
215 Py_DECREF(fo);
Tim Petersd2364e82001-11-01 20:09:42 +0000216
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
220/* Methods */
221
Tim Peters97019e42001-11-28 22:43:45 +0000222static void
223format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224{
225 register char *cp;
226 /* Subroutine for float_repr and float_print.
227 We want float numbers to be recognizable as such,
228 i.e., they should contain a decimal point or an exponent.
229 However, %g may print the number as an integer;
230 in such cases, we append ".0" to the string. */
Tim Peters97019e42001-11-28 22:43:45 +0000231
232 assert(PyFloat_Check(v));
233 PyOS_snprintf(buf, buflen, "%.*g", precision, v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234 cp = buf;
235 if (*cp == '-')
236 cp++;
237 for (; *cp != '\0'; cp++) {
238 /* Any non-digit means it's not an integer;
239 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000240 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241 break;
242 }
243 if (*cp == '\0') {
244 *cp++ = '.';
245 *cp++ = '0';
246 *cp++ = '\0';
247 }
248}
249
Tim Peters97019e42001-11-28 22:43:45 +0000250/* XXX PyFloat_AsStringEx should not be a public API function (for one
251 XXX thing, its signature passes a buffer without a length; for another,
252 XXX it isn't useful outside this file).
253*/
254void
255PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
256{
257 format_float(buf, 100, v, precision);
258}
259
Neil Schemenauer32117e52001-01-04 01:44:34 +0000260/* Macro and helper that convert PyObject obj to a C double and store
261 the value in dbl; this replaces the functionality of the coercion
Tim Peters77d8a4f2001-12-11 20:31:34 +0000262 slot function. If conversion to double raises an exception, obj is
263 set to NULL, and the function invoking this macro returns NULL. If
264 obj is not of float, int or long type, Py_NotImplemented is incref'ed,
265 stored in obj, and returned from the function invoking this macro.
266*/
Neil Schemenauer32117e52001-01-04 01:44:34 +0000267#define CONVERT_TO_DOUBLE(obj, dbl) \
268 if (PyFloat_Check(obj)) \
269 dbl = PyFloat_AS_DOUBLE(obj); \
270 else if (convert_to_double(&(obj), &(dbl)) < 0) \
271 return obj;
272
273static int
Tim Peters9fffa3e2001-09-04 05:14:19 +0000274convert_to_double(PyObject **v, double *dbl)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000275{
276 register PyObject *obj = *v;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000277
Neil Schemenauer32117e52001-01-04 01:44:34 +0000278 if (PyInt_Check(obj)) {
279 *dbl = (double)PyInt_AS_LONG(obj);
280 }
281 else if (PyLong_Check(obj)) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000282 *dbl = PyLong_AsDouble(obj);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000283 if (*dbl == -1.0 && PyErr_Occurred()) {
284 *v = NULL;
285 return -1;
286 }
Neil Schemenauer32117e52001-01-04 01:44:34 +0000287 }
288 else {
289 Py_INCREF(Py_NotImplemented);
290 *v = Py_NotImplemented;
291 return -1;
292 }
293 return 0;
294}
295
Guido van Rossum57072eb1999-12-23 19:00:28 +0000296/* Precisions used by repr() and str(), respectively.
297
298 The repr() precision (17 significant decimal digits) is the minimal number
299 that is guaranteed to have enough precision so that if the number is read
300 back in the exact same binary value is recreated. This is true for IEEE
301 floating point by design, and also happens to work for all other modern
302 hardware.
303
304 The str() precision is chosen so that in most cases, the rounding noise
305 created by various operations is suppressed, while giving plenty of
306 precision for practical use.
307
308*/
309
310#define PREC_REPR 17
311#define PREC_STR 12
312
Tim Peters97019e42001-11-28 22:43:45 +0000313/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
314 XXX they pass a char buffer without passing a length.
315*/
Guido van Rossum57072eb1999-12-23 19:00:28 +0000316void
Fred Drakefd99de62000-07-09 05:02:18 +0000317PyFloat_AsString(char *buf, PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000318{
Tim Peters97019e42001-11-28 22:43:45 +0000319 format_float(buf, 100, v, PREC_STR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000320}
321
Tim Peters72f98e92001-05-08 15:19:57 +0000322void
323PyFloat_AsReprString(char *buf, PyFloatObject *v)
324{
Tim Peters97019e42001-11-28 22:43:45 +0000325 format_float(buf, 100, v, PREC_REPR);
Tim Peters72f98e92001-05-08 15:19:57 +0000326}
327
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000328/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000329static int
Fred Drakefd99de62000-07-09 05:02:18 +0000330float_print(PyFloatObject *v, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331{
332 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000333 format_float(buf, sizeof(buf), v,
334 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000336 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337}
338
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000340float_repr(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341{
342 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000343 format_float(buf, sizeof(buf), v, PREC_REPR);
Guido van Rossum57072eb1999-12-23 19:00:28 +0000344 return PyString_FromString(buf);
345}
346
347static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000348float_str(PyFloatObject *v)
Guido van Rossum57072eb1999-12-23 19:00:28 +0000349{
350 char buf[100];
Tim Peters97019e42001-11-28 22:43:45 +0000351 format_float(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000352 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353}
354
355static int
Fred Drakefd99de62000-07-09 05:02:18 +0000356float_compare(PyFloatObject *v, PyFloatObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357{
358 double i = v->ob_fval;
359 double j = w->ob_fval;
360 return (i < j) ? -1 : (i > j) ? 1 : 0;
361}
362
Guido van Rossum9bfef441993-03-29 10:43:31 +0000363static long
Fred Drakefd99de62000-07-09 05:02:18 +0000364float_hash(PyFloatObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000365{
Tim Peters39dce292000-08-15 03:34:48 +0000366 return _Py_HashDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000367}
368
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000370float_add(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000372 double a,b;
373 CONVERT_TO_DOUBLE(v, a);
374 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000375 PyFPE_START_PROTECT("add", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000376 a = a + b;
377 PyFPE_END_PROTECT(a)
378 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379}
380
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000381static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000382float_sub(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000384 double a,b;
385 CONVERT_TO_DOUBLE(v, a);
386 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000387 PyFPE_START_PROTECT("subtract", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000388 a = a - b;
389 PyFPE_END_PROTECT(a)
390 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391}
392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000394float_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000396 double a,b;
397 CONVERT_TO_DOUBLE(v, a);
398 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000399 PyFPE_START_PROTECT("multiply", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000400 a = a * b;
401 PyFPE_END_PROTECT(a)
402 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403}
404
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000406float_div(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407{
Neil Schemenauer32117e52001-01-04 01:44:34 +0000408 double a,b;
409 CONVERT_TO_DOUBLE(v, a);
410 CONVERT_TO_DOUBLE(w, b);
411 if (b == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413 return NULL;
414 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000415 PyFPE_START_PROTECT("divide", return 0)
Neil Schemenauer32117e52001-01-04 01:44:34 +0000416 a = a / b;
417 PyFPE_END_PROTECT(a)
418 return PyFloat_FromDouble(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
420
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000422float_classic_div(PyObject *v, PyObject *w)
423{
424 double a,b;
425 CONVERT_TO_DOUBLE(v, a);
426 CONVERT_TO_DOUBLE(w, b);
Guido van Rossum1832de42001-09-04 03:51:09 +0000427 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000428 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
429 return NULL;
430 if (b == 0.0) {
431 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
432 return NULL;
433 }
434 PyFPE_START_PROTECT("divide", return 0)
435 a = a / b;
436 PyFPE_END_PROTECT(a)
437 return PyFloat_FromDouble(a);
438}
439
440static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000441float_rem(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000443 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000444 double mod;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000445 CONVERT_TO_DOUBLE(v, vx);
446 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 return NULL;
450 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000451 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000452 mod = fmod(vx, wx);
Guido van Rossum9263e781999-05-06 14:26:34 +0000453 /* note: checking mod*wx < 0 is incorrect -- underflows to
454 0 if wx < sqrt(smallest nonzero double) */
455 if (mod && ((wx < 0) != (mod < 0))) {
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000456 mod += wx;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000457 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000458 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460}
461
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000463float_divmod(PyObject *v, PyObject *w)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000464{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000465 double vx, wx;
Guido van Rossum9263e781999-05-06 14:26:34 +0000466 double div, mod, floordiv;
Neil Schemenauer32117e52001-01-04 01:44:34 +0000467 CONVERT_TO_DOUBLE(v, vx);
468 CONVERT_TO_DOUBLE(w, wx);
Guido van Rossum15ecff41991-10-20 20:16:45 +0000469 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000471 return NULL;
472 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000473 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000474 mod = fmod(vx, wx);
Tim Peters78fc0b52000-09-16 03:54:24 +0000475 /* fmod is typically exact, so vx-mod is *mathematically* an
Guido van Rossum9263e781999-05-06 14:26:34 +0000476 exact multiple of wx. But this is fp arithmetic, and fp
477 vx - mod is an approximation; the result is that div may
478 not be an exact integral value after the division, although
479 it will always be very close to one.
480 */
Guido van Rossum15ecff41991-10-20 20:16:45 +0000481 div = (vx - mod) / wx;
Tim Petersd2e40d62001-11-01 23:12:27 +0000482 if (mod) {
483 /* ensure the remainder has the same sign as the denominator */
484 if ((wx < 0) != (mod < 0)) {
485 mod += wx;
486 div -= 1.0;
487 }
488 }
489 else {
490 /* the remainder is zero, and in the presence of signed zeroes
491 fmod returns different results across platforms; ensure
492 it has the same sign as the denominator; we'd like to do
493 "mod = wx * 0.0", but that may get optimized away */
Tim Peters4e8ab5d2001-11-01 23:59:56 +0000494 mod *= mod; /* hide "mod = +0" from optimizer */
Tim Petersd2e40d62001-11-01 23:12:27 +0000495 if (wx < 0.0)
496 mod = -mod;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000497 }
Guido van Rossum9263e781999-05-06 14:26:34 +0000498 /* snap quotient to nearest integral value */
Tim Petersd2e40d62001-11-01 23:12:27 +0000499 if (div) {
500 floordiv = floor(div);
501 if (div - floordiv > 0.5)
502 floordiv += 1.0;
503 }
504 else {
505 /* div is zero - get the same sign as the true quotient */
506 div *= div; /* hide "div = +0" from optimizers */
507 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
508 }
509 PyFPE_END_PROTECT(floordiv)
Guido van Rossum9263e781999-05-06 14:26:34 +0000510 return Py_BuildValue("(dd)", floordiv, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000511}
512
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513static PyObject *
Tim Peters63a35712001-12-11 19:57:24 +0000514float_floor_div(PyObject *v, PyObject *w)
515{
516 PyObject *t, *r;
517
518 t = float_divmod(v, w);
Tim Peters77d8a4f2001-12-11 20:31:34 +0000519 if (t == NULL || t == Py_NotImplemented)
520 return t;
521 assert(PyTuple_CheckExact(t));
522 r = PyTuple_GET_ITEM(t, 0);
523 Py_INCREF(r);
524 Py_DECREF(t);
525 return r;
Tim Peters63a35712001-12-11 19:57:24 +0000526}
527
528static PyObject *
Neil Schemenauer32117e52001-01-04 01:44:34 +0000529float_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530{
531 double iv, iw, ix;
Tim Peters32f453e2001-09-03 08:35:41 +0000532
533 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000534 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
Tim Peters97f4a332001-09-05 23:49:24 +0000535 "allowed unless all arguments are integers");
Tim Peters32f453e2001-09-03 08:35:41 +0000536 return NULL;
537 }
538
Neil Schemenauer32117e52001-01-04 01:44:34 +0000539 CONVERT_TO_DOUBLE(v, iv);
540 CONVERT_TO_DOUBLE(w, iw);
Tim Petersc54d1902000-10-06 00:36:09 +0000541
542 /* Sort out special cases here instead of relying on pow() */
Tim Peters96685bf2001-08-23 22:31:37 +0000543 if (iw == 0) { /* v**0 is 1, even 0**0 */
Tim Petersc54d1902000-10-06 00:36:09 +0000544 PyFPE_START_PROTECT("pow", return NULL)
545 if ((PyObject *)z != Py_None) {
Neil Schemenauer32117e52001-01-04 01:44:34 +0000546 double iz;
Neil Schemenauer010b0cc2001-01-08 06:29:50 +0000547 CONVERT_TO_DOUBLE(z, iz);
Tim Peters96685bf2001-08-23 22:31:37 +0000548 ix = fmod(1.0, iz);
549 if (ix != 0 && iz < 0)
550 ix += iz;
Guido van Rossum70d93461991-05-28 21:57:39 +0000551 }
Tim Petersc54d1902000-10-06 00:36:09 +0000552 else
553 ix = 1.0;
554 PyFPE_END_PROTECT(ix)
Tim Petersd2364e82001-11-01 20:09:42 +0000555 return PyFloat_FromDouble(ix);
Tim Petersc54d1902000-10-06 00:36:09 +0000556 }
Tim Peters96685bf2001-08-23 22:31:37 +0000557 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
Tim Petersc54d1902000-10-06 00:36:09 +0000558 if (iw < 0.0) {
559 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000560 "0.0 cannot be raised to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000561 return NULL;
562 }
563 return PyFloat_FromDouble(0.0);
564 }
Tim Peterse87568d2003-05-24 20:18:24 +0000565 if (iv < 0.0) {
566 /* Whether this is an error is a mess, and bumps into libm
567 * bugs so we have to figure it out ourselves.
568 */
569 if (iw != floor(iw)) {
570 PyErr_SetString(PyExc_ValueError, "negative number "
571 "cannot be raised to a fractional power");
572 return NULL;
573 }
574 /* iw is an exact integer, albeit perhaps a very large one.
575 * -1 raised to an exact integer should never be exceptional.
576 * Alas, some libms (chiefly glibc as of early 2003) return
577 * NaN and set EDOM on pow(-1, large_int) if the int doesn't
578 * happen to be representable in a *C* integer. That's a
579 * bug; we let that slide in math.pow() (which currently
580 * reflects all platform accidents), but not for Python's **.
581 */
582 if (iv == -1.0 && !Py_IS_INFINITY(iw) && iw == iw) {
583 /* XXX the "iw == iw" was to weed out NaNs. This
584 * XXX doesn't actually work on all platforms.
585 */
586 /* Return 1 if iw is even, -1 if iw is odd; there's
587 * no guarantee that any C integral type is big
588 * enough to hold iw, so we have to check this
589 * indirectly.
590 */
591 ix = floor(iw * 0.5) * 2.0;
592 return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);
593 }
594 /* Else iv != -1.0, and overflow or underflow are possible.
595 * Unless we're to write pow() ourselves, we have to trust
596 * the platform to do this correctly.
597 */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000598 }
Tim Peters96685bf2001-08-23 22:31:37 +0000599 errno = 0;
600 PyFPE_START_PROTECT("pow", return NULL)
601 ix = pow(iv, iw);
602 PyFPE_END_PROTECT(ix)
Tim Petersdc5a5082002-03-09 04:58:24 +0000603 Py_ADJUST_ERANGE1(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000604 if (errno != 0) {
Tim Peterse87568d2003-05-24 20:18:24 +0000605 /* We don't expect any errno value other than ERANGE, but
606 * the range of libm bugs appears unbounded.
607 */
608 PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
609 PyExc_ValueError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000611 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613}
614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000616float_neg(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000622float_pos(PyFloatObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623{
Tim Peters0280cf72001-09-11 21:53:35 +0000624 if (PyFloat_CheckExact(v)) {
625 Py_INCREF(v);
626 return (PyObject *)v;
627 }
628 else
629 return PyFloat_FromDouble(v->ob_fval);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000630}
631
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000633float_abs(PyFloatObject *v)
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000634{
Tim Petersfaf0cd22001-11-01 21:51:15 +0000635 return PyFloat_FromDouble(fabs(v->ob_fval));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636}
637
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000638static int
Fred Drakefd99de62000-07-09 05:02:18 +0000639float_nonzero(PyFloatObject *v)
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000640{
641 return v->ob_fval != 0.0;
642}
643
Guido van Rossum234f9421993-06-17 12:35:49 +0000644static int
Fred Drakefd99de62000-07-09 05:02:18 +0000645float_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +0000646{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 if (PyInt_Check(*pw)) {
648 long x = PyInt_AsLong(*pw);
649 *pw = PyFloat_FromDouble((double)x);
650 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000651 return 0;
652 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 else if (PyLong_Check(*pw)) {
Neal Norwitzabcb0c02003-01-28 19:21:24 +0000654 double x = PyLong_AsDouble(*pw);
655 if (x == -1.0 && PyErr_Occurred())
656 return -1;
657 *pw = PyFloat_FromDouble(x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000659 return 0;
660 }
Guido van Rossum1952e382001-09-19 01:25:16 +0000661 else if (PyFloat_Check(*pw)) {
662 Py_INCREF(*pv);
663 Py_INCREF(*pw);
664 return 0;
665 }
Guido van Rossume6eefc21992-08-14 12:06:52 +0000666 return 1; /* Can't do it */
667}
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +0000670float_long(PyObject *v)
671{
672 double x = PyFloat_AsDouble(v);
673 return PyLong_FromDouble(x);
674}
675
676static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000677float_int(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000678{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 double x = PyFloat_AsDouble(v);
Tim Peters7321ec42001-07-26 20:02:17 +0000680 double wholepart; /* integral portion of x, rounded toward 0 */
Tim Peters7321ec42001-07-26 20:02:17 +0000681
682 (void)modf(x, &wholepart);
Tim Peters7d791242002-11-21 22:26:37 +0000683 /* Try to get out cheap if this fits in a Python int. The attempt
684 * to cast to long must be protected, as C doesn't define what
685 * happens if the double is too big to fit in a long. Some rare
686 * systems raise an exception then (RISCOS was mentioned as one,
687 * and someone using a non-default option on Sun also bumped into
688 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
689 * still be vulnerable: if a long has more bits of precision than
690 * a double, casting MIN/MAX to double may yield an approximation,
691 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
692 * yield true from the C expression wholepart<=LONG_MAX, despite
693 * that wholepart is actually greater than LONG_MAX.
694 */
695 if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
696 const long aslong = (long)wholepart;
Tim Peters7321ec42001-07-26 20:02:17 +0000697 return PyInt_FromLong(aslong);
Tim Peters7d791242002-11-21 22:26:37 +0000698 }
699 return PyLong_FromDouble(wholepart);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000700}
701
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000703float_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000704{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000706 return v;
707}
708
709
Jeremy Hylton938ace62002-07-17 16:30:39 +0000710static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000711float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
712
Tim Peters6d6c1a32001-08-02 04:15:00 +0000713static PyObject *
714float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
715{
716 PyObject *x = Py_False; /* Integer zero */
717 static char *kwlist[] = {"x", 0};
718
Guido van Rossumbef14172001-08-29 15:47:46 +0000719 if (type != &PyFloat_Type)
720 return float_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
722 return NULL;
723 if (PyString_Check(x))
724 return PyFloat_FromString(x, NULL);
725 return PyNumber_Float(x);
726}
727
Guido van Rossumbef14172001-08-29 15:47:46 +0000728/* Wimpy, slow approach to tp_new calls for subtypes of float:
729 first create a regular float from whatever arguments we got,
730 then allocate a subtype instance and initialize its ob_fval
731 from the regular float. The regular float is then thrown away.
732*/
733static PyObject *
734float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
735{
736 PyObject *tmp, *new;
737
738 assert(PyType_IsSubtype(type, &PyFloat_Type));
739 tmp = float_new(&PyFloat_Type, args, kwds);
740 if (tmp == NULL)
741 return NULL;
Tim Peters2400fa42001-09-12 19:12:49 +0000742 assert(PyFloat_CheckExact(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000743 new = type->tp_alloc(type, 0);
Raymond Hettingerf4667932003-06-28 20:04:25 +0000744 if (new == NULL) {
745 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000746 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000747 }
Guido van Rossumbef14172001-08-29 15:47:46 +0000748 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
749 Py_DECREF(tmp);
750 return new;
751}
752
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000753static PyObject *
754float_getnewargs(PyFloatObject *v)
755{
756 return Py_BuildValue("(d)", v->ob_fval);
757}
758
759static PyMethodDef float_methods[] = {
760 {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
761 {NULL, NULL} /* sentinel */
762};
763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000764PyDoc_STRVAR(float_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765"float(x) -> floating point number\n\
766\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000767Convert a string or number to a floating point number, if possible.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000768
769
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000770static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000771 (binaryfunc)float_add, /*nb_add*/
772 (binaryfunc)float_sub, /*nb_subtract*/
773 (binaryfunc)float_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000774 (binaryfunc)float_classic_div, /*nb_divide*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000775 (binaryfunc)float_rem, /*nb_remainder*/
776 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000777 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778 (unaryfunc)float_neg, /*nb_negative*/
779 (unaryfunc)float_pos, /*nb_positive*/
780 (unaryfunc)float_abs, /*nb_absolute*/
781 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000782 0, /*nb_invert*/
783 0, /*nb_lshift*/
784 0, /*nb_rshift*/
785 0, /*nb_and*/
786 0, /*nb_xor*/
787 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 (coercion)float_coerce, /*nb_coerce*/
789 (unaryfunc)float_int, /*nb_int*/
790 (unaryfunc)float_long, /*nb_long*/
791 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000792 0, /* nb_oct */
793 0, /* nb_hex */
794 0, /* nb_inplace_add */
795 0, /* nb_inplace_subtract */
796 0, /* nb_inplace_multiply */
797 0, /* nb_inplace_divide */
798 0, /* nb_inplace_remainder */
799 0, /* nb_inplace_power */
800 0, /* nb_inplace_lshift */
801 0, /* nb_inplace_rshift */
802 0, /* nb_inplace_and */
803 0, /* nb_inplace_xor */
804 0, /* nb_inplace_or */
Tim Peters63a35712001-12-11 19:57:24 +0000805 float_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +0000806 float_div, /* nb_true_divide */
807 0, /* nb_inplace_floor_divide */
808 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809};
810
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000811PyTypeObject PyFloat_Type = {
812 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000813 0,
814 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000817 (destructor)float_dealloc, /* tp_dealloc */
818 (printfunc)float_print, /* tp_print */
819 0, /* tp_getattr */
820 0, /* tp_setattr */
821 (cmpfunc)float_compare, /* tp_compare */
822 (reprfunc)float_repr, /* tp_repr */
823 &float_as_number, /* tp_as_number */
824 0, /* tp_as_sequence */
825 0, /* tp_as_mapping */
826 (hashfunc)float_hash, /* tp_hash */
827 0, /* tp_call */
828 (reprfunc)float_str, /* tp_str */
829 PyObject_GenericGetAttr, /* tp_getattro */
830 0, /* tp_setattro */
831 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000832 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
833 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000834 float_doc, /* tp_doc */
835 0, /* tp_traverse */
836 0, /* tp_clear */
837 0, /* tp_richcompare */
838 0, /* tp_weaklistoffset */
839 0, /* tp_iter */
840 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000841 float_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000842 0, /* tp_members */
843 0, /* tp_getset */
844 0, /* tp_base */
845 0, /* tp_dict */
846 0, /* tp_descr_get */
847 0, /* tp_descr_set */
848 0, /* tp_dictoffset */
849 0, /* tp_init */
850 0, /* tp_alloc */
851 float_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000852};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000853
854void
Fred Drakefd99de62000-07-09 05:02:18 +0000855PyFloat_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000856{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000857 PyFloatObject *p;
858 PyFloatBlock *list, *next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000859 int i;
860 int bc, bf; /* block count, number of freed blocks */
861 int frem, fsum; /* remaining unfreed floats per block, total */
862
863 bc = 0;
864 bf = 0;
865 fsum = 0;
866 list = block_list;
867 block_list = NULL;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000868 free_list = NULL;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000869 while (list != NULL) {
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000870 bc++;
871 frem = 0;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000872 for (i = 0, p = &list->objects[0];
873 i < N_FLOATOBJECTS;
874 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000875 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000876 frem++;
877 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000878 next = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000879 if (frem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000880 list->next = block_list;
881 block_list = list;
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000882 for (i = 0, p = &list->objects[0];
883 i < N_FLOATOBJECTS;
884 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000885 if (!PyFloat_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000886 p->ob_refcnt == 0) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000887 p->ob_type = (struct _typeobject *)
888 free_list;
889 free_list = p;
890 }
891 }
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000892 }
893 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000894 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000895 bf++;
896 }
897 fsum += frem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000898 list = next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000899 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000900 if (!Py_VerboseFlag)
901 return;
902 fprintf(stderr, "# cleanup floats");
903 if (!fsum) {
904 fprintf(stderr, "\n");
905 }
906 else {
907 fprintf(stderr,
908 ": %d unfreed float%s in %d out of %d block%s\n",
909 fsum, fsum == 1 ? "" : "s",
910 bc - bf, bc, bc == 1 ? "" : "s");
911 }
912 if (Py_VerboseFlag > 1) {
913 list = block_list;
914 while (list != NULL) {
Guido van Rossumd7b5fb81999-03-19 20:59:40 +0000915 for (i = 0, p = &list->objects[0];
916 i < N_FLOATOBJECTS;
917 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000918 if (PyFloat_CheckExact(p) &&
Guido van Rossumbef14172001-08-29 15:47:46 +0000919 p->ob_refcnt != 0) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000920 char buf[100];
921 PyFloat_AsString(buf, p);
922 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000923 "# <float at %p, refcnt=%d, val=%s>\n",
924 p, p->ob_refcnt, buf);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000925 }
926 }
927 list = list->next;
Guido van Rossumf61bbc81999-03-12 00:12:21 +0000928 }
929 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000930}
Tim Peters9905b942003-03-20 20:53:32 +0000931
932/*----------------------------------------------------------------------------
933 * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
934 *
935 * TODO: On platforms that use the standard IEEE-754 single and double
936 * formats natively, these routines could simply copy the bytes.
937 */
938int
939_PyFloat_Pack4(double x, unsigned char *p, int le)
940{
941 unsigned char sign;
942 int e;
943 double f;
944 unsigned int fbits;
945 int incr = 1;
946
947 if (le) {
948 p += 3;
949 incr = -1;
950 }
951
952 if (x < 0) {
953 sign = 1;
954 x = -x;
955 }
956 else
957 sign = 0;
958
959 f = frexp(x, &e);
960
961 /* Normalize f to be in the range [1.0, 2.0) */
962 if (0.5 <= f && f < 1.0) {
963 f *= 2.0;
964 e--;
965 }
966 else if (f == 0.0)
967 e = 0;
968 else {
969 PyErr_SetString(PyExc_SystemError,
970 "frexp() result out of range");
971 return -1;
972 }
973
974 if (e >= 128)
975 goto Overflow;
976 else if (e < -126) {
977 /* Gradual underflow */
978 f = ldexp(f, 126 + e);
979 e = 0;
980 }
981 else if (!(e == 0 && f == 0.0)) {
982 e += 127;
983 f -= 1.0; /* Get rid of leading 1 */
984 }
985
986 f *= 8388608.0; /* 2**23 */
Tim Petersf1ed9342003-03-21 17:10:03 +0000987 fbits = (unsigned int)(f + 0.5); /* Round */
Tim Peters9905b942003-03-20 20:53:32 +0000988 assert(fbits <= 8388608);
989 if (fbits >> 23) {
990 /* The carry propagated out of a string of 23 1 bits. */
991 fbits = 0;
992 ++e;
993 if (e >= 255)
994 goto Overflow;
995 }
996
997 /* First byte */
998 *p = (sign << 7) | (e >> 1);
999 p += incr;
1000
1001 /* Second byte */
1002 *p = (char) (((e & 1) << 7) | (fbits >> 16));
1003 p += incr;
1004
1005 /* Third byte */
1006 *p = (fbits >> 8) & 0xFF;
1007 p += incr;
1008
1009 /* Fourth byte */
1010 *p = fbits & 0xFF;
1011
1012 /* Done */
1013 return 0;
1014
1015 Overflow:
1016 PyErr_SetString(PyExc_OverflowError,
1017 "float too large to pack with f format");
1018 return -1;
1019}
1020
1021int
1022_PyFloat_Pack8(double x, unsigned char *p, int le)
1023{
1024 unsigned char sign;
1025 int e;
1026 double f;
1027 unsigned int fhi, flo;
1028 int incr = 1;
1029
1030 if (le) {
1031 p += 7;
1032 incr = -1;
1033 }
1034
1035 if (x < 0) {
1036 sign = 1;
1037 x = -x;
1038 }
1039 else
1040 sign = 0;
1041
1042 f = frexp(x, &e);
1043
1044 /* Normalize f to be in the range [1.0, 2.0) */
1045 if (0.5 <= f && f < 1.0) {
1046 f *= 2.0;
1047 e--;
1048 }
1049 else if (f == 0.0)
1050 e = 0;
1051 else {
1052 PyErr_SetString(PyExc_SystemError,
1053 "frexp() result out of range");
1054 return -1;
1055 }
1056
1057 if (e >= 1024)
1058 goto Overflow;
1059 else if (e < -1022) {
1060 /* Gradual underflow */
1061 f = ldexp(f, 1022 + e);
1062 e = 0;
1063 }
1064 else if (!(e == 0 && f == 0.0)) {
1065 e += 1023;
1066 f -= 1.0; /* Get rid of leading 1 */
1067 }
1068
1069 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1070 f *= 268435456.0; /* 2**28 */
1071 fhi = (unsigned int)f; /* Truncate */
1072 assert(fhi < 268435456);
1073
1074 f -= (double)fhi;
1075 f *= 16777216.0; /* 2**24 */
1076 flo = (unsigned int)(f + 0.5); /* Round */
1077 assert(flo <= 16777216);
1078 if (flo >> 24) {
1079 /* The carry propagated out of a string of 24 1 bits. */
1080 flo = 0;
1081 ++fhi;
1082 if (fhi >> 28) {
1083 /* And it also progagated out of the next 28 bits. */
1084 fhi = 0;
1085 ++e;
1086 if (e >= 2047)
1087 goto Overflow;
1088 }
1089 }
1090
1091 /* First byte */
1092 *p = (sign << 7) | (e >> 4);
1093 p += incr;
1094
1095 /* Second byte */
1096 *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
1097 p += incr;
1098
1099 /* Third byte */
1100 *p = (fhi >> 16) & 0xFF;
1101 p += incr;
1102
1103 /* Fourth byte */
1104 *p = (fhi >> 8) & 0xFF;
1105 p += incr;
1106
1107 /* Fifth byte */
1108 *p = fhi & 0xFF;
1109 p += incr;
1110
1111 /* Sixth byte */
1112 *p = (flo >> 16) & 0xFF;
1113 p += incr;
1114
1115 /* Seventh byte */
1116 *p = (flo >> 8) & 0xFF;
1117 p += incr;
1118
1119 /* Eighth byte */
1120 *p = flo & 0xFF;
1121 p += incr;
1122
1123 /* Done */
1124 return 0;
1125
1126 Overflow:
1127 PyErr_SetString(PyExc_OverflowError,
1128 "float too large to pack with d format");
1129 return -1;
1130}
1131
1132double
1133_PyFloat_Unpack4(const unsigned char *p, int le)
1134{
1135 unsigned char sign;
1136 int e;
1137 unsigned int f;
1138 double x;
1139 int incr = 1;
1140
1141 if (le) {
1142 p += 3;
1143 incr = -1;
1144 }
1145
1146 /* First byte */
1147 sign = (*p >> 7) & 1;
1148 e = (*p & 0x7F) << 1;
1149 p += incr;
1150
1151 /* Second byte */
1152 e |= (*p >> 7) & 1;
1153 f = (*p & 0x7F) << 16;
1154 p += incr;
1155
1156 /* Third byte */
1157 f |= *p << 8;
1158 p += incr;
1159
1160 /* Fourth byte */
1161 f |= *p;
1162
1163 x = (double)f / 8388608.0;
1164
1165 /* XXX This sadly ignores Inf/NaN issues */
1166 if (e == 0)
1167 e = -126;
1168 else {
1169 x += 1.0;
1170 e -= 127;
1171 }
1172 x = ldexp(x, e);
1173
1174 if (sign)
1175 x = -x;
1176
1177 return x;
1178}
1179
1180double
1181_PyFloat_Unpack8(const unsigned char *p, int le)
1182{
1183 unsigned char sign;
1184 int e;
1185 unsigned int fhi, flo;
1186 double x;
1187 int incr = 1;
1188
1189 if (le) {
1190 p += 7;
1191 incr = -1;
1192 }
1193
1194 /* First byte */
1195 sign = (*p >> 7) & 1;
1196 e = (*p & 0x7F) << 4;
1197 p += incr;
1198
1199 /* Second byte */
1200 e |= (*p >> 4) & 0xF;
1201 fhi = (*p & 0xF) << 24;
1202 p += incr;
1203
1204 /* Third byte */
1205 fhi |= *p << 16;
1206 p += incr;
1207
1208 /* Fourth byte */
1209 fhi |= *p << 8;
1210 p += incr;
1211
1212 /* Fifth byte */
1213 fhi |= *p;
1214 p += incr;
1215
1216 /* Sixth byte */
1217 flo = *p << 16;
1218 p += incr;
1219
1220 /* Seventh byte */
1221 flo |= *p << 8;
1222 p += incr;
1223
1224 /* Eighth byte */
1225 flo |= *p;
1226
1227 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
1228 x /= 268435456.0; /* 2**28 */
1229
1230 /* XXX This sadly ignores Inf/NaN */
1231 if (e == 0)
1232 e = -1022;
1233 else {
1234 x += 1.0;
1235 e -= 1023;
1236 }
1237 x = ldexp(x, e);
1238
1239 if (sign)
1240 x = -x;
1241
1242 return x;
1243}