From 7049a8bc449f0347ed6fb3fbfe80cd1df6f9ed2a Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Fri, 15 Jan 2016 22:09:24 +0000 Subject: [PATCH] Fix rounding of negative numbers (#760709) Utils::round() was doing +0.5 then truncate. Correct for positive values. Wrong for negative values. E.G. Utils::round(-1.4) = trunc(-1.4 + 0.5) = trunc(-0.9) = 0 Round of -1.4 is definitely not 0. Fix this for negative values by subtracting 0.5 then truncating. Reference: How can I convert a floating-point value to an integer in C? https://www.cs.tut.fi/~jkorpela/round.html Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific copy methods --- src/Utils.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Utils.cc b/src/Utils.cc index c4707c9e..5c22d7da 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -44,7 +44,13 @@ const Glib::ustring DEV_MAPPER_PATH = "/dev/mapper/"; Sector Utils::round( double double_value ) { - return static_cast( double_value + 0.5 ) ; + // Reference: + // How can I convert a floating-point value to an integer in C? + // https://www.cs.tut.fi/~jkorpela/round.html + if ( double_value >= 0.0 ) + return static_cast( double_value + 0.5 ); + else + return static_cast( double_value - 0.5 ); } Gtk::Label * Utils::mk_label( const Glib::ustring & text