Skip to content

Commit 6ecf42b

Browse files
hyrodiumdevmotion
andauthored
Add Base.round(x::IrrationalConstant, r::RoundingMode) (#24)
* add `Base.round(x::IrrationalConstant, r::RoundingMode)` * Add tests * More tests * Revert unrelated changes --------- Co-authored-by: David Widmann <[email protected]> Co-authored-by: David Widmann <[email protected]>
1 parent 1275225 commit 6ecf42b

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "IrrationalConstants"
22
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
33
authors = ["JuliaMath"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[compat]
77
julia = "1"

src/macro.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
abstract type IrrationalConstant <: AbstractIrrational end
66

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

2023
# definitions for AbstractIrrational added in https://github.com/JuliaLang/julia/pull/34773
2124
if VERSION < v"1.5.0-DEV.301"

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,22 @@ end
131131
@test twoπ/ComplexF32(2) isa ComplexF32
132132
@test log(twoπ, ComplexF32(2)) isa ComplexF32
133133
end
134+
135+
# issue #23
136+
@testset "rounding irrationals" begin
137+
# without rounding mode
138+
@test @inferred(round(twoπ)) == 6.0
139+
@test @inferred(round(sqrt2)) == 1.0
140+
@test @inferred(round(sqrt3)) == 2.0
141+
@test @inferred(round(loghalf)) == -1.0
142+
143+
# with rounding modes
144+
for mode in (RoundDown, RoundToZero, RoundNearest, RoundNearestTiesAway, RoundNearestTiesUp)
145+
@test @inferred(round(twoπ, mode)) == 6.0
146+
@test @inferred(round(sqrt2, mode)) == 1.0
147+
end
148+
@test @inferred(round(sqrt3, RoundUp)) == 2.0
149+
for mode in (RoundUp, RoundToZero)
150+
@test @inferred(round(loghalf, mode)) == 0.0
151+
end
152+
end

0 commit comments

Comments
 (0)