From 3c48c86001064b1c5f14f912f009c8be544fc392 Mon Sep 17 00:00:00 2001 From: SundaraRaman R Date: Thu, 22 May 2025 21:41:03 +0530 Subject: [PATCH 1/2] fix writeshortest for 0.0 when hash==false --- base/ryu/shortest.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/ryu/shortest.jl b/base/ryu/shortest.jl index c1ec648bfacdd..37024f18b4df1 100644 --- a/base/ryu/shortest.jl +++ b/base/ryu/shortest.jl @@ -250,8 +250,10 @@ function writeshortest(buf::AbstractVector{UInt8}, pos, x::T, pos += 1 end if precision == -1 - @inbounds buf[pos] = UInt8('0') - pos += 1 + if hash + @inbounds buf[pos] = UInt8('0') + pos += 1 + end if typed && x isa Float32 @inbounds buf[pos] = UInt8('f') @inbounds buf[pos + 1] = UInt8('0') From 1f5c266852984006f739a988f7189f475a78e2ff Mon Sep 17 00:00:00 2001 From: SundaraRaman R Date: Sat, 24 May 2025 16:24:05 +0530 Subject: [PATCH 2/2] tests added for Ryu.writeshortest output format options --- test/ryu.jl | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/test/ryu.jl b/test/ryu.jl index 05eedef9a0da2..e885d6c10838f 100644 --- a/test/ryu.jl +++ b/test/ryu.jl @@ -19,6 +19,34 @@ todouble(sign, exp, mant) = Core.bitcast(Float64, (UInt64(sign) << 63) | (UInt64 @test Ryu.writeshortest(-Inf) == "-Inf" end +@testset "OutputOptions" begin + # plus + @test "+1" == Base.Ryu.writeshortest(1.0, true, false, false) + @test "-1" == Base.Ryu.writeshortest(-1.0, true, false, false) + + # space + @test " 1" == Ryu.writeshortest(1.0, false, true, false) + + # hash + @test "0" == Ryu.writeshortest(0.0, false, false, false) + + # precision + @test "9.9900" == Ryu.writeshortest(9.99, false, false, true, 5) + @test "1." == Ryu.writeshortest(1.0, false, false, true, 1) + + # expchar + @test "1.0d6" == Ryu.writeshortest(1e6, false, false, true, -1, UInt8('d')) + + # padexp + @test "3.0e+08" == Ryu.writeshortest(3e8, false, false, true, -1, UInt8('e'), true) + + # decchar + @test "3,14" == Ryu.writeshortest(3.14, false, false, true, -1, UInt8('e'), false, UInt8(',')) + + # compact + @test "0.333333" == Ryu.writeshortest(1/3, false, false, true, -1, UInt8('e'), false, UInt8('.'), false, true) +end + @testset "SwitchToSubnormal" begin @test "2.2250738585072014e-308" == Ryu.writeshortest(2.2250738585072014e-308) end @@ -241,6 +269,17 @@ end # Float64 @test "-Inf" == Ryu.writeshortest(Float32(-Inf)) end +@testset "OutputOptions" begin + # typed + @test "1.0f0" == Ryu.writeshortest(Float32(1.0), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + @test "Inf32" == Ryu.writeshortest(Float32(Inf), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + @test "NaN32" == Ryu.writeshortest(Float32(NaN), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + @test "3.14f0" == Ryu.writeshortest(Float32(3.14), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + + # typed and no-hash + @test "1f0" == Ryu.writeshortest(1.0f0, false, false, false, -1, UInt8('e'), false, UInt8('.'), true) +end + @testset "SwitchToSubnormal" begin @test "1.1754944e-38" == Ryu.writeshortest(1.1754944f-38) end @@ -341,6 +380,17 @@ end # Float32 @test "-Inf" == Ryu.writeshortest(Float16(-Inf)) end +@testset "OutputOptions" begin + # typed + @test "Float16(1.0)" == Ryu.writeshortest(Float16(1.0), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + @test "Inf16" == Ryu.writeshortest(Float16(Inf), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + @test "NaN16" == Ryu.writeshortest(Float16(NaN), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + @test "Float16(3.14)" == Ryu.writeshortest(Float16(3.14), false, false, true, -1, UInt8('e'), false, UInt8('.'), true) + + # typed and no-hash + @test "Float16(1)" == Ryu.writeshortest(Float16(1.0), false, false, false, -1, UInt8('e'), false, UInt8('.'), true) +end + let x=floatmin(Float16) while x <= floatmax(Float16) @test parse(Float16, Ryu.writeshortest(x)) == x