Class MathUtil

java.lang.Object
com.intellij.util.MathUtil

public final class MathUtil extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    between(double min, double med, double max, double epsilon)
     
    static boolean
    between(float min, float med, float max, float epsilon)
     
    static double
    clamp(double value, double min, double max)
    Clamps the value to fit between min and max
    static float
    clamp(float value, float min, float max)
    Clamps the value to fit between min and max
    static int
    clamp(int value, int min, int max)
    Clamps the value to fit between min and max
    static long
    clamp(long value, long min, long max)
    Clamps the value to fit between min and max
    static int
    compare(double a, double b, double epsilon)
    Imprecise version of Double.compare(double, double) with the given accuracy.
    static int
    compare(float a, float b, float epsilon)
    Imprecise version of Float.compare(float, float) with the given accuracy.
    static boolean
    equals(double a, double b, double epsilon)
     
    static boolean
    equals(float a, float b, float epsilon)
     
    static int
    Returns the closest non-negative int value to the absolute value of a.

    Methods inherited from class java.lang.Object

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

    • MathUtil

      public MathUtil()
  • Method Details

    • nonNegativeAbs

      public static int nonNegativeAbs(int a)
      Returns the closest non-negative int value to the absolute value of a. Works the same way as Math.abs(int) for all values except Integer.MIN_VALUE, for which Math.abs(int) returns negative value but this method returns Integer.MAX_VALUE.

      Use this method instead of the standard one if it's important to get non-negative result and Integer.MIN_VALUE may be passed as an argument (e.g. if the argument is a result of Object.hashCode() or Random.nextInt() call).

    • clamp

      public static int clamp(int value, int min, int max)
      Clamps the value to fit between min and max
      Parameters:
      value - value to clamp
      min - min allowed value
      max - max allowed value
      Returns:
      a clamped value that fits into [min..max] interval
      Throws:
      IllegalArgumentException - if min > max
    • clamp

      public static long clamp(long value, long min, long max)
      Clamps the value to fit between min and max
      Parameters:
      value - value to clamp
      min - min allowed value
      max - max allowed value
      Returns:
      a clamped value that fits into [min..max] interval
      Throws:
      IllegalArgumentException - if min > max
    • clamp

      public static double clamp(double value, double min, double max)
      Clamps the value to fit between min and max
      Parameters:
      value - value to clamp
      min - min allowed value
      max - max allowed value
      Returns:
      a clamped value that fits into [min..max] interval
      Throws:
      IllegalArgumentException - if min > max
    • clamp

      public static float clamp(float value, float min, float max)
      Clamps the value to fit between min and max
      Parameters:
      value - value to clamp
      min - min allowed value
      max - max allowed value
      Returns:
      a clamped value that fits into [min..max] interval
      Throws:
      IllegalArgumentException - if min > max
    • equals

      public static boolean equals(float a, float b, float epsilon)
      Returns:
      true if a == b accurate to epsilon
    • equals

      public static boolean equals(double a, double b, double epsilon)
      Returns:
      true if a == b accurate to epsilon
    • compare

      public static int compare(float a, float b, float epsilon)
      Imprecise version of Float.compare(float, float) with the given accuracy.
      Parameters:
      epsilon - Accuracy
    • compare

      public static int compare(double a, double b, double epsilon)
      Imprecise version of Double.compare(double, double) with the given accuracy.
      Parameters:
      epsilon - Accuracy
    • between

      public static boolean between(float min, float med, float max, float epsilon)
      Returns:
      true if min <= med <= max accurate to epsilon
    • between

      public static boolean between(double min, double med, double max, double epsilon)
      Returns:
      true if min <= med <= max accurate to epsilon