Java Float to Long Typecast
Here
return (long) x/y;
You are casting x
as long
but the entire expression is still float
because of y
and hence when you try to return it , it shows error. It is same as return ((long)x/y);
Better :
return (long) (x/y);
The only issue here is how things are parenthesized. You'd be fine if you wrote
return (long) (x / y);
When you wrote (long) x / y
, that was treated as ((long) x) / y
, which is a float
according to the typing rules of Java.