Created
November 27, 2017 10:37
-
-
Save pkofod/0e361627401c894135ac139c71e538ea to your computer and use it in GitHub Desktop.
Precision and time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from https://tpapp.github.io/post/log1p/ | |
# consistent random numbers | |
T = Float32 | |
srand(UInt32[0xfd909253, 0x7859c364, 0x7cd42419, 0x4c06a3b6]) | |
""" | |
err(x, [prec]) | |
Return two values, which are the log2 relative errors for calculating | |
`log(x)`, using `Base.log` and `Base.Math.JuliaLibm.log`. | |
The errors are calculated by compating to `BigFloat` calculations with the given | |
precision `prec`.1½ | |
""" | |
function err(x, prec = 1024) | |
yb = log(BigFloat(x, prec)) | |
e2(y) = Float64(log2(abs(y-yb)/abs(yb))) | |
e2(log(x)), e2(Base.Math.JuliaLibm.log(x)) | |
end | |
x = exp.(randn(Float32, 20000)*T(10)) # z > 0, Lognormal(0, 10) | |
es = map(err, x) # errors | |
using Plots; gr() # plots | |
p = scatter(log2.(x), first.(es), xlab = "log2(x)", ylab = "log2 error of Base.log", | |
legend = false) | |
hline!(log2(eps())-[0,1]) | |
Plots.png("Base_log_error_$(T).png") | |
scatter(log2.(x), last.(es) .- first.(es), xlab = "log2(x)", | |
ylab = "improvement (Base.Math.JuliaLibm.log)", legend = false) | |
Plots.png("JuliaLibm_improvement_$(T).png") | |
q = scatter(log2.(x), last.(es), xlab = "log2(x)", | |
ylab = "log2 error of Base.Math.JuliaLibm.log", legend = false) | |
hline!(log2(eps())-[0,1]) | |
Plots.png("JuliaLibm_log_error_$(T).png") | |
###################################################################### | |
# WARNING: these run for a very long time | |
###################################################################### | |
using BenchmarkTools | |
x = exp.(vcat(randn(T,200)*T(10), rand(T,200)*T(0.1)) # z > 0, more values around | |
b1 = [@belapsed log($x) for x in x] # WARNING: takes forever | |
b2 = [@belapsed Base.Math.JuliaLibm.log($x) for x in x] # ditto | |
scatter(log2.(x), b2 ./ b1, xlab = "log2(x)", | |
ylab = "time Math.JuliaLibm.log / log", legend = false, yticks = 0:0.2:1.2) | |
hline!([1]) | |
Plots.png("relative_time_$(T).png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment