Skip to content

Commit 3836cbb

Browse files
committed
Clean arithmetic methods up
1 parent f1ded04 commit 3836cbb

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

src/FixedPointNumbers.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ rawtype(::Type{X}) where {T, X <: FixedPoint{T}} = T
4545
# construction using the (approximate) intended value, i.e., N0f8
4646
*(x::Real, ::Type{X}) where {X<:FixedPoint} = X(x)
4747

48-
# comparison
49-
==(x::T, y::T) where {T <: FixedPoint} = x.i == y.i
50-
<(x::T, y::T) where {T <: FixedPoint} = x.i < y.i
51-
<=(x::T, y::T) where {T <: FixedPoint} = x.i <= y.i
5248
"""
5349
isapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y)))
5450
@@ -128,12 +124,17 @@ for f in (:zero, :oneunit, :one, :eps, :rawone, :rawtype, :floattype)
128124
$f(x::FixedPoint) = $f(typeof(x))
129125
end
130126
end
131-
for f in (:div, :fld, :fld1)
127+
for f in (:(==), :<, :<=, :div, :fld, :fld1)
132128
@eval begin
133129
$f(x::X, y::X) where {X <: FixedPoint} = $f(x.i, y.i)
134130
end
135131
end
136-
for f in (:rem, :mod, :mod1, :min, :max)
132+
for f in (:-, :~, :abs)
133+
@eval begin
134+
$f(x::X) where {X <: FixedPoint} = X($f(x.i), 0)
135+
end
136+
end
137+
for f in (:+, :-, :rem, :mod, :mod1, :min, :max)
137138
@eval begin
138139
$f(x::X, y::X) where {X <: FixedPoint} = X($f(x.i, y.i), 0)
139140
end

src/fixed.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ function rawone(::Type{Fixed{T,f}}) where {T, f}
4444
oneunit(T) << f
4545
end
4646

47-
# basic operators
48-
-(x::Fixed{T,f}) where {T,f} = Fixed{T,f}(-x.i,0)
49-
abs(x::Fixed{T,f}) where {T,f} = Fixed{T,f}(abs(x.i),0)
50-
51-
+(x::Fixed{T,f}, y::Fixed{T,f}) where {T,f} = Fixed{T,f}(x.i+y.i,0)
52-
-(x::Fixed{T,f}, y::Fixed{T,f}) where {T,f} = Fixed{T,f}(x.i-y.i,0)
47+
# unchecked arithmetic
5348

5449
# with truncation:
5550
#*(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(Base.widemul(x.i,y.i)>>f,0)

src/normed.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,9 @@ Base.Integer(x::Normed) = convert(Integer, x*1.0)
228228
Base.Rational{Ti}(x::Normed) where {Ti <: Integer} = convert(Ti, reinterpret(x))//convert(Ti, rawone(x))
229229
Base.Rational(x::Normed) = reinterpret(x)//rawone(x)
230230

231-
# Traits
232231
abs(x::Normed) = x
233232

234-
(-)(x::T) where {T <: Normed} = T(-reinterpret(x), 0)
235-
(~)(x::T) where {T <: Normed} = T(~reinterpret(x), 0)
236-
237-
+(x::Normed{T,f}, y::Normed{T,f}) where {T,f} = Normed{T,f}(convert(T, x.i+y.i),0)
238-
-(x::Normed{T,f}, y::Normed{T,f}) where {T,f} = Normed{T,f}(convert(T, x.i-y.i),0)
233+
# unchecked arithmetic
239234
*(x::T, y::T) where {T <: Normed} = convert(T,convert(floattype(T), x)*convert(floattype(T), y))
240235
/(x::T, y::T) where {T <: Normed} = convert(T,convert(floattype(T), x)/convert(floattype(T), y))
241236

0 commit comments

Comments
 (0)