Re: Float division error

"Richard A. O'Keefe" <[email protected]> Thu, 17 Apr 2014 09:59:52 +1200
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 17/04/2014, at 12:35 AM, Juan Pablo Suarez wrote:

> Hi.
> 
> I think there's a bug in prolog for float division

*SIGH*.
> 
> 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.

> L% gsi
Gambit v4.4.3

> (let ((Val1 39.2)
        (MinV 37.0)
        (MaxV 5.0))
    (/ (- Val1 MinV) MaxV))
.44000000000000056

L% cat zork.c
#include <stdio.h>

int main(void) {
    double const Val1 = 39.2;
    double const MinV = 37.0;
    double const MaxV =  5.0;

    printf("%.18f\n", (Val1-MinV)/MaxV);
    return 0;
}

L% cc zork.c
L% a.out
0.440000000000000557

> The answer should be 0.44 for Val2

No.  It SHOULDN'T.  The answer SWI Prolog gives you is
*PRECISELY* the answer required by the IEEE standard,
to the very last bit.

Please, *please*, before posting anything like this ever again
in any newsgroup, *do* learn something about floating point
arithmetic.

I am getting sick of this.


Jan, can you add to the manual, somewhere, in very large letters:

	SWI Prolog uses exactly the same floating point arithmetic
	as C.  This is normally provided by your hardware, and
	can be expected to conform to the IEEE 754 standard.  This
	is BINARY FLOATING-POINT arithmetic, not arithmetic on the
	mathematical real numbers.  If you think you have found a
	mistake in Prolog's arithmetic, you are almost certainly
	wrong, and should make sure that you understand why, for
	example, 0.3*3 is not exactly equal to 0.9, before you
	report the "bug".

People who don't bother learning how any other language does
floating point arithmetic won't read this either, but at least
we can point to it in reply.

PS: I went to the web site to check if you already had
something like this, but clicking on the link for the manual
http://www.swi-prolog.org/pldoc/doc_for?object=manual
gave me

Internal server error

stream `user_output' does not exist
In:
[49] flush_output(user_output)
[48] '$messages':print_system_message(html(...),error,[...]) at /home/vnc/prolog/lib/swipl/boot/messages.pl:1218
[46] html_write:html_expand(\ ...,pldoc_http,_G282,_G283) at /home/vnc/prolog/lib/swipl/library/http/html_write.pl:367
[42] html_write:do_expand(...div...,plweb_page,[...|...],_G303) at /home/vnc/prolog/lib/swipl/library/http/html_write.pl:432
...