From f8d6d1c1cbb312ca6d4bc0517a46e7bac6ae05c8 Mon Sep 17 00:00:00 2001 From: kimikage Date: Fri, 14 Aug 2020 15:18:02 +0900 Subject: [PATCH] Avoid inconsistent behavior of `unsafe_trunc` for `BigFloat` --- src/utilities.jl | 7 ++++--- test/normed.jl | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/utilities.jl b/src/utilities.jl index 618ce295..37813c43 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -38,14 +38,15 @@ exponent_bias(::Type{Float32}) = 127 exponent_bias(::Type{Float64}) = 1023 _unsafe_trunc(::Type{T}, x::Integer) where {T} = x % T -_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x) +_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x) +# issue #202, #211 +_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = trunc(BigInt, x) % T + if !signbit(signed(unsafe_trunc(UInt, -12.345))) # a workaround for ARM (issue #134) function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T <: Integer} unsafe_trunc(T, unsafe_trunc(signedtype(T), x)) end - # exclude BigFloat (issue #202) - _unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = unsafe_trunc(T, x) end wrapper(@nospecialize(T)) = Base.typename(T).wrapper diff --git a/test/normed.jl b/test/normed.jl index 6c751724..304b96b0 100644 --- a/test/normed.jl +++ b/test/normed.jl @@ -263,6 +263,10 @@ end # issue #150 @test all(f -> 1.0f0 % Normed{UInt32,f} == oneunit(Normed{UInt32,f}), 1:32) @test all(f -> 1.0e0 % Normed{UInt64,f} == oneunit(Normed{UInt64,f}), 1:64) + + # issu #211 + @test big"1.2" % N0f8 === 0.196N0f8 + @test reinterpret(BigFloat(0x0_01234567_89abcdef) % N63f1) === 0x01234567_89abcdef end @testset "arithmetic" begin