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

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

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

Math.negateExact介绍

暂无

代码示例

代码示例来源:origin: biezhi/30-seconds-of-java8

public static String mask(String input, int num, String mask) {
  int length = input.length();
  return num > 0
      ?
      input.substring(0, length - num).replaceAll(".", mask)
          + input.substring(length - num)
      :
      input.substring(0, Math.negateExact(num))
          + input.substring(Math.negateExact(num), length).replaceAll(".", mask);
}

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

/**
 * Creates a new {@link LRemCommand} to last {@literal count} values.
 *
 * @return a new {@link LRemCommand} to delete last {@literal count} values.
 */
public static LRemCommand last(long count) {
  Long value = count < 0L ? count : Math.negateExact(count);
  return new LRemCommand(null, value, null);
}

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

@ScalarOperator(NEGATION)
@SqlType(StandardTypes.INTEGER)
public static long negate(@SqlType(StandardTypes.INTEGER) long value)
{
  try {
    return Math.negateExact((int) value);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "integer negation overflow: " + value, e);
  }
}

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

@ScalarOperator(NEGATION)
@SqlType(StandardTypes.BIGINT)
public static long negate(@SqlType(StandardTypes.BIGINT) long value)
{
  try {
    return Math.negateExact(value);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "bigint negation overflow: " + value, e);
  }
}

代码示例来源:origin: shekhargulati/30-seconds-of-java

public static String mask(String input, int num, String mask) {
  int length = input.length();
  return num > 0
      ?
      input.substring(0, length - num).replaceAll(".", mask)
          + input.substring(length - num)
      :
      input.substring(0, Math.negateExact(num))
          + input.substring(Math.negateExact(num), length).replaceAll(".", mask);
}

代码示例来源:origin: cc.redberry/rings

/**
 * Delegates to {@link Math#negateExact(long)}
 *
 * @throws ArithmeticException if the result overflows a long
 **/
public static long safeNegate(long x) {
  return Math.negateExact(x);
}

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

/**
 * Returns the negative value of this fraction.
 * This method does not simplify the fraction.
 *
 * @return the result of {@code -this}.
 * @throws ArithmeticException if the result overflows.
 */
public Fraction negate() {
  int n = numerator;
  int d = denominator;
  if (n != 0) {
    n = Math.negateExact(n);
  } else if (d != 0) {
    d = Math.negateExact(d);
  } else {
    return this;
  }
  return new Fraction(n, d);
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Creates a new {@link LRemCommand} to last {@literal count} values.
 *
 * @return a new {@link LRemCommand} to delete last {@literal count} values.
 */
public static LRemCommand last(long count) {
  Long value = count < 0L ? count : Math.negateExact(count);
  return new LRemCommand(null, value, null);
}

代码示例来源:origin: org.springframework.data/spring-data-redis

/**
 * Creates a new {@link LRemCommand} to last {@literal count} values.
 *
 * @return a new {@link LRemCommand} to delete last {@literal count} values.
 */
public static LRemCommand last(long count) {
  Long value = count < 0L ? count : Math.negateExact(count);
  return new LRemCommand(null, value, null);
}

代码示例来源:origin: the8472/mldht

public static Runnable onceMore(Runnable loopBody) {
  AtomicInteger lock = new AtomicInteger();
  
  return () -> {
    // request execution of the runnable
    int current = lock.incrementAndGet();
    
    // another thread is executing
    if(current > 1)
      return;
    
    try {
      do {
        loopBody.run();
        
        current = lock.addAndGet(Math.negateExact(current));
      } while(current > 0);
    } catch(Throwable t) {
      lock.set(0);
      throw t;
    }
  };
}

代码示例来源:origin: net.time4j/time4j-core

/**
 * <p>Yields the absolute value of the represented calendar days. </p>
 *
 * @return  non-negative count of calendar days
 * @since   3.4/4.3
 */
/*[deutsch]
 * <p>Liefert den absoluten Betrag der repr&auml;sentierten Kalendertage. </p>
 *
 * @return  non-negative count of calendar days
 * @since   3.4/4.3
 */
public CalendarDays abs() {
  return ((this.days < 0) ? CalendarDays.of(Math.negateExact(this.days)) : this);
}

代码示例来源:origin: semuxproject/semux-core

public static Amount neg(Amount a) {
  return new Amount(Math.negateExact(a.nano));
}

代码示例来源:origin: net.time4j/time4j-core

@Override
public long get(TemporalUnit unit) {
  for (Map.Entry<IsoUnit, TemporalUnit> entry : MAP.entrySet()) {
    if (entry.getValue().equals(unit)) {
      long amount = this.duration.getPartialAmount(entry.getKey());
      if (this.duration.isNegative()) {
        amount = Math.negateExact(amount);
      }
      return amount;
    }
  }
  if (unit.equals(ChronoUnit.HALF_DAYS)) {
    long hd = Math.floorDiv(this.duration.getPartialAmount(ClockUnit.HOURS), 12);
    if (this.duration.isNegative()) {
      hd = Math.negateExact(hd);
    }
    return hd;
  }
  throw new UnsupportedTemporalTypeException(unit.toString()); // throws NPE if unit is null
}

代码示例来源:origin: org.ballerinalang/ballerina-math

public void execute(Context ctx) {
    long value = ctx.getIntArgument(0);
    ctx.setReturnValues(new BInteger(Math.negateExact(value)));
  }
}

代码示例来源:origin: io.prestosql/presto-main

@ScalarOperator(NEGATION)
@SqlType(StandardTypes.BIGINT)
public static long negate(@SqlType(StandardTypes.BIGINT) long value)
{
  try {
    return Math.negateExact(value);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "bigint negation overflow: " + value, e);
  }
}

代码示例来源:origin: io.prestosql/presto-main

@ScalarOperator(NEGATION)
@SqlType(StandardTypes.INTEGER)
public static long negate(@SqlType(StandardTypes.INTEGER) long value)
{
  try {
    return Math.negateExact((int) value);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "integer negation overflow: " + value, e);
  }
}

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

@ScalarOperator(NEGATION)
@SqlType(StandardTypes.INTEGER)
public static long negate(@SqlType(StandardTypes.INTEGER) long value)
{
  try {
    return Math.negateExact((int) value);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "integer negation overflow: " + value, e);
  }
}

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

@ScalarOperator(NEGATION)
@SqlType(StandardTypes.BIGINT)
public static long negate(@SqlType(StandardTypes.BIGINT) long value)
{
  try {
    return Math.negateExact(value);
  }
  catch (ArithmeticException e) {
    throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "bigint negation overflow: " + value, e);
  }
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
 public void testAutoCommit() throws SQLException {
  LOGGER.info("Test Auto Commit");
  long cents = 70;
  long fromAccountId = 1;
  long toAccountId = 2;
  DataSource dataSource = newDataSource();
  try(Connection connection = dataSource.getConnection();
    PreparedStatement transferStatement = connection.prepareStatement(
      "UPDATE account SET balance = ? WHERE id = ?"
    )
  ) {
    transferStatement.setLong(1, Math.negateExact(cents));
    transferStatement.setLong(2, fromAccountId);
    transferStatement.executeUpdate();
    transferStatement.setLong(1, cents);
    transferStatement.setLong(2, toAccountId);
    transferStatement.executeUpdate();
  }
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
  public void testManualCommitWithTemplate() throws SQLException {
    long cents = 70;
    long fromAccountId = 1;
    long toAccountId = 2;

    transact((Connection connection) -> {
      try (PreparedStatement transferStatement = connection.prepareStatement(
          "UPDATE account SET balance = ? WHERE id = ?"
      )) {
        transferStatement.setLong(1, Math.negateExact(cents));
        transferStatement.setLong(2, fromAccountId);
        transferStatement.executeUpdate();

        transferStatement.setLong(1, cents);
        transferStatement.setLong(2, toAccountId);
        transferStatement.executeUpdate();
      } catch (SQLException e) {
        throw new DataAccessException( e);
      }
    });
  }
}

相关文章