Skip to content

Instantly share code, notes, and snippets.

@simonster
Last active August 29, 2015 14:02
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 simonster/a6f211061d76cfda2534 to your computer and use it in GitHub Desktop.
Save simonster/a6f211061d76cfda2534 to your computer and use it in GitHub Desktop.
x^4
function test(n)
a = 0
b = 0
aulpdiff = Array(Float64, n)
bulpdiff = Array(Float64, n)
for i = 1:n
x = rand()
r1 = (x*x)*(x*x)
r2 = x^4
bigres = big(x)^4
r3 = float64(bigres)
a += r1 == r3
b += r2 == r3
aulpdiff[i] = (r1-bigres)/eps(r1)
bulpdiff[i] = (r2-bigres)/eps(r2)
end
println("multiply matches correctly rounded result: $a/$n")
println("max error of multiply: $(maximum(abs(aulpdiff))) ulp")
println(hist(aulpdiff))
println()
println("^ matches correctly rounded result: $b/$n")
println("max error of ^: $(maximum(abs(bulpdiff))) ulp")
println(hist(bulpdiff))
end
test(100000)
@andrioni
Copy link

To test iterated v. pairwise multiplication.

function test(n)
    a = 0
    b = 0
    c = 0
    aulpdiff = Array(Float64, n)
    bulpdiff = Array(Float64, n)
    culpdiff = Array(Float64, n)
    for i = 1:n
        x = rand()
        r1 = (x*x)*(x*x)
        r2 = x^4
        rr = (((x*x)*x)*x)
        bigres = big(x)^4
        r3 = float64(bigres)
        a += r1 == r3
        b += r2 == r3
        c += rr == r3
        aulpdiff[i] = (r1-bigres)/eps(r1)
        bulpdiff[i] = (r2-bigres)/eps(r2)
        culpdiff[i] = (rr-bigres)/eps(rr)
    end
    println("pairwise multiply matches correctly rounded result: $a/$n")
    println("max error of pairwise  multiply: $(maximum(abs(aulpdiff))) ulp")
    println(hist(aulpdiff))
    println()
    println("iterated multiply matches correctly rounded result: $c/$n")
    println("max error of iterated multiply: $(maximum(abs(culpdiff))) ulp")
    println(hist(culpdiff))
    println()
    println("powf matches correctly rounded result: $b/$n")
    println("max error of powf: $(maximum(abs(bulpdiff))) ulp")
    println(hist(bulpdiff))
end

@andrioni
Copy link

The results here:

julia> test(1000000)
pairwise multiply matches correctly rounded result: 504431/1000000
max error of pairwise  multiply: 1.8907513610425748 ulp
(-2.0:0.2:2.0,[85,1898,7521,19211,38505,58521,77670,92279,101519,102176,103311,101651,92591,77122,59048,38011,19288,7556,1941,96])

iterated multiply matches correctly rounded result: 656818/1000000
max error of iterated multiply: 2.018519127704391 ulp
(-2.0:0.2:2.2,[50,395,1697,6220,17133,36869,64924,98046,128887,145610,145827,128970,98512,64302,37047,17181,6218,1692,384,35,1])

powf matches correctly rounded result: 999682/1000000
max error of powf: 0.5008007537332742 ulp
(-0.55:0.05:0.55,[157,49731,50006,50037,49701,50141,49914,49881,49839,50022,50083,50137,49783,50241,49767,49940,50404,50460,49703,49795,50097,161])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment