Computer Science Canada

weird....

Author:  person [ Sun Nov 06, 2005 5:35 pm ]
Post subject:  weird....

code:

public class hi
{
    public static void main (String[] args)
    {
        System.out.println (0.1 + 0.2);
    }
}


k, so this seems like it should give me 0.3
but apparently, it outputs 0.30000000000000004
y?

Author:  [Gandalf] [ Sun Nov 06, 2005 6:00 pm ]
Post subject: 

This topic was talked about in detail here:

http://www.compsci.ca/v2/viewtopic.php?t=9833

This may not be what you are talking about

Author:  person [ Mon Nov 07, 2005 7:24 pm ]
Post subject: 

and how would i solve this problem and make java add like it should be?

Author:  wtd [ Mon Nov 07, 2005 7:40 pm ]
Post subject: 

Floating point numbers cannot be natively represented inside a computer. Therefore we have a Pretty Good(tm) way of estimating them to a fair degree of accuracy.

Author:  person [ Mon Nov 07, 2005 7:53 pm ]
Post subject: 

Quote:
Floating point numbers cannot be natively represented inside a computer. Therefore we have a Pretty Good(tm) way of estimating them to a fair degree of accuracy.


so basically, there's no way to actually do addition with decimals in java?

Author:  wtd [ Mon Nov 07, 2005 8:18 pm ]
Post subject: 

Not to complete accuracy. The issue is not restricted to Java.

Author:  person [ Mon Nov 07, 2005 8:23 pm ]
Post subject: 

Quote:
Not to complete accuracy. The issue is not restricted to Java.


k, i know languages like turing has the same problem but y does turing not output 0.30000000000004 for 0.1+0.2? does turing hava another way of calculating hte numbers?

Author:  wtd [ Mon Nov 07, 2005 8:43 pm ]
Post subject: 

person wrote:
Quote:
Not to complete accuracy. The issue is not restricted to Java.


k, i know languages like turing has the same problem but y does turing not output 0.30000000000004 for 0.1+0.2? does turing hava another way of calculating hte numbers?


It's just a matter of how it outputs the number. It like rounds to a certain place by default. Many environments will round to something like 8-10 places.

Then, rather than output:

code:
0.3000000000


It simply outputs:

code:
0.3


: