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

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

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

Math.abs介绍

[英]Returns the absolute value of the argument.

Special cases:

  • abs(-0.0) = +0.0
  • abs(+infinity) = +infinity
  • abs(-infinity) = +infinity
  • abs(NaN) = NaN
    [中]返回参数的绝对值。
    特殊情况:
    *abs(-0.0)=+0.0
    *abs(+无穷大)=+无穷大
    *abs(-无穷大)=+无穷大
    *abs(NaN)=NaN

代码示例

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

@Override
public boolean epsilonEquals (final Vector3 other, float epsilon) {
  if (other == null) return false;
  if (Math.abs(other.x - x) > epsilon) return false;
  if (Math.abs(other.y - y) > epsilon) return false;
  if (Math.abs(other.z - z) > epsilon) return false;
  return true;
}

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

/** Returns true if the value is zero.
 * @param tolerance represent an upper bound below which the value is considered zero. */
static public boolean isZero (float value, float tolerance) {
  return Math.abs(value) <= tolerance;
}

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

/** Returns true if a is nearly equal to b. The function uses the default floating error tolerance.
 * @param a the first value.
 * @param b the second value. */
static public boolean isEqual (float a, float b) {
  return Math.abs(a - b) <= FLOAT_ROUNDING_ERROR;
}

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

/** Returns true if a is nearly equal to b.
 * @param a the first value.
 * @param b the second value.
 * @param tolerance represent an upper bound below which the two values are considered equal. */
static public boolean isEqual (float a, float b, float tolerance) {
  return Math.abs(a - b) <= tolerance;
}

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

@Override
  public EventExecutor next() {
    return executors[Math.abs(idx.getAndIncrement() % executors.length)];
  }
}

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

/** Returns the distance between the given line and point. Note the specified line is not a line segment. */
public static float distanceLinePoint (float startX, float startY, float endX, float endY, float pointX, float pointY) {
  float normalLength = (float)Math.sqrt((endX - startX) * (endX - startX) + (endY - startY) * (endY - startY));
  return Math.abs((pointX - startX) * (endY - startY) - (pointY - startY) * (endX - startX)) / normalLength;
}

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

public String getServerId() {
  if (this.serverId == null) {
    this.serverId = String.valueOf(Math.abs(getUuid().getMostSignificantBits()) % 1000);
  }
  return this.serverId;
}

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

public float abs(float value) {
    return Math.abs(value);
  }
}

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

public int abs(int value) {
  return Math.abs(value);
}

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to(int max) {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % max);
}

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to100() {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % 100);
}

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to(int max) {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % max);
}

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to100() {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % 100);
}

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

/** Creates a sprite with width, height, and texture region equal to the specified size.
 * @param srcWidth The width of the texture region. May be negative to flip the sprite when drawn.
 * @param srcHeight The height of the texture region. May be negative to flip the sprite when drawn. */
public Sprite (Texture texture, int srcX, int srcY, int srcWidth, int srcHeight) {
  if (texture == null) throw new IllegalArgumentException("texture cannot be null.");
  this.texture = texture;
  setRegion(srcX, srcY, srcWidth, srcHeight);
  setColor(1, 1, 1, 1);
  setSize(Math.abs(srcWidth), Math.abs(srcHeight));
  setOrigin(width / 2, height / 2);
}

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

public void captureFloatArgument(JoinPoint tjp, float arg) {
  float tjpArg = ((Float) tjp.getArgs()[0]).floatValue();
  if (Math.abs(tjpArg - arg) > 0.000001) {
    throw new IllegalStateException(
        "argument is '" + arg + "', " +
        "but args array has '" + tjpArg + "'"
        );
  }
  this.lastBeforeFloatValue = arg;
}

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

private static void assertApproximateDifference(Date lesser, Date greater, long expected) {
  long diff = greater.getTime() - lesser.getTime();
  long variance = Math.abs(expected - diff);
  assertTrue("expected approximate difference of " + expected +
      ", but actual difference was " + diff, variance < 100);
}

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

private static void assertNegligibleDifference(Date d1, Date d2) {
  long diff = Math.abs(d1.getTime() - d2.getTime());
  assertTrue("difference exceeds threshold: " + diff, diff < 100);
}

代码示例来源:origin: google/guava

public void testFuzzyEqualsFinite() {
 for (double a : FINITE_DOUBLE_CANDIDATES) {
  for (double b : FINITE_DOUBLE_CANDIDATES) {
   for (double tolerance : FINITE_TOLERANCE_CANDIDATES) {
    assertEquals(Math.abs(a - b) <= tolerance, DoubleMath.fuzzyEquals(a, b, tolerance));
   }
  }
 }
}

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

@Test
public void SPR9486_floatFunctionResolver() {
  Number expectedResult = Math.abs(-10.2f);
  ExpressionParser parser = new SpelExpressionParser();
  SPR9486_FunctionsClass testObject = new SPR9486_FunctionsClass();
  StandardEvaluationContext context = new StandardEvaluationContext();
  Expression expression = parser.parseExpression("abs(-10.2f)");
  Number result = expression.getValue(context, testObject, Number.class);
  assertEquals(expectedResult, result);
}

代码示例来源:origin: google/guava

@GwtIncompatible // #trueLog2, Math.ulp
public void testLog2Accuracy() {
 for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
  double dmLog2 = DoubleMath.log2(d);
  double trueLog2 = trueLog2(d);
  assertTrue(Math.abs(dmLog2 - trueLog2) <= Math.ulp(trueLog2));
 }
}

相关文章