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

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

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

Math.sqrt介绍

[英]Returns the closest double approximation of the square root of the argument.

Special cases:

  • sqrt(+0.0) = +0.0
  • sqrt(-0.0) = -0.0
  • sqrt( (anything < 0) ) = NaN
  • sqrt(+infinity) = +infinity
  • sqrt(NaN) = NaN
    [中]返回参数平方根的最接近的双近似值。
    特殊情况:
    *sqrt(+0.0)=+0.0
    *sqrt(-0.0)=-0.0
    *sqrt((任何小于0的项))=NaN
    *sqrt(+无穷大)=+无穷大
    *sqrt(NaN)=NaN

代码示例

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

private static int sqrtFloor(int x) {
 // There is no loss of precision in converting an int to a double, according to
 // http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2
 return (int) Math.sqrt(x);
}

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

/**
 * @param x The x-coordinate of the other point
 * @param y The y-coordinate of the other point
 * @param z The z-coordinate of the other point
 * @return the distance between this point and the other point.
 */
public float dst (int x, int y, int z) {
  int xd = x - this.x;
  int yd = y - this.y;
  int zd = z - this.z;
  return (float)Math.sqrt(xd * xd + yd * yd + zd * zd);
}

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

public float apply (float a) {
    if (a <= 0.5f) {
      a *= 2;
      return (1 - (float)Math.sqrt(1 - a * a)) / 2;
    }
    a--;
    a *= 2;
    return ((float)Math.sqrt(1 - a * a) + 1) / 2;
  }
};

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

public static float dst (float x1, float y1, float x2, float y2) {
  final float x_d = x2 - x1;
  final float y_d = y2 - y1;
  return (float)Math.sqrt(x_d * x_d + y_d * y_d);
}

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

/**
 * @param other The other point
 * @return the distance between this point and the other vector.
 */
public float dst (GridPoint3 other) {
  int xd = other.x - x;
  int yd = other.y - y;
  int zd = other.z - z;
  return (float)Math.sqrt(xd * xd + yd * yd + zd * zd);
}

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

/** @return the distance between this point and the given point */
public float dst (float x, float y, float z) {
  final float a = x - this.x;
  final float b = y - this.y;
  final float c = z - this.z;
  return (float)Math.sqrt(a * a + b * b + c * c);
}

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

@Override
public float dst (Vector2 v) {
  final float x_d = v.x - x;
  final float y_d = v.y - y;
  return (float)Math.sqrt(x_d * x_d + y_d * y_d);
}

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

/** Returns a triangularly distributed random number between {@code min} (inclusive) and {@code max} (exclusive), where values
 * around {@code mode} are more likely.
 * @param min the lower limit
 * @param max the upper limit
 * @param mode the point around which the values are more likely */
public static float randomTriangular (float min, float max, float mode) {
  float u = random.nextFloat();
  float d = max - min;
  if (u <= (mode - min) / d) return min + (float)Math.sqrt(u * d * (mode - min));
  return max - (float)Math.sqrt((1 - u) * d * (max - mode));
}

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

/** Ratio of circumradius to shortest edge as a measure of triangle quality.
 * <p>
 * Gary L. Miller, Dafna Talmor, Shang-Hua Teng, and Noel Walkington. A Delaunay Based Numerical Method for Three Dimensions:
 * Generation, Formulation, and Partition. */
static public float triangleQuality (float x1, float y1, float x2, float y2, float x3, float y3) {
  float length1 = (float)Math.sqrt(x1 * x1 + y1 * y1);
  float length2 = (float)Math.sqrt(x2 * x2 + y2 * y2);
  float length3 = (float)Math.sqrt(x3 * x3 + y3 * y3);
  return Math.min(length1, Math.min(length2, length3)) / triangleCircumradius(x1, y1, x2, y2, x3, y3);
}

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

@Override
 public Double apply(Integer in) {
  return Math.sqrt(in);
 }
};

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

@Override
public Vector2 clamp (float min, float max) {
  final float len2 = len2();
  if (len2 == 0f) return this;
  float max2 = max * max;
  if (len2 > max2) return scl((float)Math.sqrt(max2 / len2));
  float min2 = min * min;
  if (len2 < min2) return scl((float)Math.sqrt(min2 / len2));
  return this;
}

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

@Override
public Vector3 nor () {
  final float len2 = this.len2();
  if (len2 == 0f || len2 == 1f) return this;
  return this.scl(1f / (float)Math.sqrt(len2));
}

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

@Override
public Vector3 limit2 (float limit2) {
  float len2 = len2();
  if (len2 > limit2) {
    scl((float)Math.sqrt(limit2 / len2));
  }
  return this;
}

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

@Override
public Vector2 setLength2 (float len2) {
  float oldLen2 = len2();
  return (oldLen2 == 0 || oldLen2 == len2) ? this : scl((float)Math.sqrt(len2 / oldLen2));
}

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

/** @return the scale factor on the X axis (non-negative) */
public float getScaleX () {
  return (MathUtils.isZero(val[Matrix4.M01]) && MathUtils.isZero(val[Matrix4.M02])) ? Math.abs(val[Matrix4.M00])
    : (float)Math.sqrt(getScaleXSquared());
}

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

/** @return the scale factor on the Y axis (non-negative) */
public float getScaleY () {
  return (MathUtils.isZero(val[Matrix4.M10]) && MathUtils.isZero(val[Matrix4.M12])) ? Math.abs(val[Matrix4.M11])
    : (float)Math.sqrt(getScaleYSquared());
}

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

@GwtIncompatible // TODO
private static BigInteger sqrtApproxWithDoubles(BigInteger x) {
 return DoubleMath.roundToBigInteger(Math.sqrt(DoubleUtils.bigToDouble(x)), HALF_EVEN);
}

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

public void testSqrtOfLongIsAtMostFloorSqrtMaxLong() {
 long sqrtMaxLong = (long) Math.sqrt(Long.MAX_VALUE);
 assertTrue(sqrtMaxLong <= LongMath.FLOOR_SQRT_MAX_LONG);
}

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

/** Get the angle in radians of the rotation around the specified axis. The axis must be normalized.
 * @param axisX the x component of the normalized axis for which to get the angle
 * @param axisY the y component of the normalized axis for which to get the angle
 * @param axisZ the z component of the normalized axis for which to get the angle
 * @return the angle in radians of the rotation around the specified axis */
public float getAngleAroundRad (final float axisX, final float axisY, final float axisZ) {
  final float d = Vector3.dot(this.x, this.y, this.z, axisX, axisY, axisZ);
  final float l2 = Quaternion.len2(axisX * d, axisY * d, axisZ * d, this.w);
  return MathUtils.isZero(l2) ? 0f : (float)(2.0 * Math.acos(MathUtils.clamp(
    (float)((d < 0 ? -this.w : this.w) / Math.sqrt(l2)), -1f, 1f)));
}

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

@GwtIncompatible // far too slow
public void testSqrtOfPerfectSquareAsDoubleIsPerfect() {
 // This takes just over a minute on my machine.
 for (long n = 0; n <= LongMath.FLOOR_SQRT_MAX_LONG; n++) {
  long actual = (long) Math.sqrt(n * n);
  assertTrue(actual == n);
 }
}

相关文章