java 计算自定义底数的对数

To calculate a logarithm with custom base in Java, we use the following identity:

 

1

2

3

4

5

6

7

8

9

@Test

public void givenCustomLog_shouldReturnValidResults() {

    assertEquals(customLog(2, 256), 8);

    assertEquals(customLog(10, 100), 2);

}

 

private static double customLog(double base, double logNumber) {

    return Math.log(logNumber) / Math.log(base);

}

你可能感兴趣的:(java,java)