Jump to content

  • Log In with Google      Sign In   
  • Create Account

Output Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Ryan Schurton   Members   -  Reputation: 176

Like
0Likes
Like

Posted 12 February 2013 - 06:41 PM

so am doing this exercise from my text book. and my code is as followed 

public class Census{
    public static void main(String[] args){
    
        double population;
        double birth = 86400/7;
        double death = 86400/13;
        double immigrant = 86400/45;
        double total;
        
        total = birth + immigrant - death;
        total = total * 365;
        population = total + 312032486; 
        System.out.println(population);
    }
}

 

My output to the console is 3.14812326E8

 

 When i do this problem step by step on my calculation i get the following output 312040102.7.

 

Why are the numbers so different?



Sponsor:

#2 olivaw   Members   -  Reputation: 119

Like
1Likes
Like

Posted 12 February 2013 - 06:50 PM

Ryan,

 

  In Java, the way you write constants define their types. When you write "86400/7", the compiler understands that the two numbers are integer and the result should be integer also, so instead of getting "12342.85714285714" into "birth" you are getting only "12342".

 

  Try to write your constants differently: instead of 86400, use 86400.0 or 86400d.

 

  Take a look here, in the topic "Floating-point literals".

 

Hope this helps.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS