Created
March 18, 2025 19:19
-
-
Save lemire/53f01ae394419bcb53326cc0cc65e88f to your computer and use it in GitHub Desktop.
convert float to integers in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
def float_to_scaled_int_double(f, p): | |
# Pack float as 64-bit double (IEEE 754 double precision) | |
packed = struct.pack('>d', f) | |
# Unpack as 64-bit unsigned integer | |
int_val = struct.unpack('>Q', packed)[0] | |
# Extract components (IEEE 754 double precision format): | |
# - 1 sign bit | |
# - 11 exponent bits | |
# - 52 explicit mantissa bits (+ 1 implicit bit) | |
# Mask to get the 52 explicit mantissa bits | |
mantissa_mask = 0xFFFFFFFFFFFFF # 52 ones | |
mantissa = int_val & mantissa_mask | |
# Add the implicit leading 1 bit (position 52) | |
mantissa |= (1 << 52) | |
# Get the biased exponent (bits 52-62) | |
exponent = (int_val >> 52) & 0x7FF # 11 bits | |
# Subtract bias (1023 for double precision) to get actual exponent | |
actual_exponent = exponent - 1023 | |
# Scale mantissa by 2^p | |
scaled_mantissa = mantissa * (2 ** p) | |
return scaled_mantissa, actual_exponent | |
# Example usage | |
f = 3.14159 | |
p = 10 | |
mantissa, exponent = float_to_scaled_int_double(f, p) | |
print(f"Float: {f}") | |
print(f"Scaled mantissa (54-bit): {mantissa}") | |
print(f"Exponent: {exponent}") | |
print(f"Verification: {mantissa * (2 ** exponent) / (2 ** (p + 52))} ≈ {f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment