Skip to content

Commit a4ee304

Browse files
committed
change all convert methods to constructors
1 parent 59f22cb commit a4ee304

5 files changed

Lines changed: 43 additions & 47 deletions

File tree

src/FixedPointNumbers.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ if isdefined(Base, :r_promote)
142142
Base.reducedim_initarray(A, region, oneunit(Treduce))
143143
else
144144
# Julia v0.7
145-
Base.add_sum(x::FixedPoint, y::FixedPoint) = convert(Treduce, x) + convert(Treduce, y)
145+
Base.add_sum(x::FixedPoint, y::FixedPoint) = Treduce(x) + Treduce(y)
146146
Base.reduce_empty(::typeof(Base.add_sum), ::Type{F}) where {F<:FixedPoint} = zero(Treduce)
147-
Base.reduce_first(::typeof(Base.add_sum), x::FixedPoint) = convert(Treduce, x)
148-
Base.mul_prod(x::FixedPoint, y::FixedPoint) = convert(Treduce, x) * convert(Treduce, y)
147+
Base.reduce_first(::typeof(Base.add_sum), x::FixedPoint) = Treduce(x)
148+
Base.mul_prod(x::FixedPoint, y::FixedPoint) = Treduce(x) * Treduce(y)
149149
Base.reduce_empty(::typeof(Base.mul_prod), ::Type{F}) where {F<:FixedPoint} = one(Treduce)
150-
Base.reduce_first(::typeof(Base.mul_prod), x::FixedPoint) = convert(Treduce, x)
150+
Base.reduce_first(::typeof(Base.mul_prod), x::FixedPoint) = Treduce(x)
151151
end
152152

153153

src/fixed.jl

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
138
end
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+
1517
reinterpret(::Type{Fixed{T,f}}, x::T) where {T <: Signed,f} = Fixed{T,f}(x, 0)
1618

1719
typechar(::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

5049
rem(x::Integer, ::Type{Fixed{T,f}}) where {T,f} = Fixed{T,f}(rem(x,T)<<f,0)
5150
rem(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)
5452
float(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)
6563
end
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)
6967
end
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

7472
promote_rule(ft::Type{Fixed{T,f}}, ::Type{TI}) where {T,f,TI <: Integer} = Fixed{T,f}
7573
promote_rule(::Type{Fixed{T,f}}, ::Type{TF}) where {T,f,TF <: AbstractFloat} = TF

src/normed.jl

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ struct Normed{T<:Unsigned,f} <: FixedPoint{T,f}
55
i::T
66

77
Normed{T, f}(i::Integer,_) where {T,f} = new{T, f}(i%T) # for setting by raw representation
8-
Normed{T, f}(x) where {T,f} = convert(Normed{T,f}, x)
9-
Normed{T, f}(x::Normed{T,f}) where {T,f} = x
10-
Normed{T, f}(x::AbstractChar) where {T,f} = throw(ArgumentError("Normed cannot be constructed from a Char"))
11-
Normed{T, f}(x::Complex) where {T,f} = Normed{T, f}(convert(real(typeof(x)), x))
12-
Normed{T, f}(x::Base.TwicePrecision) where {T,f} = Normed{T, f}(convert(Float64, x))
138
end
149

10+
Normed{T, f}(x::AbstractChar) where {T,f} = throw(ArgumentError("Normed cannot be constructed from a Char"))
11+
Normed{T, f}(x::Complex) where {T,f} = Normed{T, f}(convert(real(typeof(x)), x))
12+
Normed{T, f}(x::Base.TwicePrecision) where {T,f} = Normed{T, f}(convert(Float64, x))
13+
Normed{T1,f}(x::Normed{T2,f}) where {T1 <: Unsigned,T2 <: Unsigned,f} = Normed{T1,f}(convert(T1, x.i), 0)
14+
1515
typechar(::Type{X}) where {X <: Normed} = 'N'
1616
signbits(::Type{X}) where {X <: Normed} = 0
1717

@@ -38,17 +38,16 @@ one(x::Normed) = oneunit(x)
3838
rawone(v) = reinterpret(one(v))
3939

4040
# Conversions
41-
convert(::Type{U}, x::U) where {U <: Normed} = x
42-
convert(::Type{Normed{T1,f}}, x::Normed{T2,f}) where {T1 <: Unsigned,T2 <: Unsigned,f} = Normed{T1,f}(convert(T1, x.i), 0)
43-
function convert(::Type{Normed{T,f}}, x::Normed{T2}) where {T <: Unsigned,T2 <: Unsigned,f}
41+
function Normed{T,f}(x::Normed{T2}) where {T <: Unsigned,T2 <: Unsigned,f}
4442
U = Normed{T,f}
4543
y = round((rawone(U)/rawone(x))*reinterpret(x))
4644
(0 <= y) & (y <= typemax(T)) || throw_converterror(U, x)
4745
reinterpret(U, _unsafe_trunc(T, y))
4846
end
49-
convert(::Type{U}, x::Real) where {U <: Normed} = _convert(U, rawtype(U), x)
47+
N0f16(x::N0f8) = reinterpret(N0f16, convert(UInt16, 0x0101*reinterpret(x)))
48+
49+
(::Type{U})(x::Real) where {U <: Normed} = _convert(U, rawtype(U), x)
5050

51-
convert(::Type{N0f16}, x::N0f8) = reinterpret(N0f16, convert(UInt16, 0x0101*reinterpret(x)))
5251
function _convert(::Type{U}, ::Type{T}, x) where {U <: Normed,T}
5352
y = round(widen1(rawone(U))*x)
5453
(0 <= y) & (y <= typemax(T)) || throw_converterror(U, x)
@@ -61,7 +60,7 @@ _convert(::Type{U}, ::Type{UInt128}, x::Float16) where {U <: Normed} =
6160
_convert(U, UInt128, Float32(x))
6261
function _convert(::Type{U}, ::Type{UInt128}, x) where {U <: Normed}
6362
y = round(rawone(U)*x) # for UInt128, we can't widen
64-
(0 <= y) & (y <= typemax(UInt128)) & (x <= convert(Float64, typemax(U))) || throw_converterror(U, x)
63+
(0 <= y) & (y <= typemax(UInt128)) & (x <= Float64(typemax(U))) || throw_converterror(U, x)
6564
U(_unsafe_trunc(UInt128, y), 0)
6665
end
6766

@@ -70,19 +69,18 @@ rem(x::Normed, ::Type{T}) where {T <: Normed} = reinterpret(T, _unsafe_trunc(raw
7069
rem(x::Real, ::Type{T}) where {T <: Normed} = reinterpret(T, _unsafe_trunc(rawtype(T), round(rawone(T)*x)))
7170
rem(x::Float16, ::Type{T}) where {T <: Normed} = rem(Float32(x), T) # avoid overflow
7271

73-
# convert(::Type{AbstractFloat}, x::Normed) = convert(floattype(x), x)
7472
float(x::Normed) = convert(floattype(x), x)
7573

76-
convert(::Type{BigFloat}, x::Normed) = reinterpret(x)*(1/BigFloat(rawone(x)))
77-
function convert(::Type{T}, x::Normed) where {T <: AbstractFloat}
74+
Base.BigFloat(x::Normed) = reinterpret(x)*(1/BigFloat(rawone(x)))
75+
function (::Type{T})(x::Normed) where {T <: AbstractFloat}
7876
y = reinterpret(x)*(one(rawtype(x))/convert(T, rawone(x)))
7977
convert(T, y) # needed for types like Float16 which promote arithmetic to Float32
8078
end
81-
convert(::Type{Bool}, x::Normed) = x == zero(x) ? false : true
82-
convert(::Type{Integer}, x::Normed) = convert(Integer, x*1.0)
83-
convert(::Type{T}, x::Normed) where {T <: Integer} = convert(T, x*(1/oneunit(T)))
84-
convert(::Type{Rational{Ti}}, x::Normed) where {Ti <: Integer} = convert(Ti, reinterpret(x))//convert(Ti, rawone(x))
85-
convert(::Type{Rational}, x::Normed) = reinterpret(x)//rawone(x)
79+
Base.Bool(x::Normed) = x == zero(x) ? false : true
80+
Base.Integer(x::Normed) = convert(Integer, x*1.0)
81+
(::Type{T})(x::Normed) where {T <: Integer} = convert(T, x*(1/oneunit(T)))
82+
Base.Rational{Ti}(x::Normed) where {Ti <: Integer} = convert(Ti, reinterpret(x))//convert(Ti, rawone(x))
83+
Base.Rational(x::Normed) = reinterpret(x)//rawone(x)
8684

8785
# Traits
8886
abs(x::Normed) = x

test/fixed.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ end
9292
@testset "reductions" begin
9393
F8 = Fixed{Int8,8}
9494
a = F8[0.498, 0.1]
95-
acmp = convert(Float64, a[1]) + convert(Float64, a[2])
95+
acmp = Float64(a[1]) + Float64(a[2])
9696
@test sum(a) == acmp
9797
@test sum(a, dims=1) == [acmp]
9898

9999
F6 = Fixed{Int8,6}
100100
a = F6[1.2, 1.4]
101-
acmp = convert(Float64, a[1])*convert(Float64, a[2])
101+
acmp = Float64(a[1])*Float64(a[2])
102102
@test prod(a) == acmp
103103
@test prod(a, dims=1) == [acmp]
104104
end
@@ -162,7 +162,7 @@ for (T, f) in ((Int8, 7),
162162
tmax = typemax(Fixed{T, f})
163163
@test tmax == BigInt(typemax(T)) / BigInt(2)^f
164164
tol = (tmax + BigFloat(1.0)) / (sizeof(T) * 8)
165-
for x in range(-1, stop=convert(BigFloat, tmax)-tol, length=50)
165+
for x in range(-1, stop=BigFloat(tmax)-tol, length=50)
166166
@test abs(Fixed{T, f}(x) - x) <= tol
167167
end
168168
end

test/normed.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ a = N0f8[reinterpret(N0f8, 0xff), reinterpret(N0f8, 0xff)]
307307
@test sum(a, dims=1) == [2.0]
308308

309309
a = N2f14[3.2, 2.4]
310-
acmp = convert(Float64, a[1])*convert(Float64, a[2])
310+
acmp = Float64(a[1])*Float64(a[2])
311311
@test prod(a) == acmp
312312
@test prod(a, dims=1) == [acmp]
313313
end

0 commit comments

Comments
 (0)