maspack.util
Class Round

java.lang.Object
  extended by maspack.util.Round

public class Round
extends java.lang.Object

Class that supports rounding numbers with respect to k 10^n, where n is an integer and k is 1, 2, or 5, or with respect to 2^n, where n is an integer. These methods are mainly intended for automatic scaling operations.

With respect to the rounding up and down operations, if the operand is within machine precision (as defined by the global variable TOLERANCE) of one of the desired ``roundable'' values, then it will be left at that value and not rounded up or down. This is to ensure more predicatable behavior.


Field Summary
static double TOLERANCE
           
 
Constructor Summary
Round()
           
 
Method Summary
static double down125(double x)
          Rounds a number x down to the nearest value defined by
static double down125(int[] factors, double x)
          Rounds a number x down to the nearest value defined by
static double downPow2(double x)
          Rounds a number x down to the nearest value defined by
static double near125(double x)
          Rounds a number x to the nearest value defined by
static double near125(int[] factors, double x)
          Rounds a number x to the nearest value defined by
static double nearPow2(double x)
          Rounds a number x to the nearest value defined by
static double up125(double x)
          Rounds a number x up to the nearest value defined by
static double up125(int[] factors, double x)
          Rounds a number x up to the nearest value defined by
static double upPow2(double x)
          Rounds a number x up to the nearest value defined by
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOLERANCE

public static final double TOLERANCE
See Also:
Constant Field Values
Constructor Detail

Round

public Round()
Method Detail

down125

public static double down125(double x)
Rounds a number x down to the nearest value defined by
 sgn(x) k 10^n 
 
where n is an integer and k is 1, 2, or 5. If x is zero, then zero is returned. See the note about rounding tolerance in the class documentation.

Parameters:
x - number to round down
Returns:
rounded number

down125

public static double down125(int[] factors,
                             double x)
Rounds a number x down to the nearest value defined by
 sgn(x) k 10^n 
 
where n is an integer and k is 1, 2, or 5. If x is zero, then zero is returned. See the note about rounding tolerance in the class documentation.

Parameters:
factors - if not null, returns the values of k and n described above, in factors[0] and factors[1], respectively.
x - number to round down
Returns:
rounded number

up125

public static double up125(double x)
Rounds a number x up to the nearest value defined by
 sgn(x) k 10^n 
 
where n is an integer and k is 1, 2, or 5. If x is zero, then zero is returned. See the note about rounding tolerance in the class documentation.

Parameters:
x - number to round up
Returns:
rounded number

up125

public static double up125(int[] factors,
                           double x)
Rounds a number x up to the nearest value defined by
 sgn(x) k 10^n 
 
where n is an integer and k is 1, 2, or 5. If x is zero, then zero is returned. See the note about rounding tolerance in the class documentation.

Parameters:
factors - if not null, returns the values of k and n described above, in factors[0] and factors[1], respectively.
x - number to round up
Returns:
rounded number

near125

public static double near125(double x)
Rounds a number x to the nearest value defined by
 sgn(x) k 10^n 
 
where n is an integer and k is 1, 2, or 5. If x is zero, then zero is returned.

Parameters:
x - number to round
Returns:
rounded number

near125

public static double near125(int[] factors,
                             double x)
Rounds a number x to the nearest value defined by
 sgn(x) k 10^n 
 
where n is an integer and k is 1, 2, or 5. If x is zero, then zero is returned.

Parameters:
factors - if not null, returns the values of k and n described above, in factors[0] and factors[1], respectively.
x - number to round
Returns:
rounded number

downPow2

public static double downPow2(double x)
Rounds a number x down to the nearest value defined by
 sgn(x) 2^n 
 
where n is an integer. If x is zero, then zero is returned. See the note about rounding tolerance in the class documentation.

Parameters:
x - number to round down
Returns:
rounded number

upPow2

public static double upPow2(double x)
Rounds a number x up to the nearest value defined by
 sgn(x) 2^n 
 
where n is an integer. If x is zero, then zero is returned. See the note about rounding tolerance in the class documentation.

Parameters:
x - number to round up
Returns:
rounded number

nearPow2

public static double nearPow2(double x)
Rounds a number x to the nearest value defined by
 sgn(x) 2^n 
 
where n is an integer. If x is zero, then zero is returned.

Parameters:
x - number to round
Returns:
rounded number