Skip to content

Commit

Permalink
Add AbstractFloat constructors (#25)
Browse files Browse the repository at this point in the history
Amazing that the lack of this method has not been noticed previously.
  • Loading branch information
timholy authored Mar 3, 2022
1 parent 9631c4a commit 125537a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Ratios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function convert(::Type{T}, r::SimpleRatio{S}) where {T<:AbstractFloat,S}
P = promote_type(T,S)
convert(T, convert(P, r.num)/convert(P, r.den))
end
(::Type{T})(r::SimpleRatio) where T<:AbstractFloat = convert(T, r)

SimpleRatio{T}(i::Integer) where {T<:Integer} = SimpleRatio{T}(convert(T, i), oneunit(T))
SimpleRatio{T}(r::Rational{S}) where {T<:Integer, S<:Integer} = SimpleRatio(convert(T, r.num), convert(T, r.den))
Rational{T}(r::SimpleRatio{S}) where {T<:Integer, S<:Integer} = convert(T, r.num) // convert(T, r.den)
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ using FixedPointNumbers

@testset "SimpleRatio" begin
r = SimpleRatio(1,2)
@test convert(Float64, r) == 0.5
@test convert(Float32, r) == 0.5f0
@test convert(BigFloat, r) == BigFloat(1)/2
@test convert(Float64, r) === Float64(r) === 0.5
@test convert(Float32, r) === Float32(r) === 0.5f0
@test convert(BigFloat, r) == BigFloat(r) == BigFloat(1)/2

r2 = SimpleRatio(2,3)
@test r*r2 == SimpleRatio(2,6) == SimpleRatio(1,3)
Expand Down

0 comments on commit 125537a

Please sign in to comment.