Skip to content

Instantly share code, notes, and snippets.

@giordano
Created April 2, 2020 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giordano/1c2751cc4a9e59a9efde5ce06ed2312a to your computer and use it in GitHub Desktop.
Save giordano/1c2751cc4a9e59a9efde5ce06ed2312a to your computer and use it in GitHub Desktop.
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