Skip to content

Commit

Permalink
Merge ae455b7 into 19d93c0
Browse files Browse the repository at this point in the history
  • Loading branch information
fkastner committed Feb 17, 2021
2 parents 19d93c0 + ae455b7 commit 7ea2c9e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/NiceNumbers.jl
Original file line number Diff line number Diff line change
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 Expand Up @@ -165,17 +166,20 @@ norm2(v::AbstractArray{NiceNumber,1}) = sqrt(v'v)

## macro stuff
"""
nice(x, mod = @__MODULE__)
nice(x)
If `x` is an expression it replaces all occuring numbers by `NiceNumber`s.
If `x` is a number it turns it into a `NiceNumber`.
If `x` is the symbol `:im` it turns it into `NiceNumber(im)`.
Otherwise it does nothing.
"""
function nice end
nice(x) = x
nice(n::Number) = NiceNumber(n)
nice(s::Symbol) = s===:im ? NiceNumber(im) : s
nice(ex::Expr) = Expr(ex.head, map(nice, ex.args)...)

"""
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ using Test, LinearAlgebra

@testset "Macro" begin
@test @nice 4^1.5 === NiceNumber(8)
@test @nice sqrt(9//4)+7*sqrt(3)*im === NiceNumber(3//2,7,-3)
end

@testset "Show Methods" begin
Expand Down

0 comments on commit 7ea2c9e

Please sign in to comment.