Re: Float division error

Laurent Georget <[email protected]> Wed, 16 Apr 2014 15:10:15 +0200
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
Hello

> I think there's a bug in prolog for float division
This is not a bug. This is related to the way the computers manage 
floating point numbers.
> Problem :
>
> 3 ?- Val1 = 39.2, MinV = 37.0, MaxV = 5.0, Val2 is ((Val1-MinV)/MaxV).
> Val1 = 39.2,
> MinV = 37.0,
> MaxV = 5.0,
> Val2 = 0.44000000000000056.
>
>
> The answer should be 0.44 for Val2
The problem arises because computers have a limited precision for 
"floats" (floating point numbers). So, there are small rounding errors 
everywhere. This is independent of the language but as to do with the 
OS, the hardware and above all, with the representation of the floats, 
standardized by the IEEE.

For example, Python will give you the same result.

Python 2.7.5 (default, Feb 19 2014, 13:47:28)
[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> (39.2-37.0)/5.0
0.44000000000000056

> Is this behavior normal ?
Yes. Or, better said, it is inevitable.

Laurent Georget