@@ -29,26 +29,25 @@ denom(x::FixedRational) = denom(typeof(x))
2929# Otherwise, we would have type instability.
3030val_denom (:: Type{<:F} ) where {F<: FixedRational } = Val (_denom (F))
3131
32- num_type (:: Type{<:FixedRational{T}} ) where {T} = T
33- num_type (x:: FixedRational ) = num_type (typeof (x))
32+ Base. eltype (:: Type{<:FixedRational{T}} ) where {T} = T
3433
3534const DEFAULT_NUMERATOR_TYPE = Int32
3635const DEFAULT_DENOM = DEFAULT_NUMERATOR_TYPE (2 ^ 4 * 3 ^ 2 * 5 ^ 2 * 7 )
3736
3837(:: Type{F} )(x:: F ) where {F<: FixedRational } = x
39- (:: Type{F} )(x:: F2 ) where {T,T2,den,F<: FixedRational{T,den} ,F2<: FixedRational{T2,den} } = unsafe_fixed_rational (x. num, num_type (F), val_denom (F))
40- (:: Type{F} )(x:: Integer ) where {F<: FixedRational } = unsafe_fixed_rational (x * denom (F), num_type (F), val_denom (F))
41- (:: Type{F} )(x:: Rational ) where {F<: FixedRational } = unsafe_fixed_rational (widemul (x. num, denom (F)) ÷ x. den, num_type (F), val_denom (F))
38+ (:: Type{F} )(x:: F2 ) where {T,T2,den,F<: FixedRational{T,den} ,F2<: FixedRational{T2,den} } = unsafe_fixed_rational (x. num, eltype (F), val_denom (F))
39+ (:: Type{F} )(x:: Integer ) where {F<: FixedRational } = unsafe_fixed_rational (x * denom (F), eltype (F), val_denom (F))
40+ (:: Type{F} )(x:: Rational ) where {F<: FixedRational } = unsafe_fixed_rational (widemul (x. num, denom (F)) ÷ x. den, eltype (F), val_denom (F))
4241
43- Base.:* (l:: F , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (widemul (l. num, r. num) ÷ denom (F), num_type (F), val_denom (F))
44- Base.:+ (l:: F , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (l. num + r. num, num_type (F), val_denom (F))
45- Base.:- (l:: F , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (l. num - r. num, num_type (F), val_denom (F))
46- Base.:- (x:: F ) where {F<: FixedRational } = unsafe_fixed_rational (- x. num, num_type (F), val_denom (F))
42+ Base.:* (l:: F , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (widemul (l. num, r. num) ÷ denom (F), eltype (F), val_denom (F))
43+ Base.:+ (l:: F , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (l. num + r. num, eltype (F), val_denom (F))
44+ Base.:- (l:: F , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (l. num - r. num, eltype (F), val_denom (F))
45+ Base.:- (x:: F ) where {F<: FixedRational } = unsafe_fixed_rational (- x. num, eltype (F), val_denom (F))
4746
48- Base. inv (x:: F ) where {F<: FixedRational } = unsafe_fixed_rational (widemul (denom (F), denom (F)) ÷ x. num, num_type (F), val_denom (F))
47+ Base. inv (x:: F ) where {F<: FixedRational } = unsafe_fixed_rational (widemul (denom (F), denom (F)) ÷ x. num, eltype (F), val_denom (F))
4948
50- Base.:* (l:: F , r:: Integer ) where {F<: FixedRational } = unsafe_fixed_rational (l. num * r, num_type (F), val_denom (F))
51- Base.:* (l:: Integer , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (l * r. num, num_type (F), val_denom (F))
49+ Base.:* (l:: F , r:: Integer ) where {F<: FixedRational } = unsafe_fixed_rational (l. num * r, eltype (F), val_denom (F))
50+ Base.:* (l:: Integer , r:: F ) where {F<: FixedRational } = unsafe_fixed_rational (l * r. num, eltype (F), val_denom (F))
5251
5352for comp in (:(== ), :isequal , :< , :(isless), :<= )
5453 @eval Base.$ comp (x:: F , y:: F ) where {F<: FixedRational } = $ comp (x. num, y. num)
@@ -59,7 +58,7 @@ Base.isone(x::F) where {F<:FixedRational} = x.num == denom(F)
5958Base. isinteger (x:: F ) where {F<: FixedRational } = iszero (x. num % denom (F))
6059
6160Rational {R} (x:: F ) where {R,F<: FixedRational } = Rational {R} (x. num, denom (F))
62- Rational (x:: F ) where {F<: FixedRational } = Rational {num_type (F)} (x)
61+ Rational (x:: F ) where {F<: FixedRational } = Rational {eltype (F)} (x)
6362(:: Type{AF} )(x:: F ) where {AF<: AbstractFloat ,F<: FixedRational } = convert (AF, x. num) / convert (AF, denom (F))
6463(:: Type{I} )(x:: F ) where {I<: Integer ,F<: FixedRational } =
6564 let
@@ -79,13 +78,13 @@ Base.decompose(x::F) where {T,F<:FixedRational{T}} = (x.num, zero(T), denom(F))
7978function Base. promote_rule (:: Type{F1} , :: Type{F2} ) where {F1<: FixedRational ,F2<: FixedRational }
8079 _denom (F1) == _denom (F2) ||
8180 error (" Refusing to promote `FixedRational` types with mixed denominators. Use `Rational` instead." )
82- return FixedRational{promote_type (num_type (F1), num_type (F2)), _denom (F1)}
81+ return FixedRational{promote_type (eltype (F1), eltype (F2)), _denom (F1)}
8382end
8483function Base. promote_rule (:: Type{F} , :: Type{Rational{T2}} ) where {F<: FixedRational ,T2}
85- return Rational{promote_type (num_type (F),T2)}
84+ return Rational{promote_type (eltype (F),T2)}
8685end
8786function Base. promote_rule (:: Type{Rational{T2}} , :: Type{F} ) where {F<: FixedRational ,T2}
88- return Rational{promote_type (num_type (F),T2)}
87+ return Rational{promote_type (eltype (F),T2)}
8988end
9089
9190# We want to consume integers
9897
9998# Promotion with general types promotes like a rational
10099function Base. promote_rule (:: Type{T} , :: Type{T2} ) where {T2<: Real ,T<: FixedRational }
101- return promote_type (Rational{num_type (T)}, T2)
100+ return promote_type (Rational{eltype (T)}, T2)
102101end
103102function Base. promote_rule (:: Type{T2} , :: Type{T} ) where {T2<: Real ,T<: FixedRational }
104- return promote_type (Rational{num_type (T)}, T2)
103+ return promote_type (Rational{eltype (T)}, T2)
105104end
106105
107106Base. string (x:: FixedRational ) =
108107 let
109- isinteger (x) && return string (convert (num_type (x), x))
108+ isinteger (x) && return string (convert (eltype (x), x))
110109 g = gcd (x. num, denom (x))
111110 return string (div (x. num, g)) * " //" * string (div (denom (x), g))
112111 end
113112Base. show (io:: IO , x:: FixedRational ) = print (io, string (x))
114113
115114tryrationalize (:: Type{F} , x:: F ) where {F<: FixedRational } = x
116115tryrationalize (:: Type{F} , x:: Union{Rational,Integer} ) where {F<: FixedRational } = convert (F, x)
117- tryrationalize (:: Type{F} , x) where {F<: FixedRational } = unsafe_fixed_rational (round (num_type (F), x * denom (F)), num_type (F), val_denom (F))
116+ tryrationalize (:: Type{F} , x) where {F<: FixedRational } = unsafe_fixed_rational (round (eltype (F), x * denom (F)), eltype (F), val_denom (F))
118117
119118# Fix method ambiguities
120119Base. round (:: Type{T} , x:: F , r:: RoundingMode = RoundNearest) where {T>: Missing , F<: FixedRational } = round (Base. nonmissingtype_checked (T), x, r)
0 commit comments