@@ -29,26 +29,25 @@ denom(x::FixedRational) = denom(typeof(x))
29
29
# Otherwise, we would have type instability.
30
30
val_denom (:: Type{<:F} ) where {F<: FixedRational } = Val (_denom (F))
31
31
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
34
33
35
34
const DEFAULT_NUMERATOR_TYPE = Int32
36
35
const DEFAULT_DENOM = DEFAULT_NUMERATOR_TYPE (2 ^ 4 * 3 ^ 2 * 5 ^ 2 * 7 )
37
36
38
37
(:: 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))
42
41
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))
47
46
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))
49
48
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))
52
51
53
52
for comp in (:(== ), :isequal , :< , :(isless), :<= )
54
53
@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)
59
58
Base. isinteger (x:: F ) where {F<: FixedRational } = iszero (x. num % denom (F))
60
59
61
60
Rational {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)
63
62
(:: Type{AF} )(x:: F ) where {AF<: AbstractFloat ,F<: FixedRational } = convert (AF, x. num) / convert (AF, denom (F))
64
63
(:: Type{I} )(x:: F ) where {I<: Integer ,F<: FixedRational } =
65
64
let
@@ -79,13 +78,13 @@ Base.decompose(x::F) where {T,F<:FixedRational{T}} = (x.num, zero(T), denom(F))
79
78
function Base. promote_rule (:: Type{F1} , :: Type{F2} ) where {F1<: FixedRational ,F2<: FixedRational }
80
79
_denom (F1) == _denom (F2) ||
81
80
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)}
83
82
end
84
83
function 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)}
86
85
end
87
86
function 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)}
89
88
end
90
89
91
90
# We want to consume integers
98
97
99
98
# Promotion with general types promotes like a rational
100
99
function 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)
102
101
end
103
102
function 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)
105
104
end
106
105
107
106
Base. string (x:: FixedRational ) =
108
107
let
109
- isinteger (x) && return string (convert (num_type (x), x))
108
+ isinteger (x) && return string (convert (eltype (x), x))
110
109
g = gcd (x. num, denom (x))
111
110
return string (div (x. num, g)) * " //" * string (div (denom (x), g))
112
111
end
113
112
Base. show (io:: IO , x:: FixedRational ) = print (io, string (x))
114
113
115
114
tryrationalize (:: Type{F} , x:: F ) where {F<: FixedRational } = x
116
115
tryrationalize (:: 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))
118
117
119
118
# Fix method ambiguities
120
119
Base. round (:: Type{T} , x:: F , r:: RoundingMode = RoundNearest) where {T>: Missing , F<: FixedRational } = round (Base. nonmissingtype_checked (T), x, r)
0 commit comments