Re: Embarrassing Arithmetic behaviour

"Richard A. O'Keefe" <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
Isn't this a FAQ somewhere?

On 19/02/2014, at 11:04 PM, j.gressier wrote:
> 15 ?- A is 4.9 * 10000.
> A = 49000.0.
> 
> -------------------------------------------
> 16 ?- A is 4.9 * 100000.
> A = 490000.00000000006.
> -------------------------------------------

This is *CORRECT* and is evidence that
SWI Prolog is doing its calculations RIGHT
and not lying to you about the answers.

m% gsi
Gambit v4.6.0

> (* 4.9 10000)
49000.
> (* 4.9 100000)
490000.00000000006
> (* 4.9 1000000.0)
4900000.

m% cat utu.c
#include <stdio.h>

int main(void) {
    printf("%.12f\n", 4.9*10000.0);
    printf("%.12f\n", 4.9*100000.0);
    printf("%.12f\n", 4.9*1000000.0);
    return 0;
}

m% cc utu.c
m% a.out
49000.000000000000
490000.000000000058
4900000.000000000000

Welcome to the wonderful world of
binary floating point arithmetic.

This has absolutely nothing to do with SWI Prolog,
and the only embarrassing thing is the suggestion
that it is anything other than expected behaviour.

Nobody graduates from this department without
having done several hours of exercises designed
to convince them that floating point arithmetic
is not mathematical real number arithmetic.
How does anyone get through a programming course
without meeting this stuff?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.