/** * 开方 */ public static int sqrt(int x, RoundingMode mode) { return IntMath.sqrt(x, mode); }
@GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testSqrtMatchesBigInteger() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // The BigInteger implementation is tested separately, use it as the reference. // Promote the int value (rather than using intValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. assertEquals(BigIntegerMath.sqrt(valueOf(x), mode), valueOf(IntMath.sqrt(x, mode))); } } }
@GwtIncompatible // sqrt public void testPowersSqrtMaxInt() { assertEquals( /*expected=*/ IntMath.sqrt(Integer.MAX_VALUE, FLOOR), /*actual=*/ IntMath.FLOOR_SQRT_MAX_INT); }
@GwtIncompatible // sqrt public void testSqrtZeroAlwaysZero() { for (RoundingMode mode : ALL_ROUNDING_MODES) { assertEquals(0, IntMath.sqrt(0, mode)); } }
@GwtIncompatible // sqrt public void testSqrtExactMatchesFloorOrThrows() { for (int x : POSITIVE_INTEGER_CANDIDATES) { int floor = IntMath.sqrt(x, FLOOR); // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = (floor * floor == x); try { assertEquals(floor, IntMath.sqrt(x, UNNECESSARY)); assertTrue(isPerfectSquare); } catch (ArithmeticException e) { assertFalse(isPerfectSquare); } } }
checkNonNegative("x", x); if (fitsInInt(x)) { return IntMath.sqrt((int) x, mode);
@GwtIncompatible // sqrt public void testSqrtNegativeAlwaysThrows() { for (int x : NEGATIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : RoundingMode.values()) { try { IntMath.sqrt(x, mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } } }
checkNonNegative("x", x); if (fitsInInt(x)) { return IntMath.sqrt((int) x, mode);
checkNonNegative("x", x); if (fitsInInt(x)) { return IntMath.sqrt((int) x, mode);
/** * 开方 */ public static int sqrt(int x, RoundingMode mode) { return IntMath.sqrt(x, mode); }
/** * 开方 */ public static int sqrt(int x, RoundingMode mode) { return IntMath.sqrt(x, mode); }
/** * 开方 */ public static int sqrt(int x, RoundingMode mode) { return IntMath.sqrt(x, mode); }
/** * Returns the integer square root of this integer. * * @return <code>k<code> such as <code>k^2 <= this < (k + 1)^2</code>. If this integer is negative or it's impossible to find a * square root return <code>F.Sqrt(this)</code>. */ public IExpr sqrt() { try { return valueOf(IntMath.sqrt(fIntValue, RoundingMode.UNNECESSARY)); } catch (RuntimeException ex) { return F.Sqrt(this); } }
@Benchmark int sqrt(int reps) { int tmp = 0; for (int i = 0; i < reps; i++) { int j = i & ARRAY_MASK; tmp += IntMath.sqrt(positive[j], mode); } return tmp; }
@GwtIncompatible // sqrt public void testSqrtZeroAlwaysZero() { for (RoundingMode mode : ALL_ROUNDING_MODES) { assertEquals(0, IntMath.sqrt(0, mode)); } }
@GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testSqrtMatchesBigInteger() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // The BigInteger implementation is tested separately, use it as the reference. // Promote the int value (rather than using intValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. assertEquals(BigIntegerMath.sqrt(valueOf(x), mode), valueOf(IntMath.sqrt(x, mode))); } } }
@GwtIncompatible // sqrt public void testPowersSqrtMaxInt() { assertEquals( /*expected=*/ IntMath.sqrt(Integer.MAX_VALUE, FLOOR), /*actual=*/ IntMath.FLOOR_SQRT_MAX_INT); }
checkNonNegative("x", x); if (fitsInInt(x)) { return IntMath.sqrt((int) x, mode);
@GwtIncompatible // sqrt public void testSqrtExactMatchesFloorOrThrows() { for (int x : POSITIVE_INTEGER_CANDIDATES) { int floor = IntMath.sqrt(x, FLOOR); // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = (floor * floor == x); try { assertEquals(floor, IntMath.sqrt(x, UNNECESSARY)); assertTrue(isPerfectSquare); } catch (ArithmeticException e) { assertFalse(isPerfectSquare); } } }
@GwtIncompatible // sqrt public void testSqrtNegativeAlwaysThrows() { for (int x : NEGATIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : RoundingMode.values()) { try { IntMath.sqrt(x, mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) {} } } }