java.lang.Math.subtractExact()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(205)

本文整理了Java中java.lang.Math.subtractExact()方法的一些代码示例,展示了Math.subtractExact()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Math.subtractExact()方法的具体详情如下:
包路径:java.lang.Math
类名称:Math
方法名:subtractExact

Math.subtractExact介绍

暂无

代码示例

代码示例来源:origin: neo4j/neo4j

public DurationValue sub( DurationValue that )
{
  try
  {
    return duration(
        Math.subtractExact( this.months, that.months ),
        Math.subtractExact( this.days, that.days ),
        Math.subtractExact( this.seconds, that.seconds ),
        Math.subtractExact( this.nanos, that.nanos ) );
  }
  catch ( ArithmeticException e )
  {
    throw invalidDurationSubtract( this, that, e );
  }
}

代码示例来源:origin: apache/flink

/**
 * Returns the time left between the deadline and now. The result is negative if the deadline
 * has passed.
 */
public Duration timeLeft() {
  return Duration.ofNanos(Math.subtractExact(timeNanos, System.nanoTime()));
}

代码示例来源:origin: neo4j/neo4j

/**
 * Overflow safe subtraction of two longs
 *
 * @param a left-hand operand
 * @param b right-hand operand
 * @return a - b
 */
public static LongValue subtract( long a, long b )
{
  return longValue( Math.subtractExact( a, b ) );
}

代码示例来源:origin: stackoverflow.com

public static boolean willAdditionOverflow(int left, int right) {
  try {
    Math.addExact(left, right);
    return false;
  } catch (ArithmeticException e) {
    return true;
  }
}

public static boolean willSubtractionOverflow(int left, int right) {
  try {
    Math.subtractExact(left, right);
    return false;
  } catch (ArithmeticException e) {
    return true;
  }
}

代码示例来源:origin: prestodb/presto

@ScalarOperator(SUBTRACT)
@SqlType(StandardTypes.INTEGER)
public static long subtract(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)
{
  try {
    return Math.subtractExact((int) left, (int) right);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("integer subtraction overflow: %s - %s", left, right), e);
  }
}

代码示例来源:origin: prestodb/presto

@ScalarOperator(SUBTRACT)
@SqlType(StandardTypes.BIGINT)
public static long subtract(@SqlType(StandardTypes.BIGINT) long left, @SqlType(StandardTypes.BIGINT) long right)
{
  try {
    return Math.subtractExact(left, right);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("bigint subtraction overflow: %s - %s", left, right), e);
  }
}

代码示例来源:origin: neo4j/neo4j

lhs instanceof Byte || rhs instanceof Byte )
return Math.subtractExact( ((Number) lhs).longValue(), ((Number) rhs).longValue() );

代码示例来源:origin: hazelcast/hazelcast-jet

/**
 * Subtracts the value of the supplied accumulator from this one,
 * throwing an exception in the case of integer overflow.
 */
public LongAccumulator subtract(LongAccumulator that) {
  this.value = Math.subtractExact(this.value, that.value);
  return this;
}

代码示例来源:origin: hazelcast/hazelcast-jet

/**
 * Subtracts the supplied value from this accumulator, throwing an
 * exception in the case of integer overflow.
 */
public LongAccumulator subtract(long value) {
  this.value = Math.subtractExact(this.value, value);
  return this;
}

代码示例来源:origin: org.apache.flink/flink-core

/**
 * Returns the time left between the deadline and now. The result is negative if the deadline
 * has passed.
 */
public Duration timeLeft() {
  return Duration.ofNanos(Math.subtractExact(timeNanos, System.nanoTime()));
}

代码示例来源:origin: jenetics/jenetics

/**
 * Return the <a href="http://en.wikipedia.org/wiki/Unit_in_the_last_place">ULP</a>
 * distance of the given two double values.
 *
 * @param a first double.
 * @param b second double.
 * @return the ULP distance.
 * @throws ArithmeticException if the distance doesn't fit in a long value.
 */
public static long ulpDistance(final double a, final double b) {
  return Math.subtractExact(ulpPosition(a), ulpPosition(b));
}

代码示例来源:origin: org.jruby/jruby-core

/**
 * @param context
 * @return RubyInteger
 */
public IRubyObject op_minus_one(ThreadContext context) {
  try {
    return newFixnum(context.runtime, Math.subtractExact(value, 1));
  } catch (ArithmeticException ae) {
    return subtractAsBignum(context, 1);
  }
}

代码示例来源:origin: org.jruby/jruby-core

private RubyInteger subtractFixnum(ThreadContext context, RubyFixnum other) {
  try {
    return newFixnum(context.runtime, Math.subtractExact(value, other.value));
  } catch (ArithmeticException ae) {
    return subtractAsBignum(context, other.value);
  }
}

代码示例来源:origin: org.ow2.authzforce/authzforce-ce-core-pdp-api

@Override
public MediumInteger subtract(final GenericInteger subtractedVal)
{
  final int result = Math.subtractExact(value, subtractedVal.intValueExact());
  return result == value ? this : MediumInteger.valueOf(result);
}

代码示例来源:origin: org.jruby/jruby-complete

@Override
public IRubyObject op_minus(ThreadContext context, long otherValue) {
  try {
    return newFixnum(context.runtime, Math.subtractExact(value, otherValue));
  } catch (ArithmeticException ae) {
    return subtractAsBignum(context, otherValue);
  }
}

代码示例来源:origin: org.jruby/jruby-complete

/**
 * @param context
 * @return RubyInteger
 */
public IRubyObject op_minus_one(ThreadContext context) {
  try {
    return newFixnum(context.runtime, Math.subtractExact(value, 1));
  } catch (ArithmeticException ae) {
    return subtractAsBignum(context, 1);
  }
}

代码示例来源:origin: org.jruby/jruby-complete

/**
 * @param context
 * @return RubyInteger
 */
public IRubyObject op_minus_two(ThreadContext context) {
  try {
    return newFixnum(context.runtime, Math.subtractExact(value, 2));
  } catch (ArithmeticException ae) {
    return subtractAsBignum(context, 2);
  }
}

代码示例来源:origin: org.jruby/jruby-complete

private RubyInteger subtractFixnum(ThreadContext context, RubyFixnum other) {
  try {
    return newFixnum(context.runtime, Math.subtractExact(value, other.value));
  } catch (ArithmeticException ae) {
    return subtractAsBignum(context, other.value);
  }
}

代码示例来源:origin: radsz/jacop

void checkForOverflow() {
  int sumMin = 0, sumMax = 0;
  for (int i = 0; i < list.length; i++) {
    int n1 = list[i].min();
    int n2 = list[i].max();
    sumMin = Math.addExact(sumMin, n1);
    sumMax = Math.addExact(sumMax, n2);
  }
  Math.subtractExact(sumMin, sum.max());
  Math.subtractExact(sumMax, sum.min());
}

代码示例来源:origin: org.javamoney.moneta/moneta-core

@Override
public FastMoney subtract(MonetaryAmount subtrahend) {
  checkAmountParameter(subtrahend);
  if (subtrahend.isZero()) {
    return this;
  }
  return new FastMoney(Math.subtractExact(this.number, getInternalNumber(subtrahend.getNumber(), false)),
      getCurrency());
}

相关文章