(double x, RoundingMode mode)
| 53 | * rounding x according to the specified mode. |
| 54 | */ |
| 55 | @GwtIncompatible // #isMathematicalInteger, com.google.common.math.DoubleUtils |
| 56 | static double roundIntermediate(double x, RoundingMode mode) { |
| 57 | if (!isFinite(x)) { |
| 58 | throw new ArithmeticException("input is infinite or NaN"); |
| 59 | } |
| 60 | switch (mode) { |
| 61 | case UNNECESSARY: |
| 62 | checkRoundingUnnecessary(isMathematicalInteger(x)); |
| 63 | return x; |
| 64 | case FLOOR: |
| 65 | if (x >= 0.0 || isMathematicalInteger(x)) { |
| 66 | return x; |
| 67 | } else { |
| 68 | return x - 1.0; |
| 69 | } |
| 70 | case CEILING: |
| 71 | if (x <= 0.0 || isMathematicalInteger(x)) { |
| 72 | return x; |
| 73 | } else { |
| 74 | return x + 1.0; |
| 75 | } |
| 76 | case DOWN: |
| 77 | return x; |
| 78 | case UP: |
| 79 | if (isMathematicalInteger(x)) { |
| 80 | return x; |
| 81 | } else { |
| 82 | return x + Math.copySign(1.0, x); |
| 83 | } |
| 84 | case HALF_EVEN: |
| 85 | return rint(x); |
| 86 | case HALF_UP: |
| 87 | { |
| 88 | double z = rint(x); |
| 89 | if (abs(x - z) == 0.5) { |
| 90 | return x + copySign(0.5, x); |
| 91 | } else { |
| 92 | return z; |
| 93 | } |
| 94 | } |
| 95 | case HALF_DOWN: |
| 96 | { |
| 97 | double z = rint(x); |
| 98 | if (abs(x - z) == 0.5) { |
| 99 | return x; |
| 100 | } else { |
| 101 | return z; |
| 102 | } |
| 103 | } |
| 104 | default: |
| 105 | throw new AssertionError(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the {@code int} value that is equal to {@code x} rounded with the specified rounding |
no test coverage detected