@@ -5,13 +5,15 @@ struct Fixed{T <: Signed,f} <: FixedPoint{T, f}
55 # constructor for manipulating the representation;
66 # selected by passing an extra dummy argument
77 Fixed {T, f} (i:: Integer , _) where {T,f} = new {T, f} (i % T)
8- Fixed {T, f} (x) where {T,f} = convert (Fixed{T,f}, x)
9- Fixed {T, f} (x:: Fixed{T,f} ) where {T,f} = x
10- Fixed {T, f} (x:: AbstractChar ) where {T,f} = throw (ArgumentError (" Fixed cannot be constructed from a Char" ))
11- Fixed {T, f} (x:: Complex ) where {T,f} = Fixed {T, f} (convert (real (typeof (x)), x))
12- Fixed {T, f} (x:: Base.TwicePrecision ) where {T,f} = Fixed {T, f} (convert (Float64, x))
138end
149
10+ Fixed {T, f} (x:: AbstractChar ) where {T,f} = throw (ArgumentError (" Fixed cannot be constructed from a Char" ))
11+ Fixed {T, f} (x:: Complex ) where {T,f} = Fixed {T, f} (convert (real (typeof (x)), x))
12+ Fixed {T, f} (x:: Base.TwicePrecision ) where {T,f} = Fixed {T, f} (convert (Float64, x))
13+ Fixed {T,f} (x:: Integer ) where {T,f} = Fixed {T,f} (round (T, convert (widen1 (T),x)<< f),0 )
14+ Fixed {T,f} (x:: AbstractFloat ) where {T,f} = Fixed {T,f} (round (T, trunc (widen1 (T),x)<< f + rem (x,1 )* (one (widen1 (T))<< f)),0 )
15+ Fixed {T,f} (x:: Rational ) where {T,f} = Fixed {T,f} (x. num)/ Fixed {T,f} (x. den)
16+
1517reinterpret (:: Type{Fixed{T,f}} , x:: T ) where {T <: Signed ,f} = Fixed {T,f} (x, 0 )
1618
1719typechar (:: Type{X} ) where {X <: Fixed } = ' Q'
@@ -43,33 +45,29 @@ abs(x::Fixed{T,f}) where {T,f} = Fixed{T,f}(abs(x.i),0)
4345
4446
4547# # conversions and promotions
46- convert (:: Type{Fixed{T,f}} , x:: Integer ) where {T,f} = Fixed {T,f} (round (T, convert (widen1 (T),x)<< f),0 )
47- convert (:: Type{Fixed{T,f}} , x:: AbstractFloat ) where {T,f} = Fixed {T,f} (round (T, trunc (widen1 (T),x)<< f + rem (x,1 )* (one (widen1 (T))<< f)),0 )
48- convert (:: Type{Fixed{T,f}} , x:: Rational ) where {T,f} = Fixed {T,f} (x. num)/ Fixed {T,f} (x. den)
4948
5049rem (x:: Integer , :: Type{Fixed{T,f}} ) where {T,f} = Fixed {T,f} (rem (x,T)<< f,0 )
5150rem (x:: Real , :: Type{Fixed{T,f}} ) where {T,f} = Fixed {T,f} (rem (Integer (trunc (x)),T)<< f + rem (Integer (round (rem (x,1 )* (one (widen1 (T))<< f))),T),0 )
5251
53- # convert{T,f}(::Type{AbstractFloat}, x::Fixed{T,f}) = convert(floattype(x), x)
5452float (x:: Fixed ) = convert (floattype (x), x)
5553
56- convert ( :: Type{ BigFloat} , x:: Fixed{T,f} ) where {T,f} =
57- convert ( BigFloat, x. i>> f) + convert ( BigFloat, x. i& (one (widen1 (T))<< f - 1 ))/ convert ( BigFloat, one (widen1 (T))<< f)
58- convert (:: Type{TF} , x:: Fixed{T,f} ) where {TF <: AbstractFloat ,T,f} =
59- convert (TF, x. i>> f) + convert (TF, x. i& (one (widen1 (T))<< f - 1 ))/ convert (TF, one (widen1 (T))<< f)
54+ Base . BigFloat ( x:: Fixed{T,f} ) where {T,f} =
55+ BigFloat ( x. i>> f) + BigFloat ( x. i& (one (widen1 (T))<< f - 1 ))/ BigFloat ( one (widen1 (T))<< f)
56+ (:: Type{TF} )( x:: Fixed{T,f} ) where {TF <: AbstractFloat ,T,f} =
57+ TF ( x. i>> f) + TF ( x. i& (one (widen1 (T))<< f - 1 ))/ TF ( one (widen1 (T))<< f)
6058
61- convert ( :: Type{ Bool} , x:: Fixed{T,f} ) where {T,f} = x. i!= 0
62- function convert ( :: Type{ Integer} , x:: Fixed{T,f} ) where {T,f}
59+ Base . Bool ( x:: Fixed{T,f} ) where {T,f} = x. i!= 0
60+ function Base . Integer ( x:: Fixed{T,f} ) where {T,f}
6361 isinteger (x) || throw (InexactError ())
64- convert ( Integer, x. i>> f)
62+ Integer ( x. i>> f)
6563end
66- function convert (:: Type{TI} , x:: Fixed{T,f} ) where {TI <: Integer ,T,f}
64+ function (:: Type{TI} )( x:: Fixed{T,f} ) where {TI <: Integer ,T,f}
6765 isinteger (x) || throw (InexactError ())
68- convert (TI, x. i>> f)
66+ TI ( x. i>> f)
6967end
7068
71- convert (:: Type{TR} , x:: Fixed{T,f} ) where {TR <: Rational ,T,f} =
72- convert (TR, x. i>> f + (x. i& (1 << f- 1 ))// (one (widen1 (T))<< f))
69+ (:: Type{TR} )( x:: Fixed{T,f} ) where {TR <: Rational ,T,f} =
70+ TR ( x. i>> f + (x. i& (1 << f- 1 ))// (one (widen1 (T))<< f))
7371
7472promote_rule (ft:: Type{Fixed{T,f}} , :: Type{TI} ) where {T,f,TI <: Integer } = Fixed{T,f}
7573promote_rule (:: Type{Fixed{T,f}} , :: Type{TF} ) where {T,f,TF <: AbstractFloat } = TF
0 commit comments