Re: quick question

From: John Carson (donaldquixote_at_datafast.net.au)
Date: 03/11/04


Date: Thu, 11 Mar 2004 15:38:33 +1100


"Snake" <hana1@rogers.com> wrote in message
news:fOR3c.250444$Qg7.127144@news04.bloor.is.net.cable.rogers.com
> Hello experts;
> I have a quick urgent question that I really need help with.
> if we have this simple program:
>
> void main(void){
>
> double x =( 5.6+3.1 +4.2 +1.8 +2.4);
> double y = 17.1;
> if(y == x){
> cout <<"equal"<<endl;
> }
> cout << (fabs(y-x))<<endl;
> cout <<x <<" "<< y;
> getch();
> }
>
>
> why equal does not appear on the screen?(in other words y!=x)
> and why their subtraction is not equal to zero?
> the thing that if you delete 2.4 from x and you make y = 14.7, they
> will be equal,strange?!!
> Any suggestion how I can output the subtraction as zero?!!
>
> Thank you guys very much
> Cheers!!

Floating point arithmetic is approximate. This is for a couple of reasons,
but the relevant reason in the present case is that floating point numbers
are stored internally as binary (base 2) numbers and not all decimal numbers
can be stored exactly as binary numbers. For example, while 5.6 is a
terminating decimal number, it is an infinitely repeating binary number.
Since only a finite number of digits is available for storing the number, it
follows that the binary version of the number is only approximate.

Sometimes binary number are exact and sometimes the errors cancel out, but
in general the result is only approximate. You have two choices:

1. Calculate the difference between two numbers and treat them as equal if
the absolute value of the difference is less than some small number.
2. Only work with integers.

-- 
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Relevant Pages

  • REPRESENT revisited
    ... Forth-94 has few floating point display functions [none in the ... If u is greater than zero the character string shall consist ... If u is zero the string shall consist of one digit representing ...
    (comp.lang.forth)
  • Re: Simple subtraction formula returning strange results = Excel g
    ... subtraction differ only in some number of the least significant bits ... The 64-bit floating-point representation is shown in the stylized hex form &hEEEMMMMM,M...M, where "E" is the biased exponent and "M" is the mantissa. ... always results in a zero in A3. ... A1 displays as 1.79769313486232E+308, but the first 30 digits of its exact representation are 179769313486231,57081452742373. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Floating point errors in collision routines
    ... > Trying to converge a floating point calculation all the way to ... > zero is fundamentally futile. ... > Newer collision engines, like Gino's SOLID, get ... >> the plane.) ...
    (comp.lang.cpp)
  • Cfv: Floating point truncation
    ... RfD: Floating point truncation ... which, respectively, provide rounding to the nearest integer, and rounding to the nearest integer on the side of the number line towards negative infinity. ... ANS Forth does not provide a word for rounding towards zero, and returning a floating point representation of the resulting integer. ... Include a standard word, named "FTRUNC", to perform "truncation", i.e. rounding of a floating point number towards zero, returning a floating point representation of the resulting integer. ...
    (comp.lang.forth)
  • Re: Floating point arithmetic (epsilon)
    ... little point in using byte compiled code for numerics when you have access ... >we don't know the real root ahead of time. ... >enough to zero" means. ... Think not of zero when you use floating point code. ...
    (comp.lang.lisp)