ptolemy.data
Class DoubleToken

java.lang.Object
  |
  +--ptolemy.data.Token
        |
        +--ptolemy.data.ScalarToken
              |
              +--ptolemy.data.DoubleToken
All Implemented Interfaces:
java.io.Serializable

public class DoubleToken
extends ScalarToken

A token that contains a double precision number.

Note that a double cannot be losslessly converted to a long, and vice versa, as both have 64 bit representations in Java.

Since:
Ptolemy II 0.2
Version:
$Id: DoubleToken.java,v 1.82 2002/02/21 18:03:40 cxh Exp $
Author:
Neil Smyth, Yuhong Xiong, Christopher Hylands
See Also:
Token, NumberFormat, Serialized Form

Fields inherited from class ptolemy.data.ScalarToken
_unitCategoryExponents
 
Constructor Summary
DoubleToken()
          Construct a DoubleToken with value 0.0.
DoubleToken(double value)
          Construct a DoubleToken with the specified value.
DoubleToken(java.lang.String init)
          Construct a DoubleToken from the specified string.
 
Method Summary
 ScalarToken absolute()
          Return a DoubleToken containing the absolute value of the value of this token.
 Token add(Token rightArgument)
          Return a new token whose value is the sum of this token and the argument.
 Token addReverse(Token leftArgument)
          Return a new token whose value is the sum of this token and the argument.
 Complex complexValue()
          Return the value of this token as a Complex.
static Token convert(Token token)
          Convert the specified token into an instance of DoubleToken.
 Token divide(Token divisor)
          Return a new token whose value is the value of this token divided by the value of the argument token.
 Token divideReverse(Token dividend)
          Return a new token whose value is the value of the argument token divided by the value of this token.
 double doubleValue()
          Return the value in the token as a double.
 boolean equals(java.lang.Object object)
          Return true if the argument is an instance of DoubleToken with the same value.
 Type getType()
          Return the type of this token.
 int hashCode()
          Return a hash code value for this token.
 BooleanToken isCloseTo(Token token)
          Test that the value of this token is close to the argument token and that the units of this TOken and the argument token are equal.
 BooleanToken isCloseTo(Token token, double epsilon)
          Test that the value of this token is close to the argument token and that the units of this token and the argument token equal.
 BooleanToken isEqualTo(Token token)
          Test the value and units of this token and the argument token for equality.
 BooleanToken isLessThan(ScalarToken token)
          Check if the value of this token is strictly less than that of the argument token.
 Token modulo(Token rightArgument)
          Return a new token whose value is the value of this token modulo the value of the argument token.
 Token moduloReverse(Token leftArgument)
          Return a new token whose value is the value of the argument token modulo the value of this token.
 Token multiply(Token rightFactor)
          Return a new token whose value is the value of this token multiplied by the value of the argument token.
 Token multiplyReverse(Token leftFactor)
          Return a new token whose value is the value of the argument token multiplied by the value of this token.
 Token one()
          Returns a new DoubleToken with value 1.0.
 Token subtract(Token rightArgument)
          Return a new token whose value is the value of the argument token subtracted from the value of this token.
 Token subtractReverse(Token leftArgument)
          Return a new token whose value is the value of this token subtracted from the value of the argument token.
 java.lang.String toString()
          Return the value of this token as a string that can be parsed by the expression language to recover a token with the same value.
 Token zero()
          Returns a new DoubleToken with value 0.0.
 
Methods inherited from class ptolemy.data.ScalarToken
_addCategoryExponents, _areUnitsEqual, _copyOfCategoryExponents, _isUnitless, _subtractCategoryExponents, fixValue, intValue, inUnitsOf, longValue, setUnitCategory, unitsString
 
Methods inherited from class ptolemy.data.Token
_notSupportedMessage
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DoubleToken

public DoubleToken()
Construct a DoubleToken with value 0.0.

DoubleToken

public DoubleToken(double value)
Construct a DoubleToken with the specified value.

DoubleToken

public DoubleToken(java.lang.String init)
            throws IllegalActionException
Construct a DoubleToken from the specified string.
Throws:
IllegalActionException - If the Token could not be created with the given String.
Method Detail

absolute

public ScalarToken absolute()
Return a DoubleToken containing the absolute value of the value of this token.
Overrides:
absolute in class ScalarToken
Returns:
A DoubleToken.

add

public Token add(Token rightArgument)
          throws IllegalActionException
Return a new token whose value is the sum of this token and the argument. Type resolution also occurs here, with the returned Token type chosen to achieve a lossless conversion.
Overrides:
add in class Token
Parameters:
rightArgument - The token to add to this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be added to this token, or the units of this token and the argument token are not the same.

addReverse

public Token addReverse(Token leftArgument)
                 throws IllegalActionException
Return a new token whose value is the sum of this token and the argument. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
addReverse in class Token
Parameters:
leftArgument - The token to add this token to.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be added to this token, or the units of this token and the argument token are not the same.

complexValue

public Complex complexValue()
Return the value of this token as a Complex. The real part of the Complex is the value of this token, the imaginary part is set to 0.
Overrides:
complexValue in class ScalarToken
Returns:
A Complex

convert

public static Token convert(Token token)
                     throws IllegalActionException
Convert the specified token into an instance of DoubleToken. This method does lossless conversion. If the argument is already an instance of DoubleToken, it is returned without any change. Otherwise, if the argument is below DoubleToken in the type hierarchy, it is converted to an instance of DoubleToken or one of the subclasses of DoubleToken and returned. If none of the above condition is met, an exception is thrown.
Parameters:
token - The token to be converted to a DoubleToken.
Returns:
A DoubleToken.
Throws:
IllegalActionException - If the conversion cannot be carried out.

divide

public Token divide(Token divisor)
             throws IllegalActionException
Return a new token whose value is the value of this token divided by the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
divide in class Token
Parameters:
divisor - The token to divide this token by
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can divide the value of this token.

divideReverse

public Token divideReverse(Token dividend)
                    throws IllegalActionException
Return a new token whose value is the value of the argument token divided by the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
divideReverse in class Token
Parameters:
dividend - The token to be divided by the value of this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be divided by this token.

doubleValue

public double doubleValue()
Return the value in the token as a double.
Overrides:
doubleValue in class ScalarToken
Returns:
The value contained in this token as a double.

equals

public boolean equals(java.lang.Object object)
Return true if the argument is an instance of DoubleToken with the same value.
Overrides:
equals in class Token
Parameters:
object - An instance of Object.
Returns:
True if the argument is an instance of DoubleToken with the same value.

getType

public Type getType()
Return the type of this token.
Overrides:
getType in class ScalarToken
Returns:
BaseType.DOUBLE

hashCode

public int hashCode()
Return a hash code value for this token. This method returns the integer portion of the contained double.
Overrides:
hashCode in class Token
Returns:
A hash code value for this token.

isCloseTo

public BooleanToken isCloseTo(Token token)
                       throws IllegalActionException
Test that the value of this token is close to the argument token and that the units of this TOken and the argument token are equal. The value of the ptolemy.math.Complex epsilon field is used to determine whether the two Tokens are close.

If A and B are the values of the tokens, and if the following is true:

  absolute(A-B) < epsilon
  
and the units of A and B are equal, then A and B are considered close.
Overrides:
isCloseTo in class Token
Parameters:
token - The token to test closeness of this token with.
Returns:
a boolean token that contains the value true if the value of this token is close to the value of the argument token and the units of both tokens are equal.
Throws:
IllegalActionException - If the argument token is not of a type that can be compared with this token.
See Also:
Complex.epsilon, isEqualTo(ptolemy.data.Token)

isCloseTo

public BooleanToken isCloseTo(Token token,
                              double epsilon)
                       throws IllegalActionException
Test that the value of this token is close to the argument token and that the units of this token and the argument token equal. The value of the epsilon argument is used to determine whether the two Tokens are close.

If A and B are the values of the tokens, and if the following is true:

  abs(A-B) < epsilon
  
and the units of A and B are equal, then A and B are considered close.

There are two isCloseTo() methods so that we can use different values of epsilon in different threads without modifying the value of math.Complex.epsilon.

Overrides:
isCloseTo in class Token
Parameters:
token - The token to test closeness of this token with.
epsilon - The value that we use to determine whether two tokens are close.
Returns:
a boolean token that contains the value true if the value of this token is close to the value of the argument token and the units of both tokens are equal.
Throws:
IllegalActionException - If the argument token is not of a type that can be compared with this token.
See Also:
isEqualTo(ptolemy.data.Token)

isEqualTo

public BooleanToken isEqualTo(Token token)
                       throws IllegalActionException
Test the value and units of this token and the argument token for equality. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
isEqualTo in class Token
Parameters:
token - The token to test equality of this token with.
Returns:
a boolean token that contains the value true if the value and units of this token are equal to those of the argument token.
Throws:
IllegalActionException - If the argument token is not of a type that can be compared with this token.
See Also:
isCloseTo(ptolemy.data.Token)

isLessThan

public BooleanToken isLessThan(ScalarToken token)
                        throws IllegalActionException
Check if the value of this token is strictly less than that of the argument token.
Overrides:
isLessThan in class ScalarToken
Parameters:
arg - A ScalarToken.
Returns:
A BooleanToken with value true if this token is strictly less than the argument.
Throws:
IllegalActionException - If the type of the argument token is incomparable with the type of this token, or the units of this token and the argument are not the same.

modulo

public Token modulo(Token rightArgument)
             throws IllegalActionException
Return a new token whose value is the value of this token modulo the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
modulo in class Token
Parameters:
rightArgument - The token to modulo this token by.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be used with modulo, or the units of this token and the argument token are not the same.

moduloReverse

public Token moduloReverse(Token leftArgument)
                    throws IllegalActionException
Return a new token whose value is the value of the argument token modulo the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
moduloReverse in class Token
Parameters:
leftArgument - The token to apply modulo to by the value of this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can apply modulo by this token, or if the units of this token are not the same as those of the argument.

multiply

public Token multiply(Token rightFactor)
               throws IllegalActionException
Return a new token whose value is the value of this token multiplied by the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
multiply in class Token
Parameters:
rightFactor - The token to multiply this token by.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be multiplied to this token.

multiplyReverse

public Token multiplyReverse(Token leftFactor)
                      throws IllegalActionException
Return a new token whose value is the value of the argument token multiplied by the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
multiplyReverse in class Token
Parameters:
leftFactor - The token to be multiplied by the value of this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be multiplied by this token.

one

public Token one()
Returns a new DoubleToken with value 1.0.
Overrides:
one in class Token
Returns:
A new DoubleToken with value 1.0.

subtract

public Token subtract(Token rightArgument)
               throws IllegalActionException
Return a new token whose value is the value of the argument token subtracted from the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
subtract in class Token
Parameters:
rightArgument - The token to subtract to this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be subtracted from this token, or the units of this token and the argument token are not the same.

subtractReverse

public Token subtractReverse(Token leftArgument)
                      throws IllegalActionException
Return a new token whose value is the value of this token subtracted from the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
subtractReverse in class Token
Parameters:
leftArgument - The token to subtract this token from.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be added to this token, or the units of this token and the argument token are not the same.

toString

public java.lang.String toString()
Return the value of this token as a string that can be parsed by the expression language to recover a token with the same value. The exact form of the number depends on its value, and may be either decimal or exponential. In general, exponential is used for numbers whose magnitudes are very large or very small, except for zero which is always represented as 0.0. The behavior is roughly the same as Double.toString(), except that we limit the precision to seven fractional digits. If you really must have better precision, then use Double.toString(token.doubleValue()). If this token has a unit, the return string also includes a unit string produced by the unitsString() method in the super class.
Overrides:
toString in class Token
Returns:
A String representing the double value and the units (if any) of this token.
See Also:
ScalarToken.unitsString()

zero

public Token zero()
Returns a new DoubleToken with value 0.0.
Overrides:
zero in class Token
Returns:
A DoubleToken with value 0.0.