From 1bfc0d07ef78e84a4f02e409c04f60a19519bdc6 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 3 Oct 2016 12:09:32 -0500 Subject: [PATCH] Add a few more constructors/conversions --- src/DoubleDouble.jl | 3 +++ test/runtests.jl | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/DoubleDouble.jl b/src/DoubleDouble.jl index 842efdd..5511147 100644 --- a/src/DoubleDouble.jl +++ b/src/DoubleDouble.jl @@ -84,6 +84,8 @@ convert{T<:AbstractFloat}(::Type{Double{T}}, x::Irrational) = convert(Double{T}, convert(::Type{BigFloat}, x::Single) = big(x.hi) convert(::Type{BigFloat}, x::Double) = big(x.hi) + big(x.lo) +convert{T}(::Type{Single{T}}, x::Integer) = convert(Single{T}, T(x)) +convert{T}(::Type{Double{T}}, x::Integer) = convert(Double{T}, T(x)) promote_rule{T<:AbstractFloat}(::Type{Single{T}}, ::Type{T}) = Single{T} promote_rule{T<:AbstractFloat}(::Type{Double{T}}, ::Type{T}) = Double{T} @@ -93,6 +95,7 @@ promote_rule{T<:AbstractFloat}(::Type{Double{T}}, ::Type{Single{T}}) = Double{T} promote_rule{s,T<:AbstractFloat}(::Type{Irrational{s}}, ::Type{Single{T}}) = Double{Float64} +Single(x::Real) = convert(Single{Float64}, Float64(x)) Double(x::Real) = convert(Double{Float64}, Float64(x)) Double(x::BigFloat) = convert(Double{Float64}, x) diff --git a/test/runtests.jl b/test/runtests.jl index da8149c..06d9fc6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,5 +46,16 @@ a = Double(big"3.1") @test a == Double(3.1, -8.881784197001253e-17) @test BigFloat(a) == big"3.099999999999999999999999999999995069619342368676216176696466982586064542459781" -@test Double(3) == Double(3.0, 0.0) -@test Double(big(3)) == Double(3.0, 0.0) +@test Single(3) === Single(3.0) +@test Double(3) === Double(3.0, 0.0) +@test Double(big(3)) === Double(3.0, 0.0) + +@test convert(Single{Float64}, 1) === Single(1.0) +@test convert(Single{Float32}, 1) === Single(1.0f0) +@test convert(Double{Float64}, 1) === Double(1.0, 0.0) +@test convert(Double{Float32}, 1) === Double(1.0f0, 0.0f0) + +@test Single{Float32}(3) === Single{Float32}(3.0f0) +@test Double{Float32}(3) === Double{Float32}(3.0f0, 0.0f0) +@test Single{Float32}(BigFloat(3)) === Single{Float32}(3.0f0) +@test Double{Float32}(BigFloat(3)) === Double{Float32}(3.0f0, 0.0f0)