A stupid question about a calculation

A stupid question about a calculation

Page 1 of 1

1 Replies - 649 Views - Last Post: 10 October 2009 - 10:12 AM Rate Topic: -----

#1 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

A stupid question about a calculation

Posted 10 October 2009 - 10:06 AM

 double z = (weight - FREE_WEIGHT) /5;
   	
   	double x = Math.ceil(z);



If weight = 33 and FREE_WEIGHT = 10, why is z returning 4 when it should be returning 5

Ultimately, why is z passing through 4 instead of 4.6 which is what is should pass through
Is This A Good Question/Topic? 0
  • +

Replies To: A stupid question about a calculation

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12298
  • View blog
  • Posts: 45,399
  • Joined: 27-December 08

Re: A stupid question about a calculation

Posted 10 October 2009 - 10:12 AM

I'm guessing weight and FREE_WEIGHT are ints. Basically, since all of the operands are ints, they are calculated as such. The result is then converted to a double (after truncation) and assigned to z.

Remember, all the operands are implicitly converted based on the order of magnitude. For example, if weight was a double and FREE_WEIGHT was an int, then the value stored in FREE_WEIGHT is converted to a double. Here is the order of magnitude in descending order for numerical primitives: double, float, long, int, short, byte.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1