From 8555fa333e46aea194b9c9c7f1b2a132e99c0527 Mon Sep 17 00:00:00 2001 From: Milan Date: Thu, 7 Apr 2022 14:09:50 +0100 Subject: [PATCH] fix: no rounding for max keepbits --- Project.toml | 2 +- src/round_nearest.jl | 8 ++++++-- test/round_nearest.jl | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index cee9b3d..6a87974 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BitInformation" uuid = "de688a37-743e-4ac2-a6f0-bd62414d1aa7" authors = ["Milan and contributors"] -version = "0.5.0" +version = "0.5.1" [deps] Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" diff --git a/src/round_nearest.jl b/src/round_nearest.jl index eb62a54..0488846 100644 --- a/src/round_nearest.jl +++ b/src/round_nearest.jl @@ -1,8 +1,12 @@ """Shift integer to push the mantissa in the right position. Used to determine round up or down in the tie case. `keepbits` is the number of mantissa bits to -be kept (i.e. not zero-ed) after rounding.""" +be kept (i.e. not zero-ed) after rounding. Special case: shift is -1 for +keepbits == significand_bits(T) to avoid a round away from 0 where no rounding +should be applied.""" function get_shift(::Type{T},keepbits::Integer) where {T<:Base.IEEEFloat} - return Base.significand_bits(T) - keepbits + shift = Base.significand_bits(T) - keepbits # normal case + shift -= keepbits == Base.significand_bits(T) # to avoid round away from 0 + return shift end """Returns for a Float-type `T` and `keepbits`, the number of mantissa bits to be diff --git a/test/round_nearest.jl b/test/round_nearest.jl index ac5f189..3c8fd80 100644 --- a/test/round_nearest.jl +++ b/test/round_nearest.jl @@ -55,7 +55,7 @@ end @testset "No rounding for keepbits=10,23,52" begin for (T,k) in zip([Float16,Float32,Float64], - [11,24,53]) + [10,23,52]) A = rand(T,200,300) Ar = round(A,k) @test A == Ar