diff --git a/src/Ratios.jl b/src/Ratios.jl index 0e3ebd2..7566d25 100644 --- a/src/Ratios.jl +++ b/src/Ratios.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 3b45b49..99ea2ef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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)