From 125537ad28b8eefd130186ca031058574b384b6e Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 3 Mar 2022 10:40:36 -0600 Subject: [PATCH] Add AbstractFloat constructors (#25) Amazing that the lack of this method has not been noticed previously. --- src/Ratios.jl | 2 ++ test/runtests.jl | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) 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)