
-----------------------------------
rto
Fri Oct 10, 2008 8:08 am

Easy Problem?
-----------------------------------

class test
{
    public static void main ( String 
the output answer : 1.7999999999999998

My question : why is this not returning 1.8 as the output instead? have i misused the Math.round function

Edit: Whilst I do not think this to be important, i should tell you that im running this program through READY because DOS is currently blocked for my school computers.

-----------------------------------
wtd
Fri Oct 10, 2008 10:52 am

RE:Easy Problem?
-----------------------------------
Adding a double and an int causes the result to be coerced to a double.

-----------------------------------
rto
Fri Oct 10, 2008 11:28 am

RE:Easy Problem?
-----------------------------------
yes but shouldnt the answer be 1.80 ? if its being ROUNDED than i dont understand why its showing this as such a long decimal.

-----------------------------------
S_Grimm
Fri Oct 10, 2008 11:34 am

RE:Easy Problem?
-----------------------------------
public class test
{ 
    public static void main ( String 

Mod Edit: Code tags...


Both variables have to be inside the brackets

-----------------------------------
Clayton
Fri Oct 10, 2008 11:45 am

RE:Easy Problem?
-----------------------------------
Because d2 is not exactly 4.8 when you add it, try it out. As a double representation of 4.8, you don't quite get 4.8 when you show it in memory.

-----------------------------------
jbking
Fri Oct 10, 2008 12:18 pm

RE:Easy Problem?
-----------------------------------
rto,

  Have you tried converting .8 from base 10 to base 2? If not, here is an attempt at the conversion

4/5 = 1/2 + 1/4 + 1/16 + ...

Where for each we just multiply the decimal by 2 and take its integer part for whether it is part of the number and the fractional part to continue the process, so

.8  X 2 = 1.6  ,  so 1/2 is part of the representation...

.6 X 2 = 1.2 , so 1/4 is part of the representation....

.2 X 2 = .4 , so no 1/8 in the representation...

.4 X 2 = .8 , so no 1/16 in the representation, but we also hit where we started so this is a repeating fractional element kind of like how 3 / 7 expressed in decimal form has a repeating fraction part, so would the .8 in binary continues on and on...
