Class Maths


  • public class Maths
    extends java.lang.Object
    Utilities for working with math.
    Author:
    Garret Wilson
    • Constructor Summary

      Constructors 
      Constructor Description
      Maths()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int floor​(int value, int position)
      Finds the floor of a value with a particular precision of a specified digit.
      static long floor​(long value, int position)
      Finds the floor of a value with a particular precision of a specified digit.
      static double max​(double a, double b, double c)
      Returns the greatest of three double values
      static short max​(short a, short b)
      Returns the greater of two short values
      static double min​(double a, double b, double c)
      Returns the least of three double values
      static int min​(java.util.Collection<? extends java.lang.Integer> integers)
      Determines the lowest integer from a collection of integers.
      static int min​(java.util.Collection<? extends java.lang.Integer> integers, int max)
      Determines the lowest integer from a collection of integers with a given maximum.
      static float round​(float a, int position)
      Rounds a value with a particular precision of a specified digit.
      • Methods inherited from class java.lang.Object

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

      • Maths

        public Maths()
    • Method Detail

      • floor

        public static int floor​(int value,
                                int position)
        Finds the floor of a value with a particular precision of a specified digit. A position of zero will result in the input value. Otherwise, the value will effectively be divided by 10 to the power of position before rounding, then multiplied by the same value after rounding.

        For example, if a long value represents milliseconds, calling floor(value, -3) will result in rounding down to the nearest millisecond.

        Parameters:
        value - The value to round.
        position - The position relative to the decimal to which the value should be rounded, with negative numbers indicating fractional positions and positive numbers representing integer positions.
        Returns:
        The value rounded down at the given decimal position.
      • floor

        public static long floor​(long value,
                                 int position)
        Finds the floor of a value with a particular precision of a specified digit. A position of zero will result in the input value. Otherwise, the value will effectively be divided by 10 to the power of position before rounding, then multiplied by the same value after rounding.

        For example, if a long value represents milliseconds, calling floor(value, -3) will result in rounding down to the nearest millisecond.

        Parameters:
        value - The value to round.
        position - The position relative to the decimal to which the value should be rounded, with negative numbers indicating fractional positions and positive numbers representing integer positions.
        Returns:
        The value rounded down at the given decimal position.
      • round

        public static float round​(float a,
                                  int position)
        Rounds a value with a particular precision of a specified digit. A position of zero will round to a normal integer. Otherwise, the value will be effectively divided by 10 to the power of position before rounding, then multiplied by the same value after rounding.
        Parameters:
        a - A float value.
        position - The position relative to the decimal to which the value should be rounded, with negative numbers indicating fractional positions and positive numbers representing integer positions.
        Returns:
        The value rounded at the given position.
      • max

        public static short max​(short a,
                                short b)
        Returns the greater of two short values
        Parameters:
        a - An argument.
        b - Another argument.
        Returns:
        The larger of a and b.
        See Also:
        Math.max(int, int)
      • max

        public static double max​(double a,
                                 double b,
                                 double c)
        Returns the greatest of three double values
        Parameters:
        a - An argument.
        b - Another argument.
        c - Yet another argument.
        Returns:
        The greatest of a, b, and c.
        See Also:
        Math.max(double, double)
      • min

        public static double min​(double a,
                                 double b,
                                 double c)
        Returns the least of three double values
        Parameters:
        a - An argument.
        b - Another argument.
        c - Yet another argument.
        Returns:
        The least of a, b, and c.
        See Also:
        Math.min(double, double)
      • min

        public static int min​(java.util.Collection<? extends java.lang.Integer> integers)
        Determines the lowest integer from a collection of integers.
        Parameters:
        integers - The collection of integers.
        Returns:
        The minimum of the given integers, or the Integer.MAX_VALUE if there are no integers.
        Throws:
        java.lang.NullPointerException - if the given collection is null or one of its elements is null.
      • min

        public static int min​(java.util.Collection<? extends java.lang.Integer> integers,
                              int max)
        Determines the lowest integer from a collection of integers with a given maximum.
        Parameters:
        integers - The collection of integers.
        max - The maximum value this method can return.
        Returns:
        The minimum of the given integers, or the given maximum if there are no integers.
        Throws:
        java.lang.NullPointerException - if the given collection is null or one of its elements is null.