From a166f61180880971b5b916604a74ee98955bf385 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Mon, 13 Dec 2021 14:46:38 +0530 Subject: Implement human units conversion in terms of log1024. This generalizes better and is mathematically cleaner. * dump.scm (floor-log1024): New function. (human-units): Use floor-log1024. --- dump.scm | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/dump.scm b/dump.scm index 2e9493f..57c9001 100755 --- a/dump.scm +++ b/dump.scm @@ -544,17 +544,22 @@ case-insensitive." (string-length suffix))) str)) +(define (floor-log1024 x) + "Return the floor of the base 1024 logarithm of X." + (if (< x 1024) + 0 + (1+ (floor-log1024 (/ x 1024))))) + (define (human-units bytes) "Return number of BYTES as a string with human-readable units." - (cond - ((< bytes 1024) - (format #f "~a B" bytes)) - ((< bytes (expt 1024 2)) - (format #f "~a KiB" (round-quotient bytes 1024))) - ((< bytes (expt 1024 3)) - (format #f "~a MiB" (round-quotient bytes (expt 1024 2)))) - (else - (format #f "~a GiB" (round-quotient bytes (expt 1024 3)))))) + (let* ((integer-log (floor-log1024 bytes))) + (format #f "~a ~a" + (round-quotient bytes (expt 1024 (min integer-log 3))) + (case integer-log + ((0) "B") + ((1) "KiB") + ((2) "MiB") + (else "GiB"))))) ;; This wrapper function is necessary to work around bugs in (ccwl ;; graphviz) whereby backslashes in node labels are escaped as \\, and -- cgit v1.2.3