SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.number
Class NumberUtils

java.lang.Object
  extended by com.numericalmethod.suanshu.number.NumberUtils

public class NumberUtils
extends java.lang.Object

This class collects the utility functions to manipulate data of type Number.

These functions are static and stateless.


Nested Class Summary
static interface NumberUtils.Comparable<T extends java.lang.Number>
          We need a precision parameter to determine whether two numbers are close enough to be treated as equal.
 
Method Summary
static int compare(java.lang.Number num1, java.lang.Number num2, double epsilon)
          Compare two Numbers.
static boolean equal(java.lang.Number num1, java.lang.Number num2, double epsilon)
          Check the equality of two Numbers, up to a precision.
static boolean isReal(java.lang.Number number)
          Check if a Number is a real number.
static java.lang.Number parse(java.lang.String str)
          Construct a Number from a String.
 
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(java.lang.Number num1,
                            java.lang.Number num2,
                            double epsilon)
Check the equality of two Numbers, up to a precision.

This method compares two numbers up to a precision. The input type must subclass Number and implements NumberUtils.Comparable.

Parameters:
num1 - a number
num2 - a number
epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
Returns:
true if their double values are equal as defined by DoubleUtils.equal(double, double, double), or by the Object's Object.equals(java.lang.Object); false otherwise

compare

public static int compare(java.lang.Number num1,
                          java.lang.Number num2,
                          double epsilon)
Compare two Numbers.

The two inputs can be different subclasses of Number.

  • If both can be casted to double, they are compared in the double domain; e.g., 2 vs. new Double(3)
  • If one is a double, then it is casted into the same Field as the other number for comparison; e.g., 4 vs. 3 + 0i
  • If neither is double, then the two numbers must be of the same class for comparison; e.g., 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 d1 is close enough to d2, 1 if d1 > d2, -1 if d1 < d2

isReal

public static boolean isReal(java.lang.Number number)
Check if a Number is a real number. Specifically, we check whether doubleValue() can be called meaningfully.

TODO: this function needs to be extended or modified when a new Number subclass is implemented.

Parameters:
number - a number
Returns:
true iff the number is real, i.e., in Rn

parse

public static java.lang.Number parse(java.lang.String str)
Construct a Number from a String.

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"
 
Note: having spaces in the real part between sign and number is illegal, e.g.,
 - 1.23+4.56i
 + 1.23+4.56i
 

Parameters:
str - a number in String format
Returns:
a number of type subclass Number such as Double, Complex

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.