[Cryptography] floating point

Jerry Leichter leichter at lrw.com
Wed Dec 24 12:08:12 EST 2014

On Dec 24, 2014, at 1:39 AM, John Denker <jsd at av8n.com> wrote:
>> [...] "never print more digits of precision than your floating point
>> numbers can actually represent" is one way of getting at it. 
>> Following this rule avoids all kinds of traps people fall into when 
>> they think they can get more precision out of a calculation than the
>> numbers themselves can actually carry.  Seen it happen....
> That's just completely backwards.  If you want to roundtrip a
> number -- i.e. write it back out and read it back in, producing
> the same internal representation -- it is quite likely that you
> need to print /more/ digits than the internal representation
> can handle.
I was talking about printing numbers for humans to read - i.e., final results.

Attempting to save and then read back FP numbers in decimal format is either straightforward - if your programming language provides a direct way to do it - or an "advanced topic" if it doesn't.  (Modula-3 may have been the first language to provide an explicit way to do this; its default method for printing an FP method produced (one of the set of) decimal representations for the FP value given that was (a) as short as possible; (b) if read by the corresponding default FP reader, would re-create the same bit pattern as the original value.  This isn't true of at least C or FORTRAN:  FORTRAN D or F or G format, and the C %f/%g formats they inspired, when given no precision, simply arbitrarily give you 6 digits.  If you want to do a round-trip, you have to figure out the appropriate representation for yourself.

At about the same time, a pair of papers by different sets of authors, by coincidence, were submitted to and appeared in some ACM conference (I forget which), one on how to print FP numbers efficiently and correctly, one on how to read them.  Most implementations at the time were either inefficient, incorrect, or often both.

Those papers, and the Modula-3 approach, have influenced implementations since.

> Roundtripping IEEE double precision requires 17 decimal digits.
... or less.  If the goal is to minimize what you print, it's a harder problem.

> Perhaps the people who don't care enough about the subject to
> dig beyond one-sentence slogans should refrain from sneering 
> at people who do.
And perhaps those who do know about a subject shouldn't look down their noses at those who don't, but try to provide helpful guidance.  Knowing the details of FP arithmetic doesn't make you a better person - it just makes you a person who knows the details of FP arithmetic.

If I can get across to those without detailed knowledge that:  If you're doing fairly simple stuff and don't have to worry about ultimate performance or high accuracy, following a few simple rules will keep you out of trouble; and if you need more, go ask an expert - I feel I've done my job.
                                                        -- Jerry

More information about the cryptography mailing list