FileDocCategorySizeDatePackage
SmallFloat.javaAPI DocApache Lucene 1.94515Mon Feb 20 09:20:18 GMT 2006org.apache.lucene.util

SmallFloat

public class SmallFloat extends Object
Floating point numbers smaller than 32 bits.
author
yonik
version
$Id$

Fields Summary
Constructors Summary
Methods Summary
public static floatbyte315ToFloat(byte b)
byteToFloat(b, mantissaBits=3, zeroExponent=15)

    // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
    // is only a little bit faster (anywhere from 0% to 7%)
    if (b == 0) return 0.0f;
    int bits = (b&0xff) << (24-3);
    bits += (63-15) << 24;
    return Float.intBitsToFloat(bits);
  
public static floatbyte52ToFloat(byte b)
byteToFloat(b, mantissaBits=5, zeroExponent=2)

    // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
    // is only a little bit faster (anywhere from 0% to 7%)
    if (b == 0) return 0.0f;
    int bits = (b&0xff) << (24-5);
    bits += (63-2) << 24;
    return Float.intBitsToFloat(bits);
  
public static floatbyteToFloat(byte b, int numMantissaBits, int zeroExp)
Converts an 8 bit float to a 32 bit float.

    // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
    // is only a little bit faster (anywhere from 0% to 7%)
    if (b == 0) return 0.0f;
    int bits = (b&0xff) << (24-numMantissaBits);
    bits += (63-zeroExp) << 24;
    return Float.intBitsToFloat(bits);
  
public static bytefloatToByte(float f, int numMantissaBits, int zeroExp)
Converts a 32 bit float to an 8 bit float.
Values less than zero are all mapped to zero.
Values are truncated (rounded down) to the nearest 8 bit value.
Values between zero and the smallest representable value are rounded up.

param
f the 32 bit float to be converted to an 8 bit float (byte)
param
numMantissaBits the number of mantissa bits to use in the byte, with the remainder to be used in the exponent
param
zeroExp the zero-point in the range of exponent values
return
the 8 bit float representation

    // Adjustment from a float zero exponent to our zero exponent,
    // shifted over to our exponent position.
    int fzero = (63-zeroExp)<<numMantissaBits;
    int bits = Float.floatToRawIntBits(f);
    int smallfloat = bits >> (24-numMantissaBits);
    if (smallfloat < fzero) {
      return (bits<=0) ?
        (byte)0   // negative numbers and zero both map to 0 byte
       :(byte)1;  // underflow is mapped to smallest non-zero number.
    } else if (smallfloat >= fzero + 0x100) {
      return -1;  // overflow maps to largest number
    } else {
      return (byte)(smallfloat - fzero);
    }
  
public static bytefloatToByte315(float f)
floatToByte(b, mantissaBits=3, zeroExponent=15)
smallest non-zero value = 5.820766E-10
largest value = 7.5161928E9
epsilon = 0.125

    int bits = Float.floatToRawIntBits(f);
    int smallfloat = bits >> (24-3);
    if (smallfloat < (63-15)<<3) {
      return (bits<=0) ? (byte)0 : (byte)1;
    }
    if (smallfloat >= ((63-15)<<3) + 0x100) {
      return -1;
    }
    return (byte)(smallfloat - ((63-15)<<3));
 
public static bytefloatToByte52(float f)
floatToByte(b, mantissaBits=5, zeroExponent=2)
smallest nonzero value = 0.033203125
largest value = 1984.0
epsilon = 0.03125

    int bits = Float.floatToRawIntBits(f);
    int smallfloat = bits >> (24-5);
    if (smallfloat < (63-2)<<5) {
      return (bits<=0) ? (byte)0 : (byte)1;
    }
    if (smallfloat >= ((63-2)<<5) + 0x100) {
      return -1;
    }
    return (byte)(smallfloat - ((63-2)<<5));