private static long bigIntegerDecimalToGenericIntegerType(BigInteger bigInteger, int sourceScale, long minValue, long maxValue) { BigDecimal bigDecimal = new BigDecimal(bigInteger, sourceScale); BigInteger unscaledValue = bigDecimal.setScale(0, FLOOR).unscaledValue(); if (unscaledValue.compareTo(BigInteger.valueOf(maxValue)) > 0) { return maxValue; } if (unscaledValue.compareTo(BigInteger.valueOf(minValue)) < 0) { return minValue; } return unscaledValue.longValueExact(); }
public static long encodeShortScaledValue(BigDecimal value, int scale) { checkArgument(scale >= 0); return value.setScale(scale, UNNECESSARY).unscaledValue().longValueExact(); }
public static long encodeShortScaledValue(BigDecimal value, int scale) { checkArgument(scale >= 0); return value.setScale(scale, UNNECESSARY).unscaledValue().longValueExact(); }
public void setV(Object v) { if (v instanceof String) { this.v = Numeric.toBigInt((String) v).longValueExact(); } else if (v instanceof Integer) { this.v = ((Integer) v).longValue(); } else { this.v = (Long) v; } }
@UsedByGeneratedCode public static long shortDecimalToShortDecimal(long value, int sourcePrecision, int sourceScale, int resultPrecision, int resultScale) { return bigintToBigintFloorSaturatedCast(BigInteger.valueOf(value), sourceScale, resultPrecision, resultScale).longValueExact(); }
@UsedByGeneratedCode public static long genericIntegerTypeToShortDecimal(long value, int resultPrecision, int resultScale) { return bigDecimalToBigintFloorSaturatedCast(BigDecimal.valueOf(value), resultPrecision, resultScale).longValueExact(); }
private static Long shortDecimal(String value) { return new BigDecimal(value).unscaledValue().longValueExact(); }
public static Long shortDecimal(String value) { return new BigDecimal(value).unscaledValue().longValueExact(); }
@UsedByGeneratedCode public static long longDecimalToShortDecimal(Slice value, int sourcePrecision, int sourceScale, int resultPrecision, int resultScale) { return bigintToBigintFloorSaturatedCast(decodeUnscaledValue(value), sourceScale, resultPrecision, resultScale).longValueExact(); }
public static void outputShortDecimal(DecimalType type, LongDecimalWithOverflowAndLongState state, BlockBuilder out) { if (state.getLong() == 0) { out.appendNull(); } else { writeShortDecimal(out, average(state, type).unscaledValue().longValueExact()); } }
public static Long calculateLiteralValue( String calculation, Map<String, Long> inputs) { try { ParserRuleContext tree = parseTypeCalculation(calculation); CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs); BigInteger result = visitor.visit(tree); return result.longValueExact(); } catch (StackOverflowError e) { throw new ParsingException("Type calculation is too large (stack overflow while parsing)"); } }
private void assertAddReturnOverflow(BigInteger left, BigInteger right) { Slice result = unscaledDecimal(); long overflow = addWithOverflow(unscaledDecimal(left), unscaledDecimal(right), result); BigInteger actual = unscaledDecimalToBigInteger(result); BigInteger expected = left.add(right).remainder(TWO.pow(UnscaledDecimal128Arithmetic.UNSCALED_DECIMAL_128_SLICE_LENGTH * 8 - 1)); BigInteger expectedOverflow = left.add(right).divide(TWO.pow(UnscaledDecimal128Arithmetic.UNSCALED_DECIMAL_128_SLICE_LENGTH * 8 - 1)); assertEquals(actual, expected); assertEquals(overflow, expectedOverflow.longValueExact()); }
boolean isSynced() throws Exception { EthSyncing ethSyncing = web3j.ethSyncing().send(); if (ethSyncing.isSyncing()) { return false; } else { EthBlock ethBlock = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send(); long timestamp = ethBlock.getBlock().getTimestamp().longValueExact() * 1000; return System.currentTimeMillis() - syncThreshold < timestamp; } }
} else { try { rec.put(i - 1, ((BigInteger) value).longValueExact()); } catch (ArithmeticException ae) {
/** Return the long that {@code n} stores, or throws an exception if the * stored value cannot be converted to a long that stores the exact same * value. */ public static long toLongExact(Number n) { if (n instanceof Byte || n instanceof Short || n instanceof Integer || n instanceof Long) { return n.longValue(); } else if (n instanceof Float || n instanceof Double) { double d = n.doubleValue(); if (d != Math.round(d)) { throw new IllegalArgumentException(n + " is not an integer value"); } return n.longValue(); } else if (n instanceof BigDecimal) { return ((BigDecimal) n).toBigIntegerExact().longValueExact(); } else if (n instanceof BigInteger) { return ((BigInteger) n).longValueExact(); } else { throw new IllegalArgumentException("Cannot check whether [" + n + "] of class [" + n.getClass().getName() + "] is actually a long"); } }
public static long encodeShortScaledValue(BigDecimal value, int scale) { checkArgument(scale >= 0); return value.setScale(scale, UNNECESSARY).unscaledValue().longValueExact(); }
protected static long count(int[] dimensions) { BigInteger bigCount = BigIntegerCartesianProduct.count(dimensions); if(bigCount.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) >= 0) throw new IllegalArgumentException("Dimensions too big: " + Arrays.toString(dimensions)); return bigCount.longValueExact(); } }
private long getRawQuantity(final Quantity quantity) { return BigInteger.valueOf(this.amount.getNumMicroNem()) .multiply(BigInteger.valueOf(quantity.getRaw())) .divide(BigInteger.valueOf(Amount.MICRONEMS_IN_NEM)) .longValueExact(); }
@UsedByGeneratedCode public static long genericIntegerTypeToShortDecimal(long value, int resultPrecision, int resultScale) { return bigDecimalToBigintFloorSaturatedCast(BigDecimal.valueOf(value), resultPrecision, resultScale).longValueExact(); }
@Test public void testUnsignedLongToBigInt() { Assert.assertEquals("The long round-trips as expected.", Long.MAX_VALUE, DataType.unsignedLongToBigInt(Long.MAX_VALUE).longValueExact()); BigInteger overflowAsBigInt = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE); Assert.assertEquals(new BigInteger("9223372036854775808"), overflowAsBigInt); long overflowAsLong = overflowAsBigInt.longValue(); Assert.assertEquals(-9223372036854775808L, overflowAsLong); Assert.assertEquals("Interpret signed long as unsigned long", overflowAsBigInt, DataType.unsignedLongToBigInt(overflowAsLong)); }