(value, mantissabits=5, zeroexp=2)
return b(result)
Encodes a floating point number in a single byte.
Encodes a floating point number in a single byte.
Encodes a floating point number in a single byte .
def float_to_byte(value, mantissabits=5, zeroexp=2):
    """Encodes a floating point number in a single byte.
    """

    # Assume int size == float size

    fzero = (63 - zeroexp) << mantissabits
    bits = unpack("i", pack("f", value))[0]
    smallfloat = bits >> (24 - mantissabits)
    if smallfloat < fzero:
        # Map negative numbers and 0 to 0
        # Map underflow to next smallest non-zero number
        if bits <= 0:
            result = chr(0)
        else:
            result = chr(1)
    elif smallfloat >= fzero + 0x100:
        # Map overflow to largest number
        result = chr(255)
    else:
        result = chr(smallfloat - fzero)
    return b(result)
def float_to_byte ( value , mantissabits = 5 , zeroexp = 2 ) : # Assume int size == float size fzero = ( 63 - zeroexp ) << mantissabits bits = unpack ( "i" , pack ( "f" , value ) ) [ 0 ] smallfloat = bits >> ( 24 - mantissabits ) if smallfloat < fzero : # Map negative numbers and 0 to 0 # Map underflow to next smallest non-zero number if bits <= 0 : result = chr ( 0 ) else : result = chr ( 1 ) elif smallfloat >= fzero + 0x100 : # Map overflow to largest number result = chr ( 255 ) else : result = chr ( smallfloat - fzero ) return b ( result )