byucc.jhdl.Logic.Modules.FloatingPoint.helpers
Class FloatingPoint

java.lang.Object
  extended byjava.lang.Number
      extended bybyucc.jhdl.Logic.Modules.FloatingPoint.helpers.FloatingPoint
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
JHDLFloatingPoint

public class FloatingPoint
extends java.lang.Number
implements java.lang.Cloneable

Immutable custom-precision floating-point numbers

Author:
Nathan Kitchen
See Also:
Serialized Form

Field Summary
protected  boolean isInfinite
           
protected  boolean isNaN
           
 
Constructor Summary
protected FloatingPoint(double dval, java.lang.Object objval, int numExponentBits, int numMantissaBits)
          Base constructor--calls initialize
 
Method Summary
 FloatingPoint abs()
          Returns the absolute value of this FloatingPoint
 FloatingPoint add(FloatingPoint val)
          Returns the sum of two FloatingPoints.
 java.lang.Object clone()
           
 int compareTo_g(FloatingPoint val)
          Compares two FloatingPoints.
 int compareTo_l(FloatingPoint val)
          Compares two FloatingPoints.
 int compareTo(FloatingPoint val)
          Compares two FloatingPoints.
static FloatingPoint create(java.math.BigDecimal val, FloatingPoint prototype)
          Constructs a FloatingPoint with the same numbers of bits as and the same class as prototype.
static FloatingPoint create(java.math.BigDecimal val, int numExponentBits, int numMantissaBits)
          Constructs a FloatingPoint number with the specified numbers of bits.
static FloatingPoint create(java.math.BigDecimal val, int numExponentBits, int numMantissaBits, FloatingPoint prototype)
          Constructs a FloatingPoint number with the specified numbers of bits.
static FloatingPoint create(double val, FloatingPoint prototype)
          Constructs a FloatingPoint with the same numbers of bits and the same class as prototype.
static FloatingPoint create(double val, int numExponentBits, int numMantissaBits)
          Constructs a FloatingPoint with the specified numbers of bits.
static FloatingPoint create(double val, int numExponentBits, int numMantissaBits, FloatingPoint prototype)
          Constructs a FloatingPoint with the specified numbers of bits of the same class as prototype.
static FloatingPoint create(FloatingPoint val, FloatingPoint prototype)
          Constructs a FloatingPoint with the same value as val and the same numbers of bits and class as prototype.
static FloatingPoint create(FloatingPoint val, int numExponentBits, int numMantissaBits)
          Constructs a FloatingPoint number with the specified numbers of bits.
static FloatingPoint create(FloatingPoint val, int numExponentBits, int numMantissaBits, FloatingPoint prototype)
          Constructs a FloatingPoint number with the specified numbers of bits and the same class as prototype.
static FloatingPoint create(long val, FloatingPoint prototype)
          Constructs a FloatingPoint with the same numbers of bits and the same class as prototype.
static FloatingPoint create(long val, int numExponentBits, int numMantissaBits)
          Constructs a FloatingPoint with the specified numbers of bits.
static FloatingPoint create(long val, int numExponentBits, int numMantissaBits, FloatingPoint prototype)
          Constructs a FloatingPoint with the specified numbers of bits and the same class as prototype.
static FloatingPoint create(java.lang.String str)
          Constructs a FloatingPoint from a string
static FloatingPoint create(java.lang.String str, FloatingPoint prototype)
          Constructs a FloatingPoint from a string of the same class as prototype.
 FloatingPoint divide(FloatingPoint val)
          Returns the quotient of two FloatingPoints.
 double doubleValue()
          Converts this FloatingPoint to a double.
 boolean equals(java.lang.Object o)
          Compares this FloatingPoint number with the specified Object for equality.
 float floatValue()
          Converts this FloatingPoint to a float.
protected  int getExponent()
          Returns the unbiased exponent
 int getNumExponentBits()
          Returns the number of exponent bits
 int getNumMantissaBits()
          Returns the number of mantissa bits
 int getSign()
          Returns -1 or 1 to indicate the sign bit
protected  java.math.BigInteger getSignificand()
          Returns the significand as represented internally: a positive BigInteger.
protected  void handleOverflow()
          Handles the condition where the exponent is greater than the number of exponent bits can represent.
protected  void handleUnderflow()
          Handles the condition where the exponent is less than the number of exponent bits can represent.
 int intValue()
          Converts this FloatingPoint to a int.
 boolean isInfinite()
           
 boolean isNaN()
           
 long longValue()
          Converts this FloatingPoint to a long.
 FloatingPoint multiply(FloatingPoint val)
          Returns the product of two FloatingPoints.
 FloatingPoint negate()
          Returns the FloatingPoint whose value is the opposite of the value of this
 FloatingPoint remainder(FloatingPoint val)
          Returns the remainder of this FloatingPoint divided by another (that is, this % val).
 int signum()
          Returns -1, 0, or 1 as the value of this FloatingPoint is negative, zero, or positive, respectively
 FloatingPoint subtract(FloatingPoint val)
          Returns the difference of two FloatingPoints.
 java.math.BigDecimal toBigDecimal()
          Converts this FloatingPoint to a BigDecimal.
 java.lang.String toBinaryString()
          Returns a string representing the value of this FloatingPoint and showing its bits.
 java.lang.String toString()
          Uses toBigDecimal except for special cases like infinity
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

isInfinite

protected boolean isInfinite

isNaN

protected boolean isNaN
Constructor Detail

FloatingPoint

protected FloatingPoint(double dval,
                        java.lang.Object objval,
                        int numExponentBits,
                        int numMantissaBits)
Base constructor--calls initialize

Method Detail

create

public static FloatingPoint create(double val,
                                   int numExponentBits,
                                   int numMantissaBits)
Constructs a FloatingPoint with the specified numbers of bits. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(long val,
                                   int numExponentBits,
                                   int numMantissaBits)
Constructs a FloatingPoint with the specified numbers of bits. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(java.math.BigDecimal val,
                                   int numExponentBits,
                                   int numMantissaBits)
Constructs a FloatingPoint number with the specified numbers of bits. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(FloatingPoint val,
                                   int numExponentBits,
                                   int numMantissaBits)
Constructs a FloatingPoint number with the specified numbers of bits. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(java.lang.String str)
                            throws java.lang.NumberFormatException
Constructs a FloatingPoint from a string

The string may contain a binary or hexadecimal representation of the value. The radix depends on the digits used. If the significand and exponent parts of the string contain only ones and zeroes, both the significand and the exponent are parsed as binary numbers, unless the first two characters of the string are "0x". If these two characters are present, or if either the significand or exponent part of the string contains digits besides ones and zeroes, both the significand and the exponent are parsed as hexadecimal numbers.

The number of bits used is determined from the numbers of digits in the string and the radix. If hexadecimal strings are given, the numbers of bits in the exponent and the mantissa will be multiples of four. Note that the digit that precedes the point will be treated specially when the radix is hexadecimal, because it corresponds to the hidden bit, so it can only be 1 or 0.

This is the format of the string: "0x-H.MMMM EEEE". The 0x is the optional hexadecimal prefix. Negative numbers must have a minus sign before the significand. Positive numbers are indicated by the absence of the minus sign, never by a plus sign. H represents the hidden bit, which must be given explicitly. The M's represent the bits of the mantissa, and the E's are the bits of the biased exponent.

Throws:
java.lang.NumberFormatException

create

public static FloatingPoint create(double val,
                                   int numExponentBits,
                                   int numMantissaBits,
                                   FloatingPoint prototype)
Constructs a FloatingPoint with the specified numbers of bits of the same class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(double val,
                                   FloatingPoint prototype)
Constructs a FloatingPoint with the same numbers of bits and the same class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(long val,
                                   int numExponentBits,
                                   int numMantissaBits,
                                   FloatingPoint prototype)
Constructs a FloatingPoint with the specified numbers of bits and the same class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(long val,
                                   FloatingPoint prototype)
Constructs a FloatingPoint with the same numbers of bits and the same class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(java.math.BigDecimal val,
                                   int numExponentBits,
                                   int numMantissaBits,
                                   FloatingPoint prototype)
Constructs a FloatingPoint number with the specified numbers of bits. and the same class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(java.math.BigDecimal val,
                                   FloatingPoint prototype)
Constructs a FloatingPoint with the same numbers of bits as and the same class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(FloatingPoint val,
                                   int numExponentBits,
                                   int numMantissaBits,
                                   FloatingPoint prototype)
Constructs a FloatingPoint number with the specified numbers of bits and the same class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(FloatingPoint val,
                                   FloatingPoint prototype)
Constructs a FloatingPoint with the same value as val and the same numbers of bits and class as prototype. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if val has too great a magnitude to represent with the specified number of bits in the exponent, the FloatingPoint represents positive or negative infinity instead. If the number of bits in the mantissa cannot represent val exactly, rounding is performed according to the round-toward-nearest-even mode as specified in the IEEE 754 standard.


create

public static FloatingPoint create(java.lang.String str,
                                   FloatingPoint prototype)
                            throws java.lang.NumberFormatException
Constructs a FloatingPoint from a string of the same class as prototype.

The string may contain a binary or hexadecimal representation of the value. The radix depends on the digits used. If the significand and exponent parts of the string contain only ones and zeroes, both the significand and the exponent are parsed as binary numbers, unless the first two characters of the string are "0x". If these two characters are present, or if either the significand or exponent part of the string contains digits besides ones and zeroes, both the significand and the exponent are parsed as hexadecimal numbers.

The number of bits used is determined from the numbers of digits in the string and the radix. If hexadecimal strings are given, the numbers of bits in the exponent and the mantissa will be multiples of four. Note that the digit that precedes the point will be treated specially when the radix is hexadecimal, because it corresponds to the hidden bit, so it can only be 1 or 0.

This is the format of the string: "0x-H.MMMM EEEE". The 0x is the optional hexadecimal prefix. Negative numbers must have a minus sign before the significand. Positive numbers are indicated by the absence of the minus sign, never by a plus sign. H represents the hidden bit, which must be given explicitly. The M's represent the bits of the mantissa, and the E's are the bits of the biased exponent.

Throws:
java.lang.NumberFormatException

clone

public java.lang.Object clone()

handleOverflow

protected void handleOverflow()
Handles the condition where the exponent is greater than the number of exponent bits can represent. This implementation sets the fields to represent infinity. The sign field is not changed.


handleUnderflow

protected void handleUnderflow()
Handles the condition where the exponent is less than the number of exponent bits can represent. This implementation sets the fields to represent zero. The sign field is not changed.


getExponent

protected int getExponent()
Returns the unbiased exponent


getNumExponentBits

public int getNumExponentBits()
Returns the number of exponent bits


getNumMantissaBits

public int getNumMantissaBits()
Returns the number of mantissa bits


getSign

public int getSign()
Returns -1 or 1 to indicate the sign bit


getSignificand

protected java.math.BigInteger getSignificand()
Returns the significand as represented internally: a positive BigInteger. The highest set bit is the hidden bit, unless the value is zero.


isInfinite

public boolean isInfinite()
Returns:
true if this FloatingPoint is infinite

isNaN

public boolean isNaN()
Returns:
true if this FloatingPoint is Not a Number

intValue

public int intValue()
Converts this FloatingPoint to a int. Standard narrowing primitive conversion as defined in The Java Language Specification: any fractional part of this FloatingPoint will be discarded, and if the resulting whole number is too big to fit in a int, only the low-order 32 bits are returned.

Returns:
this FloatingPoint converted to a int
Throws:
FloatingPointException - if this FloatingPoint represents an infinite value or NaN

longValue

public long longValue()
Converts this FloatingPoint to a long. Standard narrowing primitive conversion as defined in The Java Language Specification: any fractional part of this FloatingPoint will be discarded, and if the resulting whole number is too big to fit in a long, only the low-order 64 bits are returned.

Returns:
this FloatingPoint converted to a long

floatValue

public float floatValue()
Converts this FloatingPoint to a float. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if this FloatingPoint has too great a magnitude to represent as a float, it will be converted to Float.NEGATIVE_INFINITY or Float.POSITIVE_INFINITY, as appropriate.

Returns:
this FloatingPoint converted to a float

doubleValue

public double doubleValue()
Converts this FloatingPoint to a double. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if this FloatingPoint has too great a magnitude to represent as a double, it will be converted to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY, as appropriate.

Returns:
this FloatingPoint converted to a double

toString

public java.lang.String toString()
Uses toBigDecimal except for special cases like infinity


toBinaryString

public java.lang.String toBinaryString()
Returns a string representing the value of this FloatingPoint and showing its bits. The format of the string is "H.MMMM EEEE", where H is the hidden bit of the significand, the M's represent the bits of the mantissa, and the E's are the bits of the exponent (with bias included). Of course, there may be a minus sign at the beginning. If this FloatingPoint represents an infinite value or NaN, the string returned will include "Infinity", "-Infinity", or "NaN" in parentheses after the bit representation.

The output of this method is compatible with the create(String).


toBigDecimal

public java.math.BigDecimal toBigDecimal()
Converts this FloatingPoint to a BigDecimal. No precision is lost.

Throws:
FloatingPointException - if this FloatingPoint represents an infinite value or NaN

add

public FloatingPoint add(FloatingPoint val)
Returns the sum of two FloatingPoints. The FloatingPoint returned has as many exponent bits as whichever addend has more exponent bits and as many mantissa bits as whichever addend has more mantissa bits.

Parameters:
val - value to be added to this FloatingPoint
Returns:
this + val

subtract

public FloatingPoint subtract(FloatingPoint val)
Returns the difference of two FloatingPoints. The FloatingPoint returned has as many exponent bits as whichever of the two numbers has more exponent bits and as many mantissa bits as whichever number has more mantissa bits.

Parameters:
val - value to be subtracted from this FloatingPoint
Returns:
this - val

multiply

public FloatingPoint multiply(FloatingPoint val)
Returns the product of two FloatingPoints. The FloatingPoint returned has as many exponent bits as whichever of the two numbers has more exponent bits and as many mantissa bits as whichever number has more mantissa bits.

Parameters:
val - value by which this FloatingPoint is to be multiplied
Returns:
this * val

divide

public FloatingPoint divide(FloatingPoint val)
Returns the quotient of two FloatingPoints. The FloatingPoint returned has as many exponent bits as whichever of the two numbers has more exponent bits and as many mantissa bits as whichever number has more mantissa bits.

Parameters:
val - value by which this FloatingPoint is to be divided
Returns:
this / val

remainder

public FloatingPoint remainder(FloatingPoint val)
Returns the remainder of this FloatingPoint divided by another (that is, this % val). The FloatingPoint returned has as many exponent bits as whichever of the two FloatingPoints has more exponent bits, and as many mantissa bits as whichever of the two has more mantissa bits.

Parameters:
val - value by which this FloatingPoint is to be divided
Returns:
this / val

negate

public FloatingPoint negate()
Returns the FloatingPoint whose value is the opposite of the value of this


signum

public int signum()
Returns -1, 0, or 1 as the value of this FloatingPoint is negative, zero, or positive, respectively

Throws:
FloatingPointException - if this FloatingPoint represents NaN

abs

public FloatingPoint abs()
Returns the absolute value of this FloatingPoint


equals

public boolean equals(java.lang.Object o)
Compares this FloatingPoint number with the specified Object for equality. This method differs from compareTo_g and compareTo_l in the following ways:


compareTo

public int compareTo(FloatingPoint val)
Compares two FloatingPoints.

Returns:
-1, 0, or 1 as this is less than, equal to, or greater than val, respectively.
Throws:
FloatingPointException - if either FloatingPoint represents NaN

compareTo_g

public int compareTo_g(FloatingPoint val)
Compares two FloatingPoints. This method compares FloatingPoints the same way that the bytecode instructions fcmpg and dcmpg compare floats and doubles, respectively: If either of the two FloatingPoints represent NaN, it returns 1.

Returns:
-1, 0, or 1 as this is less than, equal to, or greater than val, respectively. Returns 1 if either number is NaN

compareTo_l

public int compareTo_l(FloatingPoint val)
Compares two FloatingPoints. This method compares FloatingPoints the same way that the bytecode instructions fcmpl and dcmpl compare floats and doubles, respectively: If either of the two FloatingPoints represent NaN, it returns -1.

Returns:
-1, 0, or 1 as this is less than, equal to, or greater than val, respectively. Returns -1 if either number is NaN


Copyright ? 2006 Brigham Young University, Configurable Computing Laboratory. All Rights Reserved.