From 595f2c196608d7365cde0609a2d72eadf02b2d24 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Wed, 28 May 2025 18:56:17 -0400 Subject: [PATCH 1/4] Implement parse and tryparse --- src/bfloat16.jl | 7 +++++++ test/runtests.jl | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/bfloat16.jl b/src/bfloat16.jl index c2a3ec4..79ef956 100644 --- a/src/bfloat16.jl +++ b/src/bfloat16.jl @@ -362,6 +362,13 @@ function Base.show(io::IO, x::BFloat16) end Printf.tofloat(x::BFloat16) = Float32(x) +# Parsing +Base.parse(::Type{BFloat16}, s::AbstractString) = BFloat16(parse(Float32, s)) +function Base.tryparse(::Type{BFloat16}, s::AbstractString) + r = tryparse(Float32, s) + return isnothing(r) ? nothing : BFloat16(r) +end + # Random import Random: rand, randn, randexp, AbstractRNG, Sampler diff --git a/test/runtests.jl b/test/runtests.jl index 0696ce7..2f2521d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -146,6 +146,30 @@ end @test repr(BFloat16(2)) == "BFloat16(2.0)" end +@testset "parse" for _parse in (parse, tryparse) + @test _parse(BFloat16, "Inf") === BFloat16(Inf) + @test _parse(BFloat16, "-Inf") === BFloat16(-Inf) + @test _parse(BFloat16, "NaN") === BFloat16(NaN) + @test _parse(BFloat16, "2") === BFloat16(2) + @test _parse(BFloat16, "1.3") === BFloat16(1.3) + @test _parse(BFloat16, "+234.6") === BFloat16(234.6) + @test _parse(BFloat16, " +234.7") === BFloat16(234.7) + @test _parse(BFloat16, " -234.8") === BFloat16(-234.8) + @test _parse(BFloat16, " -234.90") === BFloat16(-234.9) + @test _parse(BFloat16, " 235.10 ") === BFloat16(235.1) + @test _parse(BFloat16, "000235.20 ") === BFloat16(235.2) + @test _parse(BFloat16, "4e+03") === BFloat16(4e3) + @test _parse(BFloat16, "5.e+04") === BFloat16(5e4) + @test _parse(BFloat16, "0x1.3cp+0") === BFloat16(1.234375) + @test _parse(BFloat16, "0X1.3CP+0") === BFloat16(1.234375) +end + +@testset "tryparse" begin + @test tryparse(BFloat16, "635.3X") === nothing + @test tryparse(BFloat16, "X635.4") === nothing + @test tryparse(BFloat16, "ABCDE") === nothing +end + @testset "random" begin x = Array{BFloat16}(undef, 10) y = Array{BFloat16}(undef, 10) From 10908baeac9d391cfe4f9cb1c643c3feb74a820f Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Wed, 28 May 2025 19:02:29 -0400 Subject: [PATCH 2/4] Add 1e0e0 due to prior JuliaSyntax bug for Float32 --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 2f2521d..14321f0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -168,6 +168,7 @@ end @test tryparse(BFloat16, "635.3X") === nothing @test tryparse(BFloat16, "X635.4") === nothing @test tryparse(BFloat16, "ABCDE") === nothing + @test tryparse(BFloat16, "1e0e0") === nothing end @testset "random" begin From 182649c67427fe571320d9599c19eca5368e40d1 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Wed, 28 May 2025 19:24:01 -0400 Subject: [PATCH 3/4] Test that parse throws on non-parseable strings --- test/runtests.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 14321f0..412c1ab 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -164,11 +164,9 @@ end @test _parse(BFloat16, "0X1.3CP+0") === BFloat16(1.234375) end -@testset "tryparse" begin - @test tryparse(BFloat16, "635.3X") === nothing - @test tryparse(BFloat16, "X635.4") === nothing - @test tryparse(BFloat16, "ABCDE") === nothing - @test tryparse(BFloat16, "1e0e0") === nothing +@testset "not parseable" for str in ("635.3X", "X635.4", "ABCDE", "1e0e0") + @test tryparse(BFloat16, str) === nothing + @test_throws ArgumentError parse(BFloat16, str) end @testset "random" begin From 0dd79ca75baadf7b3c35220354254b5198be4947 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Wed, 28 May 2025 19:35:55 -0400 Subject: [PATCH 4/4] Use isnan rather than === for BFloat16(NaN) test --- test/runtests.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 412c1ab..d1b9974 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -149,7 +149,9 @@ end @testset "parse" for _parse in (parse, tryparse) @test _parse(BFloat16, "Inf") === BFloat16(Inf) @test _parse(BFloat16, "-Inf") === BFloat16(-Inf) - @test _parse(BFloat16, "NaN") === BFloat16(NaN) + nan16 = _parse(BFloat16, "NaN") + @test isnan(nan16) + @test isa(nan16, BFloat16) @test _parse(BFloat16, "2") === BFloat16(2) @test _parse(BFloat16, "1.3") === BFloat16(1.3) @test _parse(BFloat16, "+234.6") === BFloat16(234.6)