From 739ceb33604a210e97506d879b88894d55738251 Mon Sep 17 00:00:00 2001 From: mohamed82008 Date: Sun, 16 Feb 2020 11:12:55 +1100 Subject: [PATCH 1/3] zero, one and oneunit (throws) for irrationals --- base/irrationals.jl | 9 +++++++++ test/numbers.jl | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/base/irrationals.jl b/base/irrationals.jl index 4b42aa9c05a26..090f3b50c31ab 100644 --- a/base/irrationals.jl +++ b/base/irrationals.jl @@ -136,6 +136,15 @@ hash(x::Irrational, h::UInt) = 3*objectid(x) - h widen(::Type{T}) where {T<:Irrational} = T +zero(::AbstractIrrational) = false +zero(::Type{<:AbstractIrrational}) = false + +one(::AbstractIrrational) = true +one(::Type{<:AbstractIrrational}) = true + +oneunit(T::Type{<:AbstractIrrational}) = throw(ArgumentError("The number one cannot be of type $T")) +oneunit(x::T) where {T <: AbstractIrrational} = oneunit(T) + -(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 31761880f84c0..0ad40e83b8c89 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1035,6 +1035,15 @@ end @test !(1 > NaN) end +@testset "Irrational zero, one and oneunit" begin + @test one(pi) === true + @test zero(pi) === false + @test one(typeof(pi)) === true + @test zero(typeof(pi)) === false + @test_throws ArgumentError oneunit(pi) + @test_throws ArgumentError oneunit(typeof(pi)) +end + @testset "Irrationals compared with Irrationals" begin for i in (π, ℯ, γ, catalan) for j in (π, ℯ, γ, catalan) From 8e8a7257f8e99e93aeec65a395203dd5708bc459 Mon Sep 17 00:00:00 2001 From: mohamed82008 Date: Sun, 16 Feb 2020 18:33:53 +1100 Subject: [PATCH 2/3] tab to whitespace indentation --- test/numbers.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/numbers.jl b/test/numbers.jl index 0ad40e83b8c89..763c303347df1 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1036,12 +1036,12 @@ end end @testset "Irrational zero, one and oneunit" begin - @test one(pi) === true - @test zero(pi) === false - @test one(typeof(pi)) === true - @test zero(typeof(pi)) === false - @test_throws ArgumentError oneunit(pi) - @test_throws ArgumentError oneunit(typeof(pi)) + @test one(pi) === true + @test zero(pi) === false + @test one(typeof(pi)) === true + @test zero(typeof(pi)) === false + @test_throws ArgumentError oneunit(pi) + @test_throws ArgumentError oneunit(typeof(pi)) end @testset "Irrationals compared with Irrationals" begin From f96aec987adc29672823de34ee12c4e78d58663f Mon Sep 17 00:00:00 2001 From: mohamed82008 Date: Mon, 17 Feb 2020 09:43:41 +1100 Subject: [PATCH 3/3] remove oneunit for irrationals --- base/irrationals.jl | 3 --- test/numbers.jl | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/base/irrationals.jl b/base/irrationals.jl index 090f3b50c31ab..9a5151ca16c95 100644 --- a/base/irrationals.jl +++ b/base/irrationals.jl @@ -142,9 +142,6 @@ zero(::Type{<:AbstractIrrational}) = false one(::AbstractIrrational) = true one(::Type{<:AbstractIrrational}) = true -oneunit(T::Type{<:AbstractIrrational}) = throw(ArgumentError("The number one cannot be of type $T")) -oneunit(x::T) where {T <: AbstractIrrational} = oneunit(T) - -(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 763c303347df1..608c1d77317e4 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1035,13 +1035,11 @@ end @test !(1 > NaN) end -@testset "Irrational zero, one and oneunit" begin +@testset "Irrational zero and one" begin @test one(pi) === true @test zero(pi) === false @test one(typeof(pi)) === true @test zero(typeof(pi)) === false - @test_throws ArgumentError oneunit(pi) - @test_throws ArgumentError oneunit(typeof(pi)) end @testset "Irrationals compared with Irrationals" begin