java.lang.Object
ushiosan.jvm_utilities.lang.Maths

public final class Maths extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default decimal operations tolerance
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    clamp(double value, double min, double max)
    Keeps the value within a specified range.
    static double
    distance(double v1, double v2)
    Returns the distance between two values.
    static boolean
    equals(double v1, double v2)
    Check if two decimal numbers are equal.
    static boolean
    equals(double v1, double v2, double tolerance)
    Check if two decimal numbers are equal.
    static boolean
    isZero(double value)
    Checks if a decimal number is zero
    static boolean
    isZero(double value, double tolerance)
    Checks if a decimal number is zero
    static double
    lerp(double base, double objective, double amount)
    Linear interpolation between two numbers.
    static double
    lerpPrecise(double base, double objective, double amount)
    Linear interpolation between two numbers.
    static double
    normalize(double start, double end, double current)
    Linearly normalizes two numbers.
    static double
    percentage(double value, double percentage)
    Calculates the percentage of an amount.
    static double
    percentageValue(double percentage)
    Converts a percent based on hundreds to a decimal percent.
    static double
    toDegrees(double radians)
    Performs a conversion from radians to degrees
    static double
    toRadians(double degrees)
    Performs a conversion from degrees to radians

    Methods inherited from class java.lang.Object

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

    • DECIMAL_TOLERANCE

      public static final double DECIMAL_TOLERANCE
      Default decimal operations tolerance

      32-bit number tolerance

      See Also:
  • Method Details

    • lerp

      public static double lerp(double base, double objective, double amount)
      Linear interpolation between two numbers.
      Parameters:
      base - the base number
      objective - the destination number
      amount - the amount of distance in each iteration
      Returns:
      the result of the iteration
    • lerpPrecise

      public static double lerpPrecise(double base, double objective, double amount)
      Linear interpolation between two numbers.

      Unlike lerp(double, double, double), this method is more precise and performs an intermediate comparison (but has a slightly higher performance cost).

      Parameters:
      base - the base number
      objective - the destination number
      amount - the amount of distance in each iteration
      Returns:
      the result of the iteration
    • distance

      public static double distance(double v1, double v2)
      Returns the distance between two values.
      Parameters:
      v1 - value 1
      v2 - value 2
      Returns:
      the distance between two values
    • normalize

      public static double normalize(double start, double end, double current)
      Linearly normalizes two numbers. This range must not be empty.
      Parameters:
      start - the start range
      end - the end of range
      current - the value to normalize
      Returns:
      the normalized value.
    • clamp

      public static double clamp(double value, double min, double max)
      Keeps the value within a specified range.
      Parameters:
      value - the number to clamp
      min - the minimum value of the range
      max - the maximum value of the range
      Returns:
      the constrained value within the given range
    • equals

      public static boolean equals(double v1, double v2, double tolerance)
      Check if two decimal numbers are equal. Decimal number operations can lead to many errors because they take into account decimals and operations that may seem the same, in reality they are not:
      
       // JVM Operations
       float x = 0.2f;
       float y = x - 0.1f;
       if(y == 0.1){ // can be "false" because "y" is 0.99999999999999...
           // ...
       }
       
      that is why this method uses tolerance, to avoid this type of problem.
      Parameters:
      v1 - first value
      v2 - second value
      tolerance - operation tolerance
      Returns:
      true if the numbers are equal or false otherwise
    • equals

      public static boolean equals(double v1, double v2)
      Check if two decimal numbers are equal.
      Parameters:
      v1 - first value
      v2 - second value
      Returns:
      true if the numbers are equal or false otherwise
      See Also:
    • isZero

      public static boolean isZero(double value, double tolerance)
      Checks if a decimal number is zero
      Parameters:
      value - the value to check
      tolerance - inspection tolerance
      Returns:
      true if the number is 0 or false otherwise
    • isZero

      public static boolean isZero(double value)
      Checks if a decimal number is zero
      Parameters:
      value - the value to check
      Returns:
      true if the number is 0 or false otherwise
      See Also:
    • toDegrees

      public static double toDegrees(double radians)
      Performs a conversion from radians to degrees
      Parameters:
      radians - the radians to convert
      Returns:
      the conversion from radians to degrees
      See Also:
      • RADIANS_DEGREE
    • toRadians

      public static double toRadians(double degrees)
      Performs a conversion from degrees to radians
      Parameters:
      degrees - the degrees to convert
      Returns:
      the conversion from degrees to radians
      See Also:
      • DEGREE_RADIANS
    • percentage

      public static double percentage(double value, double percentage)
      Calculates the percentage of an amount.

      Example:

      
       double fiftyPercent = Maths.percentage(500, 50); // 50% -> 250
       double doubleValue = Maths.percentage(120, 200); // 200% -> 240
       
      Parameters:
      value - the value to convert
      percentage - the percentage to calculate
      Returns:
      the percentage calculated based on the given amount
      See Also:
    • percentageValue

      public static double percentageValue(double percentage)
      Converts a percent based on hundreds to a decimal percent.

      Example:

      
       double fifty = Maths.percentageValue(50); // 50% -> 0.5
       double ten = Maths.percentageValue(1);    // 10% -> 0.1
       double one = Maths.percentageValue(1);    // 01% -> 0.01
       
      Parameters:
      percentage - the percentage to calculate
      Returns:
      the decimal percentage value
      See Also: