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 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
98 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
99 #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
101 #define PyMem_MALLOC malloc
102 #define PyMem_FREE free
105 struct _floatblock
*next
;
106 PyFloatObject objects
[N_FLOATOBJECTS
];
109 typedef struct _floatblock PyFloatBlock
;
111 static PyFloatBlock
*block_list
= NULL
;
112 static PyFloatObject
*free_list
= NULL
;
114 static PyFloatObject
*
117 PyFloatObject
*p
, *q
;
118 p
= (PyFloatObject
*)PyMem_MALLOC(sizeof(PyFloatBlock
));
120 return (PyFloatObject
*)PyErr_NoMemory();
121 ((PyFloatBlock
*)p
)->next
= block_list
;
122 block_list
= (PyFloatBlock
*)p
;
123 p
= &((PyFloatBlock
*)p
)->objects
[0];
124 q
= p
+ N_FLOATOBJECTS
;
126 q
->ob_type
= (struct _typeobject
*)(q
-1);
128 return p
+ N_FLOATOBJECTS
- 1;
133 PyFloat_FromDouble(double fval
)
135 PyFloat_FromDouble(fval
)
139 register PyFloatObject
*op
;
140 if (free_list
== NULL
) {
141 if ((free_list
= fill_free_list()) == NULL
)
145 free_list
= (PyFloatObject
*)op
->ob_type
;
146 op
->ob_type
= &PyFloat_Type
;
148 _Py_NewReference(op
);
149 return (PyObject
*) op
;
156 op
->ob_type
= (struct _typeobject
*)free_list
;
168 if (op
&& PyFloat_Check(op
))
169 return PyFloat_AS_DOUBLE((PyFloatObject
*) op
);
171 if (op
== NULL
|| (nb
= op
->ob_type
->tp_as_number
) == NULL
||
172 nb
->nb_float
== NULL
) {
177 fo
= (PyFloatObject
*) (*nb
->nb_float
) (op
);
180 if (!PyFloat_Check(fo
)) {
181 PyErr_SetString(PyExc_TypeError
,
182 "nb_float should return float object");
186 val
= PyFloat_AS_DOUBLE(fo
);
195 PyFloat_AsString(buf
, v
)
200 /* Subroutine for float_repr and float_print.
201 We want float numbers to be recognizable as such,
202 i.e., they should contain a decimal point or an exponent.
203 However, %g may print the number as an integer;
204 in such cases, we append ".0" to the string. */
205 sprintf(buf
, "%.12g", v
->ob_fval
);
209 for (; *cp
!= '\0'; cp
++) {
210 /* Any non-digit means it's not an integer;
211 this takes care of NAN and INF as well. */
212 if (!isdigit(Py_CHARMASK(*cp
)))
224 float_print(v
, fp
, flags
)
227 int flags
; /* Not used but required by interface */
230 PyFloat_AsString(buf
, v
);
240 PyFloat_AsString(buf
, v
);
241 return PyString_FromString(buf
);
246 PyFloatObject
*v
, *w
;
248 double i
= v
->ob_fval
;
249 double j
= w
->ob_fval
;
250 return (i
< j
) ? -1 : (i
> j
) ? 1 : 0;
257 double intpart
, fractpart
;
260 /* This is designed so that Python numbers with the same
261 value hash to the same value, otherwise comparisons
262 of mapping keys will turn out weird */
264 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
267 fractpart
= modf(v
->ob_fval
, &e
);
271 fractpart
= modf(v
->ob_fval
, &intpart
);
274 if (fractpart
== 0.0) {
275 if (intpart
> 0x7fffffffL
|| -intpart
> 0x7fffffffL
) {
276 /* Convert to long int and use its hash... */
277 PyObject
*w
= PyLong_FromDouble(v
->ob_fval
);
280 x
= PyObject_Hash(w
);
287 /* Note -- if you change this code, also change the copy
288 in complexobject.c */
290 fractpart
= frexp(fractpart
, &expo
);
291 fractpart
= fractpart
* 2147483648.0; /* 2**31 */
292 hipart
= (long)fractpart
; /* Take the top 32 bits */
293 fractpart
= (fractpart
- (double)hipart
) * 2147483648.0;
294 /* Get the next 32 bits */
295 x
= hipart
+ (long)fractpart
+ (long)intpart
+ (expo
<< 15);
296 /* Combine everything */
309 PyFPE_START_PROTECT("add", return 0)
310 result
= v
->ob_fval
+ w
->ob_fval
;
311 PyFPE_END_PROTECT(result
)
312 return PyFloat_FromDouble(result
);
321 PyFPE_START_PROTECT("subtract", return 0)
322 result
= v
->ob_fval
- w
->ob_fval
;
323 PyFPE_END_PROTECT(result
)
324 return PyFloat_FromDouble(result
);
334 PyFPE_START_PROTECT("multiply", return 0)
335 result
= v
->ob_fval
* w
->ob_fval
;
336 PyFPE_END_PROTECT(result
)
337 return PyFloat_FromDouble(result
);
346 if (w
->ob_fval
== 0) {
347 PyErr_SetString(PyExc_ZeroDivisionError
, "float division");
350 PyFPE_START_PROTECT("divide", return 0)
351 result
= v
->ob_fval
/ w
->ob_fval
;
352 PyFPE_END_PROTECT(result
)
353 return PyFloat_FromDouble(result
);
362 double /* div, */ mod
;
365 PyErr_SetString(PyExc_ZeroDivisionError
, "float modulo");
368 PyFPE_START_PROTECT("modulo", return 0)
371 /* div = (vx - mod) / wx; */
376 PyFPE_END_PROTECT(mod
)
377 return PyFloat_FromDouble(mod
);
389 PyErr_SetString(PyExc_ZeroDivisionError
, "float divmod()");
392 PyFPE_START_PROTECT("divmod", return 0)
395 div
= (vx
- mod
) / wx
;
400 PyFPE_END_PROTECT(div
)
401 return Py_BuildValue("(dd)", div
, mod
);
404 static double powu(x
, n
)
411 while (mask
> 0 && n
>= mask
) {
428 /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
429 * The z parameter is really only going to be useful for integers and
430 * long integers. Maybe something clever with logarithms could be done.
434 iw
= ((PyFloatObject
*)w
)->ob_fval
;
436 if (iw
== intw
&& -10000 < intw
&& intw
< 10000) {
437 /* Sort out special cases here instead of relying on pow() */
438 if (intw
== 0) { /* x**0 is 1, even 0**0 */
439 PyFPE_START_PROTECT("pow", return 0)
440 if ((PyObject
*)z
!=Py_None
) {
441 ix
=fmod(1.0, z
->ob_fval
);
442 if (ix
!=0 && z
->ob_fval
<0) ix
+=z
->ob_fval
;
445 PyFPE_END_PROTECT(ix
)
446 return PyFloat_FromDouble(ix
);
449 PyFPE_START_PROTECT("pow", return 0)
453 ix
= 1./powu(iv
, -intw
);
454 PyFPE_END_PROTECT(ix
)
457 /* Sort out special cases here instead of relying on pow() */
460 PyErr_SetString(PyExc_ValueError
,
461 "0.0 to a negative power");
464 return PyFloat_FromDouble(0.0);
467 PyErr_SetString(PyExc_ValueError
,
468 "negative number to a float power");
472 PyFPE_START_PROTECT("pow", return 0)
474 PyFPE_END_PROTECT(ix
)
478 /* XXX could it be another type of error? */
479 PyErr_SetFromErrno(PyExc_OverflowError
);
482 if ((PyObject
*)z
!=Py_None
) {
483 PyFPE_START_PROTECT("pow", return 0)
484 ix
=fmod(ix
, z
->ob_fval
); /* XXX To Be Rewritten */
486 ((iv
<0 && z
->ob_fval
>0) || (iv
>0 && z
->ob_fval
<0) )) {
489 PyFPE_END_PROTECT(ix
)
491 return PyFloat_FromDouble(ix
);
498 return PyFloat_FromDouble(-v
->ob_fval
);
506 return (PyObject
*)v
;
523 return v
->ob_fval
!= 0.0;
531 if (PyInt_Check(*pw
)) {
532 long x
= PyInt_AsLong(*pw
);
533 *pw
= PyFloat_FromDouble((double)x
);
537 else if (PyLong_Check(*pw
)) {
538 *pw
= PyFloat_FromDouble(PyLong_AsDouble(*pw
));
542 return 1; /* Can't do it */
549 double x
= PyFloat_AsDouble(v
);
550 if (x
< 0 ? (x
= ceil(x
)) < (double)LONG_MIN
551 : (x
= floor(x
)) > (double)LONG_MAX
) {
552 PyErr_SetString(PyExc_OverflowError
,
553 "float too large to convert");
556 return PyInt_FromLong((long)x
);
563 double x
= PyFloat_AsDouble(v
);
564 return PyLong_FromDouble(x
);
576 static PyNumberMethods float_as_number
= {
577 (binaryfunc
)float_add
, /*nb_add*/
578 (binaryfunc
)float_sub
, /*nb_subtract*/
579 (binaryfunc
)float_mul
, /*nb_multiply*/
580 (binaryfunc
)float_div
, /*nb_divide*/
581 (binaryfunc
)float_rem
, /*nb_remainder*/
582 (binaryfunc
)float_divmod
, /*nb_divmod*/
583 (ternaryfunc
)float_pow
, /*nb_power*/
584 (unaryfunc
)float_neg
, /*nb_negative*/
585 (unaryfunc
)float_pos
, /*nb_positive*/
586 (unaryfunc
)float_abs
, /*nb_absolute*/
587 (inquiry
)float_nonzero
, /*nb_nonzero*/
594 (coercion
)float_coerce
, /*nb_coerce*/
595 (unaryfunc
)float_int
, /*nb_int*/
596 (unaryfunc
)float_long
, /*nb_long*/
597 (unaryfunc
)float_float
, /*nb_float*/
602 PyTypeObject PyFloat_Type
= {
603 PyObject_HEAD_INIT(&PyType_Type
)
606 sizeof(PyFloatObject
),
608 (destructor
)float_dealloc
, /*tp_dealloc*/
609 (printfunc
)float_print
, /*tp_print*/
612 (cmpfunc
)float_compare
, /*tp_compare*/
613 (reprfunc
)float_repr
, /*tp_repr*/
614 &float_as_number
, /*tp_as_number*/
615 0, /*tp_as_sequence*/
617 (hashfunc
)float_hash
, /*tp_hash*/
624 PyFloatBlock
*list
, *next
;
626 int bc
, bf
; /* block count, number of freed blocks */
627 int frem
, fsum
; /* remaining unfreed floats per block, total */
635 while (list
!= NULL
) {
638 for (i
= 0, p
= &list
->objects
[0];
641 if (PyFloat_Check(p
) && p
->ob_refcnt
!= 0)
646 list
->next
= block_list
;
648 for (i
= 0, p
= &list
->objects
[0];
651 if (!PyFloat_Check(p
) || p
->ob_refcnt
== 0) {
652 p
->ob_type
= (struct _typeobject
*)
667 fprintf(stderr
, "# cleanup floats");
669 fprintf(stderr
, "\n");
673 ": %d unfreed float%s in %d out of %d block%s\n",
674 fsum
, fsum
== 1 ? "" : "s",
675 bc
- bf
, bc
, bc
== 1 ? "" : "s");
677 if (Py_VerboseFlag
> 1) {
679 while (list
!= NULL
) {
680 for (i
= 0, p
= &list
->objects
[0];
683 if (PyFloat_Check(p
) && p
->ob_refcnt
!= 0) {
685 PyFloat_AsString(buf
, p
);
687 "# <float at %lx, refcnt=%d, val=%s>\n",
688 p
, p
->ob_refcnt
, buf
);