Skip to content

Commit

Permalink
Add a convert method that allows for writting Float64(::SIQuantity) o…
Browse files Browse the repository at this point in the history
…r Int(::SIQuantity)
  • Loading branch information
dhoegh committed Feb 7, 2016
1 parent 9ce64b9 commit 043a461
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SIUnits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 043a461

Please sign in to comment.