System.out.println(Math.pow(16, 0.5)); is 4.0
and
System.out.println(Math.pow(16, (1/2))); is 1.0
Why??? I need to use fraction!
System.out.println(Math.pow(16, 0.5)); is 4.0
and
System.out.println(Math.pow(16, (1/2))); is 1.0
Why??? I need to use fraction!
1/2 is 0, due to integer division, so Math.pow(16, (1/2)
is Math.pow(16, 0)
, which is 1.0.
In order for 1/2 to be evaluated using floating point division, you have to cast either 1 or 2 to double :
System.out.println(Math.pow(16, ((double)1/2)));
Or use double literal in the first place :
System.out.println(Math.pow(16, (1.0/2)));
or
System.out.println(Math.pow(16, (1/2.0)));