File: PhysQuantities.jl

package info (click to toggle)
julia 1.0.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,452 kB
  • sloc: lisp: 236,453; ansic: 55,579; cpp: 25,603; makefile: 1,685; pascal: 1,130; sh: 956; asm: 86; xml: 76
file content (26 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# This file is a part of Julia. License is MIT: https://julialang.org/license

module PhysQuantities

export PhysQuantity

# Mimic a quantity with a physical unit that is not convertible to a real number
struct PhysQuantity{n,T}   # n is like the exponent of the unit
    val::T
end
PhysQuantity{n}(x::T) where {n,T} = PhysQuantity{n,T}(x)
Base.zero(::Type{PhysQuantity{n,T}}) where {n,T} = PhysQuantity{n,T}(zero(T))
Base.zero(x::PhysQuantity) = zero(typeof(x))
Base.:+(x::PhysQuantity{n}, y::PhysQuantity{n}) where n = PhysQuantity{n}(x.val + y.val)
Base.:-(x::PhysQuantity{n}, y::PhysQuantity{n}) where n = PhysQuantity{n}(x.val - y.val)
Base.:*(x::PhysQuantity{n,T}, y::Int) where {n,T} = PhysQuantity{n}(x.val*y)
Base.:/(x::PhysQuantity{n,T}, y::Int) where {n,T} = PhysQuantity{n}(x.val/y)
Base.:*(x::PhysQuantity{n1,S}, y::PhysQuantity{n2,T}) where {n1,n2,S,T} =
    PhysQuantity{n1+n2}(x.val*y.val)
Base.:/(x::PhysQuantity{n1,S}, y::PhysQuantity{n2,T}) where {n1,n2,S,T} =
    PhysQuantity{n1-n2}(x.val/y.val)
Base.convert(::Type{PhysQuantity{0,T}}, x::Int) where T = PhysQuantity{0}(convert(T, x))
Base.convert(::Type{P}, ::Int) where P<:PhysQuantity =
    error("Int is incommensurate with PhysQuantity")

end #module