Skip to content

Commit

Permalink
Add unary plus for Rational, add test (JuliaLang#31772)
Browse files Browse the repository at this point in the history
  • Loading branch information
eulerkochy authored and Keno committed Apr 20, 2019
1 parent 4955696 commit 11fc1ec
Show file tree
Hide file tree
Showing 2 changed files with 9 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 @@ -237,7 +237,9 @@ typemax(::Type{Rational{T}}) where {T<:Integer} = one(T)//zero(T)

isinteger(x::Rational) = x.den == 1

+(x::Rational) = (+x.num) // x.den
-(x::Rational) = (-x.num) // x.den

function -(x::Rational{T}) where T<:BitSigned
x.num == typemin(T) && throw(OverflowError("rational numerator is typemin(T)"))
(-x.num) // x.den
Expand Down
7 changes: 7 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,10 @@ end

# issue #31396
@test round(1//2, RoundNearestTiesUp) === 1//1

@testset "Unary plus on Rational (issue #30749)" begin
@test +Rational(true) == 1//1
@test +Rational(false) == 0//1
@test -Rational(true) == -1//1
@test -Rational(false) == 0//1
end

0 comments on commit 11fc1ec

Please sign in to comment.