/** * 开方 */ public static long sqrt(long x, RoundingMode mode) { return LongMath.sqrt(x, mode); }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
@GwtIncompatible // TODO public void testConstantsSqrtMaxLong() { assertEquals( /*expected=*/ LongMath.sqrt(Long.MAX_VALUE, FLOOR), /*actual=*/ LongMath.FLOOR_SQRT_MAX_LONG); }
@GwtIncompatible // TODO public void testSqrtNegativeAlwaysThrows() { for (long x : NEGATIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { LongMath.sqrt(x, mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } } }
@GwtIncompatible // TODO public void testSqrtMatchesBigInteger() { for (long x : POSITIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // Promote the long value (rather than using longValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. assertEquals(BigIntegerMath.sqrt(valueOf(x), mode), valueOf(LongMath.sqrt(x, mode))); } } }
@GwtIncompatible // TODO public void testSqrtExactMatchesFloorOrThrows() { for (long x : POSITIVE_LONG_CANDIDATES) { long sqrtFloor = LongMath.sqrt(x, FLOOR); // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = (sqrtFloor * sqrtFloor == x); try { assertEquals(sqrtFloor, LongMath.sqrt(x, UNNECESSARY)); assertTrue(isPerfectSquare); } catch (ArithmeticException e) { assertFalse(isPerfectSquare); } } }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
/** * 开方 */ public static long sqrt(long x, RoundingMode mode) { return LongMath.sqrt(x, mode); }
/** * 开方 */ public static long sqrt(long x, RoundingMode mode) { return LongMath.sqrt(x, mode); }
/** * 开方 */ public static long sqrt(long x, RoundingMode mode) { return LongMath.sqrt(x, mode); }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
@Benchmark int sqrt(int reps) { int tmp = 0; for (int i = 0; i < reps; i++) { int j = i & ARRAY_MASK; tmp += LongMath.sqrt(positive[j], mode); } return tmp; }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
@GwtIncompatible // TODO public void testSqrtNegativeAlwaysThrows() { for (long x : NEGATIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { LongMath.sqrt(x, mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) {} } } }
@GwtIncompatible // TODO public void testSqrtMatchesBigInteger() { for (long x : POSITIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // Promote the long value (rather than using longValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. assertEquals(BigIntegerMath.sqrt(valueOf(x), mode), valueOf(LongMath.sqrt(x, mode))); } } }
@GwtIncompatible // TODO public void testConstantsSqrtMaxLong() { assertEquals( /*expected=*/ LongMath.sqrt(Long.MAX_VALUE, FLOOR), /*actual=*/ LongMath.FLOOR_SQRT_MAX_LONG); }
@GwtIncompatible // TODO public void testSqrtExactMatchesFloorOrThrows() { for (long x : POSITIVE_LONG_CANDIDATES) { long sqrtFloor = LongMath.sqrt(x, FLOOR); // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = (sqrtFloor * sqrtFloor == x); try { assertEquals(sqrtFloor, LongMath.sqrt(x, UNNECESSARY)); assertTrue(isPerfectSquare); } catch (ArithmeticException e) { assertFalse(isPerfectSquare); } } }