Search code examples
pythonfloating-point

How to get the exact memory representation of fp16,fp32,bf16,int8,... and so on in python?


Is there any function that can give me the exact representation (as it is stored in memory) of a number ? What I am looking for is a function with the following behaviour:

memory_repr(3,'int8') = oooooo11

memory_repr(3,'fp16') = 01000000010000000000000000000000

Basically, I want a python api to do what https://www.h-schmidt.net/FloatConverter/IEEE754.html does, that works with any data type

I tried math.frexp comes close but not quite it.


Solution

  • import struct
    
    def memory_repr(number, data_type):
        if data_type == 'int8':
            packed = struct.pack('b', number)  
        elif data_type == 'uint8':
            packed = struct.pack('B', number) 
        elif data_type == 'fp32':
            packed = struct.pack('f', number)  
        elif data_type == 'fp16':
            packed = struct.pack('e', number)  
        else:
            raise ValueError("Unsupported data type")
        packed = struct.pack('>' + format_char, number)
        return ''.join(f'{byte:08b}' for byte in packed)
    
    print(memory_repr(3, 'int8'))  
    print(memory_repr(3, 'uint8'))  
    print(memory_repr(3.0, 'fp16')) 
    print(memory_repr(3.0, 'fp32')) 
    

    Output :

    00000011
    00000011
    0100001000000000
    01000000010000000000000000000000
    

    Here I am not sure about bf16.

    Corrected for float as rightfully highlighted