blob: 7f7f50786bc47766a1abebf5ab8bdaea6deee712 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Float object implementation */
33
Guido van Rossum2a9096b1990-10-21 22:15:08 +000034/* XXX There should be overflow checks here, but it's hard to check
35 for any kind of float exception without losing portability. */
36
Guido van Rossumc0b618a1997-05-02 03:12:38 +000037#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038
Guido van Rossum3f5da241990-12-20 15:06:42 +000039#include <ctype.h>
Guido van Rossum07e3a7e1995-02-27 10:13:37 +000040#include "mymath.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossum9575a441993-04-07 14:06:14 +000042#ifdef i860
43/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
44#undef HUGE_VAL
45#endif
46
Guido van Rossum9d81b551996-06-26 18:27:19 +000047#if defined(HUGE_VAL) && !defined(CHECK)
Guido van Rossum7fa52f81991-12-16 15:43:14 +000048#define CHECK(x) if (errno != 0) ; \
49 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
50 else errno = ERANGE
Guido van Rossum9d81b551996-06-26 18:27:19 +000051#endif
52
53#ifndef CHECK
Guido van Rossum7fa52f81991-12-16 15:43:14 +000054#define CHECK(x) /* Don't know how to check */
55#endif
56
Guido van Rossum03093a21994-09-28 15:51:32 +000057#ifdef HAVE_LIMITS_H
58#include <limits.h>
59#endif
60
61#ifndef LONG_MAX
62#define LONG_MAX 0X7FFFFFFFL
63#endif
64
65#ifndef LONG_MIN
66#define LONG_MIN (-LONG_MAX-1)
67#endif
68
Guido van Rossum67ca7011995-02-13 16:38:41 +000069#ifdef __NeXT__
70#ifdef __sparc__
71/*
72 * This works around a bug in the NS/Sparc 3.3 pre-release
73 * limits.h header file.
74 * 10-Feb-1995 bwarsaw@cnri.reston.va.us
75 */
76#undef LONG_MIN
77#define LONG_MIN (-LONG_MAX-1)
78#endif
79#endif
80
Guido van Rossum07e3a7e1995-02-27 10:13:37 +000081#if !defined(__STDC__) && !defined(macintosh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082extern double fmod Py_PROTO((double, double));
83extern double pow Py_PROTO((double, double));
Guido van Rossum6923e131990-11-02 17:50:43 +000084#endif
85
Guido van Rossum93ad0df1997-05-13 21:00:42 +000086/* Special free list -- see comments for same code in intobject.c. */
87static PyFloatObject *free_list = NULL;
88#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
89#define N_FLOATOBJECTS (BLOCK_SIZE / sizeof(PyFloatObject))
90
91static PyFloatObject *
92fill_free_list()
93{
94 PyFloatObject *p, *q;
95 p = PyMem_NEW(PyFloatObject, N_FLOATOBJECTS);
96 if (p == NULL)
97 return (PyFloatObject *)PyErr_NoMemory();
98 q = p + N_FLOATOBJECTS;
99 while (--q > p)
100 *(PyFloatObject **)q = q-1;
101 *(PyFloatObject **)q = NULL;
102 return p + N_FLOATOBJECTS - 1;
103}
104
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105PyObject *
Guido van Rossum07e3a7e1995-02-27 10:13:37 +0000106#ifdef __SC__
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107PyFloat_FromDouble(double fval)
Guido van Rossum07e3a7e1995-02-27 10:13:37 +0000108#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109PyFloat_FromDouble(fval)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 double fval;
Guido van Rossum07e3a7e1995-02-27 10:13:37 +0000111#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000113 register PyFloatObject *op;
114 if (free_list == NULL) {
115 if ((free_list = fill_free_list()) == NULL)
116 return NULL;
117 }
118 op = free_list;
119 free_list = *(PyFloatObject **)free_list;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 op->ob_type = &PyFloat_Type;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000121 op->ob_fval = fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 _Py_NewReference(op);
123 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124}
125
Guido van Rossum234f9421993-06-17 12:35:49 +0000126static void
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000127float_dealloc(op)
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000128 PyFloatObject *op;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000129{
Guido van Rossum93ad0df1997-05-13 21:00:42 +0000130 *(PyFloatObject **)op = free_list;
131 free_list = op;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000132}
133
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134double
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135PyFloat_AsDouble(op)
136 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138 PyNumberMethods *nb;
139 PyFloatObject *fo;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140 double val;
141
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 if (op && PyFloat_Check(op))
143 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000144
145 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
146 nb->nb_float == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 return -1;
149 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000151 fo = (PyFloatObject*) (*nb->nb_float) (op);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152 if (fo == NULL)
153 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 if (!PyFloat_Check(fo)) {
155 PyErr_SetString(PyExc_TypeError,
156 "nb_float should return float object");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157 return -1;
158 }
159
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 val = PyFloat_AS_DOUBLE(fo);
161 Py_DECREF(fo);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000162
163 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164}
165
166/* Methods */
167
Guido van Rossum27dec7e1991-06-04 19:42:53 +0000168void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169PyFloat_AsString(buf, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170 char *buf;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 PyFloatObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172{
173 register char *cp;
174 /* Subroutine for float_repr and float_print.
175 We want float numbers to be recognizable as such,
176 i.e., they should contain a decimal point or an exponent.
177 However, %g may print the number as an integer;
178 in such cases, we append ".0" to the string. */
179 sprintf(buf, "%.12g", v->ob_fval);
180 cp = buf;
181 if (*cp == '-')
182 cp++;
183 for (; *cp != '\0'; cp++) {
184 /* Any non-digit means it's not an integer;
185 this takes care of NAN and INF as well. */
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000186 if (!isdigit(Py_CHARMASK(*cp)))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187 break;
188 }
189 if (*cp == '\0') {
190 *cp++ = '.';
191 *cp++ = '0';
192 *cp++ = '\0';
193 }
194}
195
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000196/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000197static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198float_print(v, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199 PyFloatObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200 FILE *fp;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000201 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
203 char buf[100];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 PyFloat_AsString(buf, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205 fputs(buf, fp);
Guido van Rossum90933611991-06-07 16:10:43 +0000206 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207}
208
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210float_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 PyFloatObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212{
213 char buf[100];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 PyFloat_AsString(buf, v);
215 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216}
217
218static int
219float_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220 PyFloatObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221{
222 double i = v->ob_fval;
223 double j = w->ob_fval;
224 return (i < j) ? -1 : (i > j) ? 1 : 0;
225}
226
Guido van Rossum9bfef441993-03-29 10:43:31 +0000227static long
228float_hash(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 PyFloatObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000230{
231 double intpart, fractpart;
232 int expo;
233 long x;
234 /* This is designed so that Python numbers with the same
235 value hash to the same value, otherwise comparisons
236 of mapping keys will turn out weird */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
238#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
239{
240 extended e;
241 fractpart = modf(v->ob_fval, &e);
242 intpart = e;
243}
244#else
Guido van Rossum9bfef441993-03-29 10:43:31 +0000245 fractpart = modf(v->ob_fval, &intpart);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#endif
247
Guido van Rossum9bfef441993-03-29 10:43:31 +0000248 if (fractpart == 0.0) {
249 if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
250 /* Convert to long int and use its hash... */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251 PyObject *w = PyLong_FromDouble(v->ob_fval);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000252 if (w == NULL)
253 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254 x = PyObject_Hash(w);
255 Py_DECREF(w);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000256 return x;
257 }
258 x = (long)intpart;
259 }
260 else {
Guido van Rossum919cf1a1997-01-11 19:26:21 +0000261 /* Note -- if you change this code, also change the copy
262 in complexobject.c */
263 long hipart;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000264 fractpart = frexp(fractpart, &expo);
Guido van Rossum919cf1a1997-01-11 19:26:21 +0000265 fractpart = fractpart * 2147483648.0; /* 2**31 */
266 hipart = (long)fractpart; /* Take the top 32 bits */
267 fractpart = (fractpart - (double)hipart) * 2147483648.0;
268 /* Get the next 32 bits */
269 x = hipart + (long)fractpart + (long)intpart + (expo << 15);
270 /* Combine everything */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000271 }
272 if (x == -1)
273 x = -2;
274 return x;
275}
276
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278float_add(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279 PyFloatObject *v;
280 PyFloatObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000282 double result;
283 PyFPE_START_PROTECT("add", return 0)
284 result = v->ob_fval + w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000285 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000286 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287}
288
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290float_sub(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000291 PyFloatObject *v;
292 PyFloatObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000294 double result;
295 PyFPE_START_PROTECT("subtract", return 0)
296 result = v->ob_fval - w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000297 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000298 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299}
300
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000301static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302float_mul(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303 PyFloatObject *v;
304 PyFloatObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000306 double result;
307
308 PyFPE_START_PROTECT("multiply", return 0)
309 result = v->ob_fval * w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000310 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312}
313
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315float_div(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000316 PyFloatObject *v;
317 PyFloatObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000319 double result;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000320 if (w->ob_fval == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000321 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 return NULL;
323 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000324 PyFPE_START_PROTECT("divide", return 0)
325 result = v->ob_fval / w->ob_fval;
Guido van Rossum45b83911997-03-14 04:32:50 +0000326 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327 return PyFloat_FromDouble(result);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328}
329
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331float_rem(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332 PyFloatObject *v;
333 PyFloatObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334{
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000335 double vx, wx;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000336 double /* div, */ mod;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000337 wx = w->ob_fval;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340 return NULL;
341 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000342 PyFPE_START_PROTECT("modulo", return 0)
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000343 vx = v->ob_fval;
344 mod = fmod(vx, wx);
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000345 /* div = (vx - mod) / wx; */
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000346 if (wx*mod < 0) {
347 mod += wx;
Guido van Rossum3132a5a1992-03-27 17:28:44 +0000348 /* div -= 1.0; */
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000349 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000350 PyFPE_END_PROTECT(mod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351 return PyFloat_FromDouble(mod);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352}
353
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354static PyObject *
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000355float_divmod(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356 PyFloatObject *v;
357 PyFloatObject *w;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000358{
Guido van Rossum15ecff41991-10-20 20:16:45 +0000359 double vx, wx;
360 double div, mod;
Guido van Rossum56cd67a1992-01-26 18:16:35 +0000361 wx = w->ob_fval;
Guido van Rossum15ecff41991-10-20 20:16:45 +0000362 if (wx == 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
Guido van Rossum15ecff41991-10-20 20:16:45 +0000364 return NULL;
365 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000366 PyFPE_START_PROTECT("divmod", return 0)
Guido van Rossum15ecff41991-10-20 20:16:45 +0000367 vx = v->ob_fval;
368 mod = fmod(vx, wx);
369 div = (vx - mod) / wx;
370 if (wx*mod < 0) {
371 mod += wx;
372 div -= 1.0;
373 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000374 PyFPE_END_PROTECT(div)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 return Py_BuildValue("(dd)", div, mod);
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000376}
377
Guido van Rossum363078a1996-05-24 20:45:01 +0000378static double powu(x, n)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000379 double x;
380 long n;
381{
382 double r = 1.;
383 double p = x;
384 long mask = 1;
385 while (mask > 0 && n >= mask) {
386 if (n & mask)
387 r *= p;
388 mask <<= 1;
389 p *= p;
390 }
391 return r;
392}
393
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394static PyObject *
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000395float_pow(v, w, z)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 PyFloatObject *v;
397 PyObject *w;
398 PyFloatObject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399{
400 double iv, iw, ix;
Guido van Rossum39739ea1996-01-12 01:22:56 +0000401 long intw;
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000402 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
403 * The z parameter is really only going to be useful for integers and
404 * long integers. Maybe something clever with logarithms could be done.
405 * [AMK]
406 */
Guido van Rossum39739ea1996-01-12 01:22:56 +0000407 iv = v->ob_fval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408 iw = ((PyFloatObject *)w)->ob_fval;
Guido van Rossum39739ea1996-01-12 01:22:56 +0000409 intw = (long)iw;
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000410 if (iw == intw && -10000 < intw && intw < 10000) {
Guido van Rossum39739ea1996-01-12 01:22:56 +0000411 /* Sort out special cases here instead of relying on pow() */
Guido van Rossum86c04c21996-08-09 20:50:14 +0000412 if (intw == 0) { /* x**0 is 1, even 0**0 */
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000413 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 if ((PyObject *)z!=Py_None) {
Guido van Rossum39739ea1996-01-12 01:22:56 +0000415 ix=fmod(1.0, z->ob_fval);
416 if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
417 }
418 else ix=1.0;
Guido van Rossum45b83911997-03-14 04:32:50 +0000419 PyFPE_END_PROTECT(ix)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420 return PyFloat_FromDouble(ix);
Guido van Rossum70d93461991-05-28 21:57:39 +0000421 }
Guido van Rossum86c04c21996-08-09 20:50:14 +0000422 errno = 0;
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000423 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000424 if (intw > 0)
425 ix = powu(iv, intw);
426 else
427 ix = 1./powu(iv, -intw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000428 PyFPE_END_PROTECT(ix)
Guido van Rossum86c04c21996-08-09 20:50:14 +0000429 }
430 else {
431 /* Sort out special cases here instead of relying on pow() */
Guido van Rossum39739ea1996-01-12 01:22:56 +0000432 if (iv == 0.0) {
433 if (iw < 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434 PyErr_SetString(PyExc_ValueError,
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000435 "0.0 to a negative power");
Guido van Rossum39739ea1996-01-12 01:22:56 +0000436 return NULL;
437 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438 return PyFloat_FromDouble(0.0);
Guido van Rossum39739ea1996-01-12 01:22:56 +0000439 }
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000440 if (iv < 0.0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441 PyErr_SetString(PyExc_ValueError,
Guido van Rossumc13bcca1996-08-16 20:42:57 +0000442 "negative number to a float power");
443 return NULL;
444 }
Guido van Rossum39739ea1996-01-12 01:22:56 +0000445 errno = 0;
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000446 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossum39739ea1996-01-12 01:22:56 +0000447 ix = pow(iv, iw);
Guido van Rossum45b83911997-03-14 04:32:50 +0000448 PyFPE_END_PROTECT(ix)
Guido van Rossum70d93461991-05-28 21:57:39 +0000449 }
Guido van Rossum7fa52f81991-12-16 15:43:14 +0000450 CHECK(ix);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000451 if (errno != 0) {
452 /* XXX could it be another type of error? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453 PyErr_SetFromErrno(PyExc_OverflowError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454 return NULL;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000455 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456 if ((PyObject *)z!=Py_None) {
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000457 PyFPE_START_PROTECT("pow", return 0)
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000458 ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */
459 if ( ix!=0 &&
460 ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
461 ix+=z->ob_fval;
462 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000463 PyFPE_END_PROTECT(ix)
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000464 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 return PyFloat_FromDouble(ix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466}
467
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469float_neg(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 PyFloatObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472 return PyFloat_FromDouble(-v->ob_fval);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473}
474
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476float_pos(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 PyFloatObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000479 Py_INCREF(v);
480 return (PyObject *)v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000481}
482
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483static PyObject *
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000484float_abs(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485 PyFloatObject *v;
Guido van Rossumeba1b5e1991-05-05 20:07:00 +0000486{
487 if (v->ob_fval < 0)
488 return float_neg(v);
489 else
490 return float_pos(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491}
492
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000493static int
494float_nonzero(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495 PyFloatObject *v;
Guido van Rossum50b4ef61991-05-14 11:57:01 +0000496{
497 return v->ob_fval != 0.0;
498}
499
Guido van Rossum234f9421993-06-17 12:35:49 +0000500static int
Guido van Rossume6eefc21992-08-14 12:06:52 +0000501float_coerce(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502 PyObject **pv;
503 PyObject **pw;
Guido van Rossume6eefc21992-08-14 12:06:52 +0000504{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505 if (PyInt_Check(*pw)) {
506 long x = PyInt_AsLong(*pw);
507 *pw = PyFloat_FromDouble((double)x);
508 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000509 return 0;
510 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511 else if (PyLong_Check(*pw)) {
512 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
513 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000514 return 0;
515 }
516 return 1; /* Can't do it */
517}
518
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000520float_int(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000522{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523 double x = PyFloat_AsDouble(v);
Guido van Rossum03093a21994-09-28 15:51:32 +0000524 if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
525 : (x = floor(x)) > (double)LONG_MAX) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 PyErr_SetString(PyExc_OverflowError,
527 "float too large to convert");
Guido van Rossum03093a21994-09-28 15:51:32 +0000528 return NULL;
529 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 return PyInt_FromLong((long)x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000531}
532
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000534float_long(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000536{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 double x = PyFloat_AsDouble(v);
538 return PyLong_FromDouble(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000539}
540
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000542float_float(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000544{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000546 return v;
547}
548
549
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550static PyNumberMethods float_as_number = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000551 (binaryfunc)float_add, /*nb_add*/
552 (binaryfunc)float_sub, /*nb_subtract*/
553 (binaryfunc)float_mul, /*nb_multiply*/
554 (binaryfunc)float_div, /*nb_divide*/
555 (binaryfunc)float_rem, /*nb_remainder*/
556 (binaryfunc)float_divmod, /*nb_divmod*/
Guido van Rossum0b7d02a1994-08-12 12:52:35 +0000557 (ternaryfunc)float_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000558 (unaryfunc)float_neg, /*nb_negative*/
559 (unaryfunc)float_pos, /*nb_positive*/
560 (unaryfunc)float_abs, /*nb_absolute*/
561 (inquiry)float_nonzero, /*nb_nonzero*/
Guido van Rossum27acb331991-10-24 14:55:28 +0000562 0, /*nb_invert*/
563 0, /*nb_lshift*/
564 0, /*nb_rshift*/
565 0, /*nb_and*/
566 0, /*nb_xor*/
567 0, /*nb_or*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000568 (coercion)float_coerce, /*nb_coerce*/
569 (unaryfunc)float_int, /*nb_int*/
570 (unaryfunc)float_long, /*nb_long*/
571 (unaryfunc)float_float, /*nb_float*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000572 0, /*nb_oct*/
573 0, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574};
575
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576PyTypeObject PyFloat_Type = {
577 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578 0,
579 "float",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580 sizeof(PyFloatObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +0000582 (destructor)float_dealloc, /*tp_dealloc*/
583 (printfunc)float_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 0, /*tp_getattr*/
585 0, /*tp_setattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000586 (cmpfunc)float_compare, /*tp_compare*/
587 (reprfunc)float_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588 &float_as_number, /*tp_as_number*/
589 0, /*tp_as_sequence*/
590 0, /*tp_as_mapping*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000591 (hashfunc)float_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592};