From bb2e32175b833e8a9d76ed7d7aa86440fbe22fe0 Mon Sep 17 00:00:00 2001 From: qiuyesuifeng Date: Mon, 9 Nov 2015 15:39:39 +0800 Subject: [PATCH] util: tiny refactor RoundFloat function. --- util/types/helper.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/util/types/helper.go b/util/types/helper.go index eeb12b9127..b9b0817f3b 100644 --- a/util/types/helper.go +++ b/util/types/helper.go @@ -21,19 +21,11 @@ import ( // RoundFloat uses default rounding mode, see http://www.gnu.org/software/libc/manual/html_node/Rounding.html // so we will choose the even number if the result is midway between two representable value. // e.g, 1.5 -> 2, 2.5 -> 2. -func RoundFloat(val float64) float64 { - v, frac := math.Modf(val) - if val >= 0.0 { - if frac > 0.5 || (frac == 0.5 && uint64(v)%2 != 0) { - v += 1.0 - } - } else { - if frac < -0.5 || (frac == -0.5 && uint64(math.Abs(v))%2 != 0) { - v -= 1.0 - } +func RoundFloat(f float64) float64 { + if math.Remainder(f, 1.0) < 0 { + return math.Ceil(f) } - - return v + return math.Floor(f) } func getMaxFloat(flen int, decimal int) float64 {