Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Easy Problem?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rto




PostPosted: Fri Oct 10, 2008 8:08 am   Post subject: Easy Problem?

Java:

class test
{
    public static void main ( String [] args)
    {
        int i1 = 3, i2 = 4, i3 = 5;
        double d1 = -3.4, d2 = 4.8, d3 = 7.1;
        System.out.println (Math.round (d1) + d2);
    }
}

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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Oct 10, 2008 10:52 am   Post subject: RE:Easy Problem?

Adding a double and an int causes the result to be coerced to a double.
rto




PostPosted: Fri Oct 10, 2008 11:28 am   Post subject: 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




PostPosted: Fri Oct 10, 2008 11:34 am   Post subject: RE:Easy Problem?

Java:
public class test
{
    public static void main ( String [] args)
    {
        double i1 = 3, i2 = 4, i3 = 5;
        double d1 = -3.4, d2 = 4.8, d3 = 7.1;
        System.out.println (Math.round(d1 + d2));
    }
}


Mod Edit: Code tags...


Both variables have to be inside the brackets
Clayton




PostPosted: Fri Oct 10, 2008 11:45 am   Post subject: 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




PostPosted: Fri Oct 10, 2008 12:18 pm   Post subject: 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...
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: