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

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

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

Math.addExact介绍

暂无

代码示例

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

  1. public void increment( long count )
  2. {
  3. this.count = Math.addExact( this.count, count );
  4. }

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

  1. private long secondsWithNanos( long seconds, long nanos )
  2. {
  3. return Math.addExact( seconds, nanos / NANOS_PER_SECOND );
  4. }

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

  1. private long offset( long position )
  2. {
  3. if ( position < 0 )
  4. {
  5. throw new IllegalArgumentException( "Position must be >= 0." );
  6. }
  7. return Math.addExact( position, offset );
  8. }

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

  1. static long addExact(long usedBytes, long bytes)
  2. {
  3. try {
  4. return Math.addExact(usedBytes, bytes);
  5. }
  6. catch (ArithmeticException e) {
  7. throw new RuntimeException(format("Overflow detected. usedBytes: %d, bytes: %d", usedBytes, bytes), e);
  8. }
  9. }
  10. }

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

  1. @Override
  2. public void addValue(long value)
  3. {
  4. nonNullValueCount++;
  5. minimum = Math.min(value, minimum);
  6. maximum = Math.max(value, maximum);
  7. if (!overflow) {
  8. try {
  9. sum = addExact(sum, value);
  10. }
  11. catch (ArithmeticException e) {
  12. overflow = true;
  13. }
  14. }
  15. }

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

  1. public static int toMonths(int year, int months)
  2. {
  3. try {
  4. return addExact(multiplyExact(year, 12), months);
  5. }
  6. catch (ArithmeticException e) {
  7. throw new IllegalArgumentException(e);
  8. }
  9. }

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

  1. public static int toMonths(int year, int months)
  2. {
  3. try {
  4. return addExact(multiplyExact(year, 12), months);
  5. }
  6. catch (ArithmeticException e) {
  7. throw new IllegalArgumentException(e);
  8. }
  9. }

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

  1. /**
  2. * Constructs a Deadline that is a given duration after now.
  3. */
  4. public static Deadline fromNow(Duration duration) {
  5. return new Deadline(Math.addExact(System.nanoTime(), duration.toNanos()));
  6. }
  7. }

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

  1. public static long toMillis(long day, long hour, long minute, long second, long millis)
  2. {
  3. try {
  4. long value = millis;
  5. value = addExact(value, multiplyExact(day, MILLIS_IN_DAY));
  6. value = addExact(value, multiplyExact(hour, MILLIS_IN_HOUR));
  7. value = addExact(value, multiplyExact(minute, MILLIS_IN_MINUTE));
  8. value = addExact(value, multiplyExact(second, MILLIS_IN_SECOND));
  9. return value;
  10. }
  11. catch (ArithmeticException e) {
  12. throw new IllegalArgumentException(e);
  13. }
  14. }

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

  1. public static long toMillis(long day, long hour, long minute, long second, long millis)
  2. {
  3. try {
  4. long value = millis;
  5. value = addExact(value, multiplyExact(day, MILLIS_IN_DAY));
  6. value = addExact(value, multiplyExact(hour, MILLIS_IN_HOUR));
  7. value = addExact(value, multiplyExact(minute, MILLIS_IN_MINUTE));
  8. value = addExact(value, multiplyExact(second, MILLIS_IN_SECOND));
  9. return value;
  10. }
  11. catch (ArithmeticException e) {
  12. throw new IllegalArgumentException(e);
  13. }
  14. }

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

  1. private long calcAverageLengthInSeconds( long months, long days, long seconds )
  2. {
  3. long daysInSeconds = Math.multiplyExact( days, SECONDS_PER_DAY );
  4. long monthsInSeconds = Math.multiplyExact( months, AVG_SECONDS_PER_MONTH );
  5. return Math.addExact( seconds, Math.addExact( daysInSeconds, monthsInSeconds ) );
  6. }

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

  1. /**
  2. * Overflow safe addition 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 add( long a, long b )
  9. {
  10. return longValue( Math.addExact( a, b ) );
  11. }

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

  1. public Deadline plus(Duration other) {
  2. return new Deadline(Math.addExact(timeNanos, other.toNanos()));
  3. }

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

  1. @UsedByGeneratedCode
  2. public static int checkedAdd(int x, int y)
  3. {
  4. try {
  5. return addExact(x, y);
  6. }
  7. catch (ArithmeticException e) {
  8. throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Concatenated string is too large");
  9. }
  10. }
  11. }

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

  1. public void add(Page page)
  2. {
  3. checkState(canAdd(page), "page buffer is full");
  4. pages.add(page);
  5. usedMemoryBytes += page.getRetainedSizeInBytes();
  6. rowCount = addExact(rowCount, page.getPositionCount());
  7. }

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

  1. public IndexSample combine( IndexSample sample1, IndexSample sample2 )
  2. {
  3. long indexSize = Math.addExact( sample1.indexSize(), sample2.indexSize() );
  4. long uniqueValues = Math.addExact( sample1.uniqueValues(), sample2.uniqueValues() );
  5. long sampleSize = Math.addExact( sample1.sampleSize(), sample2.sampleSize() );
  6. return new IndexSample( indexSize, uniqueValues, sampleSize );
  7. }
  8. }

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

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

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

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

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

  1. @SqlType(StandardTypes.INTEGER)
  2. public static long add(
  3. @SqlType(StandardTypes.INTEGER) long left,
  4. @SqlType(StandardTypes.INTEGER) long right)
  5. {
  6. return Math.addExact((int) left, (int) right);
  7. }

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

  1. @SqlType(StandardTypes.INTEGER)
  2. public static long addBlockPosition(
  3. @SqlType(StandardTypes.INTEGER) long first,
  4. @BlockPosition @SqlType(value = StandardTypes.INTEGER, nativeContainerType = long.class) Block block,
  5. @BlockIndex int position)
  6. {
  7. return Math.addExact((int) first, (int) INTEGER.getLong(block, position));
  8. }
  9. }

相关文章