What's wrong with this division? [duplicate]
Solution 1:
It's doing integer division in the first example as this is the default type for a numeric literal. Try changing it to -1.0/9
(or 1d/9d
- the d suffix indicates a double
) and you should get the same answer.
Solution 2:
1 and 9 are both integer. Try
1.0/9
This is why it works for fraction/9
, since fraction
is a double.
Solution 3:
When you do -1/9, it says "-1, that's an int. 9, that's an int. -1 / 9 in integer division is 0. Oh, now I need to cast to double."
Changing it to -1.0 / 9 should solve the problem.