Skip to content

Commit

Permalink
fix for Grisu on Julia 1.6 (#25)
Browse files Browse the repository at this point in the history
* fix for Grisu on Julia 1.6

Co-authored-by: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com>
  • Loading branch information
KristofferC and aviatesk committed Sep 22, 2020
1 parent f5e5329 commit fa972ff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.3.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Grisu = "42e2da0e-8278-4e71-bc24-59509adca0fe"

[compat]
julia = "1"
Expand Down
14 changes: 10 additions & 4 deletions src/Showoff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module Showoff

using Dates

if isdefined(Base, :Grisu)
import Base.Grisu
else
import Grisu
end

export showoff


Expand All @@ -14,7 +20,7 @@ end


function grisu(v::AbstractFloat, mode, requested_digits)
return tuple(Base.Grisu.grisu(v, mode, requested_digits)..., Base.Grisu.DIGITS)
return tuple(Grisu.grisu(v, mode, requested_digits)..., Grisu.DIGITS)
end


Expand Down Expand Up @@ -81,7 +87,7 @@ 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), Base.Grisu.SHORTEST, 0)
len, point, neg, digits = grisu(convert(Float32, y), Grisu.SHORTEST, 0)
precision = max(precision, len - point)
end
return max(precision, 0)
Expand Down Expand Up @@ -143,7 +149,7 @@ function format_fixed(x::AbstractFloat, precision::Integer)
return "NaN"
end

len, point, neg, digits = grisu(x, Base.Grisu.FIXED, precision)
len, point, neg, digits = grisu(x, Grisu.FIXED, precision)

buf = IOBuffer()
if x < 0
Expand Down Expand Up @@ -212,7 +218,7 @@ function format_fixed_scientific(x::AbstractFloat, precision::Integer,
grisu_precision = precision
end

len, point, neg, digits = grisu((x / 10.0^mag), Base.Grisu.FIXED, grisu_precision)
len, point, neg, digits = grisu((x / 10.0^mag), Grisu.FIXED, grisu_precision)
point += mag

@assert len > 0
Expand Down
7 changes: 6 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using Showoff
using Test
using Dates
if isdefined(Base, :Grisu)
const Grisu = Base.Grisu
else
import Grisu
end

@testset "Internals" begin
@test Showoff.@grisu_ccall(1, 2, 3) === nothing
@test Showoff.grisu(1.0, Base.Grisu.SHORTEST, 2) == (1, 1, false, Base.Grisu.DIGITS)
@test Showoff.grisu(1.0, Grisu.SHORTEST, 2) == (1, 1, false, Grisu.DIGITS)

let x = [1.0, Inf, 2.0, NaN]
@test Showoff.concrete_minimum(x) == 1.0
Expand Down

0 comments on commit fa972ff

Please sign in to comment.