Skip to content

Commit 6b08f48

Browse files
authored
Avoid inconsistent behavior of unsafe_trunc for BigFloat (#212)
1 parent fab4079 commit 6b08f48

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/utilities.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ exponent_bias(::Type{Float32}) = 127
3838
exponent_bias(::Type{Float64}) = 1023
3939

4040
_unsafe_trunc(::Type{T}, x::Integer) where {T} = x % T
41-
_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x)
41+
_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x)
42+
# issue #202, #211
43+
_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = trunc(BigInt, x) % T
44+
4245
if !signbit(signed(unsafe_trunc(UInt, -12.345)))
4346
# a workaround for ARM (issue #134)
4447
function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T <: Integer}
4548
unsafe_trunc(T, unsafe_trunc(signedtype(T), x))
4649
end
47-
# exclude BigFloat (issue #202)
48-
_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = unsafe_trunc(T, x)
4950
end
5051

5152
wrapper(@nospecialize(T)) = Base.typename(T).wrapper

test/normed.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ end
263263
# issue #150
264264
@test all(f -> 1.0f0 % Normed{UInt32,f} == oneunit(Normed{UInt32,f}), 1:32)
265265
@test all(f -> 1.0e0 % Normed{UInt64,f} == oneunit(Normed{UInt64,f}), 1:64)
266+
267+
# issu #211
268+
@test big"1.2" % N0f8 === 0.196N0f8
269+
@test reinterpret(BigFloat(0x0_01234567_89abcdef) % N63f1) === 0x01234567_89abcdef
266270
end
267271

268272
@testset "arithmetic" begin

0 commit comments

Comments
 (0)