bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] How to print the just enough number digits so the machine


From: Nelson H. F. Beebe
Subject: Re: [bug-gawk] How to print the just enough number digits so the machine representation of the number does not change?
Date: Mon, 8 Oct 2012 06:03:10 -0600 (MDT)

I beg to differ on the number of digits required for binary to decimal
conversion.  This is a well-studied problem, solved 45 years ago.

In hoc, I have

hoc> ?matula
matula(nbits):

        matula(nbits) is the number of decimal digits needed to ensure
        correct round-trip conversion between binary and decimal of
        floating-point data with nbits bits in the significand.

        matula(P) is that number for this implementation of hoc.

        In general, for decimal-to-binary conversion of d decimal digits
        to p bits, we need to ensure that

                10**d < 2**(p - 1),

        and for binary-to-decimal conversion, we require that

                2**p < 10**(d - 1).

        See David W. Matula, ``In-and-out conversions'', Comm. ACM
        11(1) 47--50, January 1968.  CODEN CACMA2.  ISSN 0001-0782.

        See also help_goldberg(), help_goldbergb(), and help_matulab().

The 64-bit IEEE 754 binary format has 53 significand bits, so we
ask for

hoc> matula(53) 
 17

The reverse relation for decimal to binary conversion is provided
by another function with this documentation:

hoc> ?goldberg
goldberg(ndecdig):

        goldberg(ndecdig) is the number of bits needed to ensure
        correct round-trip conversion between binary and decimal of
        floating-point data with ndecdig decimal digits in the
        significand.

        In general, for decimal-to-binary conversion of d decimal digits
        to p bits, we need to ensure that

                10**d < 2**(p - 1),

        and for binary-to-decimal conversion, we require that

                2**p < 10**(d - 1).

        See I. Bennett Goldberg, ``27 Bits Are Not Enough For 8-Digit
        Accuracy'', Comm. ACM, 10(2) 105--106, February 1967.  CODEN CACMA2.
        ISSN 0001-0782.

        See also help_goldbergb(), help_matula(), and help_matulab().

The IEEE 754-2008 64-bit decimal format supplies exactly 16 decimal digits,
for which we have:

hoc> goldberg(16)
 55

Notice that the two functions are not mutually symmetric: 16 decimal digits
needs two more bits than the 64-bit binary format can supply.

Regrettably, both Fortran and C, and consequently, almost all (probably all)
scripting languages get this wrong:

hoc> matula(24) ; goldberg(7) 
 9
 25

The default output format in decimal for a 32-bit IEEE binary type should be
9 digits.  The 32-bit IEEE decimal type needs 25 bits for correct round-trip
conversion.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: address@hidden  -
- 155 S 1400 E RM 233                       address@hidden  address@hidden -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]