ptolemy.math
Class Rounding

java.lang.Object
  extended byptolemy.math.Rounding
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
Rounding.General, Rounding.RoundCeiling, Rounding.RoundDown, Rounding.RoundFloor, Rounding.RoundHalfCeiling, Rounding.RoundHalfDown, Rounding.RoundHalfEven, Rounding.RoundHalfFloor, Rounding.RoundHalfUp, Rounding.RoundUnnecessary, Rounding.RoundUp, Rounding.Unknown

public abstract class Rounding
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

The Rounding class provides a type safe enumeration of strategies for handling loss of numeric resolution. Rounding is typically resolved when quantization constraints are applied to a computed result in order to satisfy the requirements of the type to which the result is to be assigned.

The rounding strategies are

A specific strategy may be chosen dynamically by invoking forName() or getName() with one of the above strategy names. Alternatively a strategy may be selected by using one of the static singletons.

The truncate and nearest strategies should be preferred since they correspond to capabilities available on many processors. Other rounding strategies may require costly code on practical hardware.

The active class functionality is provided by the quantize method which is normally invoked from Quantization.quantize.

Since:
Ptolemy II 2.1
Version:
$Id: Rounding.java,v 1.10 2004/04/13 03:45:52 cxh Exp $
Author:
Ed Willink
See Also:
Serialized Form
Accepted Rating:
Red
Proposed Rating:
Red (Ed.Willink)

Nested Class Summary
static class Rounding.General
          The general rounding strategy at the top of the rounding lattice
static class Rounding.RoundCeiling
          The round towards plus infinity rounding strategy
static class Rounding.RoundDown
          The round towards zero rounding strategy
static class Rounding.RoundFloor
          The round towards minus infinity (truncate) rounding strategy
static class Rounding.RoundHalfCeiling
          The round to nearest and halves towards plus infinity (nearest) rounding strategy
static class Rounding.RoundHalfDown
          The round to nearest and halves towards zero rounding strategy
static class Rounding.RoundHalfEven
          The round to nearest and halves towards the even value (convergent) rounding strategy
static class Rounding.RoundHalfFloor
          The round to nearest and halves towards minus infinity rounding strategy
static class Rounding.RoundHalfUp
          The round to nearest and halves away from zero rounding strategy
static class Rounding.RoundUnnecessary
          The no rounding necessary rounding strategy
static class Rounding.RoundUp
          The round away from zero rounding strategy
static class Rounding.Unknown
          The unknown rounding strategy at the bottom of the rounding lattice
 
Field Summary
static Rounding.RoundCeiling CEILING
           
static Rounding.RoundHalfEven CONVERGENT
           
static Rounding.RoundDown DOWN
           
static Rounding.RoundFloor FLOOR
           
static Rounding.General GENERAL
           
static Rounding.RoundHalfCeiling HALF_CEILING
           
static Rounding.RoundHalfDown HALF_DOWN
           
static Rounding.RoundHalfEven HALF_EVEN
           
static Rounding.RoundHalfFloor HALF_FLOOR
           
static Rounding.RoundHalfUp HALF_UP
           
static Rounding.RoundHalfCeiling NEAREST
           
static Rounding.RoundFloor TRUNCATE
           
static Rounding.Unknown UNKNOWN
           
static Rounding.RoundUnnecessary UNNECESSARY
           
static Rounding.RoundUp UP
           
 
Constructor Summary
protected Rounding(java.lang.String name)
           
 
Method Summary
protected abstract  boolean _roundUp(java.math.BigInteger intPart, double fracPart)
          Return true if intPart should be incremented to represent the rounded value of intPart + fracPart, which is positive.
 java.lang.Object clone()
          Return this, that is, return the reference to this object.
 boolean equals(java.lang.Object object)
          Determine if the argument represents the same Rounding as this object.
static Rounding forName(java.lang.String name)
          Return an instance of this class with the specified name.
static Rounding getName(java.lang.String name)
          Return an instance of this class with the specified name, or null if none exists.
 int hashCode()
          Return a hash code value for this object.
 java.math.BigInteger quantize(java.math.BigInteger intPart, double fracPart)
          Return the value of intPart after adjustment for loss of fracPart.
 java.lang.String toString()
          Return the string representation of this rounding.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

GENERAL

public static final Rounding.General GENERAL

CEILING

public static final Rounding.RoundCeiling CEILING

DOWN

public static final Rounding.RoundDown DOWN

FLOOR

public static final Rounding.RoundFloor FLOOR

TRUNCATE

public static final Rounding.RoundFloor TRUNCATE

HALF_CEILING

public static final Rounding.RoundHalfCeiling HALF_CEILING

NEAREST

public static final Rounding.RoundHalfCeiling NEAREST

HALF_DOWN

public static final Rounding.RoundHalfDown HALF_DOWN

HALF_EVEN

public static final Rounding.RoundHalfEven HALF_EVEN

CONVERGENT

public static final Rounding.RoundHalfEven CONVERGENT

HALF_FLOOR

public static final Rounding.RoundHalfFloor HALF_FLOOR

HALF_UP

public static final Rounding.RoundHalfUp HALF_UP

UNNECESSARY

public static final Rounding.RoundUnnecessary UNNECESSARY

UP

public static final Rounding.RoundUp UP

UNKNOWN

public static final Rounding.Unknown UNKNOWN
Constructor Detail

Rounding

protected Rounding(java.lang.String name)
Method Detail

clone

public java.lang.Object clone()
Return this, that is, return the reference to this object.

Returns:
This Rounding.

equals

public boolean equals(java.lang.Object object)
Determine if the argument represents the same Rounding as this object.

Parameters:
object - Another object.
Returns:
True if the argument represents the same Rounding as this object; false otherwise.

forName

public static Rounding forName(java.lang.String name)
Return an instance of this class with the specified name.

Returns:
An instance of Rounding or null.

getName

public static Rounding getName(java.lang.String name)
                        throws java.lang.IllegalArgumentException
Return an instance of this class with the specified name, or null if none exists.

Returns:
An instance of Rounding.
Throws:
java.lang.IllegalArgumentException - If the string does not match one of the known strategies.

hashCode

public int hashCode()
Return a hash code value for this object.


quantize

public java.math.BigInteger quantize(java.math.BigInteger intPart,
                                     double fracPart)
Return the value of intPart after adjustment for loss of fracPart.

Returns:
The rounded value.

toString

public java.lang.String toString()
Return the string representation of this rounding.

Returns:
A String.

_roundUp

protected abstract boolean _roundUp(java.math.BigInteger intPart,
                                    double fracPart)
Return true if intPart should be incremented to represent the rounded value of intPart + fracPart, which is positive.