Multiplying by an Integer by a Float/Double in Java -


i'm trying simple: multiply integer float/double:

private final static int = 100; ... double d = 0.5; // although dividing 2, decimal. int x = (int) (a * d); 

however, although you'd expect return x = 50, instead x = 100! same thing happens when use float d = 0.5f. know going on , why isn't working?

jls §4.2.4. floating-point operations tell rules governing floating point operation, per if of operand floating point number other operand widened floating point (if not already) , final result floating point number.

so, int 100 widened 100.0 before arithmetic operation, , result of operation 50.0.

now, since casting result in int, jsl §5.1.3. narrowing primitive conversion applicable, , double 50.0 narrowed int 50.

100 impossible result, given code have provided.

my guess when got 100, have done else.

enter image description here


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -