diff --git a/Project.toml b/Project.toml index bacebf5..98f1082 100644 --- a/Project.toml +++ b/Project.toml @@ -1,15 +1,13 @@ name = "Showoff" uuid = "992d4aef-0814-514b-bc4d-f2e9a6c4116f" author = ["Daniel C. Jones (@dcjones)"] -version = "1.0.3" +version = "1.1.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" -Grisu = "42e2da0e-8278-4e71-bc24-59509adca0fe" [compat] -Grisu = "1" -julia = "1" +julia = "1.10" [extras] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/Showoff.jl b/src/Showoff.jl index 0001f5b..f115e6c 100644 --- a/src/Showoff.jl +++ b/src/Showoff.jl @@ -2,19 +2,10 @@ module Showoff using Dates -if isdefined(Base, :Ryu) - include("ryu.jl") -else - include("grisu.jl") -end +include("ryu.jl") export showoff -# suppress compile errors when there isn't a grisu_ccall macro -macro grisu_ccall(x, mode, ndigits) - quote end -end - # Fallback function showoff(xs::AbstractArray, style=:none) result = Vector{String}(undef, length(xs)) diff --git a/src/grisu.jl b/src/grisu.jl deleted file mode 100644 index b6ab0d5..0000000 --- a/src/grisu.jl +++ /dev/null @@ -1,142 +0,0 @@ -# This is used on Julia version that have the Base.Grisu module - -import Base.Grisu - -function grisu(v::AbstractFloat, mode, requested_digits) - return tuple(Grisu.grisu(v, mode, requested_digits)..., Grisu.DIGITS) -end - -function plain_precision_heuristic(xs::AbstractArray{<:AbstractFloat}) - ys = filter(isfinite, xs) - precision = 0 - for y in ys - len, point, neg, digits = grisu(convert(Float32, y), Grisu.SHORTEST, 0) - precision = max(precision, len - point) - end - return max(precision, 0) -end - -# Print a floating point number at fixed precision. Pretty much equivalent to -# @sprintf("%0.$(precision)f", x), without the macro issues. -function format_fixed(x::AbstractFloat, precision::Integer) - @assert precision >= 0 - - if x == Inf - return "∞" - elseif x == -Inf - return "-∞" - elseif isnan(x) - return "NaN" - end - - len, point, neg, digits = grisu(x, Grisu.FIXED, precision) - - buf = IOBuffer() - if x < 0 - print(buf, '-') - end - - for c in digits[1:min(point, len)] - print(buf, convert(Char, c)) - end - - if point > len - for _ in len:point-1 - print(buf, '0') - end - elseif point < len - if point <= 0 - print(buf, '0') - end - print(buf, '.') - if point < 0 - for _ in 1:-point - print(buf, '0') - end - for c in digits[1:len] - print(buf, convert(Char, c)) - end - else - for c in digits[point+1:len] - print(buf, convert(Char, c)) - end - end - end - - trailing_zeros = precision - max(0, len - point) - if trailing_zeros > 0 && point >= len - print(buf, '.') - end - - for _ in 1:trailing_zeros - print(buf, '0') - end - - String(take!(buf)) -end - -# Print a floating point number in scientific notation at fixed precision. Sort of equivalent -# to @sprintf("%0.$(precision)e", x), but prettier printing. -function format_fixed_scientific(x::AbstractFloat, precision::Integer, - engineering::Bool) - if x == 0.0 - return "0" - elseif x == Inf - return "∞" - elseif x == -Inf - return "-∞" - elseif isnan(x) - return "NaN" - end - - mag = floor(Int, log10(abs(x))) - grisu_precision = precision - - len, point, neg, digits = grisu((x / 10.0^mag), Grisu.FIXED, grisu_precision) - point += mag - - @assert len > 0 - - buf = IOBuffer() - if x < 0 - print(buf, '-') - end - - print(buf, convert(Char, digits[1])) - nextdigit = 2 - if engineering - while (point - 1) % 3 != 0 - if nextdigit <= len - print(buf, convert(Char, digits[nextdigit])) - else - print(buf, '0') - end - nextdigit += 1 - point -= 1 - end - end - - if precision > 1 - print(buf, '.') - end - - for i in nextdigit:len - print(buf, convert(Char, digits[i])) - end - - for i in (len+1):precision - print(buf, '0') - end - - print(buf, "×10") - for c in string(point - 1) - if '0' <= c <= '9' - print(buf, superscript_numerals[c - '0' + 1]) - elseif c == '-' - print(buf, '⁻') - end - end - - return String(take!(buf)) -end - diff --git a/test/runtests.jl b/test/runtests.jl index 3d205b9..fc58994 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,11 +6,6 @@ using Printf const drops0s = !isdefined(Base, :Ryu) @testset "Internals" begin - @test Showoff.@grisu_ccall(1, 2, 3) === nothing - if isdefined(Showoff, :Grisu) - @test Showoff.grisu(1.0, Showoff.Grisu.SHORTEST, 2) == (1, 1, false, Showoff.Grisu.DIGITS) - end - let x = [1.0, Inf, 2.0, NaN] @test Showoff.concrete_minimum(x) == 1.0 @test Showoff.concrete_maximum(x) == 2.0