Package com.globalmentor.java
Class Maths
java.lang.Object
com.globalmentor.java.Maths
Utilities for working with math.
- Author:
- Garret Wilson
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 threedouble
valuesstatic short
max
(short a, short b) Returns the greater of twoshort
valuesstatic double
min
(double a, double b, double c) Returns the least of threedouble
valuesstatic 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.
-
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 twoshort
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 threedouble
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 threedouble
values- Parameters:
a
- An argument.b
- Another argument.c
- Yet another argument.- Returns:
- The least of a, b, and c.
- See Also:
-
min
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 isnull
or one of its elements isnull
.
-
min
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 isnull
or one of its elements isnull
.
-