Skip to content

Commit

Permalink
Fixing abs for Rational{<:Unsigned} (#29561)
Browse files Browse the repository at this point in the history
* Fix abs for Rational{<:Unsigned}

* Add tests for abs(x::Rational)

(cherry picked from commit 4eb1a93)
  • Loading branch information
lcontento authored and KristofferC committed Oct 10, 2018
1 parent b46c362 commit 1123f0a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ signbit(x::Rational) = signbit(x.num)
copysign(x::Rational, y::Real) = copysign(x.num,y) // x.den
copysign(x::Rational, y::Rational) = copysign(x.num,y.num) // x.den

abs(x::Rational) = Rational(abs(x.num), x.den)

typemin(::Type{Rational{T}}) where {T<:Integer} = -one(T)//zero(T)
typemax(::Type{Rational{T}}) where {T<:Integer} = one(T)//zero(T)

Expand Down
5 changes: 5 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ using Test
@test !(1//3 < NaN)
@test !(1//3 == NaN)
@test !(1//3 > NaN)

# PR 29561
@test abs(one(Rational{UInt})) === one(Rational{UInt})
@test abs(one(Rational{Int})) === one(Rational{Int})
@test abs(-one(Rational{Int})) === one(Rational{Int})
end

@testset "Rational methods" begin
Expand Down

0 comments on commit 1123f0a

Please sign in to comment.