Package dev.nm.number
Class NumberUtils
- java.lang.Object
-
- dev.nm.number.NumberUtils
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceNumberUtils.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 intcompare(Number num1, Number num2, double epsilon)Compare two numbers.static booleanequal(Number num1, Number num2, double epsilon)Check the equality of twoNumbers, up to a precision.static booleanisReal(Number number)Check if a number is a real number.static Numberparse(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 twoNumbers, up to a precision.- Parameters:
num1- a numbernum2- a numberepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueif the numbers are close enough,falseotherwise
-
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 asdoubles; e.g., 2 vs. new Double(3); - If one is a
doublebut the other is not, thedoubleis 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
num1is 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 newNumbersubclass is implemented.- Parameters:
number- a number- Returns:
trueif 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
Numbersubclass is implemented.
-
-