Why does Math.pow (long, (1/3)) always return 1?

If the problem is in power, how to write it down?

+6
java math
source share
2 answers

Try Math.pow(long, 1D/3) .

By default, numeric literals in Java are considered int. Thus, 1/3 is converted to 0, not 0.33333, as it should be. Qualifying it with 1D or 1F or 1.0 will solve the problem.

+14
source share

This is due to the fact that 1/3 is a whole division and is evaluated as 0 . You effectively increase to 0 power, which always gives 1. Instead, try 1.0/3.0 .

+27
source share

All Articles