# Math Craziness

Alex Birmingham
Ranch Hand
Posts: 54
Why do the following two lines of code equate to 0, vs 5.7 or 5?

System.out.println((12 / 21) * 10);

- and -

int = (int) ((12 /21) * 10);

Bhagat Singh Rawat
Ranch Hand
Posts: 93
Alex Birmingham wrote:Why do the following two lines of code equate to 0, vs 5.7 or 5?

System.out.println((12 / 21) * 10);

- and -

int = (int) ((12 /21) * 10);

- (12 /21) -- return 0.57 which is converted to 0 because it is int calculation by default.
- Then 0 will be multiplied by 10 which equals to 0.
- Therefore the answer will be Zero (0)

Note:
- If you change your code to......

System.out.println((12.0 /21) * 10);

Then the result will be what you were expecting.........

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
Bhagat Singh Rawat wrote:
- (12 /21) -- return 0.57 which is converted to 0 because it is int calculation by default.

All of your post is correct except for this; 12/21 in int arithmetic is 0, straight off; there's no conversion or rounding involved.

Bhagat Singh Rawat
Ranch Hand
Posts: 93
Ernest Friedman-Hill wrote:
Bhagat Singh Rawat wrote:
- (12 /21) -- return 0.57 which is converted to 0 because it is int calculation by default.

All of your post is correct except for this; 12/21 in int arithmetic is 0, straight off; there's no conversion or rounding involved.

Yeah! absolutely correct. I couldn't explain it in correct way

Alex Birmingham
Ranch Hand
Posts: 54
Ah!!! I didn't realize that Java treated numbers as specific primitive types before we assign them variables and therefore cast them.

Now I know why l or L must be added to longs, and f or F to floats.

Much appreciated.

W. Joe Smith
Ranch Hand
Posts: 710
Alex Birmingham wrote:Ah!!! I didn't realize that Java treated numbers as specific primitive types before we assign them variables and therefore cast them.

Now I know why l or L must be added to longs, and f or F to floats.

Much appreciated.

Just remember that in arithmatic calculations such as that all types are promoted to the largest type in the calculation. So in the following code all numbers are promoted to a double.

Also take notice that if you are going to assign that value to a variable you may need an explicit cast (if the cast is to a smaller primitive type) like so:

Even though the value is small enough to fit in an int, technically, the value is going to be a double and it cannot be directly placed in an int without an explicit cast.

Rob Spoor
Sheriff
Posts: 20708
68
W. Joe Smith wrote:Just remember that in arithmatic calculations such as that all types are promoted to the largest type in the calculation.

But they are never smaller than int. For instance:
But:

Also take notice that if you are going to assign that value to a variable you may need an explicit cast (if the cast is to a smaller primitive type) like so:

Surely you mean
Casts go for mathematical operations, so your example would cast 4.5 to int (making it 4), then adding 5.6 to make the result 9.6, which is a double.

W. Joe Smith
Ranch Hand
Posts: 710
Rob Prime wrote:
W. Joe Smith wrote:Just remember that in arithmatic calculations such as that all types are promoted to the largest type in the calculation.

But they are never smaller than int. For instance:
But:

Also take notice that if you are going to assign that value to a variable you may need an explicit cast (if the cast is to a smaller primitive type) like so:

Surely you mean
Casts go for mathematical operations, so your example would cast 4.5 to int (making it 4), then adding 5.6 to make the result 9.6, which is a double.

True. This is why I should never post before I drink at least 1 cup of coffee.