Skip to content

Commit

Permalink
added missing quaternion constructor and promotion rule
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Nov 6, 2013
1 parent 1288510 commit 9e407b1
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ immutable Quaternion{T<:Real} <: Number
q3::T
end

function Quaternion{A<:Real,B<:Real,C<:Real,D<:Real}(q0::A,q1::B,q2::C,q3::D)
T = promote_type(A,B,C,D)
Quaternion{T}(convert(T,q0), convert(T,q1), convert(T,q2), convert(T,q3))
end

convert{T}(::Type{Quaternion{T}}, x::Real) =
Quaternion(convert(T,x), convert(T,0), convert(T,0), convert(T,0))

Expand All @@ -22,6 +27,8 @@ promote_rule{T,S}(::Type{Complex{T}}, ::Type{Quaternion{S}}) =
promote_rule{S}(::Type{Bool}, ::Type{Quaternion{S}}) = Quaternion{S}
promote_rule{T<:Real,S}(::Type{T}, ::Type{Quaternion{S}}) =
Quaternion{promote_type(T,S)}
promote_rule{T,S}(::Type{Quaternion{T}}, ::Type{Quaternion{S}}) =
Quaternion{promote_type(T,S)}

function show(io::IO, z::Quaternion)
show(io, z.q0)
Expand Down

0 comments on commit 9e407b1

Please sign in to comment.