PrettyTables diff digits highlight?

In that case I would recommend using something other than a textual difference. My preference goes to the work by D. Stott Parker, especially the definition of the “number of (absolute) significant digits” introduced in his “\frac 1 2 ulsp algorithm”:

using PrettyTables, Crayons, Printf

function highlight_different_digits(a, b)
    # Stott Parker's definition of the "number of (absolute)
    # significant digits" of `b` seen as an approximation of `a`.
    s = -log10(2*abs(b-a)/10.0^(1+floor(log10(abs(a)))))

    j = floor(Int, min(s, 100))
    j > 0 && (j+=1) # account for the dot in 2nd place in scientific notation

    a_str = @sprintf "%.10e" a
    b_str = @sprintf "%.10e" b
    j = clamp(j, 0, lastindex(b_str))

    common    = SubString(b_str, 1, j)
    different = SubString(b_str, j+1)
    red = crayon"red bold"
    b_str = AnsiTextCell("$common$red$different")
    [a_str, b_str]
end
# Random test data
n = 10
x = rand(n)
y = x .+ 2e-3 .* (rand(n) .- 0.5)

# Corner cases
y[1] = x[1]
x[end], y[end] = (0.1, 0.099998)

table = reduce(vcat, permutedims(highlight_different_digits(x[i], y[i])) for i in eachindex(x))
pretty_table(table, header=["Reference value", "Approximation"])



Ref: Douglas Stott Parker, “Monte Carlo Arithmetic : exploiting randomness in floating-point arithmetic”, 1997 (pages 19-20).

4 Likes