Skip to content

Add support for digits keyword argument of round() #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,35 @@ end

signbit(x::X) where {X <: FixedPoint} = signbit(x.i)

~(x::X) where {X <: FixedPoint} = X(~x.i, 0)

function round(x::FixedPoint, r::RoundingMode=RoundNearest;
digits::Union{Nothing, Integer}=nothing,
sigdigits::Union{Nothing, Integer}=nothing,
base::Union{Nothing, Integer}=nothing)

d = digits === nothing ? 0 : Int(digits)

if base !== nothing && base != 10
throw(ArgumentError("`base` numbers other than 10 are not supported."))
elseif sigdigits !== nothing
throw(ArgumentError("`sigdigits` is not supported."))
elseif d < 0
throw(ArgumentError("negative `digits` is not supported."))
end
d === 0 ? _round_digits0(x, r) : _round_digits(x, r, d)
end
function _round_digits(x::X, r::RoundingMode, d::Int) where {T, f, X <: FixedPoint{T,f}}
log10_2 = 0.3010299956639812
d > floor(Int, ((f + 1) * log10_2)) && return x
r = round(float(x), r, digits=d)
typemin(X) - eps(X)/2 <= r < typemax(X) + eps(X)/2 || throw_converterror(X, r)
clamp(r, X)
end

trunc(x::X) where {X <: FixedPoint{<:Unsigned}} = floor(x)
trunc(::Type{Ti}, x::X) where {X <: FixedPoint{<:Unsigned}, Ti <: Integer} = floor(Ti, x)

for f in (:zero, :oneunit, :one, :eps, :rawone, :rawtype, :floattype)
@eval begin
$f(x::FixedPoint) = $f(typeof(x))
Expand All @@ -400,11 +429,6 @@ for f in (:(==), :<, :<=, :fld1)
$f(x::X, y::X) where {X <: FixedPoint} = $f(x.i, y.i)
end
end
for f in (:~, )
@eval begin
$f(x::X) where {X <: FixedPoint} = X($f(x.i), 0)
end
end
for f in (:mod1, :min, :max)
@eval begin
$f(x::X, y::X) where {X <: FixedPoint} = X($f(x.i, y.i), 0)
Expand All @@ -415,7 +439,7 @@ for (m, f) in ((:(:Nearest), :round),
(:(:Up), :ceil),
(:(:Down), :floor))
@eval begin
round(x::FixedPoint, ::RoundingMode{$m}) = $f(x)
_round_digits0(x::FixedPoint, ::RoundingMode{$m}) = $f(x)
round(::Type{Ti}, x::FixedPoint, ::RoundingMode{$m}) where {Ti <: Integer} = $f(Ti, x)
end
end
Expand Down
21 changes: 21 additions & 0 deletions src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,27 @@ function round(x::Fixed{T,f}) where {T, f}
z = y & intmask(x)
reinterpret(Fixed{T,f}, z - T(y & m == rawone(x)) << f)
end
function _round_digits(x::F, r::RoundingMode, d::Int) where {f, F <: Fixed{Int8, f}}
xd = x.i * Int16(d === 1 ? 10 : 100)
uf = UInt8(f)
if r isa Union{RoundingMode{:Down}, RoundingMode{:ToZero}}
t = r isa RoundingMode{:ToZero} && x.i < 0 ? fracmask(x) : Int16(0)
xr = (xd + t) >> uf
else
xr = r isa RoundingMode{:Up} ? (xd + fracmask(x)) >> uf : div_2f(xd, Val(Int(f)))
if d < 3 && xr == (Int16(d === 1 ? 10 : 100) << (0x7 - uf))
throw_converterror(F, @exp2(7 - f))
end
end
h8, h24 = Int16(0x80), Int32(1 << 23)
if d === 1
f >= 5 && return F(((xr * Int16(6553) >> (0x8 - uf) + h8) >> 0x8) % Int8, 0)
f >= 3 && return F(((xr * Int32(26843545) >> (0x4 - uf) + h24) >> 0x18) % Int8, 0)
elseif d === 2
return F(((xr * Int32(42949672) >> (0x8 - uf) + h24) >> 0x18) % Int8, 0)
end
return x
end

function trunc(::Type{Ti}, x::Fixed{T,f}) where {Ti <: Integer, T, f}
f == 0 && return convert(Ti, x.i)
Expand Down
21 changes: 19 additions & 2 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ end


# Functions
trunc(x::N) where {N <: Normed} = floor(x)
floor(x::N) where {N <: Normed} = reinterpret(N, x.i - x.i % rawone(N))
function ceil(x::Normed{T,f}) where {T, f}
f == 1 && return x
Expand All @@ -310,8 +309,26 @@ function round(x::Normed{T,f}) where {T, f}
q = rawone(x) - r
reinterpret(Normed{T,f}, r > q ? x.i + q : x.i - r)
end
function _round_digits(x::N0f8, r::RoundingMode, d::Int)
t(::RoundingMode{:Nearest}) = 0x0081
t(::RoundingMode{:ToZero}) = 0x0003
t(::RoundingMode{:Up}) = 0x00ff
t(::RoundingMode{:Down}) = 0x0003
xd = x.i * (d === 1 ? 0x000a : 0x0064)
if d === 1
x10 = xd + (x.i >> 0x5) + t(r)
y = (x10 >> 0x8) % UInt8
return N0f8(y * 0x19 + (y + 0x3) >> 0x2 + y >> 0x2, 0)
elseif d === 2
x100 = xd + ((xd + x.i) >> 0x8) + t(r)
y = (x100 >> 0x8) % UInt8
z = ((y * 0x233 + 0x212) >> 0xa) % UInt8
return N0f8(z + y + y - (y === 0x1e) - (y === 0x46), 0)
else
return x
end
end

trunc(::Type{Ti}, x::Normed) where {Ti <: Integer} = floor(Ti, x)
function floor(::Type{Ti}, x::Normed) where {Ti <: Integer}
convert(Ti, reinterpret(x) ÷ rawone(x))
end
Expand Down
39 changes: 39 additions & 0 deletions test/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,45 @@ function test_fld1_mod1(TX::Type)
end
end

function test_round_digits(TX::Type)
function check(x, r, d)
X = typeof(x)
dec = round(Int, float(rationalize(x) * 10^d), r) // (10^d)
if dec >= typemax(X) + eps(X)/2
try
round(x, r, digits=d)
catch
return true
end
return false
end
actual, expected = round(x, r, digits=d), float(dec) % X
actual === expected && return true
(actual - dec) == (dec - expected) && return true
error(actual, " != ", expected)
end
for X in target(TX, :i8)
xs = typemin(X):eps(X):typemax(X)
modes = (RoundNearest, RoundToZero, RoundUp, RoundDown)
@testset "round(::$X, $r, digits=x)" for r in modes
@test all(x -> check(x, r, 1), xs)
@test all(x -> check(x, r, 2), xs)
@test all(x -> check(x, r, 3), xs)
end
end
@testset "round(::$X, r, digits=x)" for X in target(TX; ex = :thin)
@test round(X(0.462), digits=6) ≈ X(0.462) atol=1e-6
@test trunc(X(0.462), digits=2) ≈ X(0.46) atol=1e-6
@test ceil(X(0.462), digits=1) ≈ X(0.5) atol=1e-6
@test floor(X(0.462), digits=0) ≈ X(0) atol=1e-6
end
for X in target(TX, :i8; ex = :thin)
@test_throws ArgumentError round(eps(X), digits=-1)
@test_throws ArgumentError round(eps(X), sigdigits=2)
@test_throws ArgumentError round(eps(X), base=2)
end
end

function test_isapprox(TX::Type)
@testset "approx $X" for X in target(TX, :i8, :i16; ex = :light)
xs = typemin(X):eps(X):typemax(X)-eps(X)
Expand Down
3 changes: 3 additions & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ end
@test round(Int, -1.5Q1f6, RoundUp) === -1
@test round(Int, -1.5Q1f6, RoundDown) === -2
end
@testset "rounding with digits" begin
test_round_digits(Fixed)
end
@test_throws InexactError trunc(UInt, typemin(Q0f7))
@test_throws InexactError floor(UInt, -eps(Q0f7))
end
Expand Down
3 changes: 3 additions & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ end
@test round(Int, 1.504N1f7, RoundUp) === 2
@test round(Int, 1.504N1f7, RoundDown) === 1
end
@testset "rounding with digits" begin
test_round_digits(Normed)
end
end

@testset "approx" begin
Expand Down