[Cryptography] floating point

John Denker jsd at av8n.com
Tue Dec 23 22:03:40 EST 2014


On 12/23/2014 02:05 PM, Dave Horsfall wrote:

> I still remain astonished, that, since Babbage/Lovelace/Turing/etc, that 
> many alleged programmers still do not know that 0.0 != 0.
> 
> Never compare floating-point numbers for equality; I think Henry Spencer 
> said that (I've actually shook hands with him, and a better bloke you 
> would never meet).

I have no idea what that is trying to say, but whatever it is,
that's not the right way to say it.

There are some things that you can do with floating point, and
something you cannot.
 -- On any machine that uses IEEE floating point, in any normal 
  language such as c or python, if one external representation is 
  "0.0" and another is "0.", they have the same internal representation.
  Exactly the same.  Exactly equal.  So count me as one of the "alleged
  programmers" who thinks zero is equal to zero.
 -- In fact, in IEEE double precision, any integer from zero up to 
  9007199254740992 is represented exactly.  Comparing such things 
  succeeds reliably.
 -- A lot of other things, include 0.5, 0.25, 0.75 are also represented
  exactly.
 -- Et cetera.

On the other hand, 
  python -c "print (.6 + .3 + .1 - 1)"
does not produce zero.

Floating point numbers are a subset of the rationals.  As such, the
numbers themselves are exact.  Common operations may or may not
produce exactly-representable results.  The programmer gets to
choose which operations to perform or not perform.

Sometimes I compare floating point numbers, and sometimes I don't.



More information about the cryptography mailing list