From 83d7c4ac6b264ee33bf76312f830e28bb9f61e40 Mon Sep 17 00:00:00 2001 From: tlycken Date: Wed, 16 Jul 2014 18:37:21 +0200 Subject: [PATCH 1/2] one(x) and zero(x) for x<:SIQuantity --- src/SIUnits.jl | 5 ++++- test/runtests.jl | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/SIUnits.jl b/src/SIUnits.jl index 5a37800..3e536fd 100644 --- a/src/SIUnits.jl +++ b/src/SIUnits.jl @@ -24,7 +24,10 @@ module SIUnits unit{T,m,kg,s,A,K,mol,cd}(x::SIRanges{T,m,kg,s,A,K,mol,cd}) = SIUnit{m,kg,s,A,K,mol,cd}() quantity{T,m,kg,s,A,K,mol,cd}(x::SIRanges{T,m,kg,s,A,K,mol,cd}) = SIQuantity{T,m,kg,s,A,K,mol,cd} - import Base: length, getindex, next, float64, float, int, show, start, step, last, done, first, eltype + import Base: length, getindex, next, float64, float, int, show, start, step, last, done, first, eltype, one, zero + + one(x::SIQuantity) = SIQuantity(one(x.val)) + zero(x::SIQuantity) = zero(x.val) * unit(x) # This is all nessecary because SIQuanity{T<:Real} !<: Real show(io::IO, x::SIRanges) = (show(io, x.val); show(io,unit(x))) diff --git a/test/runtests.jl b/test/runtests.jl index fbd1e6e..2f36365 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,6 +18,8 @@ end @test (1//2)V - 1V == (-1//2)V @test_throws_compat ErrorException 1//2V - 1V @test_throws_compat ErrorException 1V + 2s + 2kg +@test 1V + zero(1V) == 1V +@test 1V * one(1V) == 1V OneNewton = 1*(kg*m/s^2) @test OneNewton*(1s)^2 == 1kg*m From dd694e54f36f0412688b219d2a63b3828d182634 Mon Sep 17 00:00:00 2001 From: tlycken Date: Wed, 16 Jul 2014 19:26:08 +0200 Subject: [PATCH 2/2] Added versions for types --- src/SIUnits.jl | 2 ++ test/runtests.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/SIUnits.jl b/src/SIUnits.jl index 3e536fd..cd16353 100644 --- a/src/SIUnits.jl +++ b/src/SIUnits.jl @@ -27,7 +27,9 @@ module SIUnits import Base: length, getindex, next, float64, float, int, show, start, step, last, done, first, eltype, one, zero one(x::SIQuantity) = SIQuantity(one(x.val)) + one{T,m,kg,s,A,K,mol,cd}(::Type{SIQuantity{T,m,kg,s,A,K,mol,cd}}) = SIQuantity(one(T)) zero(x::SIQuantity) = zero(x.val) * unit(x) + zero{T,m,kg,s,A,K,mol,cd}(::Type{SIQuantity{T,m,kg,s,A,K,mol,cd}}) = zero(T) * SIUnit{m,kg,s,A,K,mol,cd}() # This is all nessecary because SIQuanity{T<:Real} !<: Real show(io::IO, x::SIRanges) = (show(io, x.val); show(io,unit(x))) diff --git a/test/runtests.jl b/test/runtests.jl index 2f36365..82767da 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,7 +19,9 @@ end @test_throws_compat ErrorException 1//2V - 1V @test_throws_compat ErrorException 1V + 2s + 2kg @test 1V + zero(1V) == 1V +@test 1V + zero(typeof(1V)) == 1V @test 1V * one(1V) == 1V +@test 1V * one(typeof(1V)) == 1V OneNewton = 1*(kg*m/s^2) @test OneNewton*(1s)^2 == 1kg*m