Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.