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

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

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

Math.multiplyExact介绍

暂无

代码示例

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

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

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

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

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

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

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

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

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

public byte[] get()
  {
    byte[] buffer;
    if (bufferPool.isEmpty()) {
      currentSize = min(multiplyExact(currentSize, 2), maxChunkSize);
      buffer = new byte[currentSize];
    }
    else {
      buffer = bufferPool.remove(0);
      currentSize = buffer.length;
    }
    usedBuffers.add(buffer);
    return buffer;
  }
}

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

public byte[] get()
  {
    byte[] buffer;
    if (bufferPool.isEmpty()) {
      currentSize = min(multiplyExact(currentSize, 2), maxChunkSize);
      buffer = new byte[currentSize];
    }
    else {
      buffer = bufferPool.remove(0);
      currentSize = buffer.length;
    }
    usedBuffers.add(buffer);
    return buffer;
  }
}

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

public static int toMonths(int year, int months)
{
  try {
    return addExact(multiplyExact(year, 12), months);
  }
  catch (ArithmeticException e) {
    throw new IllegalArgumentException(e);
  }
}

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

public static int toMonths(int year, int months)
{
  try {
    return addExact(multiplyExact(year, 12), months);
  }
  catch (ArithmeticException e) {
    throw new IllegalArgumentException(e);
  }
}

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

private long calcAverageLengthInSeconds( long months, long days, long seconds )
{
  long daysInSeconds = Math.multiplyExact( days, SECONDS_PER_DAY );
  long monthsInSeconds = Math.multiplyExact( months, AVG_SECONDS_PER_MONTH );
  return Math.addExact( seconds, Math.addExact( daysInSeconds, monthsInSeconds ) );
}

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

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

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

public static long toMillis(long day, long hour, long minute, long second, long millis)
{
  try {
    long value = millis;
    value = addExact(value, multiplyExact(day, MILLIS_IN_DAY));
    value = addExact(value, multiplyExact(hour, MILLIS_IN_HOUR));
    value = addExact(value, multiplyExact(minute, MILLIS_IN_MINUTE));
    value = addExact(value, multiplyExact(second, MILLIS_IN_SECOND));
    return value;
  }
  catch (ArithmeticException e) {
    throw new IllegalArgumentException(e);
  }
}

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

public static long toMillis(long day, long hour, long minute, long second, long millis)
{
  try {
    long value = millis;
    value = addExact(value, multiplyExact(day, MILLIS_IN_DAY));
    value = addExact(value, multiplyExact(hour, MILLIS_IN_HOUR));
    value = addExact(value, multiplyExact(minute, MILLIS_IN_MINUTE));
    value = addExact(value, multiplyExact(second, MILLIS_IN_SECOND));
    return value;
  }
  catch (ArithmeticException e) {
    throw new IllegalArgumentException(e);
  }
}

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

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

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

@UsedByGeneratedCode
public static long tinyintToShortDecimal(long value, long precision, long scale, long tenToScale)
{
  try {
    long decimal = multiplyExact(value, tenToScale);
    if (overflows(decimal, intScale(precision))) {
      throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast TINYINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
    }
    return decimal;
  }
  catch (ArithmeticException e) {
    throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast TINYINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  }
}

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

@UsedByGeneratedCode
public static long bigintToShortDecimal(long value, long precision, long scale, long tenToScale)
{
  try {
    long decimal = multiplyExact(value, tenToScale);
    if (overflows(decimal, intScale(precision))) {
      throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast BIGINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
    }
    return decimal;
  }
  catch (ArithmeticException e) {
    throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast BIGINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  }
}

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

@UsedByGeneratedCode
public static long smallintToShortDecimal(long value, long precision, long scale, long tenToScale)
{
  try {
    long decimal = multiplyExact(value, tenToScale);
    if (overflows(decimal, intScale(precision))) {
      throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast SMALLINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
    }
    return decimal;
  }
  catch (ArithmeticException e) {
    throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast SMALLINT '%s' to DECIMAL(%s, %s)", value, precision, scale));
  }
}

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

@UsedByGeneratedCode
public static long integerToShortDecimal(long value, long precision, long scale, long tenToScale)
{
  try {
    long decimal = multiplyExact(value, tenToScale);
    if (overflows(decimal, intScale(precision))) {
      throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast INTEGER '%s' to DECIMAL(%s, %s)", value, precision, scale));
    }
    return decimal;
  }
  catch (ArithmeticException e) {
    throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast INTEGER '%s' to DECIMAL(%s, %s)", value, precision, scale));
  }
}

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

@ScalarOperator(MULTIPLY)
@SqlType(StandardTypes.BIGINT)
public static long multiply(@SqlType(StandardTypes.BIGINT) long left, @SqlType(StandardTypes.BIGINT) long right)
{
  try {
    return Math.multiplyExact(left, right);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("bigint multiplication overflow: %s * %s", left, right), e);
  }
}

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

@ScalarOperator(MULTIPLY)
@SqlType(StandardTypes.INTEGER)
public static long multiply(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)
{
  try {
    return Math.multiplyExact((int) left, (int) right);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("integer multiplication overflow: %s * %s", left, right), e);
  }
}

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

private List<RecordStresser> prepare( Condition condition, PagedFile pagedFile, RecordFormat format )
{
  int maxRecords = Math.multiplyExact( maxPages, format.getRecordsPerPage() );
  TinyLockManager locks = new TinyLockManager();
  List<RecordStresser> recordStressers = new LinkedList<>();
  for ( int threadId = 0; threadId < numberOfThreads; threadId++ )
  {
    recordStressers.add( new RecordStresser( pagedFile, condition, maxRecords, format, threadId, locks ) );
  }
  return recordStressers;
}

相关文章