Java Math. POW does not work properly

See English answer > integer division: how do you produce a double? 10

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 scores!

Solution

1 / 2 is 0. Due to integer division, math Pow (16, (1 / 2) is math Pow (16,0), i.e. 1.0

In order to evaluate 1 / 2 using floating-point division, you must convert 1 or 2 to double:

System.out.println(Math.pow(16,((double)1/2)));

Or use double text first:

System.out.println(Math.pow(16,(1.0/2)));

or

System.out.println(Math.pow(16,(1/2.0)));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>