Package dev.nm.number
Class NumberUtils
- java.lang.Object
-
- dev.nm.number.NumberUtils
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
NumberUtils.Comparable<T extends Number>
We need a precision parameter to determine whether two numbers are close enough to be treated as equal.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
compare(Number num1, Number num2, double epsilon)
Compare two numbers.static boolean
equal(Number num1, Number num2, double epsilon)
Check the equality of twoNumber
s, up to a precision.static boolean
isReal(Number number)
Check if a number is a real number.static Number
parse(String str)
Construct a number from a String.static Number[]
parseArray(String... strs)
-
-
-
Method Detail
-
equal
public static boolean equal(Number num1, Number num2, double epsilon)
Check the equality of twoNumber
s, up to a precision.- Parameters:
num1
- a numbernum2
- a numberepsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
if the numbers are close enough,false
otherwise
-
compare
public static int compare(Number num1, Number num2, double epsilon)
Compare two numbers. The two numbers must be of (possibly different) subclasses ofNumber
.- If both can be cast to
double
, they are compared asdouble
s; e.g., 2 vs. new Double(3); - If one is a
double
but the other is not, thedouble
is cast into the same field as the other number for comparison; e.g., 4 vs. 3 + 0i; - If neither is a
double
, the two numbers must be of the same class for comparison; e.g., both areComplex
.
- Parameters:
num1
- a numbernum2
- a numberepsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0; e.g., 1e-9- Returns:
- 0 if
num1
is close enough tonum2
; 1 ifnum1 > num2
; -1 ifnum1 < num2
- If both can be cast to
-
isReal
public static boolean isReal(Number number)
Check if a number is a real number. TODO: this function needs to be extended or modified when a newNumber
subclass is implemented.- Parameters:
number
- a number- Returns:
true
if the number is real, i.e., in Rn
-
parse
public static Number parse(String str)
Construct a number from a String. For example, some valid strings are:
Note: having spaces in the real part between sign and number is illegal, e.g.,"2" "2." "3 + 5i" "1.23 - 4.56i" "-1.23 - 4.56i" "-1.23 - 4.56e-7i" "-1.23 - 4.56+e7i" "i"
TODO: this function needs to be extended or modified when a new- 1.23+4.56i + 1.23+4.56i
Number
subclass is implemented.
-
-