Skip to content

Commit

Permalink
Always throw OverflowError in gcd algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Feb 25, 2016
1 parent 8677fcc commit f44b1b1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 4 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ function gcd{T<:Union{Int64,UInt64,Int128,UInt128}}(a::T, b::T)
v -= u
v >>= trailing_zeros(v)
end
T(u << k)
r = u << k
# T(r) would throw InexactError; we want OverflowError instead
r > typemax(T) && throw(OverflowError())
r % T
end

# explicit a==0 test is to handle case of lcm(0,0) correctly
Expand Down
3 changes: 1 addition & 2 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
@test gcd(typemax(Int), 1) == 1
@test gcd(-typemax(Int), 1) == 1
@test gcd(typemin(Int), 1) == 1
divex = sizeof(Int) == 4 ? OverflowError : InexactError
@test_throws divex gcd(typemin(Int), typemin(Int))
@test_throws OverflowError gcd(typemin(Int), typemin(Int))

@test lcm(2, 3) == 6
@test lcm(4, 6) == 12
Expand Down

0 comments on commit f44b1b1

Please sign in to comment.