diff --git a/base/irrationals.jl b/base/irrationals.jl index f3a9817f1ee35..d5fae08ffe276 100644 --- a/base/irrationals.jl +++ b/base/irrationals.jl @@ -153,6 +153,8 @@ zero(::Type{<:AbstractIrrational}) = false one(::AbstractIrrational) = true one(::Type{<:AbstractIrrational}) = true +sign(x::AbstractIrrational) = ifelse(x < zero(x), -1.0, 1.0) + -(x::AbstractIrrational) = -Float64(x) for op in Symbol[:+, :-, :*, :/, :^] @eval $op(x::AbstractIrrational, y::AbstractIrrational) = $op(Float64(x),Float64(y)) diff --git a/test/numbers.jl b/test/numbers.jl index 4875de7fc3bb2..149b34633fb8a 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -490,6 +490,8 @@ end @test isa(sign(2//3), Rational{Int}) @test isa(2//3 + 2//3im, Complex{Rational{Int}}) @test isa(sign(2//3 + 2//3im), ComplexF64) + @test sign(pi) === 1.0 + @test sign(pi) === -sign(-pi) @test sign(one(UInt)) == 1 @test sign(zero(UInt)) == 0