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

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

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

Math.multiplyExact介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Obtain a {@link DataSize} representing the specified number of kilobytes.
  3. * @param kilobytes the number of kilobytes, positive or negative
  4. * @return a {@link DataSize}
  5. */
  6. public static DataSize ofKilobytes(long kilobytes) {
  7. return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB));
  8. }

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Obtain a {@link DataSize} representing the specified number of megabytes.
  3. * @param megabytes the number of megabytes, positive or negative
  4. * @return a {@link DataSize}
  5. */
  6. public static DataSize ofMegabytes(long megabytes) {
  7. return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
  8. }

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Obtain a {@link DataSize} representing the specified number of gigabytes.
  3. * @param gigabytes the number of gigabytes, positive or negative
  4. * @return a {@link DataSize}
  5. */
  6. public static DataSize ofGigabytes(long gigabytes) {
  7. return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB));
  8. }

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Obtain a {@link DataSize} representing the specified number of terabytes.
  3. * @param terabytes the number of terabytes, positive or negative
  4. * @return a {@link DataSize}
  5. */
  6. public static DataSize ofTerabytes(long terabytes) {
  7. return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB));
  8. }

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

  1. public byte[] get()
  2. {
  3. byte[] buffer;
  4. if (bufferPool.isEmpty()) {
  5. currentSize = min(multiplyExact(currentSize, 2), maxChunkSize);
  6. buffer = new byte[currentSize];
  7. }
  8. else {
  9. buffer = bufferPool.remove(0);
  10. currentSize = buffer.length;
  11. }
  12. usedBuffers.add(buffer);
  13. return buffer;
  14. }
  15. }

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

  1. public byte[] get()
  2. {
  3. byte[] buffer;
  4. if (bufferPool.isEmpty()) {
  5. currentSize = min(multiplyExact(currentSize, 2), maxChunkSize);
  6. buffer = new byte[currentSize];
  7. }
  8. else {
  9. buffer = bufferPool.remove(0);
  10. currentSize = buffer.length;
  11. }
  12. usedBuffers.add(buffer);
  13. return buffer;
  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: 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 multiplication 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 multiply( long a, long b )
  9. {
  10. return longValue( Math.multiplyExact( a, b ) );
  11. }

代码示例来源: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: spring-projects/spring-framework

  1. /**
  2. * Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}.
  3. * @param amount the amount of the size, measured in terms of the unit,
  4. * positive or negative
  5. * @return a corresponding {@link DataSize}
  6. */
  7. public static DataSize of(long amount, DataUnit unit) {
  8. Assert.notNull(unit, "Unit must not be null");
  9. return new DataSize(Math.multiplyExact(amount, unit.size().toBytes()));
  10. }

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

  1. @UsedByGeneratedCode
  2. public static long tinyintToShortDecimal(long value, long precision, long scale, long tenToScale)
  3. {
  4. try {
  5. long decimal = multiplyExact(value, tenToScale);
  6. if (overflows(decimal, intScale(precision))) {
  7. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast TINYINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  8. }
  9. return decimal;
  10. }
  11. catch (ArithmeticException e) {
  12. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast TINYINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  13. }
  14. }

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

  1. @UsedByGeneratedCode
  2. public static long bigintToShortDecimal(long value, long precision, long scale, long tenToScale)
  3. {
  4. try {
  5. long decimal = multiplyExact(value, tenToScale);
  6. if (overflows(decimal, intScale(precision))) {
  7. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast BIGINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  8. }
  9. return decimal;
  10. }
  11. catch (ArithmeticException e) {
  12. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast BIGINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  13. }
  14. }

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

  1. @UsedByGeneratedCode
  2. public static long smallintToShortDecimal(long value, long precision, long scale, long tenToScale)
  3. {
  4. try {
  5. long decimal = multiplyExact(value, tenToScale);
  6. if (overflows(decimal, intScale(precision))) {
  7. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast SMALLINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  8. }
  9. return decimal;
  10. }
  11. catch (ArithmeticException e) {
  12. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast SMALLINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  13. }
  14. }

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

  1. @UsedByGeneratedCode
  2. public static long integerToShortDecimal(long value, long precision, long scale, long tenToScale)
  3. {
  4. try {
  5. long decimal = multiplyExact(value, tenToScale);
  6. if (overflows(decimal, intScale(precision))) {
  7. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast INTEGER '%s' to DECIMAL(%s, %s)", value, precision, scale));
  8. }
  9. return decimal;
  10. }
  11. catch (ArithmeticException e) {
  12. throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast INTEGER '%s' to DECIMAL(%s, %s)", value, precision, scale));
  13. }
  14. }

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

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

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

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

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

  1. private List<RecordStresser> prepare( Condition condition, PagedFile pagedFile, RecordFormat format )
  2. {
  3. int maxRecords = Math.multiplyExact( maxPages, format.getRecordsPerPage() );
  4. TinyLockManager locks = new TinyLockManager();
  5. List<RecordStresser> recordStressers = new LinkedList<>();
  6. for ( int threadId = 0; threadId < numberOfThreads; threadId++ )
  7. {
  8. recordStressers.add( new RecordStresser( pagedFile, condition, maxRecords, format, threadId, locks ) );
  9. }
  10. return recordStressers;
  11. }

相关文章