Skip to content

Instantly share code, notes, and snippets.

@aprell
Created February 8, 2014 17:54
Show Gist options
  • Save aprell/8887486 to your computer and use it in GitHub Desktop.
Save aprell/8887486 to your computer and use it in GitHub Desktop.
Functions for inspecting IEEE 754 floating-point numbers in Julia
function floatdump(n::Float32)
sign = bits(n)[1]
exp = bits(n)[2:9]
mant = bits(n)[10:end]
println("Sign: $sign $(sign == '1' ? "(-)" : "(+)")")
println("Exponent: $exp ($(parseint(exp, 2) - 127) + 127)")
println("Mantissa: $mant ($(parseint(mant, 2) / 2^23) * 2^23)")
end
function floatdump(n::Float64)
sign = bits(n)[1]
exp = bits(n)[2:12]
mant = bits(n)[13:end]
println("Sign: $sign $(sign == '1' ? "(-)" : "(+)")")
println("Exponent: $exp ($(parseint(exp, 2) - 1023) + 1023)")
println("Mantissa: $mant ($(parseint(mant, 2) / 2^52) * 2^52)")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment