Skip to content

Commit

Permalink
fix consistency of trimtrailingzeros (Ryu) (#40685)
Browse files Browse the repository at this point in the history
* fix consistency of trimtrailingzeros (Ryu)
  • Loading branch information
bicycle1885 authored May 4, 2021
1 parent fd2061b commit ff213c1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/ryu/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function writeexp(buf, pos, v::T,
if x == 0
buf[pos] = UInt8('0')
pos += 1
if precision > 0
if precision > 0 && !trimtrailingzeros
buf[pos] = decchar
pos += 1
for _ = 1:precision
Expand Down
5 changes: 1 addition & 4 deletions base/ryu/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ function writefixed(buf, pos, v::T,
if x == 0
buf[pos] = UInt8('0')
pos += 1
if precision > 0
if precision > 0 && !trimtrailingzeros
buf[pos] = decchar
pos += 1
if trimtrailingzeros
precision = 1
end
for _ = 1:precision
buf[pos] = UInt8('0')
pos += 1
Expand Down
11 changes: 11 additions & 0 deletions test/ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ end # Float16
@test Ryu.writefixed(7.018232e-82, 6) == "0.000000"
end

@testset "Consistency of trimtrailingzeros" begin
@test Ryu.writefixed(0.0, 1, false, false, false, UInt8('.'), true) == "0"
@test Ryu.writefixed(1.0, 1, false, false, false, UInt8('.'), true) == "1"
@test Ryu.writefixed(2.0, 1, false, false, false, UInt8('.'), true) == "2"
end
end # fixed

@testset "Ryu.writeexp" begin
Expand Down Expand Up @@ -736,6 +741,12 @@ end
@test Ryu.writeexp(1e+83, 1) == "1.0e+83"
end

@testset "Consistency of trimtrailingzeros" begin
@test Ryu.writeexp(0.0, 1, false, false, false, UInt8('e'), UInt8('.'), true) == "0e+00"
@test Ryu.writeexp(1.0, 1, false, false, false, UInt8('e'), UInt8('.'), true) == "1e+00"
@test Ryu.writeexp(2.0, 1, false, false, false, UInt8('e'), UInt8('.'), true) == "2e+00"
end

end # exp

@testset "compact" begin
Expand Down

0 comments on commit ff213c1

Please sign in to comment.