55
55
Base. iszero (x:: FixedRational ) = iszero (x. num)
56
56
Base. isone (x:: F ) where {F<: FixedRational } = x. num == denom (F)
57
57
Base. isinteger (x:: F ) where {F<: FixedRational } = iszero (x. num % denom (F))
58
- Base. convert (:: Type{F} , x:: Integer ) where {F<: FixedRational } = unsafe_fixed_rational (x * denom (F), eltype (F), val_denom (F))
59
- Base. convert (:: Type{F} , x:: Rational ) where {F<: FixedRational } = F (x)
60
- Base. convert (:: Type{Rational{R}} , x:: F ) where {R,F<: FixedRational } = Rational {R} (x. num, denom (F))
61
- Base. convert (:: Type{Rational} , x:: F ) where {F<: FixedRational } = Rational {eltype(F)} (x. num, denom (F))
62
- Base. convert (:: Type{AF} , x:: F ) where {AF<: AbstractFloat ,F<: FixedRational } = convert (AF, x. num) / convert (AF, denom (F))
63
- Base. convert (:: Type{I} , x:: F ) where {I<: Integer ,F<: FixedRational } =
58
+
59
+ Rational {R} (x:: F ) where {R,F<: FixedRational } = Rational {R} (x. num, denom (F))
60
+ Rational (x:: F ) where {F<: FixedRational } = Rational {eltype(F)} (x)
61
+ (:: Type{AF} )(x:: F ) where {AF<: AbstractFloat ,F<: FixedRational } = convert (AF, x. num) / convert (AF, denom (F))
62
+ (:: Type{I} )(x:: F ) where {I<: Integer ,F<: FixedRational } =
64
63
let
65
64
isinteger (x) || throw (InexactError (:convert , I, x))
66
65
convert (I, div (x. num, denom (F)))
67
66
end
67
+ (:: Type{Bool} )(x:: F ) where {F<: FixedRational } =
68
+ let
69
+ iszero (x) || isone (x) || throw (InexactError (:convert , Bool, x))
70
+ return x. num == denom (F)
71
+ end
72
+
68
73
Base. round (:: Type{T} , x:: F , r:: RoundingMode = RoundNearest) where {T,F<: FixedRational } = div (convert (T, x. num), convert (T, denom (F)), r)
69
74
Base. decompose (x:: F ) where {T,F<: FixedRational{T} } = (x. num, zero (T), denom (F))
70
75
78
83
function Base. promote_rule (:: Type{<:FixedRational{T1}} , :: Type{Rational{T2}} ) where {T1,T2}
79
84
return Rational{promote_type (T1,T2)}
80
85
end
81
- function Base. promote_rule (:: Type{<:FixedRational{T1}} , :: Type{T2} ) where {T1,T2}
86
+ function Base. promote_rule (:: Type{<:FixedRational{T1}} , :: Type{T2} ) where {T1,T2<: Real }
82
87
return promote_type (Rational{T1}, T2)
83
88
end
84
-
85
- # Want to consume integers:
86
- Base . promote (x :: Integer , y :: F ) where {F <: FixedRational } = ( F (x), y)
87
- Base . promote (x :: F , y :: Integer ) where {F <: FixedRational } = reverse ( promote (y, x))
89
+ function Base . promote_rule ( :: Type{F} , :: Type{<:Integer} ) where {F <: FixedRational }
90
+ # Want to consume integers:
91
+ return F
92
+ end
88
93
89
94
Base. string (x:: FixedRational ) =
90
95
let
@@ -93,7 +98,6 @@ Base.string(x::FixedRational) =
93
98
return string (div (x. num, g)) * " //" * string (div (denom (x), g))
94
99
end
95
100
Base. show (io:: IO , x:: FixedRational ) = print (io, string (x))
96
- Base. zero (:: Type{F} ) where {F<: FixedRational } = unsafe_fixed_rational (0 , eltype (F), val_denom (F))
97
101
98
102
tryrationalize (:: Type{F} , x:: F ) where {F<: FixedRational } = x
99
103
tryrationalize (:: Type{F} , x:: Union{Rational,Integer} ) where {F<: FixedRational } = convert (F, x)
0 commit comments