Class MathTools

java.lang.Object
com.github.tommyettinger.digital.MathTools

public final class MathTools extends Object
Mathematical operations not provided by java.lang.Math.
Includes code that was originally part of the Uncommon Maths software package as Maths. Also includes code adapted from libGDX as their MathUtils class. There's also cbrt(float) by Marc B. Reynolds, building on the legendary fast inverse square root, and a generalized bias/gain function, barronSpline(float, float, float), popularized by Jon Barron. The fastFloor(float) and fastCeil(float) methods were devised by Riven on JavaGaming.org . factorial(float) and gamma(float) are by T. J. Stieltjes. The lerp methods that operate on angles are originally from MathUtils in libGDX, which credits Nathan Sweet.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final float
    The float value that is closer than any other to e, the base of the natural logarithms.
    static final float
    2 to the -24 as a float; this is equal to Math.ulp(0.5f), and is the smallest non-zero distance possible between two results of Random.nextFloat().
    static final double
    2 to the -53 as a float; this is equal to Math.ulp(0.5), and is the smallest non-zero distance possible between two results of Random.nextDouble().
    static final float
    A float that is meant to be used as the smallest reasonable tolerance for methods like isEqual(float, float, float).
    static final long[]
    1275 negative, odd long values that are calculated using a generalization of the golden ratio and exponents of those generalizations.
    static final float
    The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5; this is the "most irrational" of irrational numbers, and has various useful properties.
    static final double
    The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5, as a double; this is the "most irrational" of irrational numbers, and has various useful properties.
    static final float
    The inverse of the golden ratio, (1.0 - Math.sqrt(5.0)) * -0.5 or GOLDEN_RATIO - 1.0; this also has various useful properties.
    static final double
    The inverse of the golden ratio, (1.0 - Math.sqrt(5.0)) * -0.5 or GOLDEN_RATIO - 1.0, as a double; this also has various useful properties.
    static final float
    The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5; this is the "most irrational" of irrational numbers, and has various useful properties.
    static final double
    The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5, as a double; this is the "most irrational" of irrational numbers, and has various useful properties.
    static final float
    The conjugate of the golden ratio, (1.0 - Math.sqrt(5.0)) * 0.5 or 1.0 - GOLDEN_RATIO; this also has various useful properties.
    static final double
    The conjugate of the golden ratio, (1.0 - Math.sqrt(5.0)) * 0.5 or 1.0 - GOLDEN_RATIO, as a double; this also has various useful properties.
    static final float
    The float value that is closer than any other to Math.sqrt(2.0), the ratio of the hypotenuse of an isosceles right triangle to one of its legs.
    static final double
    The double value that is closer than any other to Math.sqrt(2.0), the ratio of the hypotenuse of an isosceles right triangle to one of its legs.
    static final float
    The float value that is closer than any other to Math.sqrt(3.0), the ratio of the diagonal length of a cube to its edge length.
    static final double
    The double value that is closer than any other to Math.sqrt(3.0), the ratio of the diagonal length of a cube to its edge length.
    static final float
    The float value that is closer than any other to Math.sqrt(5.0), which has various useful properties, such as appearing in many formulae involving the golden ratio which is of course chiefly due to being part of its calculation.
    static final double
    The double value that is closer than any other to Math.sqrt(5.0), which has various useful properties, such as appearing in many formulae involving the golden ratio which is of course chiefly due to being part of its calculation.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    barronSpline(double x, double shape, double turning)
    A generalization on bias and gain functions that can represent both; this version is branch-less.
    static float
    barronSpline(float x, float shape, float turning)
    A generalization on bias and gain functions that can represent both; this version is branch-less.
    static float
    cbrt(float x)
    An approximation of the cube-root function for float inputs and outputs.
    static int
    ceil(double t)
    Like Math.ceil(double), but returns an int.
    static int
    ceil(float value)
    Returns the smallest int greater than or equal to the specified float.
    static int
    ceilPositive(float value)
    Returns the smallest integer greater than or equal to the specified float.
    static double
    clamp(double value, double min, double max)
    If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
    static float
    clamp(float value, float min, float max)
    If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
    static int
    clamp(int value, int min, int max)
    If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
    static long
    clamp(long value, long min, long max)
    If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
    static double
    cube(double n)
    Returns the cube (third power) of its parameter.
    static float
    cube(float n)
    Returns the cube (third power) of its parameter.
    static double
    factorial(double x)
    A close approximation to the factorial function for real numbers, using an algorithm by T.
    static float
    factorial(float x)
    A close approximation to the factorial function for real numbers, using an algorithm by T.
    static int
    fastCeil(float t)
    Like Math.ceil(double), but takes a float and returns an int.
    static int
    fastFloor(float t)
    Like Math.floor(double), but takes a float and returns an int.
    static int
    fibonacci(int n)
    Binet's formula for the Fibonacci sequence, which is a closed-form expression where which each resulting value is the sum of the two proceeding values.
    static long
    fibonacci(long n)
    Binet's formula for the Fibonacci sequence, which is a closed-form expression where which each resulting value is the sum of the two proceeding values.
    static int
    floor(double t)
    Like Math.floor(double) , but returns an int.
    static int
    floor(float value)
    Returns the largest int less than or equal to the specified float.
    static int
    floorPositive(float value)
    Returns the largest int less than or equal to the specified float.
    static double
    gamma(double x)
    A close approximation to the gamma function for positive doubles, using an algorithm by T.
    static float
    gamma(float x)
    A close approximation to the gamma function for positive floats, using an algorithm by T.
    static long
    greatestCommonDivisor(long a, long b)
    Determines the greatest common divisor of a pair of natural numbers using the Euclidean algorithm.
    static double
    invSqrt(double x)
    Fast inverse square root, best known for its implementation in Quake III Arena.
    static float
    invSqrt(float x)
    Fast inverse square root, best known for its implementation in Quake III Arena.
    static boolean
    isEqual(double a, double b, double tolerance)
    Equivalent to libGDX's isEqual() method in MathUtils; this compares two doubles for equality and allows the given tolerance during comparison.
    static boolean
    isEqual(float a, float b)
    Equivalent to libGDX's isEqual() method in MathUtils; this compares two floats for equality and allows just enough tolerance to ignore a rounding error.
    static boolean
    isEqual(float a, float b, float tolerance)
    Equivalent to libGDX's isEqual() method in MathUtils; this compares two floats for equality and allows the given tolerance during comparison.
    static boolean
    isPowerOfTwo(int value)
    Returns true if value is a power of two or is equal to Integer.MIN_VALUE; false otherwise.
    static long
    isqrt(long n)
    Integer square root (using floor), maintaining correct results even for very large long values.
    static boolean
    isZero(double value, double tolerance)
    Returns true if the value is zero.
    static boolean
    isZero(float value)
    Returns true if the value is zero (using the default tolerance, FLOAT_ROUNDING_ERROR, as outer bound).
    static boolean
    isZero(float value, float tolerance)
    Returns true if the value is zero, using the given tolerance.
    static double
    lerp(double fromValue, double toValue, double progress)
    Linearly interpolates between fromValue to toValue on progress position.
    static float
    lerp(float fromValue, float toValue, float progress)
    Linearly interpolates between fromValue to toValue on progress position.
    static double
    lerpAngle(double fromRadians, double toRadians, double progress)
    Linearly interpolates between two angles in radians.
    static float
    lerpAngle(float fromRadians, float toRadians, float progress)
    Linearly interpolates between two angles in radians.
    static double
    lerpAngleDeg(double fromDegrees, double toDegrees, double progress)
    Linearly interpolates between two angles in degrees.
    static float
    lerpAngleDeg(float fromDegrees, float toDegrees, float progress)
    Linearly interpolates between two angles in degrees.
    static double
    lerpAngleTurns(double fromTurns, double toTurns, double progress)
    Linearly interpolates between two angles in turns.
    static float
    lerpAngleTurns(float fromTurns, float toTurns, float progress)
    Linearly interpolates between two angles in turns.
    static double
    log(double base, double arg)
    Calculate logarithms for arbitrary bases.
    static float
    log(float base, float value)
    Calculate logarithms for arbitrary bases.
    static float
    log2(float value)
     
    static long
    longFloor(double t)
    Like Math.floor(double), but returns a long.
    static long
    longFloor(float t)
    Like Math.floor(double), but takes a float and returns a long.
    static double
    map(double inRangeStart, double inRangeEnd, double outRangeStart, double outRangeEnd, double value)
    Linearly map a value from one range to another.
    static float
    map(float inRangeStart, float inRangeEnd, float outRangeStart, float outRangeEnd, float value)
    Linearly map a value from one range to another.
    static int
    Given any odd int a, this finds another odd int b such that a * b == 1.
    static long
    Given any odd long a, this finds another odd long b such that a * b == 1L.
    static int
    Returns the next higher power of two relative to n, or n if it is already a power of two.
    static double
    norm(double rangeStart, double rangeEnd, double value)
    Linearly normalizes value from a range.
    static float
    norm(float rangeStart, float rangeEnd, float value)
    Linearly normalizes value from a range.
    static float
    nthrt(float x, float n)
    Returns the nth root of x.
    static long
    raiseToPower(int value, int power)
    Calculate the first argument raised to the power of the second.
    static double
    remainder(double op, double d)
    Like the modulo operator %, but the result will always match the sign of d instead of op.
    static float
    remainder(float op, float d)
    Like the modulo operator %, but the result will always match the sign of d instead of op.
    static int
    round(float value)
    Returns the closest integer to the specified float.
    static int
    roundPositive(float value)
    Returns the closest integer to the specified float.
    static double
    square(double n)
    Returns the square (second power) of its parameter.
    static float
    square(float n)
    Returns the square (second power) of its parameter.
    static double
    sway(double value)
    Very similar to TrigTools.sinTurns(double) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently.
    static float
    sway(float value)
    Very similar to TrigTools.sinTurns(float) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently.
    static double
    swayCubic(double value)
    Very similar to TrigTools.sinTurns(double) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently.
    static float
    swayCubic(float value)
    Very similar to TrigTools.sinTurns(float) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently.
    static double
    swayTight(double value)
    Takes any double and produces a double in the 0.0 to 1.0 range, with a graph of input to output that looks much like a sine wave, curving to have a flat slope when given an integer input and a steep slope when the input is halfway between two integers, smoothly curving at any points between those extremes.
    static float
    swayTight(float value)
    Takes any float and produces a float in the 0f to 1f range, with a graph of input to output that looks much like a sine wave, curving to have a flat slope when given an integer input and a steep slope when the input is halfway between two integers, smoothly curving at any points between those extremes.
    static double
    truncate(double n)
    Forces precision loss on the given double so very small fluctuations away from an integer will be erased.
    static float
    truncate(float n)
    Forces precision loss on the given float so very small fluctuations away from an integer will be erased.
    static double
    zigzag(double value)
    Takes any double and produces a double in the -1.0 to 1.0 range, with similar inputs producing close to a consistent rate of up and down through the range.
    static float
    zigzag(float value)
    Takes any float and produces a float in the -1f to 1f range, with similar inputs producing close to a consistent rate of up and down through the range.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FLOAT_ROUNDING_ERROR

      public static final float FLOAT_ROUNDING_ERROR
      A float that is meant to be used as the smallest reasonable tolerance for methods like isEqual(float, float, float). This is sufficient if the rounding error is the result of one addition or subtraction between numbers smaller than 16 (closer to 0). More math operations or larger numbers can produce larger rounding errors; you can try multiplying this constant by, for instance, 64, and using that as the tolerance if you need precision with three-digit numbers (16 * 64 is 1024, so 0 to 999 should be precise there). A larger rounding error can introduce false-positive equivalence with very small inputs.
      See Also:
    • EPSILON

      public static final float EPSILON
      2 to the -24 as a float; this is equal to Math.ulp(0.5f), and is the smallest non-zero distance possible between two results of Random.nextFloat(). Useful for converting a 24-bit int or long value to a gradient between 0 and 1.
      See Also:
    • EPSILON_D

      public static final double EPSILON_D
      2 to the -53 as a float; this is equal to Math.ulp(0.5), and is the smallest non-zero distance possible between two results of Random.nextDouble(). Useful for converting a 53-bit long value to a gradient between 0 and 1.
      See Also:
    • E

      public static final float E
      The float value that is closer than any other to e, the base of the natural logarithms.
      See Also:
    • ROOT2

      public static final float ROOT2
      The float value that is closer than any other to Math.sqrt(2.0), the ratio of the hypotenuse of an isosceles right triangle to one of its legs.
      See Also:
    • ROOT2_D

      public static final double ROOT2_D
      The double value that is closer than any other to Math.sqrt(2.0), the ratio of the hypotenuse of an isosceles right triangle to one of its legs.
      See Also:
    • ROOT3

      public static final float ROOT3
      The float value that is closer than any other to Math.sqrt(3.0), the ratio of the diagonal length of a cube to its edge length.
      See Also:
    • ROOT3_D

      public static final double ROOT3_D
      The double value that is closer than any other to Math.sqrt(3.0), the ratio of the diagonal length of a cube to its edge length.
      See Also:
    • ROOT5

      public static final float ROOT5
      The float value that is closer than any other to Math.sqrt(5.0), which has various useful properties, such as appearing in many formulae involving the golden ratio which is of course chiefly due to being part of its calculation.
      See Also:
    • ROOT5_D

      public static final double ROOT5_D
      The double value that is closer than any other to Math.sqrt(5.0), which has various useful properties, such as appearing in many formulae involving the golden ratio which is of course chiefly due to being part of its calculation.
      See Also:
    • GOLDEN_RATIO

      public static final float GOLDEN_RATIO
      The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5; this is the "most irrational" of irrational numbers, and has various useful properties.
      The same as PHI.
      See Also:
    • PHI

      public static final float PHI
      The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5; this is the "most irrational" of irrational numbers, and has various useful properties.
      The same as GOLDEN_RATIO.
      See Also:
    • GOLDEN_RATIO_D

      public static final double GOLDEN_RATIO_D
      The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5, as a double; this is the "most irrational" of irrational numbers, and has various useful properties.
      The same as PHI_D.
      See Also:
    • PHI_D

      public static final double PHI_D
      The famous golden ratio, (1.0 + Math.sqrt(5.0)) * 0.5, as a double; this is the "most irrational" of irrational numbers, and has various useful properties.
      The same as GOLDEN_RATIO_D.
      See Also:
    • GOLDEN_RATIO_INVERSE

      public static final float GOLDEN_RATIO_INVERSE
      The inverse of the golden ratio, (1.0 - Math.sqrt(5.0)) * -0.5 or GOLDEN_RATIO - 1.0; this also has various useful properties.
      See Also:
    • GOLDEN_RATIO_INVERSE_D

      public static final double GOLDEN_RATIO_INVERSE_D
      The inverse of the golden ratio, (1.0 - Math.sqrt(5.0)) * -0.5 or GOLDEN_RATIO - 1.0, as a double; this also has various useful properties.
      See Also:
    • PSI

      public static final float PSI
      The conjugate of the golden ratio, (1.0 - Math.sqrt(5.0)) * 0.5 or 1.0 - GOLDEN_RATIO; this also has various useful properties.
      See Also:
    • PSI_D

      public static final double PSI_D
      The conjugate of the golden ratio, (1.0 - Math.sqrt(5.0)) * 0.5 or 1.0 - GOLDEN_RATIO, as a double; this also has various useful properties.
      See Also:
    • GOLDEN_LONGS

      public static final long[] GOLDEN_LONGS
      1275 negative, odd long values that are calculated using a generalization of the golden ratio and exponents of those generalizations. Mostly, these are useful because they are all 64-bit constants that have an irrational-number-like pattern to their bits, which makes them pretty much all useful as increments for large counters (also called Weyl sequences) and also sometimes as multipliers for data that should be somewhat random. The earlier numbers in the array are closer to the bit patterns of irrational numbers. Note that these are not at all uniformly-distributed, and should not be used for tasks where random negative longs must be uniform. See Martin Roberts' blog post for more information on how these were constructed. This can be organized into groups of increasing size -- 1 number from the 1D sequence in that post, 2 numbers from the 2D sequence, 3 numbers from the 3D sequence, etc.
  • Method Details

    • raiseToPower

      public static long raiseToPower(int value, int power)
      Calculate the first argument raised to the power of the second. This method only supports non-negative powers.
      Parameters:
      value - The number to be raised.
      power - The exponent (must be positive).
      Returns:
      value raised to power.
    • log

      public static double log(double base, double arg)
      Calculate logarithms for arbitrary bases.
      Parameters:
      base - The base for the logarithm.
      arg - The value to calculate the logarithm for.
      Returns:
      The log of arg in the specified base.
    • log

      public static float log(float base, float value)
      Calculate logarithms for arbitrary bases.
      Parameters:
      base - the logarithm base to use
      value - what value to get the logarithm of, using the given base
      Returns:
      the logarithm of value with the given base
    • log2

      public static float log2(float value)
      Parameters:
      value - what value to get the logarithm of, using base 2
      Returns:
      the logarithm of value with base 2
    • isEqual

      public static boolean isEqual(double a, double b, double tolerance)
      Equivalent to libGDX's isEqual() method in MathUtils; this compares two doubles for equality and allows the given tolerance during comparison. An example is 0.3 - 0.2 == 0.1 vs. isEqual(0.3 - 0.2, 0.1, 0.000001); the first is incorrectly false, while the second is correctly true.
      Parameters:
      a - the first float to compare
      b - the second float to compare
      tolerance - the maximum difference between a and b permitted for this to return true, inclusive
      Returns:
      true if a and b have a difference less than or equal to tolerance, or false otherwise.
    • isEqual

      public static boolean isEqual(float a, float b)
      Equivalent to libGDX's isEqual() method in MathUtils; this compares two floats for equality and allows just enough tolerance to ignore a rounding error. An example is 0.3f - 0.2f == 0.1f vs. isEqual(0.3f - 0.2f, 0.1f); the first is incorrectly false, while the second is correctly true. This uses FLOAT_ROUNDING_ERROR as its tolerance.
      Parameters:
      a - the first float to compare
      b - the second float to compare
      Returns:
      true if a and b are equal or extremely close to equal, or false otherwise.
    • isEqual

      public static boolean isEqual(float a, float b, float tolerance)
      Equivalent to libGDX's isEqual() method in MathUtils; this compares two floats for equality and allows the given tolerance during comparison. An example is 0.3f - 0.2f == 0.1f vs. isEqual(0.3f - 0.2f, 0.1f, 0.000001f); the first is incorrectly false, while the second is correctly true. See FLOAT_ROUNDING_ERROR for advice on choosing a value for tolerance.
      Parameters:
      a - the first float to compare
      b - the second float to compare
      tolerance - the maximum difference between a and b permitted for this to return true, inclusive
      Returns:
      true if a and b have a difference less than or equal to tolerance, or false otherwise.
    • clamp

      public static int clamp(int value, int min, int max)
      If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
      Note that it can often be just as easy to directly call the same code this calls, while being slightly friendlier to inlining in large methods: Math.min(Math.max(value, min), max).
      Parameters:
      value - The value to check.
      min - The minimum permitted value.
      max - The maximum permitted value.
      Returns:
      value if it is between the specified limits, min if the value is too low, or max if the value is too high.
    • clamp

      public static long clamp(long value, long min, long max)
      If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
      Note that it can often be just as easy to directly call the same code this calls, while being slightly friendlier to inlining in large methods: Math.min(Math.max(value, min), max).
      Parameters:
      value - The value to check.
      min - The minimum permitted value.
      max - The maximum permitted value.
      Returns:
      value if it is between the specified limits, min if the value is too low, or max if the value is too high.
    • clamp

      public static double clamp(double value, double min, double max)
      If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
      Note that it can often be just as easy to directly call the same code this calls, while being slightly friendlier to inlining in large methods: Math.min(Math.max(value, min), max).
      Parameters:
      value - The value to check.
      min - The minimum permitted value.
      max - The maximum permitted value.
      Returns:
      value if it is between the specified limits, min if the value is too low, or max if the value is too high.
    • clamp

      public static float clamp(float value, float min, float max)
      If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
      Note that it can often be just as easy to directly call the same code this calls, while being slightly friendlier to inlining in large methods: Math.min(Math.max(value, min), max).
      Parameters:
      value - The value to check.
      min - The minimum permitted value.
      max - The maximum permitted value.
      Returns:
      value if it is between the specified limits, min if the value is too low, or max if the value is too high.
    • remainder

      public static float remainder(float op, float d)
      Like the modulo operator %, but the result will always match the sign of d instead of op.
      Parameters:
      op - the dividend; negative values are permitted and wrap instead of producing negative results
      d - the divisor; if this is negative then the result will be negative, otherwise it will be positive
      Returns:
      the remainder of the division of op by d, with a sign matching d
    • remainder

      public static double remainder(double op, double d)
      Like the modulo operator %, but the result will always match the sign of d instead of op.
      Parameters:
      op - the dividend; negative values are permitted and wrap instead of producing negative results
      d - the divisor; if this is negative then the result will be negative, otherwise it will be positive
      Returns:
      the remainder of the division of op by d, with a sign matching d
    • greatestCommonDivisor

      public static long greatestCommonDivisor(long a, long b)
      Determines the greatest common divisor of a pair of natural numbers using the Euclidean algorithm. This method only works with natural numbers. If negative integers are passed in, the absolute values will be used. The return value is always positive.
      Parameters:
      a - The first value.
      b - The second value.
      Returns:
      The greatest common divisor.
    • modularMultiplicativeInverse

      public static int modularMultiplicativeInverse(int a)
      Given any odd int a, this finds another odd int b such that a * b == 1.
      This is incompatible with GWT, but it should usually only find uses in exploratory code or in tests anyway... It is only incompatible because it tends to rely on multiplication overflow to work. The overload that takes a long and gets the inverse modulo (2 to the 64) is GWT-compatible.
      Parameters:
      a - any odd int; note that even numbers do not have inverses modulo 2 to the 32
      Returns:
      the multiplicative inverse of a modulo 4294967296 (or, 2 to the 32)
    • modularMultiplicativeInverse

      public static long modularMultiplicativeInverse(long a)
      Given any odd long a, this finds another odd long b such that a * b == 1L.
      Parameters:
      a - any odd long; note that even numbers do not have inverses modulo 2 to the 64
      Returns:
      the multiplicative inverse of a modulo 18446744073709551616 (or, 2 to the 64)
    • isqrt

      public static long isqrt(long n)
      Integer square root (using floor), maintaining correct results even for very large long values. This version treats negative inputs as unsigned and returns positive square roots for them (these are usually large).
      This is based on code used by Python, but isn't identical. Notably, this doesn't branch except in the for loop, and it handles negative inputs differently.
      Parameters:
      n - a long value that will be treated as if unsigned
      Returns:
      the square root of n, rounded down to the next lower long if the result isn't already a long
    • cbrt

      public static float cbrt(float x)
      An approximation of the cube-root function for float inputs and outputs. This can be about twice as fast as Math.cbrt(double). It correctly returns negative results when given negative inputs.
      Has very low relative error (less than 1E-9) when inputs are uniformly distributed between -512 and 512, and absolute mean error of less than 1E-6 in the same scenario. Uses a bit-twiddling method similar to one presented in Hacker's Delight and also used in early 3D graphics (see Wikipedia for more, but this code approximates cbrt(x) and not 1/sqrt(x)). This specific code was originally by Marc B. Reynolds, posted in his "Stand-alone-junk" repo .
      If you need to work with doubles, or need higher precision, use Math.cbrt(double).
      Parameters:
      x - any finite float to find the cube root of
      Returns:
      the cube root of x, approximated
    • nthrt

      public static float nthrt(float x, float n)
      Returns the nth root of x. Any values within FLOAT_ROUNDING_ERROR of an int are rounded to that int to reduce the likelihood of floating-point error adversely affecting the result.
      For a detailed description of how this function handles negative roots and roots of negative numbers, refer to the documentation for Math.pow(double, double). This implementation starts by calling Math.pow(x, 1f / n), so consider the documentation in Math for the reciprocal of the power.
      Unlike cbrt(float), this is not an approximation, and isn't any faster than calling Math.pow() with the reciprocal of the power. Its advantage is in precision when integer results are mathematically correct, but Math won't calculate them correctly on its own.
      Parameters:
      x - a number to find the nth root of
      n - the degree of the root; may be negative or non-integer
      Returns:
      a number which, when raised to the power n, yields x
    • invSqrt

      public static float invSqrt(float x)
      Fast inverse square root, best known for its implementation in Quake III Arena. This is an algorithm that estimates the float value of 1/sqrt(x). It has comparable performance to the more-straightforward 1f/(float)Math.sqrt(x) on HotSpot JDKs, but this method outperforms the Math-based approach by over 40% on GraalVM 17. Some other platforms, such as Android and GWT, may have similar or very different performance relative to using Math, so if you expect to use this method often, you should test it in your app on the platforms you target. Precision will always be best with Math.
      It is often used for vector normalization, i.e. scaling it to a length of 1. For example, it can be used to compute angles of incidence and reflection for lighting and shading.
      For more information, see Wikipedia
      Parameters:
      x - a non-negative finite float to find the inverse square root of
      Returns:
      the inverse square root of x, approximated
    • invSqrt

      public static double invSqrt(double x)
      Fast inverse square root, best known for its implementation in Quake III Arena. This is an algorithm that estimates the double value of 1/sqrt(x). It has comparable performance to the more-straightforward 1.0/Math.sqrt(x) on HotSpot JDKs, but this method may outperform the Math-based approach on GraalVM 17 (the float version, invSqrt(float), does so by 40% or more). Some other platforms, such as Android and GWT, may have similar or very different performance relative to using Math, so if you expect to use this method often, you should test it in your app on the platforms you target. Precision will always be best with Math.
      It is often used for vector normalization, i.e. scaling it to a length of 1. For example, it can be used to compute angles of incidence and reflection for lighting and shading.
      For more information, see Wikipedia
      Parameters:
      x - a non-negative finite double to find the inverse square root of
      Returns:
      the inverse square root of x, approximated
    • barronSpline

      public static float barronSpline(float x, float shape, float turning)
      A generalization on bias and gain functions that can represent both; this version is branch-less. This is based on this micro-paper by Jon Barron, which generalizes the earlier bias and gain rational functions by Schlick. The second and final page of the paper has useful graphs of what the s (shape) and t (turning point) parameters do; shape should be 0 or greater, while turning must be between 0 and 1, inclusive. This effectively combines two different curving functions so that they continue into each other when x equals turning. The shape parameter will cause this to imitate "smoothstep-like" splines when greater than 1 (where the values ease into their starting and ending levels), or to be the inverse when less than 1 (where values start like square root does, taking off very quickly, but also end like square does, landing abruptly at the ending level). You should only give x values between 0 and 1, inclusive.
      Parameters:
      x - progress through the spline, from 0 to 1, inclusive
      shape - must be greater than or equal to 0; values greater than 1 are "normal interpolations"
      turning - a value between 0.0 and 1.0, inclusive, where the shape changes
      Returns:
      a float between 0 and 1, inclusive
    • barronSpline

      public static double barronSpline(double x, double shape, double turning)
      A generalization on bias and gain functions that can represent both; this version is branch-less. This is based on this micro-paper by Jon Barron, which generalizes the earlier bias and gain rational functions by Schlick. The second and final page of the paper has useful graphs of what the s (shape) and t (turning point) parameters do; shape should be 0 or greater, while turning must be between 0 and 1, inclusive. This effectively combines two different curving functions so that they continue into each other when x equals turning. The shape parameter will cause this to imitate "smoothstep-like" splines when greater than 1 (where the values ease into their starting and ending levels), or to be the inverse when less than 1 (where values start like square root does, taking off very quickly, but also end like square does, landing abruptly at the ending level). You should only give x values between 0 and 1, inclusive.
      Parameters:
      x - progress through the spline, from 0 to 1, inclusive
      shape - must be greater than or equal to 0; values greater than 1 are "normal interpolations"
      turning - a value between 0.0 and 1.0, inclusive, where the shape changes
      Returns:
      a double between 0 and 1, inclusive
    • nextPowerOfTwo

      public static int nextPowerOfTwo(int n)
      Returns the next higher power of two relative to n, or n if it is already a power of two. This returns 2 if n is any value less than 2 (including negative numbers, but also 1, which is a power of two).
      Parameters:
      n - the lower bound for the result
      Returns:
      the next higher power of two that is greater than or equal to n
    • isPowerOfTwo

      public static boolean isPowerOfTwo(int value)
      Returns true if value is a power of two or is equal to Integer.MIN_VALUE; false otherwise.
      Parameters:
      value - any int
      Returns:
      true if value is a power of two (when treated as unsigned)
    • gamma

      public static double gamma(double x)
      A close approximation to the gamma function for positive doubles, using an algorithm by T. J. Stieltjes. Source here. This is exactly equivalent to MathExtras.factorial(x - 1.0).
      Parameters:
      x - a real number; should usually be positive
      Returns:
      the approximate gamma of the given x
    • factorial

      public static double factorial(double x)
      A close approximation to the factorial function for real numbers, using an algorithm by T. J. Stieltjes. This performs a variable number of multiplications that starts at 1 when x is between 5 and 6, and requires more multiplications the lower x goes (to potentially many if x is, for instance, -1000.0, which would need 1006 multiplications per call). As such, you should try to call this mostly on x values that are positive or have a low magnitude. Source here.
      Parameters:
      x - a real number; should not be both large and negative
      Returns:
      the generalized factorial of the given x
    • gamma

      public static float gamma(float x)
      A close approximation to the gamma function for positive floats, using an algorithm by T. J. Stieltjes. Source here. This is exactly equivalent to MathExtras.factorial(x - 1f).
      This does all of its math on doubles internally and only casts to float at the end.
      Parameters:
      x - a real number; should usually be positive
      Returns:
      the approximate gamma of the given x
    • factorial

      public static float factorial(float x)
      A close approximation to the factorial function for real numbers, using an algorithm by T. J. Stieltjes. This performs a variable number of multiplications that starts at 1 when x is between 5 and 6, and requires more multiplications the lower x goes (to potentially many if x is, for instance, -1000.0, which would need 1006 multiplications per call). As such, you should try to call this mostly on x values that are positive or have a low magnitude. Source here.
      This does all of its math on doubles internally and only casts to float at the end.
      Parameters:
      x - a real number; should not be both large and negative
      Returns:
      the generalized factorial of the given x
    • fibonacci

      public static int fibonacci(int n)
      Binet's formula for the Fibonacci sequence, which is a closed-form expression where which each resulting value is the sum of the two proceeding values. This has several useful applications, such as finding values within Pascal's triangle, which is itself useful in various areas of mathematics involving polynomial functions.
      Negative inputs are allowed here, but may behave differently than positive inputs. When given non-negative integer inputs, this is only correct for inputs from 0 to 46 inclusive; the largest Fibonacci number this can correctly calculate is 1836311903, given an input of 46. You can get a larger range of values by passing a long input to fibonacci(long).
      For more information see Wikipedia. This does not use the exact constant values in the "Computation by rounding" section, because minuscule adjustments to those constants proved to counterbalance accrued floating-point error for a few more inputs.
      Parameters:
      n - an int index; should be less than 47
      Returns:
      the Fibonacci number at index n, as an int
    • fibonacci

      public static long fibonacci(long n)
      Binet's formula for the Fibonacci sequence, which is a closed-form expression where which each resulting value is the sum of the two proceeding values. This has several useful applications, such as finding values within Pascal's triangle, which is itself useful in various areas of mathematics involving polynomial functions.
      Negative inputs are allowed here, but may behave differently than positive inputs. When given non-negative integer inputs, this is only correct for inputs from 0 to 77 inclusive; the largest Fibonacci number this can correctly calculate is 5527939700884757, given an input of 77. This means that all Fibonacci numbers that can be stored in a non-negative int can be produced by this method, as well as a substantial amount of non-negative long Fibonacci numbers. If you only have inputs that are less than 47, and you want int results, you can use fibonacci(int) instead.
      For more information see Wikipedia. This does not use the exact constant values in the "Computation by rounding" section, because minuscule adjustments to those constants proved to counterbalance accrued floating-point error for a few more inputs.
      Parameters:
      n - a long index; should be less than 78
      Returns:
      the Fibonacci number at index n, as a long
    • square

      public static float square(float n)
      Returns the square (second power) of its parameter. Purely here for convenience.
      Parameters:
      n - any float
      Returns:
      n * n
    • square

      public static double square(double n)
      Returns the square (second power) of its parameter. Purely here for convenience.
      Parameters:
      n - any double
      Returns:
      n * n
    • cube

      public static float cube(float n)
      Returns the cube (third power) of its parameter. Purely here for convenience.
      Parameters:
      n - any float
      Returns:
      n * n * n
    • cube

      public static double cube(double n)
      Returns the cube (third power) of its parameter. Purely here for convenience.
      Parameters:
      n - any double
      Returns:
      n * n * n
    • longFloor

      public static long longFloor(double t)
      Like Math.floor(double), but returns a long. Doesn't consider "weird doubles" like INFINITY and NaN. This is only faster than (long)Math.floor(t) on Java 8 for supported desktop platforms.
      Parameters:
      t - the double to find the floor for
      Returns:
      the floor of t, as a long
    • longFloor

      public static long longFloor(float t)
      Like Math.floor(double), but takes a float and returns a long. Doesn't consider "weird floats" like INFINITY and NaN. This is only faster than (long)Math.floor(t) on Java 8 for supported desktop platforms.
      Parameters:
      t - the double to find the floor for
      Returns:
      the floor of t, as a long
    • floor

      public static int floor(double t)
      Like Math.floor(double) , but returns an int. Doesn't consider "weird doubles" like INFINITY and NaN. This is only faster than (int)Math.floor(t) on Java 8 for supported desktop platforms.
      Parameters:
      t - the float to find the floor for
      Returns:
      the floor of t, as an int
    • fastFloor

      public static int fastFloor(float t)
      Like Math.floor(double), but takes a float and returns an int. Doesn't consider "weird floats" like INFINITY and NaN. This method will only properly floor floats from -16384 to Integer.MAX_VALUE - 16384, or 2147467263. Unlike floor(double), longFloor(float), and longFloor(double), this is significantly faster than (int)Math.floor(t).
      Taken from libGDX MathUtils.
      Parameters:
      t - a float from -16384 to 2147467263 (both inclusive)
      Returns:
      the floor of t, as an int
    • ceil

      public static int ceil(double t)
      Like Math.ceil(double), but returns an int. Doesn't consider "weird doubles" like INFINITY and NaN. This is only faster than (int)Math.ceil(t) on Java 8 for supported desktop platforms.
      Parameters:
      t - the float to find the ceiling for
      Returns:
      the ceiling of t, as an int
    • fastCeil

      public static int fastCeil(float t)
      Like Math.ceil(double), but takes a float and returns an int. Doesn't consider "weird floats" like INFINITY and NaN. This method will only properly ceil floats from -16384 to Integer.MAX_VALUE - 16384, or 2147467263. Unlike ceil(float), this is significantly faster than (int)Math.ceil(t).
      Parameters:
      t - the float to find the ceiling for
      Returns:
      the ceiling of t, as an int
    • floor

      public static int floor(float value)
      Returns the largest int less than or equal to the specified float. Doesn't consider "weird floats" like INFINITY and NaN. This is only faster than (int)Math.floor(t) on Java 8 for supported desktop platforms.
      Taken from libGDX MathUtils.
      Parameters:
      value - any float
      Returns:
      the floor of value, as an int
    • floorPositive

      public static int floorPositive(float value)
      Returns the largest int less than or equal to the specified float. This method will only properly floor floats that are positive. Note, this method simply casts the float to int.
      Parameters:
      value - any positive float
    • ceil

      public static int ceil(float value)
      Returns the smallest int greater than or equal to the specified float. Doesn't consider "weird floats" like INFINITY and NaN. This is only faster than (int)Math.ceil(t) on Java 8 for supported desktop platforms.
      Parameters:
      value - a float from -(2^14) to (Float.MAX_VALUE - 2^14)
    • ceilPositive

      public static int ceilPositive(float value)
      Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats that are positive.
      Parameters:
      value - any positive float
    • round

      public static int round(float value)
      Returns the closest integer to the specified float. This method will only properly round floats from -(2^14) to (Float.MAX_VALUE - 2^14).
      Parameters:
      value - a float from -(2^14) to (Float.MAX_VALUE - 2^14)
    • roundPositive

      public static int roundPositive(float value)
      Returns the closest integer to the specified float. This method will only properly round floats that are positive.
      Parameters:
      value - any positive float
    • truncate

      public static float truncate(float n)
      Forces precision loss on the given float so very small fluctuations away from an integer will be erased. This is meant primarily for cleaning up floats, so they can be presented without needing scientific notation. It leaves about 3 decimal digits after the point intact, and should make any digits after that simply 0.
      Parameters:
      n - any float, but typically a fairly small one (between -8 and 8, as a guideline)
      Returns:
      n with its 13 least significant bits effectively removed
    • truncate

      public static double truncate(double n)
      Forces precision loss on the given double so very small fluctuations away from an integer will be erased. This is meant primarily for cleaning up doubles, so they can be presented without needing scientific notation. It leaves about 3 decimal digits after the point intact, and should make any digits after that simply 0.
      Parameters:
      n - any double, but typically a fairly small one (between -8 and 8, as a guideline)
      Returns:
      n with its 42 least significant bits effectively removed
    • lerp

      public static float lerp(float fromValue, float toValue, float progress)
      Linearly interpolates between fromValue to toValue on progress position.
      Parameters:
      fromValue - starting float value; can be any finite float
      toValue - ending float value; can be any finite float
      progress - how far the interpolation should go, between 0 (equal to fromValue) and 1 (equal to toValue)
    • norm

      public static float norm(float rangeStart, float rangeEnd, float value)
      Linearly normalizes value from a range. Range must not be empty. This is the inverse of lerp(float, float, float).
      Parameters:
      rangeStart - range start normalized to 0
      rangeEnd - range end normalized to 1
      value - value to normalize
      Returns:
      normalized value; values outside the range are not clamped to 0 and 1
    • map

      public static float map(float inRangeStart, float inRangeEnd, float outRangeStart, float outRangeEnd, float value)
      Linearly map a value from one range to another. Input range must not be empty. This is the same as chaining norm(float, float, float) from input range and lerp(float, float, float) to output range.
      Parameters:
      inRangeStart - input range start
      inRangeEnd - input range end
      outRangeStart - output range start
      outRangeEnd - output range end
      value - value to map
      Returns:
      mapped value; values outside the input range are not clamped to output range
    • lerp

      public static double lerp(double fromValue, double toValue, double progress)
      Linearly interpolates between fromValue to toValue on progress position.
      Parameters:
      fromValue - starting double value; can be any finite double
      toValue - ending double value; can be any finite double
      progress - how far the interpolation should go, between 0 (equal to fromValue) and 1 (equal to toValue)
    • norm

      public static double norm(double rangeStart, double rangeEnd, double value)
      Linearly normalizes value from a range. Range must not be empty. This is the inverse of lerp(double, double, double).
      Parameters:
      rangeStart - range start normalized to 0
      rangeEnd - range end normalized to 1
      value - value to normalize
      Returns:
      normalized value; values outside the range are not clamped to 0 and 1
    • map

      public static double map(double inRangeStart, double inRangeEnd, double outRangeStart, double outRangeEnd, double value)
      Linearly map a value from one range to another. Input range must not be empty. This is the same as chaining norm(double, double, double) from input range and lerp(double, double, double) to output range.
      Parameters:
      inRangeStart - input range start
      inRangeEnd - input range end
      outRangeStart - output range start
      outRangeEnd - output range end
      value - value to map
      Returns:
      mapped value; values outside the input range are not clamped to output range
    • lerpAngle

      public static float lerpAngle(float fromRadians, float toRadians, float progress)
      Linearly interpolates between two angles in radians. Takes into account that angles wrap at PI2 and always takes the direction with the smallest delta angle.
      Parameters:
      fromRadians - start angle in radians
      toRadians - target angle in radians
      progress - interpolation value in the range [0, 1]
      Returns:
      the interpolated angle in the range [0, PI2)
    • lerpAngleDeg

      public static float lerpAngleDeg(float fromDegrees, float toDegrees, float progress)
      Linearly interpolates between two angles in degrees. Takes into account that angles wrap at 360 degrees and always takes the direction with the smallest delta angle.
      Parameters:
      fromDegrees - start angle in degrees
      toDegrees - target angle in degrees
      progress - interpolation value in the range [0, 1]
      Returns:
      the interpolated angle in the range [0, 360)
    • lerpAngleTurns

      public static float lerpAngleTurns(float fromTurns, float toTurns, float progress)
      Linearly interpolates between two angles in turns. Takes into account that angles wrap at 1.0 and always takes the direction with the smallest delta angle. This version, unlike the versions for radians and degrees, avoids any modulus operation (instead calling fastFloor(float) twice).
      Parameters:
      fromTurns - start angle in turns
      toTurns - target angle in turns
      progress - interpolation value in the range [0, 1]
      Returns:
      the interpolated angle in the range [0, 1)
    • lerpAngle

      public static double lerpAngle(double fromRadians, double toRadians, double progress)
      Linearly interpolates between two angles in radians. Takes into account that angles wrap at PI2 and always takes the direction with the smallest delta angle.
      Parameters:
      fromRadians - start angle in radians
      toRadians - target angle in radians
      progress - interpolation value in the range [0, 1]
      Returns:
      the interpolated angle in the range [0, PI2)
    • lerpAngleDeg

      public static double lerpAngleDeg(double fromDegrees, double toDegrees, double progress)
      Linearly interpolates between two angles in degrees. Takes into account that angles wrap at 360 degrees and always takes the direction with the smallest delta angle.
      Parameters:
      fromDegrees - start angle in degrees
      toDegrees - target angle in degrees
      progress - interpolation value in the range [0, 1]
      Returns:
      the interpolated angle in the range [0, 360)
    • lerpAngleTurns

      public static double lerpAngleTurns(double fromTurns, double toTurns, double progress)
      Linearly interpolates between two angles in turns. Takes into account that angles wrap at 1.0 and always takes the direction with the smallest delta angle. This version, unlike the versions for radians and degrees, avoids any modulus operation (instead calling floor(double) twice).
      Parameters:
      fromTurns - start angle in turns
      toTurns - target angle in turns
      progress - interpolation value in the range [0, 1]
      Returns:
      the interpolated angle in the range [0, 1)
    • isZero

      public static boolean isZero(float value)
      Returns true if the value is zero (using the default tolerance, FLOAT_ROUNDING_ERROR, as outer bound).
      Parameters:
      value - any float
    • isZero

      public static boolean isZero(float value, float tolerance)
      Returns true if the value is zero, using the given tolerance.
      Parameters:
      value - any float
      tolerance - represent an outer bound below which the value is considered zero.
    • isZero

      public static boolean isZero(double value, double tolerance)
      Returns true if the value is zero. A suggested tolerance is 0x1p-20, which is the same value the float overload uses for its default tolerance, or a smaller number to reduce false-positives, such as 0x1p-32.
      Parameters:
      value - any double
      tolerance - represent an outer bound below which the value is considered zero.
    • zigzag

      public static float zigzag(float value)
      Takes any float and produces a float in the -1f to 1f range, with similar inputs producing close to a consistent rate of up and down through the range. This is meant for noise, where it may be useful to limit the amount of change between nearby points' noise values and prevent sudden "jumps" in noise value. An input of any even number should produce something very close to -1f, any odd number should produce something very close to 1f, and any number halfway between two incremental integers (like 8.5f or -10.5f) should produce 0f or a very small fraction. This method is closely related to sway(float), which will smoothly curve its output to produce more values that are close to -1 or 1.
      Parameters:
      value - any float
      Returns:
      a float from -1f (inclusive) to 1f (inclusive)
    • sway

      public static float sway(float value)
      Very similar to TrigTools.sinTurns(float) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently. This looks like a squished sine wave when graphed, and is essentially just interpolating between each pair of odd and even inputs using what FastNoise calls QUINTIC interpolation. This interpolation is slightly flatter at peaks and valleys than a sine wave is.
      An input of any even number should produce something very close to -1f, any odd number should produce something very close to 1f, and any number halfway between two incremental integers (like 8.5f or -10.5f) should produce 0f or a very small fraction. In the (unlikely) event that this is given a float that is too large to represent many or any non-integer values, this will simply return -1f or 1f.
      This version of a sway method uses quintic interpolation; it uses up to the fifth power of value.
      Parameters:
      value - any float other than NaN or infinite values; extremely large values can't work properly
      Returns:
      a float from -1f (inclusive) to 1f (inclusive)
    • swayCubic

      public static float swayCubic(float value)
      Very similar to TrigTools.sinTurns(float) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently. This looks like a squished sine wave when graphed, and is essentially just interpolating between each pair of odd and even inputs using what is sometimes called HERMITE interpolation. This interpolation is rounder at peaks and valleys than a sine wave is; it is also called smoothstep in GLSL, and is called cubic here because it gets the third power of a value.
      An input of any even number should produce something very close to -1f, any odd number should produce something very close to 1f, and any number halfway between two incremental integers (like 8.5f or -10.5f) should produce 0f or a very small fraction. In the (unlikely) event that this is given a float that is too large to represent many or any non-integer values, this will simply return -1f or 1f.
      Parameters:
      value - any float other than NaN or infinite values; extremely large values can't work properly
      Returns:
      a float from -1f (inclusive) to 1f (inclusive)
    • swayTight

      public static float swayTight(float value)
      Takes any float and produces a float in the 0f to 1f range, with a graph of input to output that looks much like a sine wave, curving to have a flat slope when given an integer input and a steep slope when the input is halfway between two integers, smoothly curving at any points between those extremes. This is meant for noise, where it may be useful to limit the amount of change between nearby points' noise values and prevent both sudden "jumps" in noise value and "cracks" where a line takes a sudden jagged movement at an angle.
      An input of any even number should produce something very close to 0f, any odd number should produce something very close to 1f, and any number halfway between two incremental integers (like 8.5f or -10.5f) should produce 0.5f. In the (unlikely) event that this is given a float that is too large to represent many or any non-integer values, this will simply return 0f or 1f. This version is called "Tight" because its range is tighter than sway(float).
      This version of a sway method uses quintic interpolation; it uses up to the fifth power of value.
      Parameters:
      value - any float other than NaN or infinite values; extremely large values can't work properly
      Returns:
      a float from 0f (inclusive) to 1f (inclusive)
    • zigzag

      public static double zigzag(double value)
      Takes any double and produces a double in the -1.0 to 1.0 range, with similar inputs producing close to a consistent rate of up and down through the range. This is meant for noise, where it may be useful to limit the amount of change between nearby points' noise values and prevent sudden "jumps" in noise value. An input of any even number should produce something very close to -1.0, any odd number should produce something very close to 1.0, and any number halfway between two incremental integers (like 8.5 or -10.5) should produce 0.0 or a very small fraction. This method is closely related to sway(double), which will smoothly curve its output to produce more values that are close to -1 or 1.
      Parameters:
      value - any double
      Returns:
      a double from -1.0 (inclusive) to 1.0 (inclusive)
    • sway

      public static double sway(double value)
      Very similar to TrigTools.sinTurns(double) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently. This looks like a squished sine wave when graphed, and is essentially just interpolating between each pair of odd and even inputs using what FastNoise calls QUINTIC interpolation. This interpolation is slightly flatter at peaks and valleys than a sine wave is.
      An input of any even number should produce something very close to -1.0, any odd number should produce something very close to 1.0, and any number halfway between two incremental integers (like 8.5 or -10.5) should produce 0.0 or a very small fraction. In the (unlikely) event that this is given a double that is too large to represent many or any non-integer values, this will simply return -1.0 or 1.0.
      This version of a sway method uses quintic interpolation; it uses up to the fifth power of value.
      Parameters:
      value - any double other than NaN or infinite values; extremely large values can't work properly
      Returns:
      a double from -1.0 (inclusive) to 1.0 (inclusive)
    • swayCubic

      public static double swayCubic(double value)
      Very similar to TrigTools.sinTurns(double) with half frequency, or Math.sin(double) with Math.PI frequency, but optimized (and shaped) a little differently. This looks like a squished sine wave when graphed, and is essentially just interpolating between each pair of odd and even inputs using what is sometimes called HERMITE interpolation. This interpolation is rounder at peaks and valleys than a sine wave is; it is also called smoothstep in GLSL, and is called cubic here because it gets the third power of a value.
      An input of any even number should produce something very close to -1.0, any odd number should produce something very close to 1.0, and any number halfway between two incremental integers (like 8.5 or -10.5) should produce 0.0 or a very small fraction. In the (unlikely) event that this is given a double that is too large to represent many or any non-integer values, this will simply return -1.0 or 1.0.
      Parameters:
      value - any double other than NaN or infinite values; extremely large values can't work properly
      Returns:
      a double from -1.0 (inclusive) to 1.0 (inclusive)
    • swayTight

      public static double swayTight(double value)
      Takes any double and produces a double in the 0.0 to 1.0 range, with a graph of input to output that looks much like a sine wave, curving to have a flat slope when given an integer input and a steep slope when the input is halfway between two integers, smoothly curving at any points between those extremes. This is meant for noise, where it may be useful to limit the amount of change between nearby points' noise values and prevent both sudden "jumps" in noise value and "cracks" where a line takes a sudden jagged movement at an angle.
      An input of any even number should produce something very close to 0.0, any odd number should produce something very close to 1.0, and any number halfway between two incremental integers (like 8.5 or -10.5) should produce 0.5. In the (unlikely) event that this is given a double that is too large to represent many or any non-integer values, this will simply return 0.0 or 1.0. This version is called "Tight" because its range is tighter than sway(double).
      This version of a sway method uses quintic interpolation; it uses up to the fifth power of value.
      Parameters:
      value - any double other than NaN or infinite values; extremely large values can't work properly
      Returns:
      a double from 0.0 (inclusive) to 1.0 (inclusive)