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

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

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

Math.subtractExact介绍

暂无

代码示例

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

  1. public DurationValue sub( DurationValue that )
  2. {
  3. try
  4. {
  5. return duration(
  6. Math.subtractExact( this.months, that.months ),
  7. Math.subtractExact( this.days, that.days ),
  8. Math.subtractExact( this.seconds, that.seconds ),
  9. Math.subtractExact( this.nanos, that.nanos ) );
  10. }
  11. catch ( ArithmeticException e )
  12. {
  13. throw invalidDurationSubtract( this, that, e );
  14. }
  15. }

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

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

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

  1. /**
  2. * Overflow safe subtraction of two longs
  3. *
  4. * @param a left-hand operand
  5. * @param b right-hand operand
  6. * @return a - b
  7. */
  8. public static LongValue subtract( long a, long b )
  9. {
  10. return longValue( Math.subtractExact( a, b ) );
  11. }

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

  1. public static boolean willAdditionOverflow(int left, int right) {
  2. try {
  3. Math.addExact(left, right);
  4. return false;
  5. } catch (ArithmeticException e) {
  6. return true;
  7. }
  8. }
  9. public static boolean willSubtractionOverflow(int left, int right) {
  10. try {
  11. Math.subtractExact(left, right);
  12. return false;
  13. } catch (ArithmeticException e) {
  14. return true;
  15. }
  16. }

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

  1. @ScalarOperator(SUBTRACT)
  2. @SqlType(StandardTypes.INTEGER)
  3. public static long subtract(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)
  4. {
  5. try {
  6. return Math.subtractExact((int) left, (int) right);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("integer subtraction overflow: %s - %s", left, right), e);
  10. }
  11. }

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

  1. @ScalarOperator(SUBTRACT)
  2. @SqlType(StandardTypes.BIGINT)
  3. public static long subtract(@SqlType(StandardTypes.BIGINT) long left, @SqlType(StandardTypes.BIGINT) long right)
  4. {
  5. try {
  6. return Math.subtractExact(left, right);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("bigint subtraction overflow: %s - %s", left, right), e);
  10. }
  11. }

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

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

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

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

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

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

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

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

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

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

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

  1. /**
  2. * @param context
  3. * @return RubyInteger
  4. */
  5. public IRubyObject op_minus_one(ThreadContext context) {
  6. try {
  7. return newFixnum(context.runtime, Math.subtractExact(value, 1));
  8. } catch (ArithmeticException ae) {
  9. return subtractAsBignum(context, 1);
  10. }
  11. }

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

  1. private RubyInteger subtractFixnum(ThreadContext context, RubyFixnum other) {
  2. try {
  3. return newFixnum(context.runtime, Math.subtractExact(value, other.value));
  4. } catch (ArithmeticException ae) {
  5. return subtractAsBignum(context, other.value);
  6. }
  7. }

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

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

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

  1. @Override
  2. public IRubyObject op_minus(ThreadContext context, long otherValue) {
  3. try {
  4. return newFixnum(context.runtime, Math.subtractExact(value, otherValue));
  5. } catch (ArithmeticException ae) {
  6. return subtractAsBignum(context, otherValue);
  7. }
  8. }

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

  1. /**
  2. * @param context
  3. * @return RubyInteger
  4. */
  5. public IRubyObject op_minus_one(ThreadContext context) {
  6. try {
  7. return newFixnum(context.runtime, Math.subtractExact(value, 1));
  8. } catch (ArithmeticException ae) {
  9. return subtractAsBignum(context, 1);
  10. }
  11. }

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

  1. /**
  2. * @param context
  3. * @return RubyInteger
  4. */
  5. public IRubyObject op_minus_two(ThreadContext context) {
  6. try {
  7. return newFixnum(context.runtime, Math.subtractExact(value, 2));
  8. } catch (ArithmeticException ae) {
  9. return subtractAsBignum(context, 2);
  10. }
  11. }

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

  1. private RubyInteger subtractFixnum(ThreadContext context, RubyFixnum other) {
  2. try {
  3. return newFixnum(context.runtime, Math.subtractExact(value, other.value));
  4. } catch (ArithmeticException ae) {
  5. return subtractAsBignum(context, other.value);
  6. }
  7. }

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

  1. void checkForOverflow() {
  2. int sumMin = 0, sumMax = 0;
  3. for (int i = 0; i < list.length; i++) {
  4. int n1 = list[i].min();
  5. int n2 = list[i].max();
  6. sumMin = Math.addExact(sumMin, n1);
  7. sumMax = Math.addExact(sumMax, n2);
  8. }
  9. Math.subtractExact(sumMin, sum.max());
  10. Math.subtractExact(sumMax, sum.min());
  11. }

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

  1. @Override
  2. public FastMoney subtract(MonetaryAmount subtrahend) {
  3. checkAmountParameter(subtrahend);
  4. if (subtrahend.isZero()) {
  5. return this;
  6. }
  7. return new FastMoney(Math.subtractExact(this.number, getInternalNumber(subtrahend.getNumber(), false)),
  8. getCurrency());
  9. }

相关文章