Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiplicative and addititive identities #30

Merged
merged 2 commits into from
Jul 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/SIUnits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ 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))
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)))
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ 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 + 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
Expand Down