Java Float Number Create toFloat(int hbits)

Here you can find the source of toFloat(int hbits)

Description

to Float

License

Apache License

Declaration

public static float toFloat(int hbits) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static float toFloat(int hbits) {
        int mant = hbits & 0x03ff; // 10 bits mantissa
        int exp = hbits & 0x7c00; // 5 bits exponent
        if (exp == 0x7c00) // NaN/Inf
            exp = 0x3fc00; // -> NaN/Inf
        else if (exp != 0) // normalized value
        {//from   w  w  w . jav  a2  s  .  c o  m
            exp += 0x1c000; // exp - 15 + 127
            if (mant == 0 && exp > 0x1c400) // smooth transition
                return Float.intBitsToFloat((hbits & 0x8000) << 16
                        | exp << 13 | 0x3ff);
        } else if (mant != 0) // && exp==0 -> subnormal
        {
            exp = 0x1c400; // make it normal
            do {
                mant <<= 1; // mantissa * 2
                exp -= 0x400; // decrease exp by 1
            } while ((mant & 0x400) == 0); // while not normal
            mant &= 0x3ff; // discard subnormal bit
        } // else +/-0 -> +/-0
        return Float.intBitsToFloat( // combine all parts
                (hbits & 0x8000) << 16 // sign  << ( 31 - 15 )
                        | (exp | mant) << 13); // value << ( 23 - 10 )
    }
}

Related

  1. toFloat(final String value)
  2. toFloat(final String value)
  3. toFloat(final String value)
  4. toFloat(Float num, float defaultValue)
  5. toFloat(int colorComponent)
  6. toFloat(int majorVersion, int minorVersion)
  7. toFloat(int value)
  8. toFloat(int x)
  9. toFloat(int[] array)