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

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

本文整理了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

  1. public static String mask(String input, int num, String mask) {
  2. int length = input.length();
  3. return num > 0
  4. ?
  5. input.substring(0, length - num).replaceAll(".", mask)
  6. + input.substring(length - num)
  7. :
  8. input.substring(0, Math.negateExact(num))
  9. + input.substring(Math.negateExact(num), length).replaceAll(".", mask);
  10. }

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

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

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

  1. @ScalarOperator(NEGATION)
  2. @SqlType(StandardTypes.INTEGER)
  3. public static long negate(@SqlType(StandardTypes.INTEGER) long value)
  4. {
  5. try {
  6. return Math.negateExact((int) value);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "integer negation overflow: " + value, e);
  10. }
  11. }

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

  1. @ScalarOperator(NEGATION)
  2. @SqlType(StandardTypes.BIGINT)
  3. public static long negate(@SqlType(StandardTypes.BIGINT) long value)
  4. {
  5. try {
  6. return Math.negateExact(value);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "bigint negation overflow: " + value, e);
  10. }
  11. }

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

  1. public static String mask(String input, int num, String mask) {
  2. int length = input.length();
  3. return num > 0
  4. ?
  5. input.substring(0, length - num).replaceAll(".", mask)
  6. + input.substring(length - num)
  7. :
  8. input.substring(0, Math.negateExact(num))
  9. + input.substring(Math.negateExact(num), length).replaceAll(".", mask);
  10. }

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

  1. /**
  2. * Delegates to {@link Math#negateExact(long)}
  3. *
  4. * @throws ArithmeticException if the result overflows a long
  5. **/
  6. public static long safeNegate(long x) {
  7. return Math.negateExact(x);
  8. }

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

  1. /**
  2. * Returns the negative value of this fraction.
  3. * This method does not simplify the fraction.
  4. *
  5. * @return the result of {@code -this}.
  6. * @throws ArithmeticException if the result overflows.
  7. */
  8. public Fraction negate() {
  9. int n = numerator;
  10. int d = denominator;
  11. if (n != 0) {
  12. n = Math.negateExact(n);
  13. } else if (d != 0) {
  14. d = Math.negateExact(d);
  15. } else {
  16. return this;
  17. }
  18. return new Fraction(n, d);
  19. }

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

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

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

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

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

  1. public static Runnable onceMore(Runnable loopBody) {
  2. AtomicInteger lock = new AtomicInteger();
  3. return () -> {
  4. // request execution of the runnable
  5. int current = lock.incrementAndGet();
  6. // another thread is executing
  7. if(current > 1)
  8. return;
  9. try {
  10. do {
  11. loopBody.run();
  12. current = lock.addAndGet(Math.negateExact(current));
  13. } while(current > 0);
  14. } catch(Throwable t) {
  15. lock.set(0);
  16. throw t;
  17. }
  18. };
  19. }

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

  1. /**
  2. * <p>Yields the absolute value of the represented calendar days. </p>
  3. *
  4. * @return non-negative count of calendar days
  5. * @since 3.4/4.3
  6. */
  7. /*[deutsch]
  8. * <p>Liefert den absoluten Betrag der repr&auml;sentierten Kalendertage. </p>
  9. *
  10. * @return non-negative count of calendar days
  11. * @since 3.4/4.3
  12. */
  13. public CalendarDays abs() {
  14. return ((this.days < 0) ? CalendarDays.of(Math.negateExact(this.days)) : this);
  15. }

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

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

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

  1. @Override
  2. public long get(TemporalUnit unit) {
  3. for (Map.Entry<IsoUnit, TemporalUnit> entry : MAP.entrySet()) {
  4. if (entry.getValue().equals(unit)) {
  5. long amount = this.duration.getPartialAmount(entry.getKey());
  6. if (this.duration.isNegative()) {
  7. amount = Math.negateExact(amount);
  8. }
  9. return amount;
  10. }
  11. }
  12. if (unit.equals(ChronoUnit.HALF_DAYS)) {
  13. long hd = Math.floorDiv(this.duration.getPartialAmount(ClockUnit.HOURS), 12);
  14. if (this.duration.isNegative()) {
  15. hd = Math.negateExact(hd);
  16. }
  17. return hd;
  18. }
  19. throw new UnsupportedTemporalTypeException(unit.toString()); // throws NPE if unit is null
  20. }

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

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

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

  1. @ScalarOperator(NEGATION)
  2. @SqlType(StandardTypes.BIGINT)
  3. public static long negate(@SqlType(StandardTypes.BIGINT) long value)
  4. {
  5. try {
  6. return Math.negateExact(value);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "bigint negation overflow: " + value, e);
  10. }
  11. }

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

  1. @ScalarOperator(NEGATION)
  2. @SqlType(StandardTypes.INTEGER)
  3. public static long negate(@SqlType(StandardTypes.INTEGER) long value)
  4. {
  5. try {
  6. return Math.negateExact((int) value);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "integer negation overflow: " + value, e);
  10. }
  11. }

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

  1. @ScalarOperator(NEGATION)
  2. @SqlType(StandardTypes.INTEGER)
  3. public static long negate(@SqlType(StandardTypes.INTEGER) long value)
  4. {
  5. try {
  6. return Math.negateExact((int) value);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "integer negation overflow: " + value, e);
  10. }
  11. }

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

  1. @ScalarOperator(NEGATION)
  2. @SqlType(StandardTypes.BIGINT)
  3. public static long negate(@SqlType(StandardTypes.BIGINT) long value)
  4. {
  5. try {
  6. return Math.negateExact(value);
  7. }
  8. catch (ArithmeticException e) {
  9. throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "bigint negation overflow: " + value, e);
  10. }
  11. }

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

  1. @Test
  2. public void testAutoCommit() throws SQLException {
  3. LOGGER.info("Test Auto Commit");
  4. long cents = 70;
  5. long fromAccountId = 1;
  6. long toAccountId = 2;
  7. DataSource dataSource = newDataSource();
  8. try(Connection connection = dataSource.getConnection();
  9. PreparedStatement transferStatement = connection.prepareStatement(
  10. "UPDATE account SET balance = ? WHERE id = ?"
  11. )
  12. ) {
  13. transferStatement.setLong(1, Math.negateExact(cents));
  14. transferStatement.setLong(2, fromAccountId);
  15. transferStatement.executeUpdate();
  16. transferStatement.setLong(1, cents);
  17. transferStatement.setLong(2, toAccountId);
  18. transferStatement.executeUpdate();
  19. }
  20. }

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

  1. @Test
  2. public void testManualCommitWithTemplate() throws SQLException {
  3. long cents = 70;
  4. long fromAccountId = 1;
  5. long toAccountId = 2;
  6. transact((Connection connection) -> {
  7. try (PreparedStatement transferStatement = connection.prepareStatement(
  8. "UPDATE account SET balance = ? WHERE id = ?"
  9. )) {
  10. transferStatement.setLong(1, Math.negateExact(cents));
  11. transferStatement.setLong(2, fromAccountId);
  12. transferStatement.executeUpdate();
  13. transferStatement.setLong(1, cents);
  14. transferStatement.setLong(2, toAccountId);
  15. transferStatement.executeUpdate();
  16. } catch (SQLException e) {
  17. throw new DataAccessException( e);
  18. }
  19. });
  20. }
  21. }

相关文章