2 /* Float object implementation */
4 /* XXX There should be overflow checks here, but it's hard to check
5 for any kind of float exception without losing portability. */
14 #if !defined(__STDC__)
15 extern double fmod(double, double);
16 extern double pow(double, double);
20 /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */
21 extern int finite(double);
24 /* Special free list -- see comments for same code in intobject.c. */
25 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
26 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
27 #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
30 struct _floatblock
*next
;
31 PyFloatObject objects
[N_FLOATOBJECTS
];
34 typedef struct _floatblock PyFloatBlock
;
36 static PyFloatBlock
*block_list
= NULL
;
37 static PyFloatObject
*free_list
= NULL
;
39 static PyFloatObject
*
43 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
44 p
= (PyFloatObject
*) PyMem_MALLOC(sizeof(PyFloatBlock
));
46 return (PyFloatObject
*) PyErr_NoMemory();
47 ((PyFloatBlock
*)p
)->next
= block_list
;
48 block_list
= (PyFloatBlock
*)p
;
49 p
= &((PyFloatBlock
*)p
)->objects
[0];
50 q
= p
+ N_FLOATOBJECTS
;
52 Py_TYPE(q
) = (struct _typeobject
*)(q
-1);
54 return p
+ N_FLOATOBJECTS
- 1;
69 static PyTypeObject FloatInfoType
= {0, 0, 0, 0, 0, 0};
71 PyDoc_STRVAR(floatinfo__doc__
,
74 A structseq holding information about the float type. It contains low level\n\
75 information about the precision and internal representation. Please study\n\
76 your system's :file:`float.h` for more information.");
78 static PyStructSequence_Field floatinfo_fields
[] = {
79 {"max", "DBL_MAX -- maximum representable finite float"},
80 {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
82 {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e "
84 {"min", "DBL_MIN -- Minimum positive normalizer float"},
85 {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
86 "is a normalized float"},
87 {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
89 {"dig", "DBL_DIG -- digits"},
90 {"mant_dig", "DBL_MANT_DIG -- mantissa digits"},
91 {"epsilon", "DBL_EPSILON -- Difference between 1 and the next "
92 "representable float"},
93 {"radix", "FLT_RADIX -- radix of exponent"},
94 {"rounds", "FLT_ROUNDS -- addition rounds"},
98 static PyStructSequence_Desc floatinfo_desc
= {
99 "sys.floatinfo", /* name */
100 floatinfo__doc__
, /* doc */
101 floatinfo_fields
, /* fields */
106 PyFloat_GetInfo(void)
111 floatinfo
= PyStructSequence_New(&FloatInfoType
);
112 if (floatinfo
== NULL
) {
116 #define SetIntFlag(flag) \
117 PyStructSequence_SET_ITEM(floatinfo, pos++, PyInt_FromLong(flag))
118 #define SetDblFlag(flag) \
119 PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
122 SetIntFlag(DBL_MAX_EXP
);
123 SetIntFlag(DBL_MAX_10_EXP
);
125 SetIntFlag(DBL_MIN_EXP
);
126 SetIntFlag(DBL_MIN_10_EXP
);
128 SetIntFlag(DBL_MANT_DIG
);
129 SetDblFlag(DBL_EPSILON
);
130 SetIntFlag(FLT_RADIX
);
131 SetIntFlag(FLT_ROUNDS
);
135 if (PyErr_Occurred()) {
143 PyFloat_FromDouble(double fval
)
145 register PyFloatObject
*op
;
146 if (free_list
== NULL
) {
147 if ((free_list
= fill_free_list()) == NULL
)
150 /* Inline PyObject_New */
152 free_list
= (PyFloatObject
*)Py_TYPE(op
);
153 PyObject_INIT(op
, &PyFloat_Type
);
155 return (PyObject
*) op
;
158 /**************************************************************************
159 RED_FLAG 22-Sep-2000 tim
160 PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
162 1. If v was a regular string, *pend was set to point to its terminating
163 null byte. That's useless (the caller can find that without any
164 help from this function!).
166 2. If v was a Unicode string, or an object convertible to a character
167 buffer, *pend was set to point into stack trash (the auto temp
168 vector holding the character buffer). That was downright dangerous.
170 Since we can't change the interface of a public API function, pend is
171 still supported but now *officially* useless: if pend is not NULL,
172 *pend is set to NULL.
173 **************************************************************************/
175 PyFloat_FromString(PyObject
*v
, char **pend
)
177 const char *s
, *last
, *end
, *sp
;
179 char buffer
[256]; /* for errors */
180 #ifdef Py_USING_UNICODE
181 char s_buffer
[256]; /* for objects convertible to a char buffer */
187 if (PyString_Check(v
)) {
188 s
= PyString_AS_STRING(v
);
189 len
= PyString_GET_SIZE(v
);
191 #ifdef Py_USING_UNICODE
192 else if (PyUnicode_Check(v
)) {
193 if (PyUnicode_GET_SIZE(v
) >= (Py_ssize_t
)sizeof(s_buffer
)) {
194 PyErr_SetString(PyExc_ValueError
,
195 "Unicode float() literal too long to convert");
198 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v
),
199 PyUnicode_GET_SIZE(v
),
207 else if (PyObject_AsCharBuffer(v
, &s
, &len
)) {
208 PyErr_SetString(PyExc_TypeError
,
209 "float() argument must be a string or a number");
214 while (*s
&& isspace(Py_CHARMASK(*s
)))
217 PyErr_SetString(PyExc_ValueError
, "empty string for float()");
221 /* We don't care about overflow or underflow. If the platform supports
222 * them, infinities and signed zeroes (on underflow) are fine.
223 * However, strtod can return 0 for denormalized numbers, where atof
224 * does not. So (alas!) we special-case a zero result. Note that
225 * whether strtod sets errno on underflow is not defined, so we can't
228 PyFPE_START_PROTECT("strtod", return NULL
)
229 x
= PyOS_ascii_strtod(s
, (char **)&end
);
232 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
233 byte at the end of the string, when the input is inf(inity). */
236 /* Check for inf and nan. This is done late because it rarely happens. */
248 if (PyOS_strnicmp(p
, "inf", 4) == 0) {
249 return PyFloat_FromDouble(sign
* Py_HUGE_VAL
);
252 if(PyOS_strnicmp(p
, "nan", 4) == 0) {
253 return PyFloat_FromDouble(Py_NAN
);
256 PyOS_snprintf(buffer
, sizeof(buffer
),
257 "invalid literal for float(): %.200s", s
);
258 PyErr_SetString(PyExc_ValueError
, buffer
);
261 /* Since end != s, the platform made *some* kind of sense out
262 of the input. Trust it. */
263 while (*end
&& isspace(Py_CHARMASK(*end
)))
266 PyOS_snprintf(buffer
, sizeof(buffer
),
267 "invalid literal for float(): %.200s", s
);
268 PyErr_SetString(PyExc_ValueError
, buffer
);
271 else if (end
!= last
) {
272 PyErr_SetString(PyExc_ValueError
,
273 "null byte in argument for float()");
277 /* See above -- may have been strtod being anal
279 PyFPE_START_PROTECT("atof", return NULL
)
280 x
= PyOS_ascii_atof(s
);
282 errno
= 0; /* whether atof ever set errno is undefined */
284 return PyFloat_FromDouble(x
);
288 float_dealloc(PyFloatObject
*op
)
290 if (PyFloat_CheckExact(op
)) {
291 Py_TYPE(op
) = (struct _typeobject
*)free_list
;
295 Py_TYPE(op
)->tp_free((PyObject
*)op
);
299 PyFloat_AsDouble(PyObject
*op
)
305 if (op
&& PyFloat_Check(op
))
306 return PyFloat_AS_DOUBLE((PyFloatObject
*) op
);
313 if ((nb
= Py_TYPE(op
)->tp_as_number
) == NULL
|| nb
->nb_float
== NULL
) {
314 PyErr_SetString(PyExc_TypeError
, "a float is required");
318 fo
= (PyFloatObject
*) (*nb
->nb_float
) (op
);
321 if (!PyFloat_Check(fo
)) {
322 PyErr_SetString(PyExc_TypeError
,
323 "nb_float should return float object");
327 val
= PyFloat_AS_DOUBLE(fo
);
336 format_float(char *buf
, size_t buflen
, PyFloatObject
*v
, int precision
)
342 /* Subroutine for float_repr and float_print.
343 We want float numbers to be recognizable as such,
344 i.e., they should contain a decimal point or an exponent.
345 However, %g may print the number as an integer;
346 in such cases, we append ".0" to the string. */
348 assert(PyFloat_Check(v
));
349 PyOS_snprintf(format
, 32, "%%.%ig", precision
);
350 PyOS_ascii_formatd(buf
, buflen
, format
, v
->ob_fval
);
354 for (; *cp
!= '\0'; cp
++) {
355 /* Any non-digit means it's not an integer;
356 this takes care of NAN and INF as well. */
357 if (!isdigit(Py_CHARMASK(*cp
)))
366 /* Checking the next three chars should be more than enough to
367 * detect inf or nan, even on Windows. We check for inf or nan
368 * at last because they are rare cases.
370 for (i
=0; *cp
!= '\0' && i
<3; cp
++, i
++) {
371 if (isdigit(Py_CHARMASK(*cp
)) || *cp
== '.')
373 /* found something that is neither a digit nor point
374 * it might be a NaN or INF
377 if (Py_IS_NAN(v
->ob_fval
)) {
382 if (Py_IS_INFINITY(v
->ob_fval
)) {
393 /* XXX PyFloat_AsStringEx should not be a public API function (for one
394 XXX thing, its signature passes a buffer without a length; for another,
395 XXX it isn't useful outside this file).
398 PyFloat_AsStringEx(char *buf
, PyFloatObject
*v
, int precision
)
400 format_float(buf
, 100, v
, precision
);
403 #ifdef Py_BROKEN_REPR
404 /* The following function is based on Tcl_PrintDouble,
408 #define is_infinite(d) ( (d) > DBL_MAX || (d) < -DBL_MAX )
409 #define is_nan(d) ((d) != (d))
412 format_double_repr(char *dst
, double value
)
432 if (is_infinite(value
)) {
442 * Ordinary (normal and denormal) values.
445 exp
= _PyFloat_Digits(buffer
, value
, &signum
)+1;
450 if (exp
< -3 || exp
> 17) {
452 * E format for numbers < 1e-3 or >= 1e17.
464 sprintf(dst
, "e%+d", exp
-1);
467 * F format for others.
499 format_float_repr(char *buf
, PyFloatObject
*v
)
501 assert(PyFloat_Check(v
));
502 format_double_repr(buf
, PyFloat_AS_DOUBLE(v
));
505 #endif /* Py_BROKEN_REPR */
507 /* Macro and helper that convert PyObject obj to a C double and store
508 the value in dbl; this replaces the functionality of the coercion
509 slot function. If conversion to double raises an exception, obj is
510 set to NULL, and the function invoking this macro returns NULL. If
511 obj is not of float, int or long type, Py_NotImplemented is incref'ed,
512 stored in obj, and returned from the function invoking this macro.
514 #define CONVERT_TO_DOUBLE(obj, dbl) \
515 if (PyFloat_Check(obj)) \
516 dbl = PyFloat_AS_DOUBLE(obj); \
517 else if (convert_to_double(&(obj), &(dbl)) < 0) \
521 convert_to_double(PyObject
**v
, double *dbl
)
523 register PyObject
*obj
= *v
;
525 if (PyInt_Check(obj
)) {
526 *dbl
= (double)PyInt_AS_LONG(obj
);
528 else if (PyLong_Check(obj
)) {
529 *dbl
= PyLong_AsDouble(obj
);
530 if (*dbl
== -1.0 && PyErr_Occurred()) {
536 Py_INCREF(Py_NotImplemented
);
537 *v
= Py_NotImplemented
;
543 /* Precisions used by repr() and str(), respectively.
545 The repr() precision (17 significant decimal digits) is the minimal number
546 that is guaranteed to have enough precision so that if the number is read
547 back in the exact same binary value is recreated. This is true for IEEE
548 floating point by design, and also happens to work for all other modern
551 The str() precision is chosen so that in most cases, the rounding noise
552 created by various operations is suppressed, while giving plenty of
553 precision for practical use.
560 /* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
561 XXX they pass a char buffer without passing a length.
564 PyFloat_AsString(char *buf
, PyFloatObject
*v
)
566 format_float(buf
, 100, v
, PREC_STR
);
570 PyFloat_AsReprString(char *buf
, PyFloatObject
*v
)
572 format_float(buf
, 100, v
, PREC_REPR
);
577 float_print(PyFloatObject
*v
, FILE *fp
, int flags
)
580 format_float(buf
, sizeof(buf
), v
,
581 (flags
& Py_PRINT_RAW
) ? PREC_STR
: PREC_REPR
);
582 Py_BEGIN_ALLOW_THREADS
589 float_repr(PyFloatObject
*v
)
591 #ifdef Py_BROKEN_REPR
593 format_float_repr(buf
, v
);
596 format_float(buf
, sizeof(buf
), v
, PREC_REPR
);
599 return PyString_FromString(buf
);
603 float_str(PyFloatObject
*v
)
606 format_float(buf
, sizeof(buf
), v
, PREC_STR
);
607 return PyString_FromString(buf
);
610 /* Comparison is pretty much a nightmare. When comparing float to float,
611 * we do it as straightforwardly (and long-windedly) as conceivable, so
612 * that, e.g., Python x == y delivers the same result as the platform
613 * C x == y when x and/or y is a NaN.
614 * When mixing float with an integer type, there's no good *uniform* approach.
615 * Converting the double to an integer obviously doesn't work, since we
616 * may lose info from fractional bits. Converting the integer to a double
617 * also has two failure modes: (1) a long int may trigger overflow (too
618 * large to fit in the dynamic range of a C double); (2) even a C long may have
619 * more bits than fit in a C double (e.g., on a a 64-bit box long may have
620 * 63 bits of precision, but a C double probably has only 53), and then
621 * we can falsely claim equality when low-order integer bits are lost by
622 * coercion to double. So this part is painful too.
626 float_richcompare(PyObject
*v
, PyObject
*w
, int op
)
631 assert(PyFloat_Check(v
));
632 i
= PyFloat_AS_DOUBLE(v
);
634 /* Switch on the type of w. Set i and j to doubles to be compared,
635 * and op to the richcomp to use.
637 if (PyFloat_Check(w
))
638 j
= PyFloat_AS_DOUBLE(w
);
640 else if (!Py_IS_FINITE(i
)) {
641 if (PyInt_Check(w
) || PyLong_Check(w
))
642 /* If i is an infinity, its magnitude exceeds any
643 * finite integer, so it doesn't matter which int we
644 * compare i with. If i is a NaN, similarly.
651 else if (PyInt_Check(w
)) {
652 long jj
= PyInt_AS_LONG(w
);
653 /* In the worst realistic case I can imagine, C double is a
654 * Cray single with 48 bits of precision, and long has 64
658 unsigned long abs
= (unsigned long)(jj
< 0 ? -jj
: jj
);
660 /* Needs more than 48 bits. Make it take the
664 PyObject
*ww
= PyLong_FromLong(jj
);
668 result
= float_richcompare(v
, ww
, op
);
674 assert((long)j
== jj
);
677 else if (PyLong_Check(w
)) {
678 int vsign
= i
== 0.0 ? 0 : i
< 0.0 ? -1 : 1;
679 int wsign
= _PyLong_Sign(w
);
683 if (vsign
!= wsign
) {
684 /* Magnitudes are irrelevant -- the signs alone
685 * determine the outcome.
691 /* The signs are the same. */
692 /* Convert w to a double if it fits. In particular, 0 fits. */
693 nbits
= _PyLong_NumBits(w
);
694 if (nbits
== (size_t)-1 && PyErr_Occurred()) {
695 /* This long is so large that size_t isn't big enough
696 * to hold the # of bits. Replace with little doubles
697 * that give the same outcome -- w is so large that
698 * its magnitude must exceed the magnitude of any
708 j
= PyLong_AsDouble(w
);
709 /* It's impossible that <= 48 bits overflowed. */
710 assert(j
!= -1.0 || ! PyErr_Occurred());
713 assert(wsign
!= 0); /* else nbits was 0 */
714 assert(vsign
!= 0); /* if vsign were 0, then since wsign is
715 * not 0, we would have taken the
716 * vsign != wsign branch at the start */
717 /* We want to work with non-negative numbers. */
719 /* "Multiply both sides" by -1; this also swaps the
723 op
= _Py_SwappedOp
[op
];
726 (void) frexp(i
, &exponent
);
727 /* exponent is the # of bits in v before the radix point;
728 * we know that nbits (the # of bits in w) > 48 at this point
730 if (exponent
< 0 || (size_t)exponent
< nbits
) {
735 if ((size_t)exponent
> nbits
) {
740 /* v and w have the same number of bits before the radix
741 * point. Construct two longs that have the same comparison
747 PyObject
*result
= NULL
;
748 PyObject
*one
= NULL
;
753 ww
= PyNumber_Negative(w
);
760 fracpart
= modf(i
, &intpart
);
761 vv
= PyLong_FromDouble(intpart
);
765 if (fracpart
!= 0.0) {
766 /* Shift left, and or a 1 bit into vv
767 * to represent the lost fraction.
771 one
= PyInt_FromLong(1);
775 temp
= PyNumber_Lshift(ww
, one
);
781 temp
= PyNumber_Lshift(vv
, one
);
787 temp
= PyNumber_Or(vv
, one
);
794 r
= PyObject_RichCompareBool(vv
, ww
, op
);
797 result
= PyBool_FromLong(r
);
804 } /* else if (PyLong_Check(w)) */
806 else /* w isn't float, int, or long */
810 PyFPE_START_PROTECT("richcompare", return NULL
)
832 return PyBool_FromLong(r
);
835 Py_INCREF(Py_NotImplemented
);
836 return Py_NotImplemented
;
840 float_hash(PyFloatObject
*v
)
842 return _Py_HashDouble(v
->ob_fval
);
846 float_add(PyObject
*v
, PyObject
*w
)
849 CONVERT_TO_DOUBLE(v
, a
);
850 CONVERT_TO_DOUBLE(w
, b
);
851 PyFPE_START_PROTECT("add", return 0)
854 return PyFloat_FromDouble(a
);
858 float_sub(PyObject
*v
, PyObject
*w
)
861 CONVERT_TO_DOUBLE(v
, a
);
862 CONVERT_TO_DOUBLE(w
, b
);
863 PyFPE_START_PROTECT("subtract", return 0)
866 return PyFloat_FromDouble(a
);
870 float_mul(PyObject
*v
, PyObject
*w
)
873 CONVERT_TO_DOUBLE(v
, a
);
874 CONVERT_TO_DOUBLE(w
, b
);
875 PyFPE_START_PROTECT("multiply", return 0)
878 return PyFloat_FromDouble(a
);
882 float_div(PyObject
*v
, PyObject
*w
)
885 CONVERT_TO_DOUBLE(v
, a
);
886 CONVERT_TO_DOUBLE(w
, b
);
888 PyErr_SetString(PyExc_ZeroDivisionError
, "float division");
891 PyFPE_START_PROTECT("divide", return 0)
894 return PyFloat_FromDouble(a
);
898 float_classic_div(PyObject
*v
, PyObject
*w
)
901 CONVERT_TO_DOUBLE(v
, a
);
902 CONVERT_TO_DOUBLE(w
, b
);
903 if (Py_DivisionWarningFlag
>= 2 &&
904 PyErr_Warn(PyExc_DeprecationWarning
, "classic float division") < 0)
907 PyErr_SetString(PyExc_ZeroDivisionError
, "float division");
910 PyFPE_START_PROTECT("divide", return 0)
913 return PyFloat_FromDouble(a
);
917 float_rem(PyObject
*v
, PyObject
*w
)
921 CONVERT_TO_DOUBLE(v
, vx
);
922 CONVERT_TO_DOUBLE(w
, wx
);
924 PyErr_SetString(PyExc_ZeroDivisionError
, "float modulo");
927 PyFPE_START_PROTECT("modulo", return 0)
929 /* note: checking mod*wx < 0 is incorrect -- underflows to
930 0 if wx < sqrt(smallest nonzero double) */
931 if (mod
&& ((wx
< 0) != (mod
< 0))) {
934 PyFPE_END_PROTECT(mod
)
935 return PyFloat_FromDouble(mod
);
939 float_divmod(PyObject
*v
, PyObject
*w
)
942 double div
, mod
, floordiv
;
943 CONVERT_TO_DOUBLE(v
, vx
);
944 CONVERT_TO_DOUBLE(w
, wx
);
946 PyErr_SetString(PyExc_ZeroDivisionError
, "float divmod()");
949 PyFPE_START_PROTECT("divmod", return 0)
951 /* fmod is typically exact, so vx-mod is *mathematically* an
952 exact multiple of wx. But this is fp arithmetic, and fp
953 vx - mod is an approximation; the result is that div may
954 not be an exact integral value after the division, although
955 it will always be very close to one.
957 div
= (vx
- mod
) / wx
;
959 /* ensure the remainder has the same sign as the denominator */
960 if ((wx
< 0) != (mod
< 0)) {
966 /* the remainder is zero, and in the presence of signed zeroes
967 fmod returns different results across platforms; ensure
968 it has the same sign as the denominator; we'd like to do
969 "mod = wx * 0.0", but that may get optimized away */
970 mod
*= mod
; /* hide "mod = +0" from optimizer */
974 /* snap quotient to nearest integral value */
976 floordiv
= floor(div
);
977 if (div
- floordiv
> 0.5)
981 /* div is zero - get the same sign as the true quotient */
982 div
*= div
; /* hide "div = +0" from optimizers */
983 floordiv
= div
* vx
/ wx
; /* zero w/ sign of vx/wx */
985 PyFPE_END_PROTECT(floordiv
)
986 return Py_BuildValue("(dd)", floordiv
, mod
);
990 float_floor_div(PyObject
*v
, PyObject
*w
)
994 t
= float_divmod(v
, w
);
995 if (t
== NULL
|| t
== Py_NotImplemented
)
997 assert(PyTuple_CheckExact(t
));
998 r
= PyTuple_GET_ITEM(t
, 0);
1005 float_pow(PyObject
*v
, PyObject
*w
, PyObject
*z
)
1009 if ((PyObject
*)z
!= Py_None
) {
1010 PyErr_SetString(PyExc_TypeError
, "pow() 3rd argument not "
1011 "allowed unless all arguments are integers");
1015 CONVERT_TO_DOUBLE(v
, iv
);
1016 CONVERT_TO_DOUBLE(w
, iw
);
1018 /* Sort out special cases here instead of relying on pow() */
1019 if (iw
== 0) { /* v**0 is 1, even 0**0 */
1020 return PyFloat_FromDouble(1.0);
1022 if (iv
== 0.0) { /* 0**w is error if w<0, else 1 */
1024 PyErr_SetString(PyExc_ZeroDivisionError
,
1025 "0.0 cannot be raised to a negative power");
1028 return PyFloat_FromDouble(0.0);
1031 /* Whether this is an error is a mess, and bumps into libm
1032 * bugs so we have to figure it out ourselves.
1034 if (iw
!= floor(iw
)) {
1035 PyErr_SetString(PyExc_ValueError
, "negative number "
1036 "cannot be raised to a fractional power");
1039 /* iw is an exact integer, albeit perhaps a very large one.
1040 * -1 raised to an exact integer should never be exceptional.
1041 * Alas, some libms (chiefly glibc as of early 2003) return
1042 * NaN and set EDOM on pow(-1, large_int) if the int doesn't
1043 * happen to be representable in a *C* integer. That's a
1044 * bug; we let that slide in math.pow() (which currently
1045 * reflects all platform accidents), but not for Python's **.
1047 if (iv
== -1.0 && Py_IS_FINITE(iw
)) {
1048 /* Return 1 if iw is even, -1 if iw is odd; there's
1049 * no guarantee that any C integral type is big
1050 * enough to hold iw, so we have to check this
1053 ix
= floor(iw
* 0.5) * 2.0;
1054 return PyFloat_FromDouble(ix
== iw
? 1.0 : -1.0);
1056 /* Else iv != -1.0, and overflow or underflow are possible.
1057 * Unless we're to write pow() ourselves, we have to trust
1058 * the platform to do this correctly.
1062 PyFPE_START_PROTECT("pow", return NULL
)
1064 PyFPE_END_PROTECT(ix
)
1065 Py_ADJUST_ERANGE1(ix
);
1067 /* We don't expect any errno value other than ERANGE, but
1068 * the range of libm bugs appears unbounded.
1070 PyErr_SetFromErrno(errno
== ERANGE
? PyExc_OverflowError
:
1074 return PyFloat_FromDouble(ix
);
1078 float_neg(PyFloatObject
*v
)
1080 return PyFloat_FromDouble(-v
->ob_fval
);
1084 float_abs(PyFloatObject
*v
)
1086 return PyFloat_FromDouble(fabs(v
->ob_fval
));
1090 float_nonzero(PyFloatObject
*v
)
1092 return v
->ob_fval
!= 0.0;
1096 float_coerce(PyObject
**pv
, PyObject
**pw
)
1098 if (PyInt_Check(*pw
)) {
1099 long x
= PyInt_AsLong(*pw
);
1100 *pw
= PyFloat_FromDouble((double)x
);
1104 else if (PyLong_Check(*pw
)) {
1105 double x
= PyLong_AsDouble(*pw
);
1106 if (x
== -1.0 && PyErr_Occurred())
1108 *pw
= PyFloat_FromDouble(x
);
1112 else if (PyFloat_Check(*pw
)) {
1117 return 1; /* Can't do it */
1121 float_trunc(PyObject
*v
)
1123 double x
= PyFloat_AsDouble(v
);
1124 double wholepart
; /* integral portion of x, rounded toward 0 */
1126 (void)modf(x
, &wholepart
);
1127 /* Try to get out cheap if this fits in a Python int. The attempt
1128 * to cast to long must be protected, as C doesn't define what
1129 * happens if the double is too big to fit in a long. Some rare
1130 * systems raise an exception then (RISCOS was mentioned as one,
1131 * and someone using a non-default option on Sun also bumped into
1132 * that). Note that checking for >= and <= LONG_{MIN,MAX} would
1133 * still be vulnerable: if a long has more bits of precision than
1134 * a double, casting MIN/MAX to double may yield an approximation,
1135 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
1136 * yield true from the C expression wholepart<=LONG_MAX, despite
1137 * that wholepart is actually greater than LONG_MAX.
1139 if (LONG_MIN
< wholepart
&& wholepart
< LONG_MAX
) {
1140 const long aslong
= (long)wholepart
;
1141 return PyInt_FromLong(aslong
);
1143 return PyLong_FromDouble(wholepart
);
1147 float_float(PyObject
*v
)
1149 if (PyFloat_CheckExact(v
))
1152 v
= PyFloat_FromDouble(((PyFloatObject
*)v
)->ob_fval
);
1157 float_as_integer_ratio(PyObject
*v
, PyObject
*unused
)
1165 PyObject
*py_exponent
= NULL
;
1166 PyObject
*numerator
= NULL
;
1167 PyObject
*denominator
= NULL
;
1168 PyObject
*result_pair
= NULL
;
1169 PyNumberMethods
*long_methods
= PyLong_Type
.tp_as_number
;
1171 #define INPLACE_UPDATE(obj, call) \
1176 CONVERT_TO_DOUBLE(v, self);
1178 if (Py_IS_INFINITY(self
)) {
1179 PyErr_SetString(PyExc_OverflowError
,
1180 "Cannot pass infinity to float.as_integer_ratio.");
1184 if (Py_IS_NAN(self
)) {
1185 PyErr_SetString(PyExc_ValueError
,
1186 "Cannot pass nan to float.as_integer_ratio.");
1191 PyFPE_START_PROTECT("as_integer_ratio", goto error
);
1192 float_part
= frexp(self
, &exponent
); /* self == float_part * 2**exponent exactly */
1193 PyFPE_END_PROTECT(float_part
);
1195 for (i
=0; i
<300 && float_part
!= floor(float_part
) ; i
++) {
1199 /* self == float_part * 2**exponent exactly and float_part is integral.
1200 If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part
1201 to be truncated by PyLong_FromDouble(). */
1203 numerator
= PyLong_FromDouble(float_part
);
1204 if (numerator
== NULL
) goto error
;
1206 /* fold in 2**exponent */
1207 denominator
= PyLong_FromLong(1);
1208 py_exponent
= PyLong_FromLong(labs((long)exponent
));
1209 if (py_exponent
== NULL
) goto error
;
1210 INPLACE_UPDATE(py_exponent
,
1211 long_methods
->nb_lshift(denominator
, py_exponent
));
1212 if (py_exponent
== NULL
) goto error
;
1214 INPLACE_UPDATE(numerator
,
1215 long_methods
->nb_multiply(numerator
, py_exponent
));
1216 if (numerator
== NULL
) goto error
;
1219 Py_DECREF(denominator
);
1220 denominator
= py_exponent
;
1224 /* Returns ints instead of longs where possible */
1225 INPLACE_UPDATE(numerator
, PyNumber_Int(numerator
));
1226 if (numerator
== NULL
) goto error
;
1227 INPLACE_UPDATE(denominator
, PyNumber_Int(denominator
));
1228 if (denominator
== NULL
) goto error
;
1230 result_pair
= PyTuple_Pack(2, numerator
, denominator
);
1232 #undef INPLACE_UPDATE
1234 Py_XDECREF(py_exponent
);
1235 Py_XDECREF(denominator
);
1236 Py_XDECREF(numerator
);
1240 PyDoc_STRVAR(float_as_integer_ratio_doc
,
1241 "float.as_integer_ratio() -> (int, int)\n"
1243 "Returns a pair of integers, whose ratio is exactly equal to the original\n"
1244 "float and with a positive denominator.\n"
1245 "Raises OverflowError on infinities and a ValueError on nans.\n"
1247 ">>> (10.0).as_integer_ratio()\n"
1249 ">>> (0.0).as_integer_ratio()\n"
1251 ">>> (-.25).as_integer_ratio()\n"
1256 float_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
1259 float_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1261 PyObject
*x
= Py_False
; /* Integer zero */
1262 static char *kwlist
[] = {"x", 0};
1264 if (type
!= &PyFloat_Type
)
1265 return float_subtype_new(type
, args
, kwds
); /* Wimp out */
1266 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|O:float", kwlist
, &x
))
1268 if (PyString_Check(x
))
1269 return PyFloat_FromString(x
, NULL
);
1270 return PyNumber_Float(x
);
1273 /* Wimpy, slow approach to tp_new calls for subtypes of float:
1274 first create a regular float from whatever arguments we got,
1275 then allocate a subtype instance and initialize its ob_fval
1276 from the regular float. The regular float is then thrown away.
1279 float_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1281 PyObject
*tmp
, *newobj
;
1283 assert(PyType_IsSubtype(type
, &PyFloat_Type
));
1284 tmp
= float_new(&PyFloat_Type
, args
, kwds
);
1287 assert(PyFloat_CheckExact(tmp
));
1288 newobj
= type
->tp_alloc(type
, 0);
1289 if (newobj
== NULL
) {
1293 ((PyFloatObject
*)newobj
)->ob_fval
= ((PyFloatObject
*)tmp
)->ob_fval
;
1299 float_getnewargs(PyFloatObject
*v
)
1301 return Py_BuildValue("(d)", v
->ob_fval
);
1304 /* this is for the benefit of the pack/unpack routines below */
1307 unknown_format
, ieee_big_endian_format
, ieee_little_endian_format
1308 } float_format_type
;
1310 static float_format_type double_format
, float_format
;
1311 static float_format_type detected_double_format
, detected_float_format
;
1314 float_getformat(PyTypeObject
*v
, PyObject
* arg
)
1317 float_format_type r
;
1319 if (!PyString_Check(arg
)) {
1320 PyErr_Format(PyExc_TypeError
,
1321 "__getformat__() argument must be string, not %.500s",
1322 Py_TYPE(arg
)->tp_name
);
1325 s
= PyString_AS_STRING(arg
);
1326 if (strcmp(s
, "double") == 0) {
1329 else if (strcmp(s
, "float") == 0) {
1333 PyErr_SetString(PyExc_ValueError
,
1334 "__getformat__() argument 1 must be "
1335 "'double' or 'float'");
1340 case unknown_format
:
1341 return PyString_FromString("unknown");
1342 case ieee_little_endian_format
:
1343 return PyString_FromString("IEEE, little-endian");
1344 case ieee_big_endian_format
:
1345 return PyString_FromString("IEEE, big-endian");
1347 Py_FatalError("insane float_format or double_format");
1352 PyDoc_STRVAR(float_getformat_doc
,
1353 "float.__getformat__(typestr) -> string\n"
1355 "You probably don't want to use this function. It exists mainly to be\n"
1356 "used in Python's test suite.\n"
1358 "typestr must be 'double' or 'float'. This function returns whichever of\n"
1359 "'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n"
1360 "format of floating point numbers used by the C type named by typestr.");
1363 float_setformat(PyTypeObject
*v
, PyObject
* args
)
1367 float_format_type f
;
1368 float_format_type detected
;
1369 float_format_type
*p
;
1371 if (!PyArg_ParseTuple(args
, "ss:__setformat__", &typestr
, &format
))
1374 if (strcmp(typestr
, "double") == 0) {
1376 detected
= detected_double_format
;
1378 else if (strcmp(typestr
, "float") == 0) {
1380 detected
= detected_float_format
;
1383 PyErr_SetString(PyExc_ValueError
,
1384 "__setformat__() argument 1 must "
1385 "be 'double' or 'float'");
1389 if (strcmp(format
, "unknown") == 0) {
1392 else if (strcmp(format
, "IEEE, little-endian") == 0) {
1393 f
= ieee_little_endian_format
;
1395 else if (strcmp(format
, "IEEE, big-endian") == 0) {
1396 f
= ieee_big_endian_format
;
1399 PyErr_SetString(PyExc_ValueError
,
1400 "__setformat__() argument 2 must be "
1401 "'unknown', 'IEEE, little-endian' or "
1402 "'IEEE, big-endian'");
1407 if (f
!= unknown_format
&& f
!= detected
) {
1408 PyErr_Format(PyExc_ValueError
,
1409 "can only set %s format to 'unknown' or the "
1410 "detected platform value", typestr
);
1418 PyDoc_STRVAR(float_setformat_doc
,
1419 "float.__setformat__(typestr, fmt) -> None\n"
1421 "You probably don't want to use this function. It exists mainly to be\n"
1422 "used in Python's test suite.\n"
1424 "typestr must be 'double' or 'float'. fmt must be one of 'unknown',\n"
1425 "'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n"
1426 "one of the latter two if it appears to match the underlying C reality.\n"
1428 "Overrides the automatic determination of C-level floating point type.\n"
1429 "This affects how floats are converted to and from binary strings.");
1432 float_getzero(PyObject
*v
, void *closure
)
1434 return PyFloat_FromDouble(0.0);
1437 static PyMethodDef float_methods
[] = {
1438 {"conjugate", (PyCFunction
)float_float
, METH_NOARGS
,
1439 "Returns self, the complex conjugate of any float."},
1440 {"__trunc__", (PyCFunction
)float_trunc
, METH_NOARGS
,
1441 "Returns the Integral closest to x between 0 and x."},
1442 {"as_integer_ratio", (PyCFunction
)float_as_integer_ratio
, METH_NOARGS
,
1443 float_as_integer_ratio_doc
},
1444 {"__getnewargs__", (PyCFunction
)float_getnewargs
, METH_NOARGS
},
1445 {"__getformat__", (PyCFunction
)float_getformat
,
1446 METH_O
|METH_CLASS
, float_getformat_doc
},
1447 {"__setformat__", (PyCFunction
)float_setformat
,
1448 METH_VARARGS
|METH_CLASS
, float_setformat_doc
},
1449 {NULL
, NULL
} /* sentinel */
1452 static PyGetSetDef float_getset
[] = {
1454 (getter
)float_float
, (setter
)NULL
,
1455 "the real part of a complex number",
1458 (getter
)float_getzero
, (setter
)NULL
,
1459 "the imaginary part of a complex number",
1461 {NULL
} /* Sentinel */
1464 PyDoc_STRVAR(float_doc
,
1465 "float(x) -> floating point number\n\
1467 Convert a string or number to a floating point number, if possible.");
1470 static PyNumberMethods float_as_number
= {
1471 float_add
, /*nb_add*/
1472 float_sub
, /*nb_subtract*/
1473 float_mul
, /*nb_multiply*/
1474 float_classic_div
, /*nb_divide*/
1475 float_rem
, /*nb_remainder*/
1476 float_divmod
, /*nb_divmod*/
1477 float_pow
, /*nb_power*/
1478 (unaryfunc
)float_neg
, /*nb_negative*/
1479 (unaryfunc
)float_float
, /*nb_positive*/
1480 (unaryfunc
)float_abs
, /*nb_absolute*/
1481 (inquiry
)float_nonzero
, /*nb_nonzero*/
1488 float_coerce
, /*nb_coerce*/
1489 float_trunc
, /*nb_int*/
1490 float_trunc
, /*nb_long*/
1491 float_float
, /*nb_float*/
1494 0, /* nb_inplace_add */
1495 0, /* nb_inplace_subtract */
1496 0, /* nb_inplace_multiply */
1497 0, /* nb_inplace_divide */
1498 0, /* nb_inplace_remainder */
1499 0, /* nb_inplace_power */
1500 0, /* nb_inplace_lshift */
1501 0, /* nb_inplace_rshift */
1502 0, /* nb_inplace_and */
1503 0, /* nb_inplace_xor */
1504 0, /* nb_inplace_or */
1505 float_floor_div
, /* nb_floor_divide */
1506 float_div
, /* nb_true_divide */
1507 0, /* nb_inplace_floor_divide */
1508 0, /* nb_inplace_true_divide */
1511 PyTypeObject PyFloat_Type
= {
1512 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
1514 sizeof(PyFloatObject
),
1516 (destructor
)float_dealloc
, /* tp_dealloc */
1517 (printfunc
)float_print
, /* tp_print */
1521 (reprfunc
)float_repr
, /* tp_repr */
1522 &float_as_number
, /* tp_as_number */
1523 0, /* tp_as_sequence */
1524 0, /* tp_as_mapping */
1525 (hashfunc
)float_hash
, /* tp_hash */
1527 (reprfunc
)float_str
, /* tp_str */
1528 PyObject_GenericGetAttr
, /* tp_getattro */
1529 0, /* tp_setattro */
1530 0, /* tp_as_buffer */
1531 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
|
1532 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1533 float_doc
, /* tp_doc */
1534 0, /* tp_traverse */
1536 float_richcompare
, /* tp_richcompare */
1537 0, /* tp_weaklistoffset */
1539 0, /* tp_iternext */
1540 float_methods
, /* tp_methods */
1542 float_getset
, /* tp_getset */
1545 0, /* tp_descr_get */
1546 0, /* tp_descr_set */
1547 0, /* tp_dictoffset */
1550 float_new
, /* tp_new */
1556 /* We attempt to determine if this machine is using IEEE
1557 floating point formats by peering at the bits of some
1558 carefully chosen values. If it looks like we are on an
1559 IEEE platform, the float packing/unpacking routines can
1560 just copy bits, if not they resort to arithmetic & shifts
1561 and masks. The shifts & masks approach works on all finite
1562 values, but what happens to infinities, NaNs and signed
1563 zeroes on packing is an accident, and attempting to unpack
1564 a NaN or an infinity will raise an exception.
1566 Note that if we're on some whacked-out platform which uses
1567 IEEE formats but isn't strictly little-endian or big-
1568 endian, we will fall back to the portable shifts & masks
1571 #if SIZEOF_DOUBLE == 8
1573 double x
= 9006104071832581.0;
1574 if (memcmp(&x
, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1575 detected_double_format
= ieee_big_endian_format
;
1576 else if (memcmp(&x
, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1577 detected_double_format
= ieee_little_endian_format
;
1579 detected_double_format
= unknown_format
;
1582 detected_double_format
= unknown_format
;
1585 #if SIZEOF_FLOAT == 4
1587 float y
= 16711938.0;
1588 if (memcmp(&y
, "\x4b\x7f\x01\x02", 4) == 0)
1589 detected_float_format
= ieee_big_endian_format
;
1590 else if (memcmp(&y
, "\x02\x01\x7f\x4b", 4) == 0)
1591 detected_float_format
= ieee_little_endian_format
;
1593 detected_float_format
= unknown_format
;
1596 detected_float_format
= unknown_format
;
1599 double_format
= detected_double_format
;
1600 float_format
= detected_float_format
;
1602 #ifdef Py_BROKEN_REPR
1603 /* Initialize floating point repr */
1604 _PyFloat_DigitsInit();
1606 /* Init float info */
1607 if (FloatInfoType
.tp_name
== 0)
1608 PyStructSequence_InitType(&FloatInfoType
, &floatinfo_desc
);
1612 PyFloat_CompactFreeList(size_t *pbc
, size_t *pbf
, size_t *bsum
)
1615 PyFloatBlock
*list
, *next
;
1617 size_t bc
= 0, bf
= 0; /* block count, number of freed blocks */
1618 size_t fsum
= 0; /* total unfreed ints */
1619 int frem
; /* remaining unfreed ints per block */
1624 while (list
!= NULL
) {
1627 for (i
= 0, p
= &list
->objects
[0];
1630 if (PyFloat_CheckExact(p
) && Py_REFCNT(p
) != 0)
1635 list
->next
= block_list
;
1637 for (i
= 0, p
= &list
->objects
[0];
1640 if (!PyFloat_CheckExact(p
) ||
1641 Py_REFCNT(p
) == 0) {
1642 Py_TYPE(p
) = (struct _typeobject
*)
1649 PyMem_FREE(list
); /* XXX PyObject_FREE ??? */
1666 size_t bc
, bf
; /* block count, number of freed blocks */
1667 size_t fsum
; /* total unfreed floats per block */
1669 PyFloat_CompactFreeList(&bc
, &bf
, &fsum
);
1671 if (!Py_VerboseFlag
)
1673 fprintf(stderr
, "# cleanup floats");
1675 fprintf(stderr
, "\n");
1679 ": %" PY_FORMAT_SIZE_T
"d unfreed floats%s in %"
1680 PY_FORMAT_SIZE_T
"d out of %"
1681 PY_FORMAT_SIZE_T
"d block%s\n",
1682 fsum
, fsum
== 1 ? "" : "s",
1683 bc
- bf
, bc
, bc
== 1 ? "" : "s");
1685 if (Py_VerboseFlag
> 1) {
1687 while (list
!= NULL
) {
1688 for (i
= 0, p
= &list
->objects
[0];
1691 if (PyFloat_CheckExact(p
) &&
1692 Py_REFCNT(p
) != 0) {
1694 PyFloat_AsString(buf
, p
);
1695 /* XXX(twouters) cast refcount to
1696 long until %zd is universally
1700 "# <float at %p, refcnt=%ld, val=%s>\n",
1701 p
, (long)Py_REFCNT(p
), buf
);
1709 /*----------------------------------------------------------------------------
1710 * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
1712 * TODO: On platforms that use the standard IEEE-754 single and double
1713 * formats natively, these routines could simply copy the bytes.
1716 _PyFloat_Pack4(double x
, unsigned char *p
, int le
)
1718 if (float_format
== unknown_format
) {
1739 /* Normalize f to be in the range [1.0, 2.0) */
1740 if (0.5 <= f
&& f
< 1.0) {
1747 PyErr_SetString(PyExc_SystemError
,
1748 "frexp() result out of range");
1754 else if (e
< -126) {
1755 /* Gradual underflow */
1756 f
= ldexp(f
, 126 + e
);
1759 else if (!(e
== 0 && f
== 0.0)) {
1761 f
-= 1.0; /* Get rid of leading 1 */
1764 f
*= 8388608.0; /* 2**23 */
1765 fbits
= (unsigned int)(f
+ 0.5); /* Round */
1766 assert(fbits
<= 8388608);
1768 /* The carry propagated out of a string of 23 1 bits. */
1776 *p
= (sign
<< 7) | (e
>> 1);
1780 *p
= (char) (((e
& 1) << 7) | (fbits
>> 16));
1784 *p
= (fbits
>> 8) & 0xFF;
1794 PyErr_SetString(PyExc_OverflowError
,
1795 "float too large to pack with f format");
1800 const char *s
= (char*)&y
;
1803 if ((float_format
== ieee_little_endian_format
&& !le
)
1804 || (float_format
== ieee_big_endian_format
&& le
)) {
1809 for (i
= 0; i
< 4; i
++) {
1818 _PyFloat_Pack8(double x
, unsigned char *p
, int le
)
1820 if (double_format
== unknown_format
) {
1824 unsigned int fhi
, flo
;
1841 /* Normalize f to be in the range [1.0, 2.0) */
1842 if (0.5 <= f
&& f
< 1.0) {
1849 PyErr_SetString(PyExc_SystemError
,
1850 "frexp() result out of range");
1856 else if (e
< -1022) {
1857 /* Gradual underflow */
1858 f
= ldexp(f
, 1022 + e
);
1861 else if (!(e
== 0 && f
== 0.0)) {
1863 f
-= 1.0; /* Get rid of leading 1 */
1866 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1867 f
*= 268435456.0; /* 2**28 */
1868 fhi
= (unsigned int)f
; /* Truncate */
1869 assert(fhi
< 268435456);
1872 f
*= 16777216.0; /* 2**24 */
1873 flo
= (unsigned int)(f
+ 0.5); /* Round */
1874 assert(flo
<= 16777216);
1876 /* The carry propagated out of a string of 24 1 bits. */
1880 /* And it also progagated out of the next 28 bits. */
1889 *p
= (sign
<< 7) | (e
>> 4);
1893 *p
= (unsigned char) (((e
& 0xF) << 4) | (fhi
>> 24));
1897 *p
= (fhi
>> 16) & 0xFF;
1901 *p
= (fhi
>> 8) & 0xFF;
1909 *p
= (flo
>> 16) & 0xFF;
1913 *p
= (flo
>> 8) & 0xFF;
1924 PyErr_SetString(PyExc_OverflowError
,
1925 "float too large to pack with d format");
1929 const char *s
= (char*)&x
;
1932 if ((double_format
== ieee_little_endian_format
&& !le
)
1933 || (double_format
== ieee_big_endian_format
&& le
)) {
1938 for (i
= 0; i
< 8; i
++) {
1947 _PyFloat_Unpack4(const unsigned char *p
, int le
)
1949 if (float_format
== unknown_format
) {
1962 sign
= (*p
>> 7) & 1;
1963 e
= (*p
& 0x7F) << 1;
1968 f
= (*p
& 0x7F) << 16;
1974 "can't unpack IEEE 754 special value "
1975 "on non-IEEE platform");
1986 x
= (double)f
/ 8388608.0;
1988 /* XXX This sadly ignores Inf/NaN issues */
2005 if ((float_format
== ieee_little_endian_format
&& !le
)
2006 || (float_format
== ieee_big_endian_format
&& le
)) {
2011 for (i
= 0; i
< 4; i
++) {
2025 _PyFloat_Unpack8(const unsigned char *p
, int le
)
2027 if (double_format
== unknown_format
) {
2030 unsigned int fhi
, flo
;
2040 sign
= (*p
>> 7) & 1;
2041 e
= (*p
& 0x7F) << 4;
2046 e
|= (*p
>> 4) & 0xF;
2047 fhi
= (*p
& 0xF) << 24;
2053 "can't unpack IEEE 754 special value "
2054 "on non-IEEE platform");
2081 x
= (double)fhi
+ (double)flo
/ 16777216.0; /* 2**24 */
2082 x
/= 268435456.0; /* 2**28 */
2100 if ((double_format
== ieee_little_endian_format
&& !le
)
2101 || (double_format
== ieee_big_endian_format
&& le
)) {
2106 for (i
= 0; i
< 8; i
++) {