net.dclausen.microfloat
Class MicroFloat

java.lang.Object
  extended by net.dclausen.microfloat.MicroFloat

public final class MicroFloat
extends java.lang.Object

A software implementation of IEEE-754 single precision math which does not rely on the float data type. This class overloads the int data type by storing float data in it. See the package description for more information.

See Also:
Float, MicroDouble

Field Summary
static int E
          A single-precision version of MicroDouble.E
static int MAX_VALUE
          A constant holding the same value as Float.MAX_VALUE
static int MIN_VALUE
          A constant holding the same value as Float.MIN_VALUE
static int NaN
          A constant holding the same value as Float.NaN
static int NEGATIVE_INFINITY
          A constant holding the same value as Float.NEGATIVE_INFINITY
static int NEGATIVE_ZERO
          A constant holding the value of -0.0f
static int ONE
          A constant holding the value of 1.0f
static int ONE_HALF
          A constant holding the value of 0.5f
static int PI
          A single-precision version of MicroDouble.PI
static int POSITIVE_INFINITY
          A constant representing the same value as Float.POSITIVE_INFINITY
static int TWO
          A constant holding the value of 2.0f
static int ZERO
          A constant holding the value of 0.0f
 
Constructor Summary
MicroFloat(int f)
          Constructs a newly-allocated MicroFloat object that represents the argument.
MicroFloat(java.lang.String s)
          Constructs a newly-allocated MicroFloat object that represents the argument.
 
Method Summary
static int abs(int f)
          Mimics Math.abs(float)
static int add(int f1, int f2)
          Returns the sum of the two float arguments according to section 15.18.2 of the JLS.
static byte byteValue(int f)
          Convert the given float to a byte as would happen in a casting operation specified by section 5.1.3 of the JLS.
static int ceil(int f)
          Mimics Math.ceil(double), using single precision.
static int compare(int f1, int f2)
          Mimics Float.compare(float, float).
static int div(int f1, int f2)
          Returns the quotient of the two float arguments according to section 15.17.2 of the JLS.
static int doubleToFloat(long d)
          Convert the given double to a float as would happen in a casting operation specified by section 5.1.3 of the JLS.
static long doubleValue(int f)
          Convert the given float to a double as would happen in a casting operation specified by section 5.1.2 of the JLS.
static boolean eq(int f1, int f2)
          Returns true if the specified numbers are considered equal according to section 15.21.1 of the JLS.
 boolean equals(java.lang.Object obj)
          Compares this object against the specified object.
 int floatValue()
          Returns the float value of this MicroFloat object.
static int floor(int f)
          Mimics Math.floor(double), using single precision.
static boolean ge(int f1, int f2)
          Returns true if the first argument is considered greater than or equal to the second argument according to section 15.20.1 of the JLS.
static boolean gt(int f1, int f2)
          Returns true if the first argument is considered greater than the second argument according to section 15.20.1 of the JLS.
 int hashCode()
          Returns a hash code for this MicroFloat object.
static int intToFloat(int x)
          Convert the given int to a float as would happen in a casting operation specified by section 5.1.2 of the JLS.
static int intValue(int f)
          Convert the given float to an int as would happen in a casting operation specified by section 5.1.3 of the JLS.
static boolean isInfinite(int f)
          Mimics Float.isInfinite(float)
static boolean isNaN(int f)
          Mimics Float.isNaN(float)
static boolean isZero(int f)
          Returns true if the specified number has zero magnitude, false otherwise.
static boolean le(int f1, int f2)
          Returns true if the first argument is considered less than or equal to the second argument according to section 15.20.1 of the JLS.
static int longToFloat(long x)
          Convert the given long to a float as would happen in a casting operation specified by section 5.1.2 of the JLS.
static long longValue(int f)
          Convert the given float to a long as would happen in a casting operation specified by section 5.1.3 of the JLS.
static boolean lt(int f1, int f2)
          Returns true if the first argument is considered less than the second argument according to section 15.20.1 of the JLS.
static int max(int f1, int f2)
          Mimics Math.max(float, float).
static int min(int f1, int f2)
          Mimics Math.min(float, float).
static int mod(int f1, int f2)
          Returns the remainder of the two float arguments according to section 15.17.3 of the JLS.
static int mul(int f1, int f2)
          Returns the product of the two float arguments according to section 15.17.1 of the JLS.
static boolean ne(int f1, int f2)
          Returns true if the specified numbers are considered unequal according to section 15.21.1 of the JLS.
static int negate(int f)
          Returns the negation of a float value.
static int parseFloat(java.lang.String s)
          Mimics Float.parseFloat(String).
static int rint(int f)
          Mimics Math.rint(double), using single precision.
static int round(int f)
          Mimics Math.round(double), using single precision.
static short shortValue(int f)
          Convert the given float to a short as would happen in a casting operation specified by section 5.1.3 of the JLS.
static int sub(int f1, int f2)
          Returns the difference of the two float arguments according to section 15.18.2 of the JLS.
 java.lang.String toString()
          Returns a String object representing this MicroFloat's value.
static java.lang.String toString(int f)
          Mimics Float.toString(float).
static int truncate(int f)
          Returns the float of greatest magnitude (furthest from zero) that is equal to a mathematical integer and which has a mignitude not greater than the argument's magnitude.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

POSITIVE_INFINITY

public static final int POSITIVE_INFINITY
A constant representing the same value as Float.POSITIVE_INFINITY

See Also:
Constant Field Values

NEGATIVE_INFINITY

public static final int NEGATIVE_INFINITY
A constant holding the same value as Float.NEGATIVE_INFINITY

See Also:
Constant Field Values

NaN

public static final int NaN
A constant holding the same value as Float.NaN

See Also:
Constant Field Values

MAX_VALUE

public static final int MAX_VALUE
A constant holding the same value as Float.MAX_VALUE

See Also:
Constant Field Values

MIN_VALUE

public static final int MIN_VALUE
A constant holding the same value as Float.MIN_VALUE

See Also:
Constant Field Values

E

public static final int E
A single-precision version of MicroDouble.E

See Also:
Constant Field Values

PI

public static final int PI
A single-precision version of MicroDouble.PI

See Also:
Constant Field Values

ZERO

public static final int ZERO
A constant holding the value of 0.0f

See Also:
Constant Field Values

NEGATIVE_ZERO

public static final int NEGATIVE_ZERO
A constant holding the value of -0.0f

See Also:
Constant Field Values

ONE

public static final int ONE
A constant holding the value of 1.0f

See Also:
Constant Field Values

TWO

public static final int TWO
A constant holding the value of 2.0f

See Also:
Constant Field Values

ONE_HALF

public static final int ONE_HALF
A constant holding the value of 0.5f

See Also:
Constant Field Values
Constructor Detail

MicroFloat

public MicroFloat(int f)
Constructs a newly-allocated MicroFloat object that represents the argument.

Parameters:
f - the float value to be represented by the MicroFloat.

MicroFloat

public MicroFloat(java.lang.String s)
Constructs a newly-allocated MicroFloat object that represents the argument.

Parameters:
s - a String to be converted to a MicroFloat.
Throws:
java.lang.NumberFormatException - if the String does not contain a parsable number.
See Also:
parseFloat(String)
Method Detail

isNaN

public static boolean isNaN(int f)
Mimics Float.isNaN(float)


isInfinite

public static boolean isInfinite(int f)
Mimics Float.isInfinite(float)


isZero

public static boolean isZero(int f)
Returns true if the specified number has zero magnitude, false otherwise.

Parameters:
f - the float value to be tested.
Returns:
true if the value of the argument is positive zero or negative zero; false otherwise.

abs

public static int abs(int f)
Mimics Math.abs(float)


negate

public static int negate(int f)
Returns the negation of a float value. Special cases:

This method takes the place of the unary - operator.

Parameters:
f - the float value whose negated value is to be determined
Returns:
the negation of the argument.

eq

public static boolean eq(int f1,
                         int f2)
Returns true if the specified numbers are considered equal according to section 15.21.1 of the JLS. Special cases:

This method takes the place of the == operator.

Parameters:
f1 - the first float value to be compared.
f2 - the second float value to be compared.
Returns:
true if the two values are considered equal; false otherwise.

ne

public static boolean ne(int f1,
                         int f2)
Returns true if the specified numbers are considered unequal according to section 15.21.1 of the JLS. Special cases: The value returned by ne is always the opposite of the value returned by eq for the same arguments.

This method takes the place of the != operator.

Parameters:
f1 - the first float value to be compared.
f2 - the second float value to be compared.
Returns:
true if the two values are considered equal; false otherwise.

lt

public static boolean lt(int f1,
                         int f2)
Returns true if the first argument is considered less than the second argument according to section 15.20.1 of the JLS. Special cases:

This method takes the place of the < operator.

Parameters:
f1 - the first float value to be compared.
f2 - the second float value to be compared.
Returns:
true if the first value is less than the second value; false otherwise.

le

public static boolean le(int f1,
                         int f2)
Returns true if the first argument is considered less than or equal to the second argument according to section 15.20.1 of the JLS. Special cases:

This method takes the place of the <= operator.

Parameters:
f1 - the first float value to be compared.
f2 - the second float value to be compared.
Returns:
true if the first value is less than or equal to the second value; false otherwise.

gt

public static boolean gt(int f1,
                         int f2)
Returns true if the first argument is considered greater than the second argument according to section 15.20.1 of the JLS. Special cases:

This method takes the place of the > operator.

Parameters:
f1 - the first float value to be compared.
f2 - the second float value to be compared.
Returns:
true if the first value is greater than the second value; false otherwise.

ge

public static boolean ge(int f1,
                         int f2)
Returns true if the first argument is considered greater than or equal to the second argument according to section 15.20.1 of the JLS. Special cases:

This method takes the place of the >= operator.

Parameters:
f1 - the first float value to be compared.
f2 - the second float value to be compared.
Returns:
true if the first value is greater than or equal to the second value; false otherwise.

compare

public static int compare(int f1,
                          int f2)
Mimics Float.compare(float, float).

Note that when using this method (as well as Float.compare), the following rules apply:


max

public static int max(int f1,
                      int f2)
Mimics Math.max(float, float).


min

public static int min(int f1,
                      int f2)
Mimics Math.min(float, float).


intToFloat

public static int intToFloat(int x)
Convert the given int to a float as would happen in a casting operation specified by section 5.1.2 of the JLS. This is a widening primitive conversion which will not result in a loss of magnitude, but might result in a loss of precision.

Parameters:
x - the int to be converted
Returns:
the float representation of the argument

longToFloat

public static int longToFloat(long x)
Convert the given long to a float as would happen in a casting operation specified by section 5.1.2 of the JLS. This is a widening primitive conversion which will not result in a loss of magnitude, but might result in a loss of precision.

Parameters:
x - the long to be converted
Returns:
the float representation of the argument

doubleToFloat

public static int doubleToFloat(long d)
Convert the given double to a float as would happen in a casting operation specified by section 5.1.3 of the JLS. This is a narrowing primitive conversion which may result in a loss of magnitude and/or precision.

Parameters:
d - the double to be converted
Returns:
the float representation of the argument

byteValue

public static byte byteValue(int f)
Convert the given float to a byte as would happen in a casting operation specified by section 5.1.3 of the JLS. This is a narrowing primitive conversion which may result in a loss of magnitude and/or precision.

Note that this is a non-intuitive conversion. If the argument is outside of the range of the byte type, the result is basically meaningless.

Parameters:
f - the float to be converted
Returns:
the byte representation of the argument

shortValue

public static short shortValue(int f)
Convert the given float to a short as would happen in a casting operation specified by section 5.1.3 of the JLS. This is a narrowing primitive conversion which may result in a loss of magnitude and/or precision.

Note that this is a non-intuitive conversion. If the argument is outside of the range of the short type, the result is basically meaningless.

Parameters:
f - the float to be converted
Returns:
the short representation of the argument

intValue

public static int intValue(int f)
Convert the given float to an int as would happen in a casting operation specified by section 5.1.3 of the JLS. This is a narrowing primitive conversion which may result in a loss of magnitude and/or precision.

Parameters:
f - the float to be converted
Returns:
the int representation of the argument

longValue

public static long longValue(int f)
Convert the given float to a long as would happen in a casting operation specified by section 5.1.3 of the JLS. This is a narrowing primitive conversion which may result in a loss of magnitude and/or precision.

Parameters:
f - the float to be converted
Returns:
the long representation of the argument

doubleValue

public static long doubleValue(int f)
Convert the given float to a double as would happen in a casting operation specified by section 5.1.2 of the JLS. This is a widening primitive conversion which will result in neither a loss of magnitude nor precision.

Parameters:
f - the float to be converted
Returns:
the double representation of the argument

add

public static int add(int f1,
                      int f2)
Returns the sum of the two float arguments according to section 15.18.2 of the JLS.

This method takes the place of the binary + operator.

Parameters:
f1 - the first float value to be summed.
f2 - the second float value to be summed.
Returns:
the sum of the two arguments

sub

public static int sub(int f1,
                      int f2)
Returns the difference of the two float arguments according to section 15.18.2 of the JLS.

This method takes the place of the binary - operator.

Parameters:
f1 - the first float value
f2 - the second float value
Returns:
the difference of the two arguments

mul

public static int mul(int f1,
                      int f2)
Returns the product of the two float arguments according to section 15.17.1 of the JLS.

This method takes the place of the * operator.

Parameters:
f1 - the first float value
f2 - the second float value
Returns:
the product of the two arguments

div

public static int div(int f1,
                      int f2)
Returns the quotient of the two float arguments according to section 15.17.2 of the JLS.

This method takes the place of the / operator.

Parameters:
f1 - the float dividend
f2 - the float divisor
Returns:
the quotient of the two arguments

mod

public static int mod(int f1,
                      int f2)
Returns the remainder of the two float arguments according to section 15.17.3 of the JLS.

This method takes the place of the % operator.

Parameters:
f1 - the float dividend
f2 - the float divisor
Returns:
the remainder of the two arguments

truncate

public static int truncate(int f)
Returns the float of greatest magnitude (furthest from zero) that is equal to a mathematical integer and which has a mignitude not greater than the argument's magnitude. Special cases:

Parameters:
f - a float value.
Returns:
the float of greatest magnitude (furthest from zero) whose magnitude is not greater than the argument's and which is equal to a mathematical integer.

rint

public static int rint(int f)
Mimics Math.rint(double), using single precision.


floor

public static int floor(int f)
Mimics Math.floor(double), using single precision.


ceil

public static int ceil(int f)
Mimics Math.ceil(double), using single precision.


round

public static int round(int f)
Mimics Math.round(double), using single precision.


parseFloat

public static int parseFloat(java.lang.String s)
Mimics Float.parseFloat(String).

This implementation is known to be inaccurate, and does not always return the same value as Float.parseFloat. However the difference should be no greater than 1 ulp.

Throws:
java.lang.NumberFormatException - if the string does not contain a parsable number.

toString

public static java.lang.String toString(int f)
Mimics Float.toString(float).

This implementation is known to be inaccurate, and does not always return the same value as Float.toString. However the difference should be no greater than 1 ulp.


floatValue

public int floatValue()
Returns the float value of this MicroFloat object.


toString

public java.lang.String toString()
Returns a String object representing this MicroFloat's value. Equivalent to toString(floatValue()).

Overrides:
toString in class java.lang.Object
See Also:
toString(int)

hashCode

public int hashCode()
Returns a hash code for this MicroFloat object. Equivalent to floatValue().

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Compares this object against the specified object. Equivalent to ((obj instanceof MicroFloat) && (compare(((MicroFloat) obj).floatValue(), floatValue()) == 0))

Overrides:
equals in class java.lang.Object
See Also:
compare(int, int)