unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Steve Sprang <steve.sprang@gmail.com>
To: guix-devel@gnu.org
Subject: [PATCH] build: Fix potential type error when generating human-friendly byte count strings.
Date: Wed, 9 Sep 2015 14:05:35 -0700	[thread overview]
Message-ID: <CA+xn8YCMk5tJjB_rEFEPwWm4ef3enCuRJ03PR1f8bH8TvGrUwQ@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 199 bytes --]

This is a follow up tweak to my previous "progress bar" patch. With a
really slow throughput it's possible to get fractional sub-KiB byte counts,
so I added some additional number massaging.

-Steve

[-- Attachment #1.2: Type: text/html, Size: 255 bytes --]

[-- Attachment #2: type-fix.patch --]
[-- Type: text/x-patch, Size: 1972 bytes --]

From daccd58e5fbe366ebcd33092f70ca45bc79285e0 Mon Sep 17 00:00:00 2001
From: Steve Sprang <scs@stevesprang.com>
Date: Wed, 9 Sep 2015 13:59:52 -0700
Subject: [PATCH] build: Fix potential type error when generating
 human-friendly byte count strings.

* guix/build/download.scm (number->integer): New function.
  (seconds->string): Use new rounding function.
  (byte-count->string):  Use new rounding function.
---
 guix/build/download.scm | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/guix/build/download.scm b/guix/build/download.scm
index 6e85174..a0b8d92 100644
--- a/guix/build/download.scm
+++ b/guix/build/download.scm
@@ -49,6 +49,10 @@
   ;; Size of the HTTP receive buffer.
   65536)
 
+(define (number->integer n)
+  "Given an arbitrary number N, round it and return the exact result."
+  (inexact->exact (round n)))
+
 (define (duration->seconds duration)
   "Return the number of seconds represented by DURATION, a 'time-duration'
 object, as an inexact number."
@@ -60,7 +64,7 @@ object, as an inexact number."
 format."
   (if (not (number? duration))
       "00:00:00"
-      (let* ((total-seconds (inexact->exact (round duration)))
+      (let* ((total-seconds (number->integer duration))
              (extra-seconds (modulo total-seconds 3600))
              (hours         (quotient total-seconds 3600))
              (mins          (quotient extra-seconds 60))
@@ -75,8 +79,8 @@ way."
         (GiB (expt 1024. 3))
         (TiB (expt 1024. 4)))
     (cond
-     ((< size KiB) (format #f "~dB" (inexact->exact size)))
-     ((< size MiB) (format #f "~dKiB" (inexact->exact (round (/ size KiB)))))
+     ((< size KiB) (format #f "~dB"     (number->integer size)))
+     ((< size MiB) (format #f "~dKiB"   (number->integer (/ size KiB))))
      ((< size GiB) (format #f "~,1fMiB" (/ size MiB)))
      ((< size TiB) (format #f "~,2fGiB" (/ size GiB)))
      (else         (format #f "~,3fTiB" (/ size TiB))))))
-- 
2.5.0


             reply	other threads:[~2015-09-09 21:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-09 21:05 Steve Sprang [this message]
2015-09-10  2:42 ` [PATCH] build: Fix potential type error when generating human-friendly byte count strings Mark H Weaver
2015-09-10  3:31   ` Steve Sprang
2015-09-15  3:27     ` Steve Sprang
2015-09-15  3:40       ` Mark H Weaver

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+xn8YCMk5tJjB_rEFEPwWm4ef3enCuRJ03PR1f8bH8TvGrUwQ@mail.gmail.com \
    --to=steve.sprang@gmail.com \
    --cc=guix-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).