Created
April 2, 2020 23:54
-
-
Save giordano/1c2751cc4a9e59a9efde5ce06ed2312a to your computer and use it in GitHub Desktop.
This file contains 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
function fraction_floats(float_type, int_type) | |
# Size in bits of the integer type | |
int_size = sizeof(int_type) * 8 | |
# Number of bits of the mantissa of the floating point type | |
mantissa_size = Base.significand_bits(float_type) | |
s = BigInt(2) ^ (mantissa_size + 1) | |
# Make sure this is the maximum integer of type `int_type` representable | |
# with the floating point type | |
@assert s == maxintfloat(float_type, int_type) | |
for n in (mantissa_size + 2):int_size | |
s += BigInt((BigInt(2) ^ n - BigInt(2) ^ (n - 1)) / BigInt(2) ^ (n - (mantissa_size + 1))) | |
end | |
# Return the fraction of the `int_type` numbers `x` for which `float_type(x) == float_type(x + 1)` | |
return (1 - s / BigInt(2) ^ int_size) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment