У меня есть следующий, метод работы в Java:оператор Модульное дает неожиданный вывод в Java
/**
* Determines if n is a power of z
*
* @param z the number that n may be a power of
* @param n the number that may be a power of z
* @return true if n is a power of z
*/
public boolean isPowerOf(int z, int n) {
double output = Math.log(n)/Math.log(z);
if(output % 1 > 0) {
return false;
} else {
return true;
}
}
isPowerOf(3, 729); //returns true, because 3^6 = 729
отлично работает п могучим, но я попробовал это по-другому в первый раз:
public boolean isPowerOf(int z, int n) {
double output = Math.log(n) % Math.log(z);
if(output != 0) {
return false;
} else {
return true;
}
}
Однако, для log(729) % log(3)
, кажется, возвращает 1.0986122886681093
, а результат log(729)/log(3) is 6
.
Любой, кто может сказать мне, что приводит к тому, что оператор modulo все еще дает 1.09
остаток здесь?
Все, что мне нужно было знать, спасибо большое! –