Package ptolemy.data
Class FloatToken
- java.lang.Object
-
- ptolemy.data.Token
-
- ptolemy.data.ScalarToken
-
- ptolemy.data.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 Summary
Fields Modifier and Type Field Description static FloatToken
NIL
A token that represents a missing value.static FloatToken
ONE
A FloatToken with the value 1.0.static FloatToken
ZERO
A FloatToken with the value 0.0.-
Fields inherited from class ptolemy.data.ScalarToken
_unitCategoryExponents
-
-
Constructor Summary
Constructors Constructor Description FloatToken()
Construct a FloatToken with value 0.0.FloatToken(float value)
Construct a FloatToken with the specified value.FloatToken(java.lang.String init)
Construct a FloatToken from the specified string.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ScalarToken
_absolute()
Return a ScalarToken containing the absolute value of the value of this token.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.protected ScalarToken
_bitwiseAnd(ScalarToken rightArgument)
Returns a token representing the bitwise AND of this token and the given token.protected ScalarToken
_bitwiseNot()
Returns a token representing the bitwise NOT of this token.protected ScalarToken
_bitwiseOr(ScalarToken rightArgument)
Returns a token representing the bitwise OR of this token and the given token.protected ScalarToken
_bitwiseXor(ScalarToken rightArgument)
Returns a token representing the bitwise XOR of this token and the given token.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.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.protected BooleanToken
_isLessThan(ScalarToken rightArgument)
Test for ordering of the values of this Token and the argument Token.protected ScalarToken
_modulo(ScalarToken rightArgument)
Return a new token whose value is the value of this token modulo the value of the argument token.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.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.static FloatToken
convert(Token token)
Convert the specified token into an instance of FloatToken.double
doubleValue()
Return the value in the token as a double.boolean
equals(java.lang.Object object)
Return true if the argument's class is FloatToken and it has the same values as this token.float
floatValue()
Return the value in the token as a float.Type
getType()
Return the type of this token.int
hashCode()
Return a hash code value for this token.boolean
isNil()
Return true if the token is nil, (aka null or missing).Token
one()
Returns a FloatToken with value 1.0.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 FloatToken with value 0.0.-
Methods inherited from class ptolemy.data.ScalarToken
_addCategoryExponents, _areUnitsEqual, _copyOfCategoryExponents, _isEqualTo, _isUnitless, _subtractCategoryExponents, absolute, add, addReverse, bitwiseAnd, bitwiseNot, bitwiseOr, bitwiseXor, byteValue, complexValue, divide, divideReverse, fixValue, intValue, inUnitsOf, isCloseTo, isEqualTo, isGreaterThan, isLessThan, isLessThan, leftShift, logicalRightShift, longValue, modulo, moduloReverse, multiply, multiplyReverse, rightShift, setUnitCategory, shortValue, subtract, subtractReverse, unitsString
-
Methods inherited from class ptolemy.data.Token
isCloseTo, notSupportedConversionMessage, notSupportedIncomparableConversionMessage, notSupportedIncomparableMessage, notSupportedMessage, notSupportedNullNilStringMessage, pow, zeroReturnType
-
-
-
-
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, thenNIL
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 classScalarToken
- 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 classjava.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 classScalarToken
- Returns:
- The value contained in this token as a float.
-
getType
public Type getType()
Return the type of this token.- Specified by:
getType
in classScalarToken
- 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 classjava.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.
-
one
public Token one()
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 useFloat.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 classToken
- 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.
-
_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 classScalarToken
- 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 classScalarToken
- Parameters:
rightArgument
- The token to add to this token.- Returns:
- A new FloatToken containing the result.
-
_bitwiseAnd
protected ScalarToken _bitwiseAnd(ScalarToken rightArgument) throws IllegalActionException
Returns a token representing the bitwise AND of this token and the given token.- Specified by:
_bitwiseAnd
in classScalarToken
- Parameters:
rightArgument
- The FloatToken to bitwise AND with this one.- Returns:
- The bitwise AND.
- Throws:
IllegalActionException
- Always thrown by this base class.
-
_bitwiseNot
protected ScalarToken _bitwiseNot() throws IllegalActionException
Returns a token representing the bitwise NOT of this token.- Specified by:
_bitwiseNot
in classScalarToken
- Returns:
- The bitwise NOT of this token.
- Throws:
IllegalActionException
- Always thrown by this base class.
-
_bitwiseOr
protected ScalarToken _bitwiseOr(ScalarToken rightArgument) throws IllegalActionException
Returns a token representing the bitwise OR of this token and the given token.- Specified by:
_bitwiseOr
in classScalarToken
- Parameters:
rightArgument
- The FloatToken to bitwise OR with this one.- Returns:
- The bitwise OR.
- Throws:
IllegalActionException
- Always thrown by this base class.
-
_bitwiseXor
protected ScalarToken _bitwiseXor(ScalarToken rightArgument) throws IllegalActionException
Returns a token representing the bitwise XOR of this token and the given token.- Specified by:
_bitwiseXor
in classScalarToken
- Parameters:
rightArgument
- The FloatToken to bitwise XOR with this one.- Returns:
- The bitwise XOR.
- Throws:
IllegalActionException
- Always thrown by this base class.
-
_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 classScalarToken
- 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 classScalarToken
- 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 classScalarToken
- 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 classScalarToken
- 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 classScalarToken
- 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 classScalarToken
- Parameters:
rightArgument
- The token to subtract from this token.- Returns:
- A new FloatToken containing the result.
-
-