Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base.round(x::IrrationalConstant, r::RoundingMode) #24

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "IrrationalConstants"
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
authors = ["JuliaMath"]
version = "0.2.0"
version = "0.2.1"

[compat]
julia = "1"
Expand Down
3 changes: 3 additions & 0 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

abstract type IrrationalConstant <: AbstractIrrational end

# TODO: Remove definitions if they become available for `AbstractIrrational` in Base
# Ref https://github.com/JuliaLang/julia/pull/48768
function Base.show(io::IO, ::MIME"text/plain", x::IrrationalConstant)
if get(io, :compact, false)::Bool
print(io, x)
Expand All @@ -16,6 +18,7 @@ Base.:(==)(::T, ::T) where {T<:IrrationalConstant} = true
Base.:<(::T, ::T) where {T<:IrrationalConstant} = false
Base.:<=(::T, ::T) where {T<:IrrationalConstant} = true
Base.hash(x::IrrationalConstant, h::UInt) = 3*objectid(x) - h
Base.round(x::IrrationalConstant, r::RoundingMode) = round(float(x), r)

# definitions for AbstractIrrational added in https://github.com/JuliaLang/julia/pull/34773
if VERSION < v"1.5.0-DEV.301"
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,22 @@ end
@test twoπ/ComplexF32(2) isa ComplexF32
@test log(twoπ, ComplexF32(2)) isa ComplexF32
end

# issue #23
@testset "rounding irrationals" begin
# without rounding mode
@test @inferred(round(twoπ)) == 6.0
@test @inferred(round(sqrt2)) == 1.0
@test @inferred(round(sqrt3)) == 2.0
@test @inferred(round(loghalf)) == -1.0

# with rounding modes
for mode in (RoundDown, RoundToZero, RoundNearest, RoundNearestTiesAway, RoundNearestTiesUp)
@test @inferred(round(twoπ, mode)) == 6.0
@test @inferred(round(sqrt2, mode)) == 1.0
end
@test @inferred(round(sqrt3, RoundUp)) == 2.0
for mode in (RoundUp, RoundToZero)
@test @inferred(round(loghalf, mode)) == 0.0
end
end