Class Maths

java.lang.Object
com.globalmentor.java.Maths

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

    Constructors
    Constructor
    Description
     
  • Method Summary

    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(Collection<? extends Integer> integers)
    Determines the lowest integer from a collection of integers.
    static int
    min(Collection<? extends 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 Details

    • Maths

      public Maths()
  • Method Details

    • 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:
    • 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:
    • 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:
    • min

      public static int min(Collection<? extends 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:
      NullPointerException - if the given collection is null or one of its elements is null.
    • min

      public static int min(Collection<? extends 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:
      NullPointerException - if the given collection is null or one of its elements is null.