[gparted] Fix rounding of negative numbers (#760709)



commit 7049a8bc449f0347ed6fb3fbfe80cd1df6f9ed2a
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Fri Jan 15 22:09:24 2016 +0000

    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 files changed, 7 insertions(+), 1 deletions(-)
---
diff --git a/src/Utils.cc b/src/Utils.cc
index c4707c9..5c22d7d 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<Sector>( 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<Sector>( double_value + 0.5 );
+       else
+               return static_cast<Sector>( double_value - 0.5 );
 }
 
 Gtk::Label * Utils::mk_label( const Glib::ustring & text


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]