1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Float object implementation */
34 /* XXX There should be overflow checks here, but it's hard to check
35 for any kind of float exception without losing portability. */
43 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
47 #if defined(HUGE_VAL) && !defined(CHECK)
48 #define CHECK(x) if (errno != 0) ; \
49 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
54 #define CHECK(x) /* Don't know how to check */
62 #define LONG_MAX 0X7FFFFFFFL
66 #define LONG_MIN (-LONG_MAX-1)
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
77 #define LONG_MIN (-LONG_MAX-1)
81 #if !defined(__STDC__) && !defined(macintosh)
82 extern double fmod
Py_PROTO((double, double));
83 extern double pow
Py_PROTO((double, double));
87 /* On SunOS4.1 only libm.a exists. Make sure that references to all
88 needed math functions exist in the executable, so that dynamic
89 loading of mathmodule does not fail. */
90 double (*_Py_math_funcs_hack
[])() = {
91 acos
, asin
, atan
, atan2
, ceil
, cos
, cosh
, exp
, fabs
, floor
,
92 fmod
, log
, log10
, pow
, sin
, sinh
, sqrt
, tan
, tanh
96 /* Special free list -- see comments for same code in intobject.c. */
97 static PyFloatObject
*free_list
= NULL
;
98 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
99 #define N_FLOATOBJECTS (BLOCK_SIZE / sizeof(PyFloatObject))
101 static PyFloatObject
*
104 PyFloatObject
*p
, *q
;
105 p
= PyMem_NEW(PyFloatObject
, N_FLOATOBJECTS
);
107 return (PyFloatObject
*)PyErr_NoMemory();
108 q
= p
+ N_FLOATOBJECTS
;
110 *(PyFloatObject
**)q
= q
-1;
111 *(PyFloatObject
**)q
= NULL
;
112 return p
+ N_FLOATOBJECTS
- 1;
117 PyFloat_FromDouble(double fval
)
119 PyFloat_FromDouble(fval
)
123 register PyFloatObject
*op
;
124 if (free_list
== NULL
) {
125 if ((free_list
= fill_free_list()) == NULL
)
129 free_list
= *(PyFloatObject
**)free_list
;
130 op
->ob_type
= &PyFloat_Type
;
132 _Py_NewReference(op
);
133 return (PyObject
*) op
;
140 *(PyFloatObject
**)op
= free_list
;
152 if (op
&& PyFloat_Check(op
))
153 return PyFloat_AS_DOUBLE((PyFloatObject
*) op
);
155 if (op
== NULL
|| (nb
= op
->ob_type
->tp_as_number
) == NULL
||
156 nb
->nb_float
== NULL
) {
161 fo
= (PyFloatObject
*) (*nb
->nb_float
) (op
);
164 if (!PyFloat_Check(fo
)) {
165 PyErr_SetString(PyExc_TypeError
,
166 "nb_float should return float object");
170 val
= PyFloat_AS_DOUBLE(fo
);
179 PyFloat_AsString(buf
, v
)
184 /* Subroutine for float_repr and float_print.
185 We want float numbers to be recognizable as such,
186 i.e., they should contain a decimal point or an exponent.
187 However, %g may print the number as an integer;
188 in such cases, we append ".0" to the string. */
189 sprintf(buf
, "%.12g", v
->ob_fval
);
193 for (; *cp
!= '\0'; cp
++) {
194 /* Any non-digit means it's not an integer;
195 this takes care of NAN and INF as well. */
196 if (!isdigit(Py_CHARMASK(*cp
)))
208 float_print(v
, fp
, flags
)
211 int flags
; /* Not used but required by interface */
214 PyFloat_AsString(buf
, v
);
224 PyFloat_AsString(buf
, v
);
225 return PyString_FromString(buf
);
230 PyFloatObject
*v
, *w
;
232 double i
= v
->ob_fval
;
233 double j
= w
->ob_fval
;
234 return (i
< j
) ? -1 : (i
> j
) ? 1 : 0;
241 double intpart
, fractpart
;
244 /* This is designed so that Python numbers with the same
245 value hash to the same value, otherwise comparisons
246 of mapping keys will turn out weird */
248 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
251 fractpart
= modf(v
->ob_fval
, &e
);
255 fractpart
= modf(v
->ob_fval
, &intpart
);
258 if (fractpart
== 0.0) {
259 if (intpart
> 0x7fffffffL
|| -intpart
> 0x7fffffffL
) {
260 /* Convert to long int and use its hash... */
261 PyObject
*w
= PyLong_FromDouble(v
->ob_fval
);
264 x
= PyObject_Hash(w
);
271 /* Note -- if you change this code, also change the copy
272 in complexobject.c */
274 fractpart
= frexp(fractpart
, &expo
);
275 fractpart
= fractpart
* 2147483648.0; /* 2**31 */
276 hipart
= (long)fractpart
; /* Take the top 32 bits */
277 fractpart
= (fractpart
- (double)hipart
) * 2147483648.0;
278 /* Get the next 32 bits */
279 x
= hipart
+ (long)fractpart
+ (long)intpart
+ (expo
<< 15);
280 /* Combine everything */
293 PyFPE_START_PROTECT("add", return 0)
294 result
= v
->ob_fval
+ w
->ob_fval
;
295 PyFPE_END_PROTECT(result
)
296 return PyFloat_FromDouble(result
);
305 PyFPE_START_PROTECT("subtract", return 0)
306 result
= v
->ob_fval
- w
->ob_fval
;
307 PyFPE_END_PROTECT(result
)
308 return PyFloat_FromDouble(result
);
318 PyFPE_START_PROTECT("multiply", return 0)
319 result
= v
->ob_fval
* w
->ob_fval
;
320 PyFPE_END_PROTECT(result
)
321 return PyFloat_FromDouble(result
);
330 if (w
->ob_fval
== 0) {
331 PyErr_SetString(PyExc_ZeroDivisionError
, "float division");
334 PyFPE_START_PROTECT("divide", return 0)
335 result
= v
->ob_fval
/ w
->ob_fval
;
336 PyFPE_END_PROTECT(result
)
337 return PyFloat_FromDouble(result
);
346 double /* div, */ mod
;
349 PyErr_SetString(PyExc_ZeroDivisionError
, "float modulo");
352 PyFPE_START_PROTECT("modulo", return 0)
355 /* div = (vx - mod) / wx; */
360 PyFPE_END_PROTECT(mod
)
361 return PyFloat_FromDouble(mod
);
373 PyErr_SetString(PyExc_ZeroDivisionError
, "float divmod()");
376 PyFPE_START_PROTECT("divmod", return 0)
379 div
= (vx
- mod
) / wx
;
384 PyFPE_END_PROTECT(div
)
385 return Py_BuildValue("(dd)", div
, mod
);
388 static double powu(x
, n
)
395 while (mask
> 0 && n
>= mask
) {
412 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
413 * The z parameter is really only going to be useful for integers and
414 * long integers. Maybe something clever with logarithms could be done.
418 iw
= ((PyFloatObject
*)w
)->ob_fval
;
420 if (iw
== intw
&& -10000 < intw
&& intw
< 10000) {
421 /* Sort out special cases here instead of relying on pow() */
422 if (intw
== 0) { /* x**0 is 1, even 0**0 */
423 PyFPE_START_PROTECT("pow", return 0)
424 if ((PyObject
*)z
!=Py_None
) {
425 ix
=fmod(1.0, z
->ob_fval
);
426 if (ix
!=0 && z
->ob_fval
<0) ix
+=z
->ob_fval
;
429 PyFPE_END_PROTECT(ix
)
430 return PyFloat_FromDouble(ix
);
433 PyFPE_START_PROTECT("pow", return 0)
437 ix
= 1./powu(iv
, -intw
);
438 PyFPE_END_PROTECT(ix
)
441 /* Sort out special cases here instead of relying on pow() */
444 PyErr_SetString(PyExc_ValueError
,
445 "0.0 to a negative power");
448 return PyFloat_FromDouble(0.0);
451 PyErr_SetString(PyExc_ValueError
,
452 "negative number to a float power");
456 PyFPE_START_PROTECT("pow", return 0)
458 PyFPE_END_PROTECT(ix
)
462 /* XXX could it be another type of error? */
463 PyErr_SetFromErrno(PyExc_OverflowError
);
466 if ((PyObject
*)z
!=Py_None
) {
467 PyFPE_START_PROTECT("pow", return 0)
468 ix
=fmod(ix
, z
->ob_fval
); /* XXX To Be Rewritten */
470 ((iv
<0 && z
->ob_fval
>0) || (iv
>0 && z
->ob_fval
<0) )) {
473 PyFPE_END_PROTECT(ix
)
475 return PyFloat_FromDouble(ix
);
482 return PyFloat_FromDouble(-v
->ob_fval
);
490 return (PyObject
*)v
;
507 return v
->ob_fval
!= 0.0;
515 if (PyInt_Check(*pw
)) {
516 long x
= PyInt_AsLong(*pw
);
517 *pw
= PyFloat_FromDouble((double)x
);
521 else if (PyLong_Check(*pw
)) {
522 *pw
= PyFloat_FromDouble(PyLong_AsDouble(*pw
));
526 return 1; /* Can't do it */
533 double x
= PyFloat_AsDouble(v
);
534 if (x
< 0 ? (x
= ceil(x
)) < (double)LONG_MIN
535 : (x
= floor(x
)) > (double)LONG_MAX
) {
536 PyErr_SetString(PyExc_OverflowError
,
537 "float too large to convert");
540 return PyInt_FromLong((long)x
);
547 double x
= PyFloat_AsDouble(v
);
548 return PyLong_FromDouble(x
);
560 static PyNumberMethods float_as_number
= {
561 (binaryfunc
)float_add
, /*nb_add*/
562 (binaryfunc
)float_sub
, /*nb_subtract*/
563 (binaryfunc
)float_mul
, /*nb_multiply*/
564 (binaryfunc
)float_div
, /*nb_divide*/
565 (binaryfunc
)float_rem
, /*nb_remainder*/
566 (binaryfunc
)float_divmod
, /*nb_divmod*/
567 (ternaryfunc
)float_pow
, /*nb_power*/
568 (unaryfunc
)float_neg
, /*nb_negative*/
569 (unaryfunc
)float_pos
, /*nb_positive*/
570 (unaryfunc
)float_abs
, /*nb_absolute*/
571 (inquiry
)float_nonzero
, /*nb_nonzero*/
578 (coercion
)float_coerce
, /*nb_coerce*/
579 (unaryfunc
)float_int
, /*nb_int*/
580 (unaryfunc
)float_long
, /*nb_long*/
581 (unaryfunc
)float_float
, /*nb_float*/
586 PyTypeObject PyFloat_Type
= {
587 PyObject_HEAD_INIT(&PyType_Type
)
590 sizeof(PyFloatObject
),
592 (destructor
)float_dealloc
, /*tp_dealloc*/
593 (printfunc
)float_print
, /*tp_print*/
596 (cmpfunc
)float_compare
, /*tp_compare*/
597 (reprfunc
)float_repr
, /*tp_repr*/
598 &float_as_number
, /*tp_as_number*/
599 0, /*tp_as_sequence*/
601 (hashfunc
)float_hash
, /*tp_hash*/
607 /* XXX Alas, the free list is not easily and safely freeable */