Returns the base-10 logarithm of x, rounded according to the specified rounding mode. @throws IllegalArgumentException if x <= 0 @throws ArithmeticException if mode is RoundingMode#UNNECESSARY and x is not a power of ten
(int x, RoundingMode mode)
| 163 | */ |
| 164 | |
| 165 | @GwtIncompatible // need BigIntegerMath to adequately test |
| 166 | @SuppressWarnings("fallthrough") |
| 167 | public static int log10(int x, RoundingMode mode) { |
| 168 | checkPositive("x", x); |
| 169 | int logFloor = log10Floor(x); |
| 170 | int floorPow = powersOf10[logFloor]; |
| 171 | switch (mode) { |
| 172 | case UNNECESSARY: |
| 173 | checkRoundingUnnecessary(x == floorPow); |
| 174 | // fall through |
| 175 | case FLOOR: |
| 176 | case DOWN: |
| 177 | return logFloor; |
| 178 | case CEILING: |
| 179 | case UP: |
| 180 | return logFloor + lessThanBranchFree(floorPow, x); |
| 181 | case HALF_DOWN: |
| 182 | case HALF_UP: |
| 183 | case HALF_EVEN: |
| 184 | // sqrt(10) is irrational, so log10(x) - logFloor is never exactly 0.5 |
| 185 | return logFloor + lessThanBranchFree(halfPowersOf10[logFloor], x); |
| 186 | default: |
| 187 | throw new AssertionError(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private static int log10Floor(int x) { |
| 192 | /* |
nothing calls this directly
no test coverage detected