Skip to content
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

use an extension for FixedPointNumbers #32

Merged
merged 2 commits into from
Apr 25, 2023
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
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ version = "0.4.3"
[deps]
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[weakdeps]
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"

[compat]
FixedPointNumbers = "0.5, 0.6, 0.7, 0.8"
Requires = "1"
julia = "0.7, 1"

[extensions]
RatiosFixedPointNumbersExt = "FixedPointNumbers"

[extras]
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
16 changes: 16 additions & 0 deletions ext/RatiosFixedPointNumbersExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module RatiosFixedPointNumbersExt

using Ratios
isdefined(Base, :get_extension) ? (using FixedPointNumbers) : (using ..FixedPointNumbers)

using .FixedPointNumbers: FixedPoint, Fixed, Normed, rawone

rawone_noerr(::Type{Fixed{T,f}}) where {T,f} = widen(oneunit(T)) << f
rawone_noerr(::Type{N}) where N<:Normed = rawone(N)
rawone_noerr(x::FixedPoint) = rawone_noerr(typeof(x))
Base.promote_rule(::Type{SimpleRatio{S}}, ::Type{<:FixedPoint{T}}) where {S<:Integer,T<:Integer} = SimpleRatio{promote_type(S, T)}
Ratios.SimpleRatio{S}(x::FixedPoint) where S<:Integer = SimpleRatio{S}(reinterpret(x), rawone_noerr(x))
Ratios.SimpleRatio(x::FixedPoint) = SimpleRatio(reinterpret(x), rawone_noerr(x))
Base.convert(::Type{S}, x::FixedPoint) where S<:SimpleRatio = S(x)

end
17 changes: 8 additions & 9 deletions src/Ratios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module Ratios

import Base: convert, promote_rule, *, /, +, -, ^, ==, decompose

using Requires

export SimpleRatio, common_denominator

Expand Down Expand Up @@ -121,17 +120,17 @@ function common_denominator(x::SimpleRatio, ys::SimpleRatio...)
end
end


if !isdefined(Base, :get_extension)
using Requires
end

@static if !isdefined(Base, :get_extension)
function __init__()
@require FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" begin
using .FixedPointNumbers: FixedPoint, Fixed, Normed, rawone
rawone_noerr(::Type{Fixed{T,f}}) where {T,f} = widen(oneunit(T)) << f
rawone_noerr(::Type{N}) where N<:Normed = rawone(N)
rawone_noerr(x::FixedPoint) = rawone_noerr(typeof(x))
Base.promote_rule(::Type{SimpleRatio{S}}, ::Type{<:FixedPoint{T}}) where {S<:Integer,T<:Integer} = SimpleRatio{promote_type(S, T)}
SimpleRatio{S}(x::FixedPoint) where S<:Integer = SimpleRatio{S}(reinterpret(x), rawone_noerr(x))
SimpleRatio(x::FixedPoint) = SimpleRatio(reinterpret(x), rawone_noerr(x))
Base.convert(::Type{S}, x::FixedPoint) where S<:SimpleRatio = S(x)
include("../ext/RatiosFixedPointNumbersExt.jl")
end
end
end

end