Java Float Number Create toFloat(short half)

Here you can find the source of toFloat(short half)

Description

Converts a single precision (32 bit) floating point value into half precision (16 bit).

License

Apache License

Parameter

Parameter Description
half The half floating point value as a short.

Return

floating point value of the half.

Declaration

public static float toFloat(short half) 

Method Source Code

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

public class Main {
    /**//from   w  ww.  jav a  2  s. c  om
     * Converts a single precision (32 bit) floating point value into half
     * precision (16 bit).
     *
     * <p>
     * Source: <a
     * href="http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf">
     * http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf</a><br>
     * <strong>broken link</strong>
     *
     * @param half
     *            The half floating point value as a short.
     * @return floating point value of the half.
     */
    public static float toFloat(short half) {
        switch ((int) half) {
        case 0x0000:
            return 0f;
        case 0x8000:
            return -0f;
        case 0x7c00:
            return Float.POSITIVE_INFINITY;
        case 0xfc00:
            return Float.NEGATIVE_INFINITY;
        // TODO: Support for NaN?
        default:
            return Float.intBitsToFloat(
                    ((half & 0x8000) << 16) | (((half & 0x7c00) + 0x1C000) << 13) | ((half & 0x03FF) << 13));
        }
    }
}

Related

  1. toFloat(Object val)
  2. toFloat(Object value)
  3. toFloat(Object value)
  4. toFloat(Object value)
  5. toFloat(Object value)
  6. toFloat(short[] arr)
  7. toFloat(short[] src)
  8. toFloat(String parameter)
  9. toFloat(String property, float f)