Skip to content

Commit

Permalink
Correct checked_neg(unsigned(0))
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Dec 5, 2015
1 parent 071390a commit b30f184
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ The overflow protection may impose a perceptible performance penalty.
"""
function checked_neg end

checked_neg(x::Unsigned) = throw(OverflowError())
function checked_neg(x::Unsigned)
x != 0 && throw(OverflowError())
x
end
function checked_neg{T<:Union{Int8,Int16,Int32,Int64,Int128}}(x::T)
x == typemin(T) && throw(OverflowError())
-x
Expand Down
4 changes: 4 additions & 0 deletions test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ import Base: checked_abs, checked_neg, checked_add, checked_sub, checked_mul
for T in (Int8, Int16, Int32, Int64, Int128)
# regular cases
for s in (-1, +1)
@test checked_abs(T(0s)) === T(abs(0s))
@test checked_neg(T(0s)) === T(-(0s))
@test checked_abs(T(3s)) === T(abs(3s))
@test checked_neg(T(3s)) === T(-(3s))
@test checked_abs(T(s*typemax(T))) === typemax(T)
Expand Down Expand Up @@ -194,6 +196,8 @@ end

for T in (UInt8, UInt16, UInt32, UInt64, UInt128)
# regular cases
@test checked_abs(T(0)) === T(0)
@test checked_neg(T(0)) === T(0)
@test checked_abs(T(3)) === T(3)
@test_throws OverflowError checked_neg(T(3))
# regular cases
Expand Down

0 comments on commit b30f184

Please sign in to comment.