Class NumberUtils
- Author:
- Chakib Daii
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic NumberReturns the absolute value of the given number represented as string.static Numberabs(DynamicNumber dx) Returns the absolute value of the given numberDynamicNumber.abs(T x) Returns the absolute value of the given numberNumber.static NumberAdds two numeric values represented as strings.static Numberadd(DynamicNumber dx, DynamicNumber dy) Adds twoDynamicNumberinstances with type promotion.add(T left, T right) Adds twoNumbervalues.static NumberPerforms a bitwise AND operation between two dynamically-typed numeric values.static Numberand(DynamicNumber dx, DynamicNumber dy) Performs a bitwise AND operation on the given numbersDynamicNumber.and(T left, T right) Performs a bitwise AND operation between twoNumbervalues of the same type.static NumberReturns the smallest integer greater than or equal the given number represented as string to the nearest integer.static Numberceil(DynamicNumber dx) Returns the smallest integer greater than or equal the given numberDynamicNumberto the nearest integer.ceil(T x) Returns the smallest integer greater than or equal the given numberNumberto the nearest integer.private static longcheckedLongValue(Number number, Class<? extends Number> targetClass) Check for aBigInteger/BigDecimallong overflow before returning the given number as a long value.static voidcheckLeftShiftOverflow(int size, long max, long min, BigInteger value, int positions) Checks for overflow when left-shifting a BigInteger value by a given number of bits.static voidcheckLeftShiftOverflow(int size, long max, long min, BigInteger value, int positions, boolean wrap) Checks if left shift operation on the givenBigIntegervalue by a number of positions would cause overflow or underflow based on the provided limits and size.static voidcheckLeftShiftOverflow(BigInteger value, int positions) Checks if left shift operation on the givenBigIntegervalue by a number of positions would cause overflow according to the specified limits.static NumbercheckPrecision(String text, double d) Checks whether a double value retains full precision when parsed from the original string.static NumbercheckPrecision(String text, float f) Checks whether a float value retains full precision when parsed from the original string.static NumbercheckPrecision(BigDecimal expected, double d) Checks whether a double value matches the expected BigDecimal value.static NumbercheckPrecision(BigDecimal expected, float f) Checks whether a float value matches the expected BigDecimal value.static voidcheckShiftPositions(int size, int positions) Checks if the shift positions parameter is valid given the bit size of the type.static ThrowablecheckShiftPositions(int size, int positions, boolean wrap) Validates the shift positions against the allowed range for a given bit size.static intCompares two numbers, represented as strings.static intcompare(DynamicNumber dx, DynamicNumber dy) Compares two numbersDynamicNumber.static <T extends Number>
intcompare(T left, T right) Compares two numbersNumber.static <T extends Number>
TconvertNumberToTargetClass(Number number, Class<T> targetClass) Convert the given number into an instance of the given target class.static NumberDivides two numeric values represented as strings.static Numberdivide(DynamicNumber dx, DynamicNumber dy) Divides twoDynamicNumberinstances with type promotion.divide(T left, T right) Divides twoNumbervalues.private static NumberPerforms exponentiation on a givenNumberbase and adoubleexponent.static booleanChecks if two numbers, represented as strings, are equal.static booleanequals(DynamicNumber dx, DynamicNumber dy) Checks if two numbersDynamicNumberare equal.static <T extends Number>
booleanequals(T left, T right) Checks if two numbersNumberare equal.static NumberReturns the largest integer less than or equal the given number represented as string to the nearest integer.static Numberfloor(DynamicNumber dx) Returns the largest integer less than or equal the given numberDynamicNumberto the nearest integer.floor(T x) Returns the largest integer less than or equal the given numberNumberto the nearest integer.static booleanChecks if the given number is zero represented as string.static booleanisZero(DynamicNumber dx) Checks if the given numberDynamicNumberis zero.static <T extends Number>
booleanisZero(T x) Checks if the given numberNumberis zero.static NumberReturns the greater of two numeric values represented as strings.static Numbermax(DynamicNumber dx, DynamicNumber dy) Returns the greater of twoDynamicNumberinstances with type promotion.max(T left, T right) Returns the greater of twoNumbervalues.static NumberReturns the lesser of two numeric values represented as strings.static Numbermin(DynamicNumber dx, DynamicNumber dy) Returns the lesser of twoDynamicNumberinstances with type promotion.min(T left, T right) Returns the lesser of twoNumbervalues.static NumberComputes the modulo (remainder) of two numeric values represented as strings.static Numbermodulo(DynamicNumber dx, DynamicNumber dy) Computes the modulo (remainder) of twoDynamicNumberinstances with type promotion.modulo(T left, T right) Computes the modulo (remainder) of twoNumbervalues.static NumberMultiplies two numeric values represented as strings.static Numbermultiply(DynamicNumber dx, DynamicNumber dy) Multiplies twoDynamicNumberinstances with type promotion.multiply(T left, T right) Multiplies twoNumbervalues.static NumberReturns the negation of the given number represented as string.static Numbernegate(DynamicNumber dx) Returns the negation of the given numberDynamicNumber.negate(T x) Returns the negation of the given numberNumber.static NumberPerforms a bitwise NOT (inversion) operation on a dynamically-typed numeric value.static Numbernot(DynamicNumber dx) Performs a bitwise NOT operation on the given numberDynamicNumber.not(T x) Performs a bitwise NOT (inversion) operation on a numeric value.static NumberPerforms a bitwise OR operation between two dynamically-typed numeric values.static Numberor(DynamicNumber dx, DynamicNumber dy) Performs a bitwise OR operation on the given numbersDynamicNumber.or(T left, T right) Performs a bitwise OR operation between twoNumbervalues of the same type.static NumberparseDynamicNumber(Object object) Parses a dynamic numeric value from the givenObject.static NumberparseDynamicNumber(String text) Parses a string into the most appropriateNumbertype.static NumberparseDynamicNumber(String text, int radix, String originalText) Parses a string into the most appropriateNumbertype using the specified radix.static NumberparseDynamicNumber(String text, DynamicNumber radix, String originalText) Parses a string into the most appropriateNumbertype using the specified radix.static NumberPerforms a post-decrement operation on a dynamically-typed numeric value.static NumberPerforms a post-decrement operation on the givenDynamicNumber.postDecrement(T x) Performs a post-decrement operation on a numeric value.static NumberPerforms a post-increment operation on a dynamically-typed numeric value.static NumberPerforms a post-increment operation on the givenDynamicNumber.postIncrement(T x) Performs a post-increment operation on a numeric value.static NumberRaises a number represented as string to the power of another.static Numberpow(DynamicNumber base, double exponent) Raises a numberDynamicNumberto the power of another.pow(T base, T exponent) Raises a numberNumberto the power of another.private static NumberpowInvalidDouble(Number base, double baseDouble, double exponent) static NumberPerforms a pre-decrement operation on a dynamically-typed numeric value.static NumberPerforms a pre-decrement operation on the givenDynamicNumber.preDecrement(T x) Performs a pre-decrement operation on a numeric value.static NumberPerforms a pre-increment operation on a dynamically-typed numeric value.static NumberPerforms a pre-increment operation on the givenDynamicNumberinstance.preIncrement(T x) Performs a pre-increment operation on a numeric value.static NumberRounds the given number represented as string to the nearest integer.static Numberround(DynamicNumber dx) Rounds the given numberDynamicNumberto the nearest integer.round(T x) Rounds the given numberNumberto the nearest integer.static NumberPerforms a left bitwise shift on a dynamically-typed numeric value by the specified number of positions.static BigIntegershiftLeft(BigInteger bigInteger, int positions) Performs a left bitwise shift on aBigIntegervalue by the specified number of positions, with overflow checking against 64-bitlongbounds.static NumbershiftLeft(DynamicNumber dx, int positions) Shifts the given numberDynamicNumberto the left (signed shift).shiftLeft(T x, T positions) Performs a left bitwise shift on a numeric value by the specified number of positions.static NumbershiftRight(Object x, int positions) Performs a right bitwise shift on a dynamically-typed numeric value by the specified number of positions.static NumbershiftRight(DynamicNumber dx, int positions) Shifts the given numberDynamicNumberto the right (signed shift).shiftRight(T x, T positions) Performs a right bitwise shift on a numeric value by the specified number of positions.static intReturns the signum of the given number represented as string.static intsignum(DynamicNumber dx) Returns the signum of the given numberDynamicNumber.static <T extends Number>
intsignum(T x) Returns the signum of the given numberNumber.static NumberReturns the square root of the given number represented as string.static Numbersqrt(DynamicNumber dx) Returns the square root of the given numberDynamicNumber.sqrt(T x) Returns the square root of the given numberNumber.static NumberSubtracts two numeric values represented as strings.static Numbersubtract(DynamicNumber dx, DynamicNumber dy) Subtracts twoDynamicNumberinstances with type promotion.subtract(T left, T right) Subtracts twoNumbervalues.static NumbertryParseDynamicNumber(String text) Attempts to parse a given text into aNumberusing dynamic number parsing logic.static NumberunsignedShiftRight(Object x, int positions) Performs an unsigned (logical) right bitwise shift on a dynamically-typed numeric value by the specified number of positions.static BigIntegerunsignedShiftRight(BigInteger value, int n) Performs an unsigned (logical) right shift on aBigInteger.static NumberunsignedShiftRight(DynamicNumber dx, int positions) Shifts the given numberDynamicNumberto the right (unsigned shift).unsignedShiftRight(T x, T positions) Performs an unsigned (logical) right bitwise shift on a numeric value by the specified number of positions.static NumberPerforms a bitwise XOR (exclusive OR) operation between two dynamically-typed numeric values.static Numberxor(DynamicNumber dx, DynamicNumber dy) Performs a bitwise XOR operation on the given numbersDynamicNumber.xor(T left, T right) Performs a bitwise XOR (exclusive OR) operation between twoNumbervalues of the same type.
-
Field Details
-
LONG_MIN
-
LONG_MAX
-
-
Constructor Details
-
NumberUtils
private NumberUtils()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
parseDynamicNumber
Parses a dynamic numeric value from the givenObject.If the object is already an instance of
Number, it is returned as-is. If the object is aString, it is parsed usingparseDynamicNumber(String). Otherwise, an exception is thrown indicating that the value is not a valid numeric type.- Parameters:
object- the object to parse as a numeric value; expected to be either aNumberor aString.- Returns:
- the parsed
Number. - Throws:
NaftahBugError- if the object is not a valid numeric value.- See Also:
-
parseDynamicNumber
Parses a string into the most appropriateNumbertype.- Parses decimal numbers (with dot or exponent) as Float, Double, or BigDecimal (in that order).
- Parses whole numbers as Byte, Short, Integer, Long, or BigInteger (in that order).
- Parameters:
text- the input numeric string- Returns:
- the parsed Number
- Throws:
RuntimeException- if parsing fails or if value is NaN or infinite
-
tryParseDynamicNumber
Attempts to parse a given text into aNumberusing dynamic number parsing logic.This method first checks if the input text is "truthy" (i.e., not null, empty, or false-like). If the text is not truthy, it returns a dynamic number with value
0.If the text is truthy, it tries to parse it using
parseDynamicNumber(String, int, String). If parsing fails due to any exception orNaftahBugError, it returns a dynamic number with valueDouble.NaN.- Parameters:
text- the string to be parsed- Returns:
- a
Numberparsed from the input text, or0if the text is not truthy, orDouble.NaNif parsing fails
-
parseDynamicNumber
Parses a string into the most appropriateNumbertype using the specified radix.This method supports custom bases for whole numbers (e.g., base 2 to base 36), while floating-point numbers are always treated as base-10 (radix is ignored).
- If the string represents a floating-point number (contains '.' or scientific notation),
it will be parsed as
Float,Double, orBigDecimal. - Otherwise, it is parsed using the given radix into
Byte,Short,Integer,Long, orBigIntegerdepending on value range.
- Parameters:
text- the numeric string to parseradix- the base to use for whole number parsing (from 2 to 36)originalText- the original string (in case of base numbers after using- Returns:
- the parsed
Numberinstance (type chosen dynamically) - Throws:
RuntimeException- if parsing fails, the radix is invalid, or result is NaN/infinite
- If the string represents a floating-point number (contains '.' or scientific notation),
it will be parsed as
-
parseDynamicNumber
Parses a string into the most appropriateNumbertype using the specified radix.This method supports custom bases for whole numbers (e.g., base 2 to base 36), while floating-point numbers are always treated as base-10 (radix is ignored).
- If the string represents a floating-point number (contains '.' or scientific notation),
it will be parsed as
Float,Double, orBigDecimal. - Otherwise, it is parsed using the given radix into
Byte,Short,Integer,Long, orBigIntegerdepending on value range.
- Parameters:
text- the numeric string to parseradix- the base to use for whole number parsing (from 2 to 36)originalText- the original string (in case of base numbers after using- Returns:
- the parsed
Numberinstance (type chosen dynamically) - Throws:
RuntimeException- if parsing fails, the radix is invalid, or result is NaN/infinite
- If the string represents a floating-point number (contains '.' or scientific notation),
it will be parsed as
-
convertNumberToTargetClass
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException Convert the given number into an instance of the given target class.- Parameters:
number- the number to converttargetClass- the target class to convert to- Returns:
- the converted number
- Throws:
IllegalArgumentException- if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)- See Also:
-
checkedLongValue
Check for aBigInteger/BigDecimallong overflow before returning the given number as a long value.- Parameters:
number- the number to converttargetClass- the target class to convert to- Returns:
- the long value, if convertible without overflow
- Throws:
IllegalArgumentException- if there is an overflow- See Also:
-
add
Adds twoNumbervalues.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- left operandright- right operand- Returns:
- the result of addition
-
add
Adds two numeric values represented as strings.- Parameters:
left- left operandright- right operand- Returns:
- the result of addition
-
add
Adds twoDynamicNumberinstances with type promotion.- Parameters:
dx- left operanddy- right operand- Returns:
- the result of addition
-
subtract
Subtracts twoNumbervalues.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- left operandright- right operand- Returns:
- the result of subtraction
-
subtract
Subtracts two numeric values represented as strings.- Parameters:
left- left operandright- right operand- Returns:
- the result of subtraction
-
subtract
Subtracts twoDynamicNumberinstances with type promotion.- Parameters:
dx- left operanddy- right operand- Returns:
- the result of subtraction
-
multiply
Multiplies twoNumbervalues.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- left operandright- right operand- Returns:
- the result of multiplication
-
multiply
Multiplies two numeric values represented as strings.- Parameters:
left- left operandright- right operand- Returns:
- the result of multiplication
-
multiply
Multiplies twoDynamicNumberinstances with type promotion.- Parameters:
dx- left operanddy- right operand- Returns:
- the result of multiplication
-
divide
Divides twoNumbervalues.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- the dividendright- the divisor- Returns:
- the result of division
-
divide
Divides two numeric values represented as strings.- Parameters:
left- the dividendright- the divisor- Returns:
- the result of division
-
divide
Divides twoDynamicNumberinstances with type promotion.- Parameters:
dx- the dividenddy- the divisor- Returns:
- the result of division
-
modulo
Computes the modulo (remainder) of twoNumbervalues.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- the dividendright- the divisor- Returns:
- the result of division
-
modulo
Computes the modulo (remainder) of two numeric values represented as strings.- Parameters:
left- the dividendright- the divisor- Returns:
- the result of division
-
modulo
Computes the modulo (remainder) of twoDynamicNumberinstances with type promotion.- Parameters:
dx- the dividenddy- the divisor- Returns:
- the result of division
-
max
Returns the greater of twoNumbervalues.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- the first numberright- the second number- Returns:
- the greatest of
leftandright, as aNumber
-
max
Returns the greater of two numeric values represented as strings.- Parameters:
left- the first numberright- the second number- Returns:
- the greatest of
leftandright, as aNumber
-
max
Returns the greater of twoDynamicNumberinstances with type promotion.- Parameters:
dx- the first numberdy- the second number- Returns:
- the greatest of
dxanddy, as aNumber
-
min
Returns the lesser of twoNumbervalues.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- the first numberright- the second number- Returns:
- the least of
leftandright, as aNumber
-
min
Returns the lesser of two numeric values represented as strings.- Parameters:
left- the first numberright- the second number- Returns:
- the least of
leftandright, as aNumber
-
min
Returns the lesser of twoDynamicNumberinstances with type promotion.- Parameters:
dx- the first numberdy- the second number- Returns:
- the least of
dxanddy, as aNumber
-
pow
Raises a numberNumberto the power of another.- Type Parameters:
T- concrete type that extends @Number- Parameters:
base- the base numberexponent- the exponent- Returns:
- the result of raising
baseto the power ofexponent, as aNumber
-
pow
Raises a number represented as string to the power of another.- Parameters:
base- the base numberexponent- the exponent- Returns:
- the result of raising
baseto the power ofexponent, as aNumber
-
pow
Raises a numberDynamicNumberto the power of another.- Parameters:
base- the base numberexponent- the exponent- Returns:
- the result of raising
baseto the power ofexponent, as aNumber
-
doPow
Performs exponentiation on a givenNumberbase and adoubleexponent.This method converts the base to a
doubleand usesMath.pow(double, double). If the result or base isNaNorInfinity, it falls back topowInvalidDouble(Number, double, double).- Parameters:
base- the numeric base (can beInteger,Double,BigInteger, etc.)exponent- the exponent to raise the base to- Returns:
- the result of
base ^ exponent, or a fallback value if the operation is invalid
-
powInvalidDouble
Fallback method for exponentiation whenMath.pow(double, double)returnsNaNorInfinity.Attempts to use type-specific power operations for
BigIntegerandBigDecimal. If the base is neither of those, or an exception occurs, returns aDynamicNumbercontaining thedoublerepresentation of the base.- Parameters:
base- the original numeric basebaseDouble- thedoublerepresentation of the baseexponent- the exponent used in the operation- Returns:
- a fallback
Numberresult for the invalid power operation
-
round
Rounds the given numberNumberto the nearest integer.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to round- Returns:
- the rounded number
-
round
Rounds the given number represented as string to the nearest integer.- Parameters:
x- the number to round- Returns:
- the rounded number
-
round
Rounds the given numberDynamicNumberto the nearest integer.- Parameters:
dx- the number to round- Returns:
- the rounded number
-
floor
Returns the largest integer less than or equal the given numberNumberto the nearest integer.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to round- Returns:
- the rounded number
-
floor
Returns the largest integer less than or equal the given number represented as string to the nearest integer.- Parameters:
x- the number to round- Returns:
- the rounded number
-
floor
Returns the largest integer less than or equal the given numberDynamicNumberto the nearest integer.- Parameters:
dx- the number to round- Returns:
- the rounded number
-
ceil
Returns the smallest integer greater than or equal the given numberNumberto the nearest integer.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to apply ceiling to- Returns:
- the ceiling value
-
ceil
Returns the smallest integer greater than or equal the given number represented as string to the nearest integer.- Parameters:
x- the number to apply ceiling to- Returns:
- the ceiling value
-
ceil
Returns the smallest integer greater than or equal the given numberDynamicNumberto the nearest integer.- Parameters:
dx- the number to apply ceiling to- Returns:
- the ceiling value
-
negate
Returns the negation of the given numberNumber.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to negate- Returns:
- the negated number
-
negate
Returns the negation of the given number represented as string.- Parameters:
x- the number to negate- Returns:
- the negated number
-
negate
Returns the negation of the given numberDynamicNumber.- Parameters:
dx- the number to negate- Returns:
- the negated number
-
sqrt
Returns the square root of the given numberNumber.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to compute the square root of- Returns:
- the square root of the number
-
sqrt
Returns the square root of the given number represented as string.- Parameters:
x- the number to compute the square root of- Returns:
- the square root of the number
-
sqrt
Returns the square root of the given numberDynamicNumber.- Parameters:
dx- the number to compute the square root of- Returns:
- the square root of the number
-
abs
Returns the absolute value of the given numberNumber.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to compute the absolute value of- Returns:
- the absolute value of the number
-
abs
Returns the absolute value of the given number represented as string.- Parameters:
x- the number to compute the absolute value of- Returns:
- the absolute value of the number
-
abs
Returns the absolute value of the given numberDynamicNumber.- Parameters:
dx- the number to compute the absolute value of- Returns:
- the absolute value of the number
-
signum
Returns the signum of the given numberNumber.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to compute the signum of- Returns:
- -1 if the number is negative, 0 if zero, and 1 if positive
-
signum
Returns the signum of the given number represented as string.- Parameters:
x- the number to compute the signum of- Returns:
- -1 if the number is negative, 0 if zero, and 1 if positive
-
signum
Returns the signum of the given numberDynamicNumber.- Parameters:
dx- the number to compute the signum of- Returns:
- -1 if the number is negative, 0 if zero, and 1 if positive
-
isZero
Checks if the given numberNumberis zero.- Type Parameters:
T- concrete type that extends @Number- Parameters:
x- the number to check- Returns:
trueif the number is zero;falseotherwise
-
isZero
Checks if the given number is zero represented as string.- Parameters:
x- the number to check- Returns:
trueif the number is zero;falseotherwise
-
isZero
Checks if the given numberDynamicNumberis zero.- Parameters:
dx- the number to check- Returns:
trueif the number is zero;falseotherwise
-
equals
Checks if two numbersNumberare equal.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- the first numberright- the second number- Returns:
trueifleftandrightare equal in value;falseotherwise
-
equals
Checks if two numbers, represented as strings, are equal.- Parameters:
left- the first numberright- the second number- Returns:
trueifleftandrightare equal in value;falseotherwise
-
equals
Checks if two numbersDynamicNumberare equal.- Parameters:
dx- the first numberdy- the second number- Returns:
trueifdxanddyare equal in value;falseotherwise
-
compare
Compares two numbersNumber.- Type Parameters:
T- concrete type that extends @Number- Parameters:
left- the first numberright- the second number- Returns:
- a negative integer if
left < right; zero ifleft == right; a positive integer ifleft > right
-
compare
Compares two numbers, represented as strings.- Parameters:
left- the first numberright- the second number- Returns:
- a negative integer if
left < right; zero ifleft == right; a positive integer ifleft > right
-
compare
Compares two numbersDynamicNumber.- Parameters:
dx- the first numberdy- the second number- Returns:
- a negative integer if
dx < dy; zero ifdx == dy; a positive integer ifdx > dy
-
and
Performs a bitwise AND operation between twoNumbervalues of the same type.This method wraps both input numbers in
DynamicNumber, then delegates the operation toand(DynamicNumber, DynamicNumber). It supports all types that can be safely converted intoDynamicNumberand that allow bitwise operations.- Type Parameters:
T- the type of the input numbers, which must extendNumber.- Parameters:
left- the first operand.right- the second operand.- Returns:
- the result of the bitwise AND operation as a
Number. - Throws:
NaftahBugError- if the underlying number types do not support bitwise operations.- See Also:
-
and
Performs a bitwise AND operation between two dynamically-typed numeric values.This method accepts arbitrary
Objectinputs, attempts to wrap them asDynamicNumberinstances, and delegates the bitwise operation toand(DynamicNumber, DynamicNumber). It is useful in dynamic or loosely-typed contexts where inputs may beNumber,String, or other types.- Parameters:
left- the first operand; expected to be convertible to aDynamicNumber.right- the second operand; expected to be convertible to aDynamicNumber.- Returns:
- the result of the bitwise AND operation as a
Number. - Throws:
NaftahBugError- if either input is not a valid number or if bitwise operations are not supported on the types.- See Also:
-
and
Performs a bitwise AND operation on the given numbersDynamicNumber.- Parameters:
dx- the first numberdy- the first number- Returns:
- the number representing
left & right
-
or
Performs a bitwise OR operation between twoNumbervalues of the same type.Both operands are converted to
DynamicNumber, and the operation is delegated toor(DynamicNumber, DynamicNumber). This method is useful when working with strongly typed numeric values that support bitwise logic (e.g., integers).- Type Parameters:
T- the type of the input numbers, extendingNumber.- Parameters:
left- the first operand.right- the second operand.- Returns:
- the result of the bitwise OR operation as a
Number. - Throws:
NaftahBugError- if bitwise operations are not supported on the operand types.- See Also:
-
or
Performs a bitwise OR operation between two dynamically-typed numeric values.This method accepts arbitrary
Objectinputs, converts them toDynamicNumber, and delegates the operation toor(DynamicNumber, DynamicNumber). It is designed for dynamic contexts where the input types may vary at runtime.- Parameters:
left- the first operand; must be convertible to aDynamicNumber.right- the second operand; must be convertible to aDynamicNumber.- Returns:
- the result of the bitwise OR operation as a
Number. - Throws:
NaftahBugError- if either operand is not a valid numeric value or bitwise operations are not supported.- See Also:
-
or
Performs a bitwise OR operation on the given numbersDynamicNumber.- Parameters:
dx- the first numberdy- the first number- Returns:
- the number representing
left | right
-
xor
Performs a bitwise XOR (exclusive OR) operation between twoNumbervalues of the same type.Both operands are wrapped as
DynamicNumberinstances, and the operation is delegated toxor(DynamicNumber, DynamicNumber). This method supports bitwise XOR on numeric types that allow such operations (typically integers).- Type Parameters:
T- the type of the input numbers, extendingNumber.- Parameters:
left- the first operand.right- the second operand.- Returns:
- the result of the bitwise XOR operation as a
Number. - Throws:
NaftahBugError- if the operand types do not support bitwise operations.- See Also:
-
xor
Performs a bitwise XOR (exclusive OR) operation between two dynamically-typed numeric values.This method accepts any
Objectinputs, converts them toDynamicNumberinstances, and delegates the operation toxor(DynamicNumber, DynamicNumber). It is suitable for dynamic contexts where inputs may vary in type (e.g.,Number,String, etc.).- Parameters:
left- the first operand; must be convertible to aDynamicNumber.right- the second operand; must be convertible to aDynamicNumber.- Returns:
- the result of the bitwise XOR operation as a
Number. - Throws:
NaftahBugError- if either input is not a valid numeric type or does not support bitwise operations.- See Also:
-
xor
Performs a bitwise XOR operation on the given numbersDynamicNumber.- Parameters:
dx- the first numberdy- the first number- Returns:
- the number representing
left ^ right
-
not
Performs a bitwise NOT (inversion) operation on a numeric value.The input number is wrapped as a
DynamicNumber, and the operation is delegated tonot(DynamicNumber). This method supports numeric types that allow bitwise operations (typically integers).- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to invert.- Returns:
- the result of the bitwise NOT operation as a
Number. - Throws:
NaftahBugError- if the number type does not support bitwise operations.- See Also:
-
not
Performs a bitwise NOT (inversion) operation on a dynamically-typed numeric value.This method accepts any
Objectthat can be converted to aDynamicNumber, and delegates the operation tonot(DynamicNumber). It is intended for use in dynamic contexts where the input type may vary.- Parameters:
x- the value to invert; must be convertible to aDynamicNumber.- Returns:
- the result of the bitwise NOT operation as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or does not support bitwise operations.- See Also:
-
not
Performs a bitwise NOT operation on the given numberDynamicNumber.- Parameters:
dx- the number to apply bitwise NOT operation to- Returns:
- the number representing
~x
-
shiftLeft
Performs a left bitwise shift on a numeric value by the specified number of positions.The input number is wrapped as a
DynamicNumber, and the shift operation is delegated toshiftLeft(DynamicNumber, int). This method is suitable for statically typedNumbervalues that support bitwise operations (typically integer types).- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to shift.positions- the number of bit positions to shift to the left; must be non-negative.- Returns:
- the result of the left shift operation as a
Number. - Throws:
NaftahBugError- if the number type does not support bitwise operations.IllegalArgumentException- if the shift amount is negative or exceeds allowed bounds.- See Also:
-
shiftLeft
Performs a left bitwise shift on a dynamically-typed numeric value by the specified number of positions.This method accepts any
Objectthat can be converted to aDynamicNumber, and delegates the shift operation toshiftLeft(DynamicNumber, int). It is designed for use in dynamic or loosely typed contexts.- Parameters:
x- the value to shift; must be convertible to aDynamicNumber.positions- the number of bit positions to shift to the left; must be non-negative.- Returns:
- the result of the left shift operation as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or does not support bitwise operations.IllegalArgumentException- if the shift amount is negative or exceeds allowed bounds.- See Also:
-
shiftLeft
Shifts the given numberDynamicNumberto the left (signed shift).This is equivalent to multiplying the value by
2^n.- Parameters:
dx- the number to shiftpositions- the number of bits to shift to the left; must be non-negative- Returns:
- the result of
value << n
-
shiftLeft
Performs a left bitwise shift on aBigIntegervalue by the specified number of positions, with overflow checking against 64-bitlongbounds.This method ensures that the shift will not produce a value that exceeds the range of a 64-bit signed integer, and throws an exception if overflow is detected.
- Parameters:
bigInteger- theBigIntegervalue to shift.positions- the number of bit positions to shift to the left; must be non-negative.- Returns:
- a new
BigIntegerrepresenting the result of the shift. - Throws:
NaftahBugError- if the shift amount is invalid (e.g., negative or too large).ArithmeticException- if the shift would cause overflow beyondlongrange.- See Also:
-
shiftRight
Performs a right bitwise shift on a numeric value by the specified number of positions.The input value is converted to a
DynamicNumber, and the operation is delegated toshiftRight(DynamicNumber, int). This operation is safe and does not cause overflow.- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to shift.positions- the number of bit positions to shift to the right; must be non-negative.- Returns:
- the result of the right shift operation as a
Number. - Throws:
NaftahBugError- if the number type does not support bitwise operations.IllegalArgumentException- if the shift amount is negative.- See Also:
-
shiftRight
Performs a right bitwise shift on a dynamically-typed numeric value by the specified number of positions.This method accepts any
Objectthat can be converted to aDynamicNumber, and delegates the operation toshiftRight(DynamicNumber, int). It is designed for use in dynamic contexts where the input type may vary at runtime.- Parameters:
x- the value to shift; must be convertible to aDynamicNumber.positions- the number of bit positions to shift to the right; must be non-negative.- Returns:
- the result of the right shift operation as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or does not support bitwise operations.IllegalArgumentException- if the shift amount is negative.- See Also:
-
shiftRight
Shifts the given numberDynamicNumberto the right (signed shift).This is equivalent to dividing the value by
2^n, with sign extension.- Parameters:
dx- the number to shiftpositions- the number of bits to shift to the right; must be non-negative- Returns:
- the result of
value >> n
-
unsignedShiftRight
Performs an unsigned (logical) right bitwise shift on a numeric value by the specified number of positions.The input number is converted to a
DynamicNumber, and the operation is delegated tounsignedShiftRight(DynamicNumber, int). This shift fills the left bits with zeros regardless of sign.- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to shift.positions- the number of bit positions to shift to the right; must be non-negative.- Returns:
- the result of the unsigned right shift operation as a
Number. - Throws:
NaftahBugError- if the number type does not support bitwise operations.IllegalArgumentException- if the shift amount is negative.- See Also:
-
unsignedShiftRight
Performs an unsigned (logical) right bitwise shift on a dynamically-typed numeric value by the specified number of positions.This method accepts any
Objectconvertible toDynamicNumber, and delegates the operation tounsignedShiftRight(DynamicNumber, int).- Parameters:
x- the value to shift; must be convertible to aDynamicNumber.positions- the number of bit positions to shift to the right; must be non-negative.- Returns:
- the result of the unsigned right shift operation as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or does not support bitwise operations.IllegalArgumentException- if the shift amount is negative.- See Also:
-
unsignedShiftRight
Shifts the given numberDynamicNumberto the right (unsigned shift).This is equivalent to dividing the value by
2^n, with sign extension.- Parameters:
dx- the number to shiftpositions- the number of bits to shift to the right; must be non-negative- Returns:
- the result of an unsigned right shift (zero-fill) of
valuebynbits
-
preIncrement
Performs a pre-increment operation on a numeric value.The input number is wrapped as a
DynamicNumber, and the increment operation is delegated topreIncrement(DynamicNumber).- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to increment.- Returns:
- the result of the pre-increment operation as a
Number. - Throws:
NaftahBugError- if the input type is unsupported for arithmetic operations.- See Also:
-
preIncrement
Performs a pre-increment operation on a dynamically-typed numeric value.This method accepts any
Objectconvertible to aDynamicNumberand delegates the increment operation topreIncrement(DynamicNumber).- Parameters:
x- the value to increment; must be convertible toDynamicNumber.- Returns:
- the result of the pre-increment operation as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or unsupported for arithmetic.- See Also:
-
preIncrement
Performs a pre-increment operation on the givenDynamicNumberinstance.This method handles numeric overflow by promoting the underlying number type to a larger or more precise numeric type when the maximum value is reached:
- Byte and Short promote to Integer.
- Integer promotes to Long.
- Long promotes to BigInteger.
- Float promotes to Double.
- Double promotes to BigDecimal.
For
BigIntegerandBigDecimal, it simply adds one without promotion. If no overflow occurs, it increments normally.- Parameters:
dx- theDynamicNumberto increment.- Returns:
- the incremented number as a
Number. - Throws:
NaftahBugError- if the underlying numeric type is not supported.
-
postIncrement
Performs a post-increment operation on a numeric value.The input number is wrapped as a
DynamicNumber, and the increment operation is delegated topostIncrement(DynamicNumber). The returned value is the original value before the increment.- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to increment.- Returns:
- the original value before increment as a
Number. - Throws:
NaftahBugError- if the input type is unsupported for arithmetic operations.- See Also:
-
postIncrement
Performs a post-increment operation on a dynamically-typed numeric value.This method accepts any
Objectconvertible to aDynamicNumberand delegates the increment operation topostIncrement(DynamicNumber). The returned value is the original value before the increment.- Parameters:
x- the value to increment; must be convertible toDynamicNumber.- Returns:
- the original value before increment as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or unsupported for arithmetic.- See Also:
-
postIncrement
Performs a post-increment operation on the givenDynamicNumber.The method returns the original value before incrementing. It handles overflow by promoting the underlying numeric type to a larger or more precise type when needed:
- Byte and Short promote to Integer.
- Integer promotes to Long.
- Long promotes to
BigInteger. - Float promotes to Double.
- Double promotes to
BigDecimal.
For
BigIntegerandBigDecimal, it increments without promotion. If the type is unsupported, it throws aNaftahBugError.- Parameters:
dx- theDynamicNumberto post-increment.- Returns:
- the original value before the increment as a
Number. - Throws:
NaftahBugError- if the number type is unsupported.
-
preDecrement
Performs a pre-decrement operation on a numeric value.The input number is wrapped as a
DynamicNumber, and the decrement operation is delegated topreDecrement(DynamicNumber).- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to decrement.- Returns:
- the result of the pre-decrement operation as a
Number. - Throws:
NaftahBugError- if the input type is unsupported for arithmetic operations.- See Also:
-
preDecrement
Performs a pre-decrement operation on a dynamically-typed numeric value.This method accepts any
Objectconvertible to aDynamicNumberand delegates the decrement operation topreDecrement(DynamicNumber).- Parameters:
x- the value to decrement; must be convertible toDynamicNumber.- Returns:
- the result of the pre-decrement operation as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or unsupported for arithmetic.- See Also:
-
preDecrement
Performs a pre-decrement operation on the givenDynamicNumber.This method handles underflow by promoting the underlying number type to a larger or more precise numeric type when the minimum value is reached:
- Byte and Short promote to Integer.
- Integer promotes to Long.
- Long promotes to
BigInteger. - Float promotes to Double.
- Double promotes to
BigDecimal.
BigIntegerandBigDecimal, it decrements without promotion. If no underflow occurs, it decrements normally.- Parameters:
dx- theDynamicNumberto decrement.- Returns:
- the decremented number as a
Number. - Throws:
NaftahBugError- if the underlying numeric type is not supported.
-
postDecrement
Performs a post-decrement operation on a numeric value.The input number is wrapped as a
DynamicNumber, and the decrement operation is delegated topostDecrement(DynamicNumber). The returned value is the original value before the decrement.- Type Parameters:
T- the type of the input number, extendingNumber.- Parameters:
x- the number to decrement.- Returns:
- the original value before decrement as a
Number. - Throws:
NaftahBugError- if the input type is unsupported for arithmetic operations.- See Also:
-
postDecrement
Performs a post-decrement operation on a dynamically-typed numeric value.This method accepts any
Objectconvertible to aDynamicNumberand delegates the decrement operation topostDecrement(DynamicNumber). The returned value is the original value before the decrement.- Parameters:
x- the value to decrement; must be convertible toDynamicNumber.- Returns:
- the original value before decrement as a
Number. - Throws:
NaftahBugError- if the input is not a valid numeric value or unsupported for arithmetic.- See Also:
-
postDecrement
Performs a post-decrement operation on the givenDynamicNumber.The method returns the original value before decrementing. It handles underflow by promoting the underlying numeric type to a larger or more precise type when needed:
- Byte and Short promote to Integer.
- Integer promotes to Long.
- Long promotes to
BigInteger. - Float promotes to Double.
- Double promotes to
BigDecimal.
BigIntegerandBigDecimal, it decrements without promotion. If the type is unsupported, it throws aNaftahBugError.- Parameters:
dx- theDynamicNumberto post-decrement.- Returns:
- the original value before the decrement as a
Number. - Throws:
NaftahBugError- if the number type is unsupported.
-
unsignedShiftRight
Performs an unsigned (logical) right shift on aBigInteger.Unlike signed right shift, this operation fills high-order bits with zeros regardless of the sign of the
BigInteger. SinceBigIntegerdoes not have a built-in unsigned right shift, this method emulates it for non-negative values. If the value is negative, the behavior may not match unsigned semantics in lower-level languages like Java's primitive types.- Parameters:
value- theBigIntegerto shift; must not benulln- the number of bits to shift right; must be non-negative- Returns:
- the result of shifting
valueto the right bynbits, using zero-fill - Throws:
NullPointerException- ifvalueisnullIllegalArgumentException- ifnis negative
-
checkPrecision
Checks whether a double value retains full precision when parsed from the original string. Throws a NumberFormatException if precision is lost.- Parameters:
text- the original string representing the numberd- the parsed double value- Returns:
- the double value if precision is preserved
- Throws:
NumberFormatException- if precision is lost during parsing
-
checkPrecision
Checks whether a float value retains full precision when parsed from the original string. Throws a NumberFormatException if precision is lost.- Parameters:
text- the original string representing the numberf- the parsed float value- Returns:
- the float value if precision is preserved
- Throws:
NumberFormatException- if precision is lost during parsing
-
checkPrecision
Checks whether a double value matches the expected BigDecimal value. If not, returns the expected BigDecimal as a fallback.- Parameters:
expected- the expected BigDecimal valued- the double value to compare- Returns:
- the original double if precise, otherwise the expected BigDecimal
-
checkPrecision
Checks whether a float value matches the expected BigDecimal value. If not, returns the expected BigDecimal as a fallback.- Parameters:
expected- the expected BigDecimal valuef- the float value to compare- Returns:
- the original float if precise, otherwise the expected BigDecimal
-
checkLeftShiftOverflow
public static void checkLeftShiftOverflow(int size, long max, long min, BigInteger value, int positions) Checks for overflow when left-shifting a BigInteger value by a given number of bits.This method verifies that the shift amount is within valid bounds and that the value, when shifted, will not exceed the range of a 64-bit signed long. If an overflow condition is detected, an exception is thrown with a descriptive message.
- Parameters:
size- the maximum allowed shift size (in bits). If set to -1, no upper bound check is performed.value- theBigIntegervalue to be shifted.positions- the number of bits to shift left; must be non-negative and less thansizeifsizeis not -1.- Throws:
NaftahBugError- if the shift amount is invalid (negative or out of range).ArithmeticException- if shifting the value would overflow the range of along.
-
checkLeftShiftOverflow
Checks if left shift operation on the givenBigIntegervalue by a number of positions would cause overflow according to the specified limits.This method delegates to the general
checkLeftShiftOverflow(int, long, long, BigInteger, int, boolean)method with default size and limits (-1 means no limit).- Parameters:
value- theBigIntegervalue to be shifted left.positions- the number of bit positions to shift left.- Throws:
NaftahBugError- if the shift positions are invalid or if the shifted value would overflow.
-
checkLeftShiftOverflow
public static void checkLeftShiftOverflow(int size, long max, long min, BigInteger value, int positions, boolean wrap) Checks if left shift operation on the givenBigIntegervalue by a number of positions would cause overflow or underflow based on the provided limits and size.Throws
NaftahBugErrorwrapping anArithmeticExceptionif overflow or underflow conditions are detected and wrap is true; otherwise throws the rawArithmeticException.- Parameters:
size- the bit size of the type being shifted, or -1 if unknown/unbounded.max- the maximum allowed long value before shifting, or -1 if no maximum limit.min- the minimum allowed long value before shifting, or -1 if no minimum limit.value- theBigIntegervalue to be shifted.positions- the number of bits to shift left.wrap- if true, wraps anyArithmeticExceptionin aNaftahBugError.- Throws:
NaftahBugError- if wrap is true and invalid shift or overflow/underflow detected.ArithmeticException- if wrap is false and invalid shift or overflow/underflow detected.
-
checkShiftPositions
public static void checkShiftPositions(int size, int positions) Checks if the shift positions parameter is valid given the bit size of the type.Throws a
NaftahBugErrorif the positions are invalid.- Parameters:
size- the bit size of the type being shifted, or -1 if unbounded.positions- the number of bit positions to shift.- Throws:
NaftahBugError- if positions is negative or exceeds size - 1.
-
checkShiftPositions
Validates the shift positions against the allowed range for a given bit size.Returns a wrapped
NaftahBugErroror rawArithmeticExceptionif invalid, or null if valid.- Parameters:
size- the bit size of the type being shifted, or -1 if unbounded.positions- the number of bits to shift.wrap- if true, returns aNaftahBugErrorwrapping the exception; otherwise raw.- Returns:
- a Throwable representing the error if invalid, or null if valid.
-