[erlang-questions] erlang float comparison

Richard O'Keefe ok@REDACTED
Tue May 15 00:38:56 CEST 2012


On 15/05/2012, at 1:53 AM, Angel J. Alvarez Miguel wrote:
> 
> Sorry folks, im just a bit puzzled for the strangeness of fp operations
> 
> I am talking about things like this:
> 
> 8> 1.0000000000000001 > 1.0. 
> false
> 9> 1.0000000000000002 > 1.0.
> true
> 
> And i wander that there is some method for avoiding such beasts..

There is nothing in the least strange about those results.
Floating point numbers are not mathematical real numbers.
They have a bounded, indeed a fixed, precision.
Single-precision IEEE floats have about 7 digits;
double-precision ones have about 16 digits.
Considering only strictly positive numbers, and being
rather vaguer about the details than I'd like, in order
to keep this simple,

double precision can distinguish x from y
only when |x-y|/max(|x|,|y|) is greater than about 10**-16.

When you enter a decimal number like 1.0000000000000001
it is rounded to the nearest number that can be represented
as a float, and that happens to be 1.0.

> 
> Well ill be happy just if i can compare if Something > 1.0 and it just returns
> false unless Something is really greater than 1.0 (whatever the accuracy is)

There is not the least problem here with > .
The problem is that the Something you *type* (or try to calculate)
is not the Something' you *get*; the very best you have any right
to hope for is that Something' is Something rounded to the nearest
representable number.  For any single
+ - * / or sqrt, that's what you get.  For anything more complicated,
Good Luck (you're going to need it)!

Oh yeah, one other thing: decimal->binary conversion and binary->
decimal conversion are quite hard to get right.  I believe Scheme
was the first programming language to require that the results be
as good as possible.  C does not.  In particular, you should not
be surprised if scanf(printf(x)) != x; since other languages often
use the C libraries for binary<->decimal conversion, you can expect
similar problems in any language that does not explicitly guarantee
best-possible binary<->decimal conversion (and life being what it
is, don't be too surprised to find problems in implementations of
languages that _do_ make such guarantees; implementors may be
unaware of the difficulties).

You know, I have to wonder what the people who fund the development
of autonomous weapons think they are playing at, sometimes...




More information about the erlang-questions mailing list