Please code me a python app that i can use to better understand number representation…
- User
Please code me a python app that i can use to better understand number representation "floating-point"," brain floating point", "integer" in all common levels of precision
- Assistant
Below is a simple Python app that allows you to interactively explore different number representations: floating-point (IEEE 754 format), "brain floating point" (I assume that you are referring to a hypothetical format meant for educational purposes, as "brain floating point" is not a standard term), and integer representations in various precisions.
This app will cover:
- IEEE 754 floating-point: single precision (32-bit) and double precision (64-bit).
- Integers: 8-bit, 16-bit, 32-bit, and 64-bit.
- Brain floating-point: for demonstration purposes, we will create a simple custom 16-bit floating-point format (1 sign bit, 5 exponent bits, and 10 fraction bits). This is a hypothetical format for educational purposes.
Please note that Python's
structmodule does not support arbitrary floating-point formats, so the "brain floating-point" representation will be simulated using bit manipulation.import struct import ctypes def ieee_float(value, precision='single'): if precision == 'single': return struct.unpack('!f', struct.pack('!I', value))[0] elif precision == 'double': return struct.unpack('!d', struct.pack('!Q', value))[0] else: raise ValueError("Unknown precision. Choose 'single' or 'double'.") def int_representation(value, num_bits=32, signed=True): if num_bits == 8: fmt = 'b' if signed else 'B' elif num_bits == 16: fmt = 'h' if signed else 'H' elif num_bits == 32: fmt = 'i' if signed else 'I' elif num_bits == 64: fmt = 'q' if signed else 'Q' else: raise ValueError("Unsupported number of bits.") packed = struct.pack(fmt, value) return struct.unpack(fmt, packed)[0] def brain_float_encode(value): # This function takes a floating-point value and encodes it into a # custom 16-bit floating-point format (1 sign, 5 exponent, 10 fraction) # For simplicity, we will use a biased exponent. sign = 0 if value < 0: sign = 1 value = -value # Normalize the value exponent = 0 while value >= 2.0: value /= 2.0 exponent += 1 while value < 1.0: value *= 2.0 exponent -= 1 # Apply bias exponent_bias = 15 exponent += exponent_bias if exponent >= 31: # Overflow, set to infinity return (sign << 15) | (31 << 10) elif exponent <= 0: # Underflow, set to zero return sign << 15 # Convert fraction to integer fraction = int(value * (1 << 10)) & 0x03FF return (sign << 15) | ((exponent & 0x1F) << 10) | fraction def brain_float_decode(value): # Decode the custom 16-bit floating-point format back to a Python float sign = (value >> 15) & 0x01 exponent = (value >> 10) & 0x1F fraction = value & 0x03FF exponent_bias = 15 exponent -= exponent_bias # Convert fraction to float fraction /= (1 << 10) result = (1 + fraction) * (2 ** exponent) if sign == 1: result = -result return result def main(): num = float(input("Enter a number to explore its representations: ")) print("Choose representation:") print("1. IEEE 754 Single Precision (32-bit)") print("2. IEEE 754 Double Precision (64-bit)") print("3. Integer (choose between 8-bit, 16-bit, 32-bit, 64-bit)") print("4. Brain Floating Point (custom 16-bit)") choice = int(input("Enter your choice (1-4): ")) if choice == 1: packed = struct.pack('!f', num) print("IEEE 754 Single Precision representation:") print("Binary:", ''.join(f'{byte:08b}' for byte in packed)) elif choice == 2: packed = struct.pack('!d', num) print("IEEE 754 Double Precision representation:") print("Binary:", ''.join(f'{byte:08b}' for byte in packed)) elif choice == 3: bits = int(input("Enter number of bits (8, 16, 32, 64): ")) signed = input("Signed (y/n)? ") == 'y' try: result = int_representation(int(num), num_bits=bits, signed=signed) print("Integer representation:", result) except struct.error as e: print(f"Error: {e}") elif choice == 4: encoded = brain_float_encode(num) print("Brain Floating Point (custom 16-bit) representation:") print("Binary:", f'{encoded:016b}') decoded = brain_float_decode(encoded) print("Decoded value:", decoded) else: print("Invalid choice.") if __name__ == "__main__": main()To run this program, copy the code into a Python file and execute it in your Python environment. You'll be prompted to enter a number, select its representation, and possibly choose additional details such as bit numbers for integers. Keep in mind that the "Brain Floating Point" part is purely demonstrative and does not represent any real floating-point standard.
- User
In the code add a custom floating-point representation optimized for ML workloads, it can have 8 dynamic range bits and 4 exponent bits
- Assistant
Let's create a custom floating-point representation optimized for machine learning workloads. We'll call this the "ML Float," which will have the following format:
- 1 sign bit
- 8 dynamic range bits (significand)
- 4 exponent bits
- 1 bias bit (to distinguish between small/large dynamic ranges)
This gives us a total of 14 bits, which is unconventional but designed for the purpose of this exercise. The bias bit is used to toggle between two different scales of exponents, allowing a larger dynamic range.
Below is the modified Python app that includes our custom ML Float representation:
import struct def ml_float_encode(value): # Custom ML Float: 1 sign bit, 8 significand bits, 4 exponent bits, 1 bias bit sign = 0 if value < 0: sign = 1 value = -value # Normalize value exponent = 0 bias = 0 while value >= 2.0: value /= 2.0 exponent += 1 while value < 1.0 and exponent > -7: value *= 2.0 exponent -= 1 # Toggle bias bit and continue scaling down for small numbers if necessary if exponent < 0: exponent = -exponent - 1 bias = 1 if exponent > 15: # Overflow to infinity return (sign << 13) | (15 << 8) | (1 << 7) # Infinity representation with bias bit set # Convert fraction to integer dynamic_range = int(value * (1 << 8)) & 0xFF return (sign << 13) | ((exponent & 0x0F) << 8) | (bias << 7) | dynamic_range def ml_float_decode(value): # Decode the 14-bit ML Float back to a Python float sign = (value >> 13) & 0x01 exponent = (value >> 8) & 0x0F bias = (value >> 7) & 0x01 dynamic_range = value & 0x7F if exponent == 15 and bias == 1: # Infinity representation return float('inf') if sign == 0 else float('-inf') # Apply exponent handling based on bias bit if bias == 1: exponent = -1 * (exponent + 1) # Convert dynamic range back to float significand = dynamic_range / (1 << 8) result = (1 + significand) * (2 ** exponent) if sign == 1: result = -result return result # Modify the main function to include the ML Float representation def main(): num = float(input("Enter a number to explore its representations: ")) print("Choose representation:") print("1. IEEE 754 Single Precision (32-bit)") print("2. IEEE 754 Double Precision (64-bit)") print("3. Integer (choose between 8-bit, 16-bit, 32-bit, 64-bit)") print("4. Brain Floating Point (custom 16-bit)") print("5. ML Float (custom 14-bit for ML workloads)") choice = int(input("Enter your choice (1-5): ")) # ... [Other options from previous code] elif choice == 5: encoded = ml_float_encode(num) print("ML Float (custom 14-bit for ML workloads) representation:") print("Binary:", f'{encoded:014b}') decoded = ml_float_decode(encoded) print("Decoded value:", decoded) else: print("Invalid choice.") # Run the main function if __name__ == "__main__": main()This program now includes a custom 14-bit ML Float representation designed with a given specification. To use this representation, run the program and choose option 5 when prompted. You can then observe the binary encoding and decoding of the number you input. Keep in mind that handling of edge cases like NaN and subnormal numbers is not included in this simplified educational format.