Skip to content

Commit 8851961

Browse files
bicycle1885staticfloat
authored andcommitted
fix consistency of trimtrailingzeros (Ryu) (#40685)
* fix consistency of trimtrailingzeros (Ryu) (cherry picked from commit ff213c1)
1 parent e07a0b9 commit 8851961

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

base/ryu/exp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function writeexp(buf, pos, v::T,
1919
end
2020
buf[pos] = UInt8('0')
2121
pos += 1
22-
if precision > 0
22+
if precision > 0 && !trimtrailingzeros
2323
buf[pos] = decchar
2424
pos += 1
2525
for _ = 1:precision

base/ryu/fixed.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ function writefixed(buf, pos, v::T,
1919
end
2020
buf[pos] = UInt8('0')
2121
pos += 1
22-
if precision > 0
22+
if precision > 0 && !trimtrailingzeros
2323
buf[pos] = decchar
2424
pos += 1
25-
if trimtrailingzeros
26-
precision = 1
27-
end
2825
for _ = 1:precision
2926
buf[pos] = UInt8('0')
3027
pos += 1

test/ryu.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,11 @@ end # Float16
544544
@test Ryu.writefixed(7.018232e-82, 6) == "0.000000"
545545
end
546546

547+
@testset "Consistency of trimtrailingzeros" begin
548+
@test Ryu.writefixed(0.0, 1, false, false, false, UInt8('.'), true) == "0"
549+
@test Ryu.writefixed(1.0, 1, false, false, false, UInt8('.'), true) == "1"
550+
@test Ryu.writefixed(2.0, 1, false, false, false, UInt8('.'), true) == "2"
551+
end
547552
end # fixed
548553

549554
@testset "Ryu.writeexp" begin
@@ -736,6 +741,12 @@ end
736741
@test Ryu.writeexp(1e+83, 1) == "1.0e+83"
737742
end
738743

744+
@testset "Consistency of trimtrailingzeros" begin
745+
@test Ryu.writeexp(0.0, 1, false, false, false, UInt8('e'), UInt8('.'), true) == "0e+00"
746+
@test Ryu.writeexp(1.0, 1, false, false, false, UInt8('e'), UInt8('.'), true) == "1e+00"
747+
@test Ryu.writeexp(2.0, 1, false, false, false, UInt8('e'), UInt8('.'), true) == "2e+00"
748+
end
749+
739750
end # exp
740751

741752
@testset "compact" begin

0 commit comments

Comments
 (0)