Attached is a better patch. Seems that when you are editing the commit
message, you cannot correct the patch anymore...

The change from the patch is formatting and using 512 instead of 500,
as per the commit message.

- Adam

-- 
Adam Majer
ad...@zombino.com
>From e689bf73dcc677142f24fe96c3fa6dc703346fbc Mon Sep 17 00:00:00 2001
From: Adam Majer <ad...@zombino.com>
Date: Sat, 6 Nov 2010 20:08:38 -0500
Subject: [PATCH] Fix a broken human-readable unit calculations

Statistics::getUnitFactor and Statistics::getUnitString were not
consistent and difficult to understand. The readout displayed
incorrect values like 0.9kBps while the actual rate was 0.9MBps.

The current function will switch to next-higher rate value when rate
exceeds 512.0 units/s.
---
 src/statistics.cpp |   29 +++++++++++++++++------------
 1 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/src/statistics.cpp b/src/statistics.cpp
index a39e33c..aff28fe 100644
--- a/src/statistics.cpp
+++ b/src/statistics.cpp
@@ -81,13 +81,12 @@ float Statistics::getUnitFactor(dataUnit unit, long long value)
     {
         case humanReadableBit:
         case humanReadableByte:
-            factor *= 1024 * 1024 * 1024;
-            for(int i = 3; i >= 0; --i)
+            for(int i = 0; i < 3; i++)
             {
-                if(value * (unit % 2 == 0 ? 8 : 1) >= factor)
+                if(value / factor < 512.0)
                     return factor;
 
-                factor /= 1024;
+                factor *= 1024.0;
             }
             return factor;
         case bit:
@@ -114,14 +113,20 @@ string Statistics::getUnitString(dataUnit unit, long long value)
     switch(unit)
     {
         case humanReadableBit:
-        case humanReadableByte:
-            if(value >= 1024 * 1024 * 1024 / (unit % 2 == 0 ? 8 : 1))
-                return 'G' + description;
-            if(value >= 1024 * 1024 / (unit % 2 == 0 ? 8 : 1))
-                return 'M' + description;
-            if(value >= 1024 / (unit % 2 == 0 ? 8 : 1))
-                return 'k' + description;
-            return description;
+        case humanReadableByte: 
+        {
+            const string units[4] = { "", "k", "M", "G" };
+
+            value *= (unit % 2 == 0 ? 8 : 1);
+            for(int i=0; i<4; i++){
+                if(value < 512)
+                    return units[i] + description;
+
+                value /= 1024;
+            }
+
+            return units[3] + description; // return 'G'units/s
+        }
         case bit:
         case byte:
             return description;
-- 
1.7.1

Reply via email to