# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.number

## Class NumberUtils

• public final class NumberUtils
extends Object
These are the utility functions to manipulate Numbers.
• ### Nested Class Summary

Nested Classes
Modifier and Type Class and 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
Modifier and Type Method and 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 two Numbers, 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)
Convert an array of numbers in String to an array of numbers in Number.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Method Detail

• #### equal

public static boolean equal(Number num1,
Number num2,
double epsilon)
Check the equality of two Numbers, up to a precision.
Parameters:
num1 - a number
num2 - a number
epsilon - 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 of Number.
• If both can be cast to double, they are compared as doubles; e.g., 2 vs. new Double(3);
• If one is a double but the other is not, the double 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 are Complex.
Parameters:
num1 - a number
num2 - a number
epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0; e.g., 1e-9
Returns:
0 if num1 is close enough to num2; 1 if num1 &gt; num2; -1 if num1 &lt; num2
• #### 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 new Number 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:
 "2"
"2."
"3 + 5i"
"1.23 - 4.56i"
"-1.23 - 4.56i"
"-1.23 - 4.56e-7i"
"-1.23 - 4.56+e7i"
"i"

Note: having spaces in the real part between sign and number is illegal, e.g.,
 - 1.23+4.56i
+ 1.23+4.56i

TODO: this function needs to be extended or modified when a new Number subclass is implemented.
Parameters:
str - a number in String
Returns:
a number of type subclass-ing from Number, such as Double, Complex