In [1]:
# helpers from http://nbviewer.jupyter.org/url/www.maths.usyd.edu.au/u/olver/teaching/MATH3976/notes/04.ipynb
# by Sheehan Olver
using Printf
printred(x)=print("\x1b[1m\x1b[31m"*x*"\x1b[0m")
printgreen(x)=print("\x1b[7m\x1b[32m"*x*"\x1b[0m")
printblue(x)=print("\x1b[4m\x1b[34m"*x*"\x1b[0m")

function decode_Float64(x::Float64; name="")
    s = bitstring(x)
    sgn = parse(Int,s[1],base=2)
    e = parse(Int,s[2:12],base=2)
    f = parse(Int,s[13:64],base=2)
    ffrac = f/2^52

    ebias = 1023

    if length(name) > 0
        @printf("The computer representation of %s is:\n", name)
        print("  ")
        printred(s[1:1])
        printgreen(s[2:12])
        printblue(s[13:end])
        println()
        println("  which decodes to ")
    end

    println("  | sign |   exponent  |    mantissa    ")
    print("     ");printred(s[1:1]);print("     ")
    printgreen(s[2:12]);print("   ")
    printblue(s[13:end])
    println()

    fstr = (@sprintf( "%.17f", ffrac) )[3:end]

    if e == 0
        println( @sprintf("= (-1)^(%i) x 2^(%+4i)   x 0.%s = %s ", sgn, 1-ebias, fstr, string(x)) )
    elseif e == 2^11 - 1
        println( @sprintf("= (-1)^(%i) x 2^( Inf)   x 1.%s = %s ", sgn, fstr, string(x)) )
    else
        println( @sprintf("= (-1)^(%i) x 2^(%+5i)   x 1.%s = %s ", sgn, e-ebias, fstr, string(x)) )
    end
end

macro show_float(var)
    varname = string(var)
    :(decode_Float64($var;name=$varname))
end
Out[1]:
@show_float (macro with 1 method)
In [2]:
##
@show_float(Float64(42.0))
The computer representation of Float64(42.0) is:
  0100000001000101000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     10000000100   0101000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(   +5)   x 1.31250000000000000 = 42.0 
In [3]:
##
@show_float(Float64(-42.0))
The computer representation of Float64(-42.0) is:
  1100000001000101000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     1     10000000100   0101000000000000000000000000000000000000000000000000
= (-1)^(1) x 2^(   +5)   x 1.31250000000000000 = -42.0 
In [4]:
##
@show_float(Float64(-42.0))
The computer representation of Float64(-42.0) is:
  1100000001000101000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     1     10000000100   0101000000000000000000000000000000000000000000000000
= (-1)^(1) x 2^(   +5)   x 1.31250000000000000 = -42.0 
In [5]:
## Question from class, isn't that exponent wrong?
# The exponent of 42 is 0b10000000100 = 1028
# Let's try some other numbers to investigate
@show_float(Float64(41.0))
@show_float(Float64(1.0))
The computer representation of Float64(41.0) is:
  0100000001000100100000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     10000000100   0100100000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(   +5)   x 1.28125000000000000 = 41.0 
The computer representation of Float64(1.0) is:
  0011111111110000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     01111111111   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(   +0)   x 1.00000000000000000 = 1.0 
In [6]:
## The exponent for 1.0 is
# 0b01111111111 = 1023
In [7]:
## Let's keep going
@show_float(Float64(0.25))
The computer representation of Float64(0.25) is:
  0011111111010000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     01111111101   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(   -2)   x 1.00000000000000000 = 0.25 
In [8]:
## Here the exponent is 0b01111111101 = 1021.
## So the the exponent is interpreted as 0b01111111101 - 1023.
## With one exception :)
In [9]:
##
@show_float(Float64(0))
The computer representation of Float64(0) is:
  0000000000000000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     00000000000   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(-1022)   x 0.00000000000000000 = 0.0 
In [10]:
##
@show_float(Float64(2^(-1022)))
@show_float(Float64(2^(-1022))/2)
The computer representation of Float64(2 ^ -1022) is:
  0000000000010000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     00000000001   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(-1022)   x 1.00000000000000000 = 2.2250738585072014e-308 
The computer representation of Float64(2 ^ -1022) / 2 is:
  0000000000001000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     00000000000   1000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(-1022)   x 0.50000000000000000 = 1.1125369292536007e-308 
In [11]:
##
@show_float(nextfloat(0.0))
The computer representation of nextfloat(0.0) is:
  0000000000000000000000000000000000000000000000000000000000000001
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     00000000000   0000000000000000000000000000000000000000000000000001
= (-1)^(0) x 2^(-1022)   x 0.00000000000000022 = 5.0e-324 
In [12]:
##
@show_float(-0.0)
The computer representation of -0.0 is:
  1000000000000000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     1     00000000000   0000000000000000000000000000000000000000000000000000
= (-1)^(1) x 2^(-1022)   x 0.00000000000000000 = -0.0 
In [13]:
##
@show_float(Float64(pi))
@show_float(Float64(1.0))
The computer representation of Float64(pi) is:
  0100000000001001001000011111101101010100010001000010110100011000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     10000000000   1001001000011111101101010100010001000010110100011000
= (-1)^(0) x 2^(   +1)   x 1.57079632679489656 = 3.141592653589793 
The computer representation of Float64(1.0) is:
  0011111111110000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     01111111111   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(   +0)   x 1.00000000000000000 = 1.0 
In [14]:
##
@show_float(1.0)
@show_float(nextfloat(1.0))
The computer representation of 1.0 is:
  0011111111110000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     01111111111   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(   +0)   x 1.00000000000000000 = 1.0 
The computer representation of nextfloat(1.0) is:
  0011111111110000000000000000000000000000000000000000000000000001
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     01111111111   0000000000000000000000000000000000000000000000000001
= (-1)^(0) x 2^(   +0)   x 1.00000000000000022 = 1.0000000000000002 
In [15]:
##
@show_float(0.0)
@show_float(nextfloat(0.0))
The computer representation of 0.0 is:
  0000000000000000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     00000000000   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(-1022)   x 0.00000000000000000 = 0.0 
The computer representation of nextfloat(0.0) is:
  0000000000000000000000000000000000000000000000000000000000000001
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     00000000000   0000000000000000000000000000000000000000000000000001
= (-1)^(0) x 2^(-1022)   x 0.00000000000000022 = 5.0e-324 
In [16]:
##
@show_float(eps(1.0))
The computer representation of eps(1.0) is:
  0011110010110000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     01111001011   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(  -52)   x 1.00000000000000000 = 2.220446049250313e-16 
In [17]:
##
@show_float(Inf)
The computer representation of Inf is:
  0111111111110000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     11111111111   0000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^( Inf)   x 1.00000000000000000 = Inf 
In [18]:
##
@show_float(-Inf)
The computer representation of -Inf is:
  1111111111110000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     1     11111111111   0000000000000000000000000000000000000000000000000000
= (-1)^(1) x 2^( Inf)   x 1.00000000000000000 = -Inf 
In [19]:
##
@show_float(NaN)
The computer representation of NaN is:
  0111111111111000000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     11111111111   1000000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^( Inf)   x 1.50000000000000000 = NaN 
In [20]:
## Note that we have exact arithmetic between integers up to +-2^52
@show_float(5.0+6)
The computer representation of 5.0 + 6 is:
  0100000000100110000000000000000000000000000000000000000000000000
  which decodes to 
  | sign |   exponent  |    mantissa    
     0     10000000010   0110000000000000000000000000000000000000000000000000
= (-1)^(0) x 2^(   +3)   x 1.37500000000000000 = 11.0 
In [21]:
## Some bad code for f(x) = x (x > 0)

function myfun(x::Float64)
    for i=1:70
        x= sqrt(x)
    end
    for i=1:70
        x = x^2
    end
    return x
end
Out[21]:
myfun (generic function with 1 method)