From 043a4610098ee511e8b908d191f8053ed04435fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=B8egh?= Date: Sun, 7 Feb 2016 19:33:11 +0100 Subject: [PATCH] Add a convert method that allows for writting Float64(::SIQuantity) or Int(::SIQuantity) --- src/SIUnits.jl | 3 ++- test/runtests.jl | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/SIUnits.jl b/src/SIUnits.jl index 82c9e99..01e376d 100644 --- a/src/SIUnits.jl +++ b/src/SIUnits.jl @@ -297,10 +297,11 @@ module SIUnits # Forwarding methods that do not affect units import Base: conj conj(x::SIQuantity) = typeof(x)(conj(x.val)) - + # remove when they are fully deprecated in 0.5 as the convert bellow covers all cases. As constructers call convert as fallback float64(x::SIQuantity) = float64(x.val) float(x::SIQuantity) = float(x.val) int(x::SIQuantity) = int(x.val) + convert{T1<:@compat(Union{Integer, AbstractFloat}), T<:Number,m,kg,s,A,K,mol,cd,rad,sr}(::Type{T1},x::SIQuantity{T,m,kg,s,A,K,mol,cd,rad,sr}) = convert(T1, x.val) *(x::SIUnit,y::SIUnit) = tup2u(tup(x)+tup(y))() *{T}(x::SIUnit,y::SIQuantity{T}) = to_q(quantity(T,tup(y)+tup(x)),y.val) diff --git a/test/runtests.jl b/test/runtests.jl index bfca381..12e5229 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -172,3 +172,11 @@ for func in (sin,cos,tan,cot,sec,csc) @test func(1.23rad) == func(1.23) @test_approx_eq func(1.23deg) func(as(1.23deg,rad)) end + +if VERSION <= v"0.4.0" + var = 5*V + for f in [Float32, Float64, Int64, Int32, UInt32, BigFloat] + @test f(var) = f(var.val) + end + +end