Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math.Ceil ??

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me. Why am i getting 1.0 here in the following code instead of 2.0
?
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Try:

pgno = Math.ceil(15.0/8);

Then think: what the difference is between the two lines?
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the api the method Math.ceil has the following signature :
public static double ceil(double a)

However when you divide 15/8 you return an int, if you divide 15.0/8.0 you are using a floating point hence the the answer will be a double and the Math.ceil method will function as required.
[ March 17, 2005: Message edited by: Nigel Browne ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys.I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic