Class NumberHelper
- Author:
- Daniel Kuppitz (http://gremlin.guru)
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal BiFunction<Number,Number, Number> final BiFunction<Number,Number, Integer> final BiFunction<Number,Number, Number> final BiFunction<Number,Number, Number> final BiFunction<Number,Number, Number> final BiFunction<Number,Number, Number> final BiFunction<Number,Number, Number> -
Method Summary
Modifier and TypeMethodDescriptionstatic NumberAdds two numbers returning the highest common number class between them.static booleanbothAreNaN(Object first, Object second) static Numberstatic NumberCasts the given number to the specified numeric type if it can fit into it.static NumberCoerces the given number to the specified numeric type if it can fit into it.static IntegerCompares two numbers.static NumberDivides two numbers returning the highest common number class between them callingdiv(Number, Number, boolean)with afalse.static NumberDivides two numbers returning the highest common number class between them.static booleaneitherAreNaN(Object first, Object second) getHighestCommonNumberClass(boolean forceFloatingPoint, Number... numbers) getHighestCommonNumberClass(Number... numbers) static booleanstatic booleanisNegativeInfinity(Object value) static booleanisPositiveInfinity(Object value) static Comparablemax(Comparable a, Comparable b) Gets the larger number of the two provided returning the highest common number class between them.static NumberGets the larger number of the two provided returning the highest common number class between them.static Comparablemin(Comparable a, Comparable b) Gets the smaller number of the two provided returning the highest common number class between them.static NumberGets the smaller number of the two provided returning the highest common number class between them.static NumberMultiplies two numbers returning the highest common number class between them.static NumberSubtracts two numbers returning the highest common number class between them.
-
Field Details
-
add
-
sub
-
mul
-
div
-
min
-
max
-
cmp
-
-
Method Details
-
getHighestCommonNumberClass
-
getHighestCommonNumberClass
-
add
Adds two numbers returning the highest common number class between them.This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an
ArithmeticExceptionis thrown. For floating-point numbers, ifdoubleoverflows, the result isDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYinstead of an exception.a = 1, b = 1 -> 2 a = null, b = 1 -> null a = 1, b = null -> 1 a = null, b = null -> null- Parameters:
a- should be thought of as the seed to be modified bybb- the modifier to {code a}
-
sub
Subtracts two numbers returning the highest common number class between them.This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an
ArithmeticExceptionis thrown. For floating-point numbers, ifdoubleoverflows, the result isDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYinstead of an exception.a = 1, b = 1 -> 0 a = null, b = 1 -> null a = 1, b = null -> 1 a = null, b = null -> null- Parameters:
a- should be thought of as the seed to be modified bybb- the modifier to {code a}
-
mul
Multiplies two numbers returning the highest common number class between them.This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an
ArithmeticExceptionis thrown. For floating-point numbers, ifdoubleoverflows, the result isDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYinstead of an exception.a = 1, b = 2 -> 2 a = null, b = 1 -> null a = 1, b = null -> 1 a = null, b = null -> null- Parameters:
a- should be thought of as the seed to be modified bybb- the modifier to {code a}
-
div
Divides two numbers returning the highest common number class between them callingdiv(Number, Number, boolean)with afalse. -
div
Divides two numbers returning the highest common number class between them.This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an
ArithmeticExceptionis thrown. For floating-point numbers, ifdoubleoverflows, the result isDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYinstead of an exception.a = 4, b = 2 -> 2 a = null, b = 1 -> null a = 1, b = null -> 1 a = null, b = null -> null- Parameters:
a- should be thought of as the seed to be modified bybb- the modifier to {code a}forceFloatingPoint- when set totrueensures that the return value is the highest common floating number class
-
min
Gets the smaller number of the two provided returning the highest common number class between them.a = 4, b = 2 -> 2 a = null, b = 1 -> 1 a = 1, b = null -> 1 a = null, b = null -> null a = NaN, b = 1 -> 1 a = 1, b = NaN -> 1 a = NaN, b = NaN -> NaN -
min
Gets the smaller number of the two provided returning the highest common number class between them.a = 4, b = 2 -> 2 a = null, b = 1 -> 1 a = 1, b = null -> 1 a = null, b = null -> null a = NaN, b = 1 -> 1 a = 1, b = NaN -> 1 a = NaN, b = NaN -> NaN -
max
Gets the larger number of the two provided returning the highest common number class between them.a = 4, b = 2 -> 4 a = null, b = 1 -> 1 a = 1, b = null -> 1 a = null, b = null -> null a = NaN, b = 1 -> 1 a = 1, b = NaN -> 1 a = NaN, b = NaN -> NaN -
max
Gets the larger number of the two provided returning the highest common number class between them.a = 4, b = 2 -> 4 a = null, b = 1 -> 1 a = 1, b = null -> 1 a = null, b = null -> null a = NaN, b = 1 -> 1 a = 1, b = NaN -> 1 a = NaN, b = NaN -> NaN -
compare
Compares two numbers. Follows orderability semantics for NaN, which places NaN after +Inf.a = 4, b = 2 -> 1 a = 2, b = 4 -> -1 a = null, b = 1 -> -1 a = 1, b = null -> 1 a = null, b = null -> 0 a = NaN, b = NaN -> 0 a = NaN, b = Inf -> 1 -
coerceTo
Coerces the given number to the specified numeric type if it can fit into it. Otherwise, retains the original type.- Parameters:
a- the number to be coercedclazz- the target numeric type class- Returns:
- the coerced number in the specified type or the original type if it cannot fit
- Throws:
IllegalArgumentException- if the specified numeric type is unsupported
-
castTo
Casts the given number to the specified numeric type if it can fit into it. Otherwise, throw.- Parameters:
a- the number to be casttypeToken- the number token denoting the desired type to cast- Returns:
- the number cast to the specified type
- Throws:
IllegalArgumentException- if the specified numeric type is unsupportedArithmeticException- if the number overflows
-
castTo
-
isNaN
-
eitherAreNaN
-
bothAreNaN
-
isPositiveInfinity
-
isNegativeInfinity
-