Skip to content

Commit 25e20bd

Browse files
committed
Fix _unsafe_trunc to reduce the likelihood of arbitrary values (#291)
1 parent e342a70 commit 25e20bd

File tree

3 files changed

+12
-36
lines changed

3 files changed

+12
-36
lines changed

src/utilities.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ _unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x)
4242
# issue #202, #211
4343
_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = trunc(BigInt, x) % T
4444

45-
if !signbit(signed(unsafe_trunc(UInt, -12.345)))
46-
# a workaround for ARM (issue #134)
47-
function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T <: Integer}
48-
if T === UInt32
49-
copysign(unsafe_trunc(T, abs(x)), x)
50-
else
51-
unsafe_trunc(T, unsafe_trunc(signedtype(T), x))
52-
end
45+
# issue #288
46+
function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T <: Integer}
47+
if T <: ShortInts
48+
return unsafe_trunc(Int32, x) % T
49+
elseif T <: Unsigned
50+
return copysign(unsafe_trunc(T, abs(x)), x)
51+
else
52+
return unsafe_trunc(T, x)
5353
end
5454
end

test/fixed.jl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,8 @@ end
3131
issue288_out = String(take!(buf))
3232

3333
@testset "issue288" begin
34-
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
35-
if issue288_in == expected_issue288 # just leave it in the report
36-
@test issue288_in == expected_issue288
37-
else
38-
@test_broken issue288_in == expected_issue288
39-
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
40-
end
41-
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
42-
if issue288_out == expected_issue288 # just leave it in the report
43-
@test issue288_out == expected_issue288
44-
else
45-
@test_broken issue288_out == expected_issue288
46-
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
47-
end
34+
@test issue288_in == "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
35+
@test issue288_out == "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
4836
end
4937

5038
function test_op(fun::Fun, fx::F, fy::F, fxf, fyf, tol) where {Fun, F}

test/normed.jl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,8 @@ end
3131
issue288_out = String(take!(buf))
3232

3333
@testset "issue288" begin
34-
expected_issue288 = "1.0N0f8 1.0N0f16 1.0N0f16 1.0N0f8 "
35-
if issue288_in == expected_issue288 # just leave it in the report
36-
@test issue288_in == expected_issue288
37-
else
38-
@test_broken issue288_in == expected_issue288
39-
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
40-
end
41-
expected_issue288 = "0.004N0f8 2.0e-5N0f16 2.0e-5N0f16 0.004N0f8 "
42-
if issue288_out == expected_issue288 # just leave it in the report
43-
@test issue288_out == expected_issue288
44-
else
45-
@test_broken issue288_out == expected_issue288
46-
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
47-
end
34+
@test issue288_in == "1.0N0f8 1.0N0f16 1.0N0f16 1.0N0f8 "
35+
@test issue288_out == "0.004N0f8 2.0e-5N0f16 2.0e-5N0f16 0.004N0f8 "
4836
end
4937

5038
@testset "domain of f" begin

0 commit comments

Comments
 (0)