[erlang-questions] erlang float comparison

Richard O'Keefe ok@REDACTED
Tue May 15 00:23:07 CEST 2012


On 14/05/2012, at 7:41 PM, Angel J. Alvarez Miguel wrote:

> 
> Hi guys
> 
> 
> I need to compare two floats something like 0.99999998... vs 1.000000
> and we came across accuracy problems when testing dihedral angles on a molecule...
> 
> 
> so we read http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

You did read the bit at the top, didn't you?
As the author has said elsewhere,

	Years ago I wrote an article about how to do epsilon
	floating-point comparisons by using integer comparisons.
	That article has been quite popular (it is frequently
	cited, and the code samples have been used by a number
	of companies) and this worries me a bit, because the
	article has some flaws.  I’m not going to link to the
	article because I want to replace it, not send people
	looking for it.

The crucial questions are "what exactly do you want to accomplish,
*semantically*, and what is the problem you are *really* trying
to solve by doing that?"

IEEE formats look simple until you discover that there are, in
reality, endianness issues and worse.  For my Smalltalk system I
had to implement a number of functions that I couldn't rely on
being present in C; the code is unpleasantly riddled with
machine-dependent #ifdefs.  The way things are stored in memory
and the way they are stored in registers aren't always related
the way you expect.

> is think we should start with...
>  
> <<Padding:16,Myint:64>> = term_to_binary(1.9999976,[{minor_version,1}]).

http://www.erlang.org/doc/man/erlang.html#term_to_binary-2
speaks of "THE 64-bit IEEE format", but of course there is no
such animal.  The IEEE format is a register format; both floats
and an integers have endianness issues, and it looks as though
here they do not match.




More information about the erlang-questions mailing list