long aLong = ...; int anInt = new BigDecimal(aLong).intValueExact(); // throws ArithmeticException // if outside bounds
public static int intValue(BigDecimal decimal) { if (decimal == null) { return 0; } int scale = decimal.scale(); if (scale >= -100 && scale <= 100) { return decimal.intValue(); } return decimal.intValueExact(); }
private Integer convertImpl(Object input) { if (input instanceof Integer) { return (Integer) input; } else if (isNumberIsh(input)) { BigDecimal value; try { value = new BigDecimal(input.toString()); } catch (NumberFormatException e) { return null; } try { return value.intValueExact(); } catch (ArithmeticException e) { return null; } } else { return null; } }
private Integer getInteger(SqlNode n) { return n == null ? null : ((BigDecimal) SqlLiteral.value(n)).intValueExact(); }
@Override protected HiveIntervalDayTime getIntervalDayTime(String arg) { BigDecimal bd = new BigDecimal(arg); BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger()); BigDecimal bdNanos = bd.subtract(bdSeconds); return new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue()); } }
@Override public int intValueExact() { return bigDecimalValue().intValueExact(); }
@Override protected HiveIntervalDayTime getIntervalDayTime(String arg) { BigDecimal bd = new BigDecimal(arg); BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger()); BigDecimal bdNanos = bd.subtract(bdSeconds); return new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue()); } }
int signOf2 = n2.signum(); try { // Perform X^(A+B)=X^A*X^B (B = remainder) double dn1 = n1.doubleValue(); // Compare the same row of digits according to context if (!CalculatorUtils.isEqual(n1, dn1)) throw new Exception(); // Cannot convert n1 to double n2 = n2.multiply(new BigDecimal(signOf2)); // n2 is now positive BigDecimal remainderOf2 = n2.remainder(BigDecimal.ONE); BigDecimal n2IntPart = n2.subtract(remainderOf2); // Calculate big part of the power using context - // bigger range and performance but lower accuracy BigDecimal intPow = n1.pow(n2IntPart.intValueExact(), CalculatorConstants.DEFAULT_CONTEXT); BigDecimal doublePow = new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue())); result = intPow.multiply(doublePow); } catch (Exception e) { if (e instanceof CalculatorException) throw (CalculatorException) e; throw new CalculatorException( CalculatorConstants.Errors.UNSUPPORTED_NUMBER_ + "power!"); } // Fix negative power if (signOf2 == -1) result = BigDecimal.ONE.divide(result, CalculatorConstants.BIG_SCALE, RoundingMode.HALF_UP);
public BigDecimal eval(Number vv1, Number vv2) { BigDecimal v1 = (BigDecimal) vv1; BigDecimal v2 = (BigDecimal) vv2; assertNotNull(v1, v2); /*- * Thanks to Gene Marin: * http://stackoverflow.com/questions/3579779/how-to-do-a-fractional-power-on-bigdecimal-in-java */ int signOf2 = ((BigDecimal) v2).signum(); double dn1 = v1.doubleValue(); v2 = v2.multiply(new BigDecimal(signOf2)); // n2 is now positive BigDecimal remainderOf2 = v2.remainder(BigDecimal.ONE); BigDecimal n2IntPart = v2.subtract(remainderOf2); BigDecimal intPow = v1.pow(n2IntPart.intValueExact(), mc); BigDecimal doublePow = new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue())); BigDecimal result = intPow.multiply(doublePow, mc); if (signOf2 == -1) { result = BigDecimal.ONE.divide(result, mc.getPrecision(), RoundingMode.HALF_UP); } return result; } });
@Override public int nextInt() throws IOException { Object peeked = require(Object.class, Token.NUMBER); int result; if (peeked instanceof Number) { result = ((Number) peeked).intValue(); } else if (peeked instanceof String) { try { result = Integer.parseInt((String) peeked); } catch (NumberFormatException e) { try { BigDecimal asDecimal = new BigDecimal((String) peeked); result = asDecimal.intValueExact(); } catch (NumberFormatException e2) { throw typeMismatch(peeked, Token.NUMBER); } } } else { throw typeMismatch(peeked, Token.NUMBER); } remove(); return result; }
try { return bigDec.intValueExact(); } catch(ArithmeticException e) {} return bigDec.longValue();
lineNumber = 0; } else if (value instanceof BigDecimal) { lineNumber = ((BigDecimal) value).intValueExact(); } else{ lineNumber = value.intValue();
@Override public int nextInt() throws IOException { switch (delegate.getCurrentBsonType()) { case DOUBLE: return (int) delegate.readDouble(); case INT64: return (int) delegate.readInt64(); case DECIMAL128: return delegate.readDecimal128().bigDecimalValue().intValueExact(); default: return delegate.readInt32(); } }
BigDecimal bdNanos = bd.subtract(bdSeconds); return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo, new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue())); default:
BigDecimal bdNanos = bd.subtract(bdSeconds); return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo, new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue())); default:
@Override public int intValueExact() { return bigDecimalValue().intValueExact(); }
throw new IllegalStateException ("Max session-timeout in minutes is "+org.eclipse.jetty.server.session.SessionHandler.MAX_INACTIVE_MINUTES); context.getSessionHandler().setMaxInactiveInterval(asDecimal.intValueExact() * 60);
bd = (BigDecimal) value; try { bd.intValueExact(); return true; } catch (ArithmeticException e) {
@Override public Integer toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder, Integer maxLength, Integer scale) { if (l == 0) { return null; } if (equalsAny(actualType, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE, PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE, PUnsignedTinyint.INSTANCE, PFloat.INSTANCE, PUnsignedFloat.INSTANCE, PDouble.INSTANCE, PUnsignedDouble.INSTANCE)) { return actualType.getCodec().decodeInt(b, o, sortOrder); } else if (actualType == PDecimal.INSTANCE) { BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder); return bd.intValueExact(); } throwConstraintViolationException(actualType, this); return null; }
public static ViewportProfile getViewportProfile(RequestEnvelope requestEnvelope) { ViewportState viewportState = requestEnvelope.getContext().getViewport(); Shape shape = viewportState.getShape(); int currentPixelWidth = viewportState.getCurrentPixelWidth().intValueExact(); int currentPixelHeight = viewportState.getCurrentPixelHeight().intValueExact(); int dpi = viewportState.getDpi().intValueExact(); Orientation orientation = getOrientation(currentPixelHeight, currentPixelWidth);