Skip to content

Add support for Normed construction from Rational (Fixes #157) #169

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

Merged
merged 1 commit into from
Jan 21, 2020
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
8 changes: 7 additions & 1 deletion src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ function _convert(::Type{F}, x::AbstractFloat) where {T, f, F <: Fixed{T,f}}
end

function _convert(::Type{F}, x::Rational) where {T, f, F <: Fixed{T,f}}
F(x.num)/F(x.den) # TODO: optimization and input range checking
xmin = widemul(denominator(x), widen1(T)(typemin(T)) << 0x1 - 0x1)
xmax = widemul(denominator(x), oneunit(widen1(T)) << bitwidth(T) - 0x1)
if xmin <= (widen1(numerator(x)) << UInt8(f + 1)) < xmax
reinterpret(F, round(T, convert(floattype(T), x) * @exp2(f)))
else
throw_converterror(F, x)
end
end

# unchecked arithmetic
Expand Down
8 changes: 8 additions & 0 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ function _convert(::Type{N}, x::Tf) where {T, f, N <: Normed{T,f}, Tf <: Union{F
return reinterpret(N, unsafe_trunc(T, yi >> (ex & bits)))
end

function _convert(::Type{N}, x::Rational) where {T, f, N <: Normed{T,f}}
if 0 <= x <= Rational(typemax(N))
reinterpret(N, round(T, convert(floattype(T), x) * rawone(N)))
else
throw_converterror(N, x)
end
end

rem(x::N, ::Type{N}) where {N <: Normed} = x
rem(x::Normed, ::Type{N}) where {T, N <: Normed{T}} = reinterpret(N, _unsafe_trunc(T, round((rawone(N)/rawone(x))*reinterpret(x))))
rem(x::Real, ::Type{N}) where {T, N <: Normed{T}} = reinterpret(N, _unsafe_trunc(T, round(rawone(N)*x)))
Expand Down
7 changes: 4 additions & 3 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ end
@test_throws InexactError convert(Fixed{Int8, 7}, 128)

@test convert(Q2f5, -1//2) === -0.5Q2f5
@test_broken convert(Q1f6, Rational{Int8}(-3//4)) === -0.75Q1f6
@test_broken convert(Q0f7, Rational{Int16}(-3//4)) === -0.75Q0f7
@test_broken convert(Q0f7, Rational{UInt8}(3//4)) === 0.75Q0f7
@test convert(Q1f6, Rational{Int8}(-3//4)) === -0.75Q1f6
@test convert(Q0f7, Rational{Int16}(-3//4)) === -0.75Q0f7
@test convert(Q0f7, Rational{UInt8}(3//4)) === 0.75Q0f7
@test_throws ArgumentError convert(Q0f7, typemax(Rational{Int8}))

@test convert(Q0f7, Base.TwicePrecision(0.5)) === 0.5Q0f7
@test_throws InexactError convert(Q7f8, Base.TwicePrecision(0x80, 0x01))
Expand Down
7 changes: 4 additions & 3 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ end

@test convert(N0f8, 1.1f0/typemax(UInt8)) == eps(N0f8)

@test_broken convert(N0f8, 1//255) === eps(N0f8)
@test_broken convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5)
@test_broken convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5)
@test convert(N0f8, 1//255) === eps(N0f8)
@test convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5)
@test convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5)
@test_throws ArgumentError convert(N0f8, typemax(Rational{UInt8}))

@test convert(N0f8, Base.TwicePrecision(1.0)) === 1N0f8

Expand Down