Package ptolemy.data

Class FloatToken

  • All Implemented Interfaces:
    BitwiseOperationToken, PartiallyOrderedToken

    public class FloatToken
    extends ScalarToken
    A token that contains a 32-bit signed mantissa, signed exponent single precision floating-point number (IEEE 754). This class handles overflow and underflow as does normal java arithmetic on floats.

    Note that a float cannot be losslessly converted to an int, and vice versa, as both have 32-bit representations in Java.

    Since:
    Ptolemy II 6.1
    Version:
    $Id$
    Author:
    Ben Lickly; Based on DoubleToken by Neil Smyth, Yuhong Xiong, Christopher Hylands, Steve Neuendorffer
    See Also:
    Token
    Pt.AcceptedRating:
    Red (blickly)
    Pt.ProposedRating:
    Red (blickly) nil token, ONE, ZERO
    • Field Detail

      • NIL

        public static final FloatToken NIL
        A token that represents a missing value. Null or missing tokens are common in analytical systems like R and SAS where they are used to handle sparsely populated data sources. In database parlance, missing tokens are sometimes called null tokens. Since null is a Java keyword, we use the term "nil". The toString() method on a nil token returns the string "nil".
      • ONE

        public static final FloatToken ONE
        A FloatToken with the value 1.0.
      • ZERO

        public static final FloatToken ZERO
        A FloatToken with the value 0.0.
    • Constructor Detail

      • FloatToken

        public FloatToken()
        Construct a FloatToken with value 0.0.
      • FloatToken

        public FloatToken​(float value)
        Construct a FloatToken with the specified value.
        Parameters:
        value - The specified value.
      • FloatToken

        public FloatToken​(java.lang.String init)
                   throws IllegalActionException
        Construct a FloatToken from the specified string.
        Parameters:
        init - The initialization string, which is in a format suitable for java.lang.Float.parseFloat(String).
        Throws:
        IllegalActionException - If the Token could not be created with the given String.
    • Method Detail

      • convert

        public static FloatToken convert​(Token token)
                                  throws IllegalActionException
        Convert the specified token into an instance of FloatToken. This method does lossless conversion. The units of the returned token will be the same as the units of the given token. If the argument is already an instance of FloatToken, it is returned without any change. If the argument is a nil token, then NIL is returned. Otherwise, if the argument is below FloatToken in the type hierarchy, it is converted to an instance of FloatToken or one of the subclasses of FloatToken and returned. If none of the above condition is met, an exception is thrown.
        Parameters:
        token - The token to be converted to a FloatToken.
        Returns:
        A FloatToken.
        Throws:
        IllegalActionException - If the conversion cannot be carried out.
      • 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 represented as a double.
      • equals

        public boolean equals​(java.lang.Object object)
        Return true if the argument's class is FloatToken and it has the same values as this token.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        object - An instance of Object.
        Returns:
        True if the argument is a FloatToken with the same value. If either this object or the argument is a nil Token, return false.
      • floatValue

        public float floatValue()
        Return the value in the token as a float.
        Overrides:
        floatValue in class ScalarToken
        Returns:
        The value contained in this token as a float.
      • getType

        public Type getType()
        Return the type of this token.
        Specified by:
        getType in class ScalarToken
        Returns:
        BaseType.FLOAT
      • hashCode

        public int hashCode()
        Return a hash code value for this token. This method returns the integer portion of the contained float.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        A hash code value for this token.
      • isNil

        public boolean isNil()
        Return true if the token is nil, (aka null or missing). Nil or missing tokens occur when a data source is sparsely populated.
        Overrides:
        isNil in class Token
        Returns:
        True if the token is the NIL token.
      • one

        public Token one()
        Returns a FloatToken with value 1.0.
        Overrides:
        one in class Token
        Returns:
        A FloatToken with value 1.0.
      • 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 Float.toString(), except that we limit the precision to seven fractional digits. If you really must have better precision, then use Float.toString(token.floatValue()). 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 float value and the units (if any) of this token.
        See Also:
        ScalarToken.unitsString()
      • zero

        public Token zero()
        Returns a FloatToken with value 0.0.
        Overrides:
        zero in class Token
        Returns:
        A FloatToken with value 0.0.
      • _absolute

        protected ScalarToken _absolute()
        Return a ScalarToken containing the absolute value of the value of this token. If this token contains a non-negative number, it is returned directly; otherwise, a new token is is return. Note that it is explicitly allowable to return this token, since the units are the same.
        Specified by:
        _absolute in class ScalarToken
        Returns:
        An FloatToken.
      • _add

        protected ScalarToken _add​(ScalarToken rightArgument)
        Return a new token whose value is the value of the argument Token added to the value of this Token. It is assumed that the type of the argument is a FloatToken.
        Specified by:
        _add in class ScalarToken
        Parameters:
        rightArgument - The token to add to this token.
        Returns:
        A new FloatToken containing the result.
      • _divide

        protected ScalarToken _divide​(ScalarToken divisor)
        Return a new token whose value is the value of this token divided by the value of the argument token. It is assumed that the type of the argument is a FloatToken
        Specified by:
        _divide in class ScalarToken
        Parameters:
        divisor - The token to divide this token by.
        Returns:
        A new FloatToken containing the result.
      • _isCloseTo

        protected BooleanToken _isCloseTo​(ScalarToken rightArgument,
                                          double epsilon)
        Test that the value of this token is close to the first argument, where "close" means that the distance between their values is less than or equal to the second argument. It is assumed that the type of the first argument is FloatToken.
        Specified by:
        _isCloseTo in class ScalarToken
        Parameters:
        rightArgument - The token to compare to this token.
        epsilon - The distance.
        Returns:
        A token containing tue if the value of this token is close to that of the argument.
      • _isLessThan

        protected BooleanToken _isLessThan​(ScalarToken rightArgument)
                                    throws IllegalActionException
        Test for ordering of the values of this Token and the argument Token. It is assumed that the type of the argument is FloatToken.
        Specified by:
        _isLessThan in class ScalarToken
        Parameters:
        rightArgument - The token to compare to this token.
        Returns:
        A new Token containing the result.
        Throws:
        IllegalActionException - If this method is not supported by the derived class.
      • _modulo

        protected ScalarToken _modulo​(ScalarToken rightArgument)
        Return a new token whose value is the value of this token modulo the value of the argument token. It is assumed that the type of the argument is a FloatToken.
        Specified by:
        _modulo in class ScalarToken
        Parameters:
        rightArgument - The token to modulo this token by.
        Returns:
        A new FloatToken containing the result.
      • _multiply

        protected ScalarToken _multiply​(ScalarToken rightArgument)
        Return a new token whose value is the value of this token multiplied by the value of the argument token. It is assumed that the type of the argument is a FloatToken.
        Specified by:
        _multiply in class ScalarToken
        Parameters:
        rightArgument - The token to multiply this token by.
        Returns:
        A new FloatToken containing the result.
      • _subtract

        protected ScalarToken _subtract​(ScalarToken rightArgument)
        Return a new token whose value is the value of the argument token subtracted from the value of this token. It is assumed that the type of the argument is a FloatToken.
        Specified by:
        _subtract in class ScalarToken
        Parameters:
        rightArgument - The token to subtract from this token.
        Returns:
        A new FloatToken containing the result.