Skip to content

Commit

Permalink
rework type constraints in constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
fkastner committed Feb 12, 2021
1 parent db7734d commit ae455b7
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/NiceNumbers.jl
Expand Up @@ -16,7 +16,7 @@ struct NiceNumber <: Number
a::Rational{Int}
coeff::Rational{Int}
radicand::Int
function NiceNumber(a, coeff, radicand)
function NiceNumber(a::S, coeff::T, radicand::Int) where {S<:Union{Integer,Rational}, T<:Union{Integer,Rational}}
if radicand == 0
coeff = zero(coeff)
elseif coeff == 0
Expand All @@ -32,9 +32,10 @@ struct NiceNumber <: Number
new(a, coeff, radicand)
end
end
NiceNumber(x::T) where {T<:Union{Integer,Rational}} = NiceNumber(x, 0, 0)
NiceNumber(a, coeff, radicand::Rational) =
NiceNumber(a, coeff // denominator(radicand), numerator(radicand) * denominator(radicand))
NiceNumber(x::T) where {T<:Union{Integer,Rational}} = NiceNumber(x, 0, 0)
NiceNumber(a, coeff, radicand) = NiceNumber(a)+NiceNumber(coeff)*sqrt(NiceNumber(radicand))
NiceNumber(x::AbstractFloat) = NiceNumber(rationalize(Int, x), 0, 0)
NiceNumber(z::Complex{T}) where T<:Real = NiceNumber(z.re) + NiceNumber(0,z.im,-1)
NiceNumber(n::NiceNumber) = n
Expand Down

0 comments on commit ae455b7

Please sign in to comment.