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); }
/** * @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); }
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); }
/** * @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); }
/** @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); }
@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); }
/** 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)); }
/** 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); }
@Override public Double apply(Integer in) { return Math.sqrt(in); } };
@Override public Vector3 nor () { final float len2 = this.len2(); if (len2 == 0f || len2 == 1f) return this; return this.scl(1f / (float)Math.sqrt(len2)); }
@Override public Vector3 limit2 (float limit2) { float len2 = len2(); if (len2 > limit2) { scl((float)Math.sqrt(limit2 / len2)); } return this; }
@Override public Vector2 setLength2 (float len2) { float oldLen2 = len2(); return (oldLen2 == 0 || oldLen2 == len2) ? this : scl((float)Math.sqrt(len2 / oldLen2)); }
/** @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()); }
/** @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()); }
@GwtIncompatible // TODO private static BigInteger sqrtApproxWithDoubles(BigInteger x) { return DoubleMath.roundToBigInteger(Math.sqrt(DoubleUtils.bigToDouble(x)), HALF_EVEN); }
public void testSqrtOfLongIsAtMostFloorSqrtMaxLong() { long sqrtMaxLong = (long) Math.sqrt(Long.MAX_VALUE); assertTrue(sqrtMaxLong <= LongMath.FLOOR_SQRT_MAX_LONG); }
/** 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))); }
@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); } }