Folks
One of my students drew my attention to the following unexpected result from CIAO:
?- X is 1926/100.
X = 19.2600000000000029 ?
Why is the "29" appearing at the end? Is this only happening in our current version?
Wamberto Vasconcelos writes:
One of my students drew my attention to the following unexpected result from CIAO:
?- X is 1926/100.
X = 19.2600000000000029 ?
Why is the "29" appearing at the end? Is this only happening in our current version?
The reason is that the operation is internally performed using C floats and C arithmetic, and the conversion to/from a binary representation introduces errors. What happens next is that Ciao is trying to print more decimals than the IEEE floating point arithmetic guarantees to be exact. This has been changed in recent (development) versions (not on the web yet), so that only significant decimals are printed:
?- X is 1926/100.
X = 19.26 ?
yes
Let me point out that this behavior is not that bizarre: a similar behavior happens if you compile and run the following C program:
#include <stdio.h>
main() { printf("%.20lf\n", (double)1926.0/(double)100.0); }
[boris(a)salmon tmp]$ gcc float.c -o float [boris(a)salmon tmp]$ ./float 19.26000000000000156319
MCL
_____________________________________________________________________ E lengeege weth e smell nember ef vewels cen remeen quete expresseve. ============================================================================== Message: Address: Action: help majordomo(a)clip.dia.fi.upm.es Info. on useful commands subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list <whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list ----------------------------------------------------------------------------- Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/ -----------------------------------------------------------------------------
Wamberto Vasconcelos escreveu:
Folks
One of my students drew my attention to the following unexpected result from CIAO:
?- X is 1926/100.
X = 19.2600000000000029 ?
Why is the "29" appearing at the end? Is this only happening in our current version?
The result is not "bizarre" but a consequence of the way floating point numbers are represented in computers.
The default number of digits chosen for the current version of Ciao (in our installation 1.8) lets us see the 'imprecision'. Probably there should be a means of reducing the number of figures to four or five where you would get a more 'expected' result.
HTH