Package ch.njol.util

Class Math2

java.lang.Object
ch.njol.util.Math2

public abstract class Math2 extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    ceil​(double d)
    Ceils the given double and returns the result as a long.
    static long
    ceil​(float f)
     
    static int
    ceilI​(double d)
     
    static int
    ceilI​(float f)
     
    static double
    fit​(double min, double x, double max)
    Fits a number into the given interval.
    static float
    fit​(float min, float x, float max)
    Fits a number into the given interval.
    static int
    fit​(int min, int x, int max)
    Fits a number into the given interval.
    static long
    fit​(long min, long x, long max)
    Fits a number into the given interval.
    static short
    fit​(short min, short x, short max)
    Fits a number into the given interval.
    static long
    floor​(double d)
    Floors the given double and returns the result as a long.
    static long
    floor​(float f)
     
    static int
    floorI​(double d)
     
    static int
    floorI​(float f)
     
    static double
    frac​(double d)
     
    static float
    frac​(float f)
     
    static double
    max​(double... nums)
     
    static double
    max​(double a, double b, double c)
     
    static int
    max​(int... nums)
     
    static int
    max​(int a, int b, int c)
     
    static double
    min​(double... nums)
     
    static double
    min​(double a, double b, double c)
     
    static int
    min​(int... nums)
     
    static int
    min​(int a, int b, int c)
     
    static int
    minPositive​(int... nums)
    finds the smallest positive number (≥0) in the sequence
    static double
    mod​(double d, double m)
    Modulo that returns positive values even for negative arguments.
    static float
    mod​(float d, float m)
    Modulo that returns positive values even for negative arguments.
    static int
    mod​(int d, int m)
    Modulo that returns positive values even for negative arguments.
    static long
    mod​(long d, long m)
    Modulo that returns positive values even for negative arguments.
    static int
    nextPowerOfTwo​(int n)
    Gets the smallest power of two ≥n.
    static long
    nextPowerOfTwo​(long n)
    Gets the smallest power of two ≥n.
    static long
    round​(double d)
    Rounds the given double (where .5 is rounded up) and returns the result as a long.
    static long
    round​(float f)
    Rounds the given float (where .5 is rounded up) and returns the result as a long.
    static int
    roundI​(double d)
     
    static int
    roundI​(float f)
    Rounds the given float (where .5 is rounded up) and returns the result as an int.
    static double
    safe​(double d)
    Guarantees a double is neither NaN nor INF.
    static float
    safe​(float f)
    Guarantees a float is neither NaN nor INF.
    static int
    sign​(byte i)
     
    static int
    sign​(double d)
     
    static int
    sign​(float f)
     
    static int
    sign​(int i)
     
    static int
    sign​(long i)
     
    static int
    sign​(short i)
     
    static double
    smoothStep​(double x, double x1, double x2)
    Performs a hermite interpolation between the given values, or returns 0 or 1 respectively if the value is out of range.

    Methods inherited from class java.lang.Object

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

    • Math2

      public Math2()
  • Method Details

    • min

      public static int min(int a, int b, int c)
    • min

      public static int min(int... nums)
    • max

      public static int max(int a, int b, int c)
    • max

      public static int max(int... nums)
    • min

      public static double min(double a, double b, double c)
    • min

      public static double min(double... nums)
    • max

      public static double max(double a, double b, double c)
    • max

      public static double max(double... nums)
    • minPositive

      public static int minPositive(int... nums)
      finds the smallest positive number (≥0) in the sequence
      Parameters:
      nums -
      Returns:
      smallest positive number in the sequence or -1 if no number is positive
    • fit

      public static int fit(int min, int x, int max)
      Fits a number into the given interval. The method's behaviour when min > max is unspecified.
      Returns:
      x <= min ? min : x >= max ? max : x
    • fit

      public static short fit(short min, short x, short max)
      Fits a number into the given interval. The method's behaviour when min > max is unspecified.
      Returns:
      x <= min ? min : x >= max ? max : x
    • fit

      public static long fit(long min, long x, long max)
      Fits a number into the given interval. The method's behaviour when min > max is unspecified.
      Returns:
      x <= min ? min : x >= max ? max : x
    • fit

      public static float fit(float min, float x, float max)
      Fits a number into the given interval. The method's behaviour when min > max is unspecified.
      Returns:
      x <= min ? min : x >= max ? max : x
    • fit

      public static double fit(double min, double x, double max)
      Fits a number into the given interval. The method's behaviour when min > max is unspecified.
      Returns:
      x <= min ? min : x >= max ? max : x
    • mod

      public static double mod(double d, double m)
      Modulo that returns positive values even for negative arguments.
      Parameters:
      d -
      m -
      Returns:
      d%m < 0 ? d%m + m : d%m
    • mod

      public static float mod(float d, float m)
      Modulo that returns positive values even for negative arguments.
      Parameters:
      d -
      m -
      Returns:
      d%m < 0 ? d%m + m : d%m
    • mod

      public static int mod(int d, int m)
      Modulo that returns positive values even for negative arguments.
      Parameters:
      d -
      m -
      Returns:
      d%m < 0 ? d%m + m : d%m
    • mod

      public static long mod(long d, long m)
      Modulo that returns positive values even for negative arguments.
      Parameters:
      d -
      m -
      Returns:
      d%m < 0 ? d%m + m : d%m
    • floor

      public static long floor(double d)
      Floors the given double and returns the result as a long.

      This method can be up to 20 times faster than the default Math.floor(double) (both with and without casting to long).

    • ceil

      public static long ceil(double d)
      Ceils the given double and returns the result as a long.

      This method can be up to 20 times faster than the default Math.ceil(double) (both with and without casting to long).

    • round

      public static long round(double d)
      Rounds the given double (where .5 is rounded up) and returns the result as a long.

      This method is more exact and faster than Math.round(double) of Java 7 and older.

    • floorI

      public static int floorI(double d)
    • ceilI

      public static int ceilI(double d)
    • roundI

      public static int roundI(double d)
    • floor

      public static long floor(float f)
    • ceil

      public static long ceil(float f)
    • round

      public static long round(float f)
      Rounds the given float (where .5 is rounded up) and returns the result as a long.

      This method is more exact and faster than Math.round(float) of Java 7 and older.

    • floorI

      public static int floorI(float f)
    • ceilI

      public static int ceilI(float f)
    • roundI

      public static int roundI(float f)
      Rounds the given float (where .5 is rounded up) and returns the result as an int.

      This method is more exact and faster than Math.round(float) of Java 7 and older.

    • nextPowerOfTwo

      public static int nextPowerOfTwo(int n)
      Gets the smallest power of two ≥n. Returns Integer.MIN_VALUE if n > 230.
    • nextPowerOfTwo

      public static long nextPowerOfTwo(long n)
      Gets the smallest power of two ≥n. Returns Long.MIN_VALUE if n > 262.
    • frac

      public static double frac(double d)
      Returns:
      The floating point part of d in the range [0, 1)
    • frac

      public static float frac(float f)
      Returns:
      The floating point part of f in the range [0, 1)
    • sign

      public static int sign(byte i)
      Returns:
      -1 if i is negative, 0 if i is 0, or 1 if i is positive
    • sign

      public static int sign(short i)
      Returns:
      -1 if i is negative, 0 if i is 0, or 1 if i is positive
    • sign

      public static int sign(int i)
      Returns:
      -1 if i is negative, 0 if i is 0, or 1 if i is positive
    • sign

      public static int sign(long i)
      Returns:
      -1 if i is negative, 0 if i is 0, or 1 if i is positive
    • sign

      public static int sign(float f)
      Returns:
      -1 if f is negative, 0 if f is +0, -0 or NaN, or 1 if f is positive
    • sign

      public static int sign(double d)
      Returns:
      -1 if d is negative, 0 if d is +0, -0 or NaN, or 1 if d is positive
    • smoothStep

      public static double smoothStep(double x, double x1, double x2)
      Performs a hermite interpolation between the given values, or returns 0 or 1 respectively if the value is out of range.

      Specifically this method returns d * d * (3 - 2 * d), where d = fit(0, (x - x1) / (x2 - x1), 1). This is very similar to 0.5 - 0.5 * cos(PI * d).

      This function is essentially equal to GLSL's smoothstep, but with a different argument order.

      Parameters:
      x - The value to get the step at
      x1 - The lower end of the step
      x2 - The upper end of the step
      Returns:
      The step's value at x
    • safe

      public static float safe(float f)
      Guarantees a float is neither NaN nor INF. Useful for situations when safe floats are required.
      Parameters:
      f -
      Returns:
      0 if f is NaN or INF, otherwise f
    • safe

      public static double safe(double d)
      Guarantees a double is neither NaN nor INF. Useful for situations when safe doubles are required.
      Parameters:
      d -
      Returns:
      0 if d is NaN or INF, otherwise d