
-----------------------------------
person
Sun Nov 06, 2005 5:35 pm

weird....
-----------------------------------

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?

-----------------------------------
[Gandalf]
Sun Nov 06, 2005 6:00 pm


-----------------------------------
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

-----------------------------------
person
Mon Nov 07, 2005 7:24 pm


-----------------------------------
and how would i solve this problem and make java add like it should be?

-----------------------------------
wtd
Mon Nov 07, 2005 7:40 pm


-----------------------------------
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.

-----------------------------------
person
Mon Nov 07, 2005 7:53 pm


-----------------------------------
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?

-----------------------------------
wtd
Mon Nov 07, 2005 8:18 pm


-----------------------------------
Not to complete accuracy.  The issue is not restricted to Java.

-----------------------------------
person
Mon Nov 07, 2005 8:23 pm


-----------------------------------
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?

-----------------------------------
wtd
Mon Nov 07, 2005 8:43 pm


-----------------------------------
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:

0.3000000000

It simply outputs:

0.3
