all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Converting an Integer into Human Readable String
@ 2011-04-07 10:02 Nordlöw
  2011-04-07 11:34 ` Deniz Dogan
                   ` (3 more replies)
  0 siblings, 4 replies; 27+ messages in thread
From: Nordlöw @ 2011-04-07 10:02 UTC (permalink / raw)
  To: help-gnu-emacs

I looking for the function that prints file size on the mode-line in size-indication-mode. I have searched for size-indication-mode in the source but cannot find a code-references to it. So where is the function that prints for example

22k

when file is approximately 22 kilobytes big?

What about difference between
- kilobytes (kB), 1000 bytes, and
- kibibytes (KiB), 1024 bytes, as defined at

Shouldn't Emacs support both?

This is of course not that hard to write but reinvent the wheel?

http://en.wikipedia.org/wiki/Kibibyte


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 10:02 Converting an Integer into Human Readable String Nordlöw
@ 2011-04-07 11:34 ` Deniz Dogan
  2011-04-07 12:04   ` Eli Zaretskii
  2011-04-07 11:52 ` Eli Zaretskii
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 27+ messages in thread
From: Deniz Dogan @ 2011-04-07 11:34 UTC (permalink / raw)
  To: gnu.emacs.help; +Cc: help-gnu-emacs, Nordlöw

2011/4/7 Nordlöw <per.nordlow@gmail.com>:
> I looking for the function that prints file size on the mode-line in size-indication-mode. I have searched for size-indication-mode in the source but cannot find a code-references to it. So where is the function that prints for example
>
> 22k
>
> when file is approximately 22 kilobytes big?
>
> What about difference between
> - kilobytes (kB), 1000 bytes, and
> - kibibytes (KiB), 1024 bytes, as defined at
>
> Shouldn't Emacs support both?
>
> This is of course not that hard to write but reinvent the wheel?
>
> http://en.wikipedia.org/wiki/Kibibyte
>

I believe what you're looking for can be found in bindings.el (L375)
and an explanation of the format string %I in the documentation of
mode-line-format. Where the actual "translation" takes place is still
a mystery to me.

-- 
Deniz Dogan



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 10:02 Converting an Integer into Human Readable String Nordlöw
  2011-04-07 11:34 ` Deniz Dogan
@ 2011-04-07 11:52 ` Eli Zaretskii
  2011-04-07 12:51 ` Drew Adams
  2011-04-08 11:41 ` Pascal J. Bourguignon
  3 siblings, 0 replies; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-07 11:52 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Nordlöw <per.nordlow@gmail.com>
> Newsgroups: gnu.emacs.help
> Date: Thu, 7 Apr 2011 03:02:52 -0700 (PDT)
> Reply-To: gnu.emacs.help@googlegroups.com
> 
> I looking for the function that prints file size on the mode-line in size-indication-mode. I have searched for size-indication-mode in the source but cannot find a code-references to it. So where is the function that prints for example
> 
> 22k
> 
> when file is approximately 22 kilobytes big?

You won't find it in Lisp.  It is implemented in C, see
xdisp.c:pint2hrstr; the display engine puts that part on the mode line
if the variable size-indication-mode is customized to non-nil.



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 11:34 ` Deniz Dogan
@ 2011-04-07 12:04   ` Eli Zaretskii
  0 siblings, 0 replies; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-07 12:04 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Deniz Dogan <deniz.a.m.dogan@gmail.com>
> Date: Thu, 7 Apr 2011 13:34:32 +0200
> Cc: help-gnu-emacs@gnu.org, Nordlöw <per.nordlow@gmail.com>
> 
> I believe what you're looking for can be found in bindings.el (L375)
> and an explanation of the format string %I in the documentation of
> mode-line-format. Where the actual "translation" takes place is still
> a mystery to me.

All the %-formats supported by the mode line can only be interpreted
in one place: the display engine, which is where the string displayed
on the mode line is built.



^ permalink raw reply	[flat|nested] 27+ messages in thread

* RE: Converting an Integer into Human Readable String
  2011-04-07 10:02 Converting an Integer into Human Readable String Nordlöw
  2011-04-07 11:34 ` Deniz Dogan
  2011-04-07 11:52 ` Eli Zaretskii
@ 2011-04-07 12:51 ` Drew Adams
  2011-04-07 17:43   ` Eli Zaretskii
  2011-04-08 11:41 ` Pascal J. Bourguignon
  3 siblings, 1 reply; 27+ messages in thread
From: Drew Adams @ 2011-04-07 12:51 UTC (permalink / raw)
  To: gnu.emacs.help, help-gnu-emacs

> I looking for the function that prints file size on the 
> mode-line in size-indication-mode. I have searched for 
> size-indication-mode in the source but cannot find a 
> code-references to it. So where is the function that prints 
> for example 22k when file is approximately 22 kilobytes big?

(format-mode-line "%I" 4060) -> "4.6k"

`C-h f format-mode-line'




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 12:51 ` Drew Adams
@ 2011-04-07 17:43   ` Eli Zaretskii
  2011-04-07 18:13     ` Drew Adams
  0 siblings, 1 reply; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-07 17:43 UTC (permalink / raw)
  To: help-gnu-emacs

> From: "Drew Adams" <drew.adams@oracle.com>
> Date: Thu, 7 Apr 2011 05:51:46 -0700
> Cc: 
> 
> > I looking for the function that prints file size on the 
> > mode-line in size-indication-mode. I have searched for 
> > size-indication-mode in the source but cannot find a 
> > code-references to it. So where is the function that prints 
> > for example 22k when file is approximately 22 kilobytes big?
> 
> (format-mode-line "%I" 4060) -> "4.6k"

Beware: this has side effects.  It doesn't just return a string.



^ permalink raw reply	[flat|nested] 27+ messages in thread

* RE: Converting an Integer into Human Readable String
  2011-04-07 17:43   ` Eli Zaretskii
@ 2011-04-07 18:13     ` Drew Adams
  2011-04-07 18:35       ` Eli Zaretskii
  2011-04-07 18:44       ` Eli Zaretskii
  0 siblings, 2 replies; 27+ messages in thread
From: Drew Adams @ 2011-04-07 18:13 UTC (permalink / raw)
  To: 'Eli Zaretskii', help-gnu-emacs

> > (format-mode-line "%I" 4060) -> "4.6k"
> 
> Beware: this has side effects.  It doesn't just return a string.

What side effects?  Where are they documented?
Or is there a doc bug wrt this missing info?

In any case, what I wrote was incorrect.  AFAICT there is no way to pass the
number that is to be interpreted as the buffer size by "%I", except by passing
an actual BUFFER of the given size as arg.  This will do it, I guess, but it
seems a bit silly:

(with-temp-buffer
 (insert (make-string 4060 ?*))
 (format-mode-line "%I" nil nil (current-buffer)))




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 18:13     ` Drew Adams
@ 2011-04-07 18:35       ` Eli Zaretskii
  2011-04-07 18:51         ` Drew Adams
  2011-04-07 18:44       ` Eli Zaretskii
  1 sibling, 1 reply; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-07 18:35 UTC (permalink / raw)
  To: help-gnu-emacs

> From: "Drew Adams" <drew.adams@oracle.com>
> Date: Thu, 7 Apr 2011 11:13:07 -0700
> 
> > > (format-mode-line "%I" 4060) -> "4.6k"
> > 
> > Beware: this has side effects.  It doesn't just return a string.
> 
> What side effects?

It affects the display.

> Where are they documented?

In the ELisp manual.



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 18:13     ` Drew Adams
  2011-04-07 18:35       ` Eli Zaretskii
@ 2011-04-07 18:44       ` Eli Zaretskii
  2011-04-07 19:01         ` Thierry Volpiatto
       [not found]         ` <mailman.17.1302204476.27822.help-gnu-emacs@gnu.org>
  1 sibling, 2 replies; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-07 18:44 UTC (permalink / raw)
  To: help-gnu-emacs

> From: "Drew Adams" <drew.adams@oracle.com>
> Date: Thu, 7 Apr 2011 11:13:07 -0700
> 
> In any case, what I wrote was incorrect.  AFAICT there is no way to pass the
> number that is to be interpreted as the buffer size by "%I", except by passing
> an actual BUFFER of the given size as arg.  This will do it, I guess, but it
> seems a bit silly:

Yes, but this isn't:

  (ls-lisp-format-file-size 4060 t) => "   4k"

Of course, on Posix platforms you will need to (require 'ls-lisp)
first, which is undesirable.  Hmm...

It would make sense to extract the workhorse of
ls-lisp-format-file-size and make it a separate function in subr.el.
Patches welcome.



^ permalink raw reply	[flat|nested] 27+ messages in thread

* RE: Converting an Integer into Human Readable String
  2011-04-07 18:35       ` Eli Zaretskii
@ 2011-04-07 18:51         ` Drew Adams
  2011-04-07 20:10           ` Eli Zaretskii
  0 siblings, 1 reply; 27+ messages in thread
From: Drew Adams @ 2011-04-07 18:51 UTC (permalink / raw)
  To: 'Eli Zaretskii', help-gnu-emacs

> > > Beware: this has side effects.  It doesn't just return a string.
> > What side effects?  Where are they documented?
> It affects the display.  In the ELisp manual.

Can you be more specific?  Do you mean node `Emulating Mode Line'?

AFAICT, the only thing it says about display happening (side effects) is in the
final Note and the example following it.  That is, the only case it mentions
(AFAICT) where display actually happens is if you pass one of "`mode-line',
`mode-line-inactive', or `header-line' as FACE".

Did you have something else in mind, or was that it?  Is this text exhaustive
wrt side effects, or is it missing something?

[FWIW - The fact that this side effect happens (only?) wrt 3 particular faces
seems like a design bug.  A priori, a function designed "to compute the text
that _would_ appear", as an emulation, has no business actually changing the
display.  But I imagine there were some good reasons for coupling the two
functionalities (emulation and real effect).]





^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 18:44       ` Eli Zaretskii
@ 2011-04-07 19:01         ` Thierry Volpiatto
       [not found]         ` <mailman.17.1302204476.27822.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 27+ messages in thread
From: Thierry Volpiatto @ 2011-04-07 19:01 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

>> From: "Drew Adams" <drew.adams@oracle.com>
>> Date: Thu, 7 Apr 2011 11:13:07 -0700
>> 
>> In any case, what I wrote was incorrect.  AFAICT there is no way to pass the
>> number that is to be interpreted as the buffer size by "%I", except by passing
>> an actual BUFFER of the given size as arg.  This will do it, I guess, but it
>> seems a bit silly:
>
> Yes, but this isn't:
>
>   (ls-lisp-format-file-size 4060 t) => "   4k"
>
> Of course, on Posix platforms you will need to (require 'ls-lisp)
> first, which is undesirable.  Hmm...
>
> It would make sense to extract the workhorse of
> ls-lisp-format-file-size and make it a separate function in subr.el.
> Patches welcome.

anything have a function for this:

(anything-ff-human-size 4060)
==> "4.0K"

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 18:51         ` Drew Adams
@ 2011-04-07 20:10           ` Eli Zaretskii
  0 siblings, 0 replies; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-07 20:10 UTC (permalink / raw)
  To: help-gnu-emacs

> From: "Drew Adams" <drew.adams@oracle.com>
> Date: Thu, 7 Apr 2011 11:51:52 -0700
> 
> > > > Beware: this has side effects.  It doesn't just return a string.
> > > What side effects?  Where are they documented?
> > It affects the display.  In the ELisp manual.
> 
> Can you be more specific?  Do you mean node `Emulating Mode Line'?

Yes.

> AFAICT, the only thing it says about display happening (side effects) is in the
> final Note and the example following it.  That is, the only case it mentions
> (AFAICT) where display actually happens is if you pass one of "`mode-line',
> `mode-line-inactive', or `header-line' as FACE".

Or t.

> Did you have something else in mind, or was that it?  Is this text exhaustive
> wrt side effects, or is it missing something?

That text says all I know about the issue.



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
       [not found]         ` <mailman.17.1302204476.27822.help-gnu-emacs@gnu.org>
@ 2011-04-08  6:35           ` Klaus Straubinger
  2011-04-08  7:07             ` Thierry Volpiatto
  0 siblings, 1 reply; 27+ messages in thread
From: Klaus Straubinger @ 2011-04-08  6:35 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> wrote:

> anything have a function for this:
>
> (anything-ff-human-size 4060)
> ==> "4.0K"

And there is url-pretty-length from url-util.el (which is part of GNU Emacs):

    (url-pretty-length 4060)
    ==> "3k"

(In this example, the result is "3k" because 4060 is smaller than
4 * 1024.)

-- 
Klaus Straubinger


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08  6:35           ` Klaus Straubinger
@ 2011-04-08  7:07             ` Thierry Volpiatto
  2011-04-08  8:29               ` Eli Zaretskii
  0 siblings, 1 reply; 27+ messages in thread
From: Thierry Volpiatto @ 2011-04-08  7:07 UTC (permalink / raw)
  To: help-gnu-emacs

Klaus Straubinger <KSNetz@UseNet.ArcorNews.DE> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> wrote:
>
>> anything have a function for this:
>>
>> (anything-ff-human-size 4060)
>> ==> "4.0K"
>
> And there is url-pretty-length from url-util.el (which is part of GNU Emacs):
>
>     (url-pretty-length 4060)
>     ==> "3k"
>
> (In this example, the result is "3k" because 4060 is smaller than
> 4 * 1024.)
Note that all functions that use elisp to get such result fail with:

--8<---------------cut here---------------start------------->8---
(url-pretty-length 7141892608)
Debugger entered--Lisp error: (overflow-error "7141892608")
--8<---------------cut here---------------end--------------->8---

When using big numbers.
Same for format-mode-line etc...

To avoid that the calc functions can be used giving a string instead of
integer.

--8<---------------cut here---------------start------------->8---
(anything-ff-human-size "7141892608")
==> "6.7G"
(anything-ff-human-size (nth 7 (file-attributes "~/.VirtualBox/Machines/LoseDows.vdi")))
==> "6.7G"
--8<---------------cut here---------------end--------------->8---

Would be great elisp handle such numbers.

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08  7:07             ` Thierry Volpiatto
@ 2011-04-08  8:29               ` Eli Zaretskii
  2011-04-08  9:38                 ` Thierry Volpiatto
  0 siblings, 1 reply; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-08  8:29 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
> Date: Fri, 08 Apr 2011 09:07:45 +0200
> 
> Note that all functions that use elisp to get such result fail with:
> 
> --8<---------------cut here---------------start------------->8---
> (url-pretty-length 7141892608)
> Debugger entered--Lisp error: (overflow-error "7141892608")
> --8<---------------cut here---------------end--------------->8---
> 
> When using big numbers.

That's because you assume that the size is an integer.  But the doc
string of file-attributes says:

 7. Size in bytes.
  This is a floating point number if the size is too large for an integer.

So this is not a problem when used with file sizes.  Other uses will
have to take care to use a floating point number for large values.

> Same for format-mode-line etc...

format-mode-line (and the mode line itself) can only display the size
of an Emacs buffer, which can never be larger than the maximum value
of an Emacs Lisp integer on the same platform.  So this isn't a
problem, either.



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08  8:29               ` Eli Zaretskii
@ 2011-04-08  9:38                 ` Thierry Volpiatto
  0 siblings, 0 replies; 27+ messages in thread
From: Thierry Volpiatto @ 2011-04-08  9:38 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

> So this is not a problem when used with file sizes.  Other uses will
> have to take care to use a floating point number for large values.
Yes indeed, thanks.

However it seem this function show only with "M" (mega)

--8<---------------cut here---------------start------------->8---
(url-pretty-length (nth 7 (file-attributes "~/.VirtualBox/Machines/LoseDows.vdi")))
"6811.04M"
--8<---------------cut here---------------end--------------->8---

instead of showing e.g "6.8G"

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-07 10:02 Converting an Integer into Human Readable String Nordlöw
                   ` (2 preceding siblings ...)
  2011-04-07 12:51 ` Drew Adams
@ 2011-04-08 11:41 ` Pascal J. Bourguignon
  2011-04-08 15:19   ` Eli Zaretskii
                     ` (2 more replies)
  3 siblings, 3 replies; 27+ messages in thread
From: Pascal J. Bourguignon @ 2011-04-08 11:41 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> I looking for the function that prints file size on the mode-line in size-indication-mode. I have searched for size-indication-mode in the source but cannot find a code-references to it. So where is the function that prints for example
>
> 22k
>
> when file is approximately 22 kilobytes big?
>
> What about difference between
> - kilobytes (kB), 1000 bytes, and
> - kibibytes (KiB), 1024 bytes, as defined at
>
> Shouldn't Emacs support both?
>
> This is of course not that hard to write but reinvent the wheel?
>
> http://en.wikipedia.org/wiki/Kibibyte


See what you've done?

R  [  15: Nordlöw                ] Converting an Integer into Human Readable String
R      [  29: Deniz Dogan            ] 
R      [  15: Eli Zaretskii          ] 
R      [  11: Drew Adams             ] 
R      <  13: Eli Zaretskii          > 
R      <  14: Eli Zaretskii          > 
R      <  17: Drew Adams             > 
R      <  15: Eli Zaretskii          > 
R      <  19: Eli Zaretskii          > 
R      <  22: Drew Adams             > 
R      <  32: Thierry Volpiatto      > 
R      <  32: Thierry Volpiatto      > 
R          [  17: Klaus Straubinger      ] 
R              [  44: Thierry Volpiatto      ] 
R      <  23: Eli Zaretskii          > 
R      <  28: Eli Zaretskii          > 
R      <  21: Thierry Volpiatto      > 

17 posts and still no satisfying answer!  We've got a couple of C code
and library functions that don't do what you need, because they've been
written only with some specific purpose (who would fetch a universal
resources in the Gigabytes or the Petabytes?), or because they come with
a big overhead of useless code unrelated to your need.


Instead, you could have had some fun, and implement yourself the
required function.   Well, now you've got only what you merit, here I
had all the fun, you can have the function:

    (format-human-readable-big-number 123456789012
                                      *normal-format*
                                      *exceptional-format*
                                       "B" t :binary)
    --> "  114.978 GiB"


Now, you may call this function in the places of interest in emacs.

As for the C code, well, it's not the first time I've said that emacs C
code should be rewritten in lisp...


------------------------------------------------------------------------
;;; The following code is Copyright Pascal Bourguignon 1995 - 2011
;;; and distributed under the GPL 2 or later license.

;; #-emacs ;; Too bad emacs lisp doesn't implement #+/#-
(require 'cl)


(defun dichotomy (vector value compare &optional start end key)
  "
PRE:	entry is the element to be searched in the table.
        (<= start end)
RETURN: (list found index order)
POST:	(<= start index end)
        +-------------------+----------+-------+----------+----------------+
        | Case              |  found   | index |  order   |     Error      |
        +-------------------+----------+-------+----------+----------------+
        | x < a[min]        |   FALSE  |  min  |  less    |      0         |
        | a[i] < x < a[i+1] |   FALSE  |   i   |  greater |      0         |
        | x = a[i]          |   TRUE   |   i   |  equal   |      0         |
        | a[max] < x        |   FALSE  |  max  |  greater |      0         |
        +-------------------+----------+-------+----------+----------------+
"
  (setf start (or start 0)
        end   (or end (length vector))
        key   (or key (function identity)))
  (let* ((curmin start)
         (curmax end)
         (index  (truncate (+ curmin curmax) 2))
         (order  (funcall compare value (funcall key (aref vector index)))) )
    (loop while (and (/= 0 order) (/= curmin index)) do
         (if (< order 0)
             (setf curmax index)
             (setf curmin index))
         (setf index (truncate (+ curmin curmax) 2))
         (setf order (funcall compare value (funcall key (aref vector index)))))
    (when (and (< start index) (< order 0))
      (setf order 1)
      (decf index))
    (assert
     (or (< (funcall compare value (funcall key (aref vector start))) 0)
         (and (< (funcall compare (funcall key (aref vector index)) value) 0)
              (or (>= (1+ index) end)
                  (< (funcall compare value (funcall key (aref vector (1+ index)))) 0)))
         (= (funcall compare value (funcall key (aref vector index))) 0)))
    (list (= order 0) index order)))


(defun filter-prefixes (prefixes value)
  "
PREFIXES is a list of (long short (expt base exponent))
VALUE    is either an integer or a floating point value.

DO:      Filters out prefixes that are out of range for type of the
         given value, and compute the values of the symbolic exponents
         in prefixes.

RETURN:  a list of (long short base^exponent-value)
"
  (etypecase value
    (float
     (mapcar (lambda (prefix)
               (destructuring-bind (long short (expt base exponent)) prefix
                 (list long short (expt (float base) exponent))))
             prefixes))
    (integer
     (mapcan (lambda (prefix)
               (destructuring-bind (long short (expt base exponent)) prefix
                 (when (< (expt (float base) exponent) most-positive-fixnum)
                   (list (list long short (expt (float base) exponent))))))
             prefixes))))


(defun compute-prefixes ()
  "
RETURN: A hash-table mapping lists of prefix-code and type to a
        filtered and sorted vector of prefixes.
PREFIX-CODE either :si or :binary
TYPE        either float or integer
"
  (let ((table (make-hash-table :test (function equal))))
    (loop
       for value in '(0 0.0) 
       for type in '(integer float)
       do (loop
             for prefix-code in '(:si :binary)
             for prefixes     in (list *si-prefixes* *binary-prefixes*)
             do (setf (gethash (list prefix-code type) table)
                      (coerce (sort  (filter-prefixes prefixes value)
                                     (lambda (a b) (< (third a) (third b))))
                              'vector))))
    table))


(defvar *si-prefixes*
  '(("yotta" "Y" (expt 10 24))
    ("zetta" "Z" (expt 10 21))
    ("exa"   "E" (expt 10 18))
    ("peta"  "P" (expt 10 15))
    ("tera"  "T" (expt 10 12))
    ("giga"  "G" (expt 10 9))
    ("mega"  "M" (expt 10 6))
    ("kilo"  "k" (expt 10 3))
    (""      ""  (expt 10 0))
    ("milli" "m" (expt 10 -3))
    ("micro" "µ" (expt 10 -6))
    ("nano"  "n" (expt 10 -9))
    ("pico"  "p" (expt 10 -12))
    ("femto" "f" (expt 10 -15))
    ("atto"  "a" (expt 10 -18))
    ("zepto" "z" (expt 10 -21))
    ("yocto" "y" (expt 10 -24))))


(defvar *binary-prefixes*
  '(("yobi"  "Yi" (expt 2 80))
    ("zebi"  "Zi" (expt 2 70))
    ("exbi"  "Ei" (expt 2 60))
    ("pebi"  "Pi" (expt 2 50))
    ("tebi"  "Ti" (expt 2 40))
    ("gibi"  "Gi" (expt 2 30))
    ("mebi"  "Mi" (expt 2 20))
    ("kibi"  "Ki" (expt 2 10))
    (""      ""   (expt 2 0))))

(defvar *prefixes* (compute-prefixes))


(defun find-scale (num prefix-code)
  "
Find from the *prefixes* the scale of the number NUM with the given
PREFIX-CODE.
"
  (let ((prefixes  (gethash (list prefix-code (etypecase num
                                                (integer 'integer)
                                                (float   'float)))
                            *prefixes*)))
    (destructuring-bind (foundp index order)
        (dichotomy prefixes num (lambda (a b)
                                  (cond ((< a b) -1)
                                        ((< b a) +1)
                                        (t        0)))
                   0 (length prefixes) (function third))
      (cond
        ((minusp order) ; too small
         '("" "" 1))
        ((< (/ num 1000.0) (third (aref prefixes index))) ; ok
         (aref prefixes index))
        (t ; too big
         '("" "" 1))))))




#|
;; For Common Lisp.  But too bad, emacs lisp doesn't implement #+/#-.

#+common-lisp
(defun format-human-readable-big-number (num format exceptional-format
                                         base-unit short-form prefixes)
  (destructuring-bind (long short scale) (find-scale num prefixes)
    (format nil "~? ~A~A"
            (if (and (= 1 scale)
                     (or (and (< 0 (abs num)) (< (abs num) 1))
                         (<= 1000 (abs num))))
                exceptional-format
                format)
            (list (/ num scale))
            (if short-form short long)
            base-unit)))

#+common-lisp
(defvar *normal-format*       "~9,3F")

#+common-lisp
(defvar *exceptional-format*  "~13,3E")

|#

(defun format-human-readable-big-number (num format exceptional-format
                                         base-unit short-form prefixes)
  (destructuring-bind (long short scale) (find-scale num prefixes)

    (format "%s %s%s" (format (if (and (= 1 scale)
                                       (or (and (< 0 (abs num)) (< (abs num) 1))
                                           (<= 1000 (abs num))))
                                  exceptional-format
                                  format)
                              (/ num scale))
            (if short-form short long)
            base-unit)))

(defvar *normal-format*       "%9.3f")
(defvar *exceptional-format*  "%13.3e")

(defun test/format-human-readable-big-number ()
  (dolist (prefixes '(:si :binary))
    (dolist (short-form '(nil t))
      (dolist (num '(4 45 456 4567 45678 456789 467890 45678901
                     456789012 4567890123 45678901234 456789012345
                     4567890123456 45678901234567 456789012345678
                     4567890123456789 45678901234567890
                     456789012345678901
                     0.04333
                     0.4333
                     4.333 45.333 456.333 4567.333
                     45678.333 456789.333 467890.333 45678901.333
                     456789012.333 4567890123.333 45678901234.333
                     456789012345.333 4567890123456.333
                     45678901234567.333 456789012345678.333
                     4567890123456789.333 45678901234567890.333
                     456789012345678901.333 4567890123456789012.333
                     45678901234567890123.333 456789012345678901234.333
                     4567890123456789012345.333
                     45678901234567890123456.333
                     456789012345678901234567.333
                     4567890123456789012345678.333
                     45678901234567890123456789.333
                     456789012345678901234567890.333
                     4567890123456789012345678901.333
                     45678901234567890123456789012.333
                     456789012345678901234567890123.333
                     ))
        (princ (format-human-readable-big-number num
                                                 *normal-format*
                                                 *exceptional-format*
                                                 (if short-form
                                                     "B"
                                                     "byte")
                                                 short-form prefixes))
        (terpri)))))

(test/format-human-readable-big-number)
    4.000 byte
   45.000 byte
  456.000 byte
    4.567 kilobyte
   45.678 kilobyte
  456.789 kilobyte
  467.890 kilobyte
   45.679 megabyte
  456.789 megabyte
    4.568 gigabyte
   45.679 gigabyte
  456.789 gigabyte
    4.568 terabyte
   45.679 terabyte
  456.789 terabyte
    4.568 petabyte
   45.679 petabyte
  456.789 petabyte
   43.330 millibyte
  433.300 millibyte
    4.333 byte
   45.333 byte
  456.333 byte
    4.567 kilobyte
   45.678 kilobyte
  456.789 kilobyte
  467.890 kilobyte
   45.679 megabyte
  456.789 megabyte
    4.568 gigabyte
   45.679 gigabyte
  456.789 gigabyte
    4.568 terabyte
   45.679 terabyte
  456.789 terabyte
    4.568 petabyte
   45.679 petabyte
  456.789 petabyte
    4.568 exabyte
   45.679 exabyte
  456.789 exabyte
    4.568 zettabyte
   45.679 zettabyte
  456.789 zettabyte
    4.568 yottabyte
   45.679 yottabyte
  456.789 yottabyte
    4.568e+27 byte
    4.568e+28 byte
    4.568e+29 byte
    4.000 B
   45.000 B
  456.000 B
    4.567 kB
   45.678 kB
  456.789 kB
  467.890 kB
   45.679 MB
  456.789 MB
    4.568 GB
   45.679 GB
  456.789 GB
    4.568 TB
   45.679 TB
  456.789 TB
    4.568 PB
   45.679 PB
  456.789 PB
   43.330 mB
  433.300 mB
    4.333 B
   45.333 B
  456.333 B
    4.567 kB
   45.678 kB
  456.789 kB
  467.890 kB
   45.679 MB
  456.789 MB
    4.568 GB
   45.679 GB
  456.789 GB
    4.568 TB
   45.679 TB
  456.789 TB
    4.568 PB
   45.679 PB
  456.789 PB
    4.568 EB
   45.679 EB
  456.789 EB
    4.568 ZB
   45.679 ZB
  456.789 ZB
    4.568 YB
   45.679 YB
  456.789 YB
    4.568e+27 B
    4.568e+28 B
    4.568e+29 B
    4.000 byte
   45.000 byte
  456.000 byte
    4.460 kibibyte
   44.607 kibibyte
  446.083 kibibyte
  456.924 kibibyte
   43.563 mebibyte
  435.628 mebibyte
    4.254 gibibyte
   42.542 gibibyte
  425.418 gibibyte
    4.154 tebibyte
   41.545 tebibyte
  415.447 tebibyte
    4.057 pebibyte
   40.571 pebibyte
  405.710 pebibyte
    4.333e-02 byte
    4.333e-01 byte
    4.333 byte
   45.333 byte
  456.333 byte
    4.460 kibibyte
   44.608 kibibyte
  446.083 kibibyte
  456.924 kibibyte
   43.563 mebibyte
  435.628 mebibyte
    4.254 gibibyte
   42.542 gibibyte
  425.418 gibibyte
    4.154 tebibyte
   41.545 tebibyte
  415.447 tebibyte
    4.057 pebibyte
   40.571 pebibyte
  405.710 pebibyte
    3.962 exbibyte
   39.620 exbibyte
  396.201 exbibyte
    3.869 zebibyte
   38.692 zebibyte
  386.915 zebibyte
    3.778 yobibyte
   37.785 yobibyte
  377.847 yobibyte
    4.568e+27 byte
    4.568e+28 byte
    4.568e+29 byte
    4.000 B
   45.000 B
  456.000 B
    4.460 KiB
   44.607 KiB
  446.083 KiB
  456.924 KiB
   43.563 MiB
  435.628 MiB
    4.254 GiB
   42.542 GiB
  425.418 GiB
    4.154 TiB
   41.545 TiB
  415.447 TiB
    4.057 PiB
   40.571 PiB
  405.710 PiB
    4.333e-02 B
    4.333e-01 B
    4.333 B
   45.333 B
  456.333 B
    4.460 KiB
   44.608 KiB
  446.083 KiB
  456.924 KiB
   43.563 MiB
  435.628 MiB
    4.254 GiB
   42.542 GiB
  425.418 GiB
    4.154 TiB
   41.545 TiB
  415.447 TiB
    4.057 PiB
   40.571 PiB
  405.710 PiB
    3.962 EiB
   39.620 EiB
  396.201 EiB
    3.869 ZiB
   38.692 ZiB
  386.915 ZiB
    3.778 YiB
   37.785 YiB
  377.847 YiB
    4.568e+27 B
    4.568e+28 B
    4.568e+29 B
nil
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08 11:41 ` Pascal J. Bourguignon
@ 2011-04-08 15:19   ` Eli Zaretskii
  2011-04-08 19:12     ` Thierry Volpiatto
  2011-04-09  8:46   ` Eli Zaretskii
       [not found]   ` <mailman.1.1302338931.20547.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-08 15:19 UTC (permalink / raw)
  To: help-gnu-emacs

> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
> Date: Fri, 08 Apr 2011 13:41:30 +0200
> 
> Instead, you could have had some fun, and implement yourself the
> required function.   Well, now you've got only what you merit, here I
> had all the fun, you can have the function:
> 
>     (format-human-readable-big-number 123456789012
>                                       *normal-format*
>                                       *exceptional-format*
>                                        "B" t :binary)
>     --> "  114.978 GiB"
> 
> 
> Now, you may call this function in the places of interest in emacs.

Wow!  I've just installed in Emacs a much more modest variant
(reproduced below for those who don't live on the bleeding edge).

(defun file-size-human-readable (file-size &optional flavor)
  "Produce a string showing FILE-SIZE in human-readable form.

Optional second argument FLAVOR controls the units and the display format:

 If FLAVOR is nil or omitted, each kilobyte is 1024 bytes and the produced
    suffixes are \"k\", \"M\", \"G\", \"T\", etc.
 If FLAVOR is `si', each kilobyte is 1000 bytes and the produced suffixes
    are \"k\", \"M\", \"G\", \"T\", etc.
 If FLAVOR is `iec', each kilobyte is 1024 bytes and the produced suffixes
    are \"KiB\", \"MiB\", \"GiB\", \"TiB\", etc."
  (let ((power (if (or (null flavor) (eq flavor 'iec))
		   1024.0
		 1000.0))
	(post-fixes
	 ;; none, kilo, mega, giga, tera, peta, exa, zetta, yotta
	 (list "" "k" "M" "G" "T" "P" "E" "Z" "Y")))
    (while (and (>= file-size power) (cdr post-fixes))
      (setq file-size (/ file-size power)
	    post-fixes (cdr post-fixes)))
    (format "%.0f%s%s" file-size
	    (if (and (eq flavor 'iec) (string= (car post-fixes) "k"))
		"K"
	      (car post-fixes))
	    (if (eq flavor 'iec) "iB" ""))))



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08 15:19   ` Eli Zaretskii
@ 2011-04-08 19:12     ` Thierry Volpiatto
  2011-04-08 21:36       ` Eli Zaretskii
  0 siblings, 1 reply; 27+ messages in thread
From: Thierry Volpiatto @ 2011-04-08 19:12 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

>> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
>> Date: Fri, 08 Apr 2011 13:41:30 +0200
>> 
>> Instead, you could have had some fun, and implement yourself the
>> required function.   Well, now you've got only what you merit, here I
>> had all the fun, you can have the function:
>> 
>>     (format-human-readable-big-number 123456789012
>>                                       *normal-format*
>>                                       *exceptional-format*
>>                                        "B" t :binary)
>>     --> "  114.978 GiB"
>> 
>> 
>> Now, you may call this function in the places of interest in emacs.
>
> Wow!  I've just installed in Emacs a much more modest variant
> (reproduced below for those who don't live on the bleeding edge).
>
> (defun file-size-human-readable (file-size &optional flavor)
>   "Produce a string showing FILE-SIZE in human-readable form.
>
> Optional second argument FLAVOR controls the units and the display format:
>
>  If FLAVOR is nil or omitted, each kilobyte is 1024 bytes and the produced
>     suffixes are \"k\", \"M\", \"G\", \"T\", etc.
>  If FLAVOR is `si', each kilobyte is 1000 bytes and the produced suffixes
>     are \"k\", \"M\", \"G\", \"T\", etc.
>  If FLAVOR is `iec', each kilobyte is 1024 bytes and the produced suffixes
>     are \"KiB\", \"MiB\", \"GiB\", \"TiB\", etc."
>   (let ((power (if (or (null flavor) (eq flavor 'iec))
> 		   1024.0
> 		 1000.0))
> 	(post-fixes
> 	 ;; none, kilo, mega, giga, tera, peta, exa, zetta, yotta
> 	 (list "" "k" "M" "G" "T" "P" "E" "Z" "Y")))
>     (while (and (>= file-size power) (cdr post-fixes))
>       (setq file-size (/ file-size power)
> 	    post-fixes (cdr post-fixes)))
>     (format "%.0f%s%s" file-size
> 	    (if (and (eq flavor 'iec) (string= (car post-fixes) "k"))
> 		"K"
> 	      (car post-fixes))
> 	    (if (eq flavor 'iec) "iB" ""))))

The result you obtain is not what is expected (i.e same than
ls -lh)

--8<---------------cut here---------------start------------->8---
ls -l .VirtualBox/Machines/LoseDows.vdi
-rw------- 1 thierry thierry 7141892608 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi

ls -lh .VirtualBox/Machines/LoseDows.vdi
-rw------- 1 thierry thierry 6,7G 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi

(file-size-human-readable 7141892608.0)
"7G"

(anything-ff-human-size 7141892608.0)
"6.7G"
--8<---------------cut here---------------end--------------->8---


-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08 19:12     ` Thierry Volpiatto
@ 2011-04-08 21:36       ` Eli Zaretskii
  2011-04-09  5:24         ` Thierry Volpiatto
  0 siblings, 1 reply; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-08 21:36 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
> Date: Fri, 08 Apr 2011 21:12:35 +0200
> 
> The result you obtain is not what is expected (i.e same than
> ls -lh)
> 
> --8<---------------cut here---------------start------------->8---
> ls -l .VirtualBox/Machines/LoseDows.vdi
> -rw------- 1 thierry thierry 7141892608 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi
> 
> ls -lh .VirtualBox/Machines/LoseDows.vdi
> -rw------- 1 thierry thierry 6,7G 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi
> 
> (file-size-human-readable 7141892608.0)
> "7G"

Well, that's what ls-lisp.el was always doing.  But since you asked
for it, here you go (will commit as soon as Savannah is fixed):

(defun file-size-human-readable (file-size &optional flavor)
  "Produce a string showing FILE-SIZE in human-readable form.

Optional second argument FLAVOR controls the units and the display format:

 If FLAVOR is nil or omitted, each kilobyte is 1024 bytes and the produced
    suffixes are \"k\", \"M\", \"G\", \"T\", etc.
 If FLAVOR is `si', each kilobyte is 1000 bytes and the produced suffixes
    are \"k\", \"M\", \"G\", \"T\", etc.
 If FLAVOR is `iec', each kilobyte is 1024 bytes and the produced suffixes
    are \"KiB\", \"MiB\", \"GiB\", \"TiB\", etc."
  (let ((power (if (or (null flavor) (eq flavor 'iec))
		   1024.0
		 1000.0))
	(post-fixes
	 ;; none, kilo, mega, giga, tera, peta, exa, zetta, yotta
	 (list "" "k" "M" "G" "T" "P" "E" "Z" "Y")))
    (while (and (>= file-size power) (cdr post-fixes))
      (setq file-size (/ file-size power)
	    post-fixes (cdr post-fixes)))
    (format (if (> (mod file-size 1.0) 0.05)
		"%.1f%s%s"
	      "%.0f%s%s")
	    file-size
	    (if (and (eq flavor 'iec) (string= (car post-fixes) "k"))
		"K"
	      (car post-fixes))
	    (if (eq flavor 'iec) "iB" ""))))



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08 21:36       ` Eli Zaretskii
@ 2011-04-09  5:24         ` Thierry Volpiatto
  0 siblings, 0 replies; 27+ messages in thread
From: Thierry Volpiatto @ 2011-04-09  5:24 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
>> Date: Fri, 08 Apr 2011 21:12:35 +0200
>> 
>> The result you obtain is not what is expected (i.e same than
>> ls -lh)
>> 
>> --8<---------------cut here---------------start------------->8---
>> ls -l .VirtualBox/Machines/LoseDows.vdi
>> -rw------- 1 thierry thierry 7141892608 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi
>> 
>> ls -lh .VirtualBox/Machines/LoseDows.vdi
>> -rw------- 1 thierry thierry 6,7G 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi
>> 
>> (file-size-human-readable 7141892608.0)
>> "7G"
>
> Well, that's what ls-lisp.el was always doing.  But since you asked
> for it, here you go (will commit as soon as Savannah is fixed):
>
> (defun file-size-human-readable (file-size &optional flavor)
>   "Produce a string showing FILE-SIZE in human-readable form.
>
> Optional second argument FLAVOR controls the units and the display format:
>
>  If FLAVOR is nil or omitted, each kilobyte is 1024 bytes and the produced
>     suffixes are \"k\", \"M\", \"G\", \"T\", etc.
>  If FLAVOR is `si', each kilobyte is 1000 bytes and the produced suffixes
>     are \"k\", \"M\", \"G\", \"T\", etc.
>  If FLAVOR is `iec', each kilobyte is 1024 bytes and the produced suffixes
>     are \"KiB\", \"MiB\", \"GiB\", \"TiB\", etc."
>   (let ((power (if (or (null flavor) (eq flavor 'iec))
> 		   1024.0
> 		 1000.0))
> 	(post-fixes
> 	 ;; none, kilo, mega, giga, tera, peta, exa, zetta, yotta
> 	 (list "" "k" "M" "G" "T" "P" "E" "Z" "Y")))
>     (while (and (>= file-size power) (cdr post-fixes))
>       (setq file-size (/ file-size power)
> 	    post-fixes (cdr post-fixes)))
>     (format (if (> (mod file-size 1.0) 0.05)
> 		"%.1f%s%s"
> 	      "%.0f%s%s")
> 	    file-size
> 	    (if (and (eq flavor 'iec) (string= (car post-fixes) "k"))
> 		"K"
> 	      (car post-fixes))
> 	    (if (eq flavor 'iec) "iB" ""))))

(file-size-human-readable 7141892608.0)
"6.7G"

Nice, thanks.

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-08 11:41 ` Pascal J. Bourguignon
  2011-04-08 15:19   ` Eli Zaretskii
@ 2011-04-09  8:46   ` Eli Zaretskii
       [not found]   ` <mailman.1.1302338931.20547.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-09  8:46 UTC (permalink / raw)
  To: help-gnu-emacs

> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
> Date: Fri, 08 Apr 2011 13:41:30 +0200
> 
> Instead, you could have had some fun, and implement yourself the
> required function.   Well, now you've got only what you merit, here I
> had all the fun, you can have the function:
> 
>     (format-human-readable-big-number 123456789012
>                                       *normal-format*
>                                       *exceptional-format*
>                                        "B" t :binary)
>     --> "  114.978 GiB"

That's very impressive, but at closer look, I found the following
problems with this implementation:

 . It cannot be evaluated in Emacs Lisp without commenting out the
   part between "#|" and "|#".

 . It cannot be evaluated on a 32-bit machine without commenting out
   some parts of the integer test values in the test harness, due to
   integer overflows.

 . When the last argument is :binary, it produces wrong results for
   numbers between 1000*2^N and 1023*2^N.  E.g.,

    (format-human-readable-big-number 1023  "%.1f" "%13.3e" "B" t :binary)
      => "   1.023e+003 B"

   whereas I'd expect "1023 B" instead.

 . It always produces results with a fixed number of digits after the
   decimal, determined by the value of *normal-format*.  Thus, with a
   format of "%.1f" it will always produce 1 digit after the decimal,
   even if that digit is zero:

    (format-human-readable-big-number 900  "%.1f" "%13.3e" "B" t :binary)
      => "900.0 B"

   which is IMO ugly; "ls -lh" produces just "900" in this case.  This
   cannot be remedied by using "%.0f" as the normal format, because
   then it will always round to the nearest integral value, and the
   fractions will never be shown; again, this is different from "ls -lh".



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
       [not found]   ` <mailman.1.1302338931.20547.help-gnu-emacs@gnu.org>
@ 2011-04-09  9:33     ` Pascal J. Bourguignon
  2011-04-09 12:13       ` Eli Zaretskii
       [not found]       ` <mailman.0.1302351322.29796.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 27+ messages in thread
From: Pascal J. Bourguignon @ 2011-04-09  9:33 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

>> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
>> Date: Fri, 08 Apr 2011 13:41:30 +0200
>> 
>> Instead, you could have had some fun, and implement yourself the
>> required function.   Well, now you've got only what you merit, here I
>> had all the fun, you can have the function:
>> 
>>     (format-human-readable-big-number 123456789012
>>                                       *normal-format*
>>                                       *exceptional-format*
>>                                        "B" t :binary)
>>     --> "  114.978 GiB"
>
> That's very impressive, but at closer look, I found the following
> problems with this implementation:
>
>  . It cannot be evaluated in Emacs Lisp without commenting out the
>    part between "#|" and "|#".
>
>  . It cannot be evaluated on a 32-bit machine without commenting out
>    some parts of the integer test values in the test harness, due to
>    integer overflows.
>
>  . When the last argument is :binary, it produces wrong results for
>    numbers between 1000*2^N and 1023*2^N.  E.g.,
>
>     (format-human-readable-big-number 1023  "%.1f" "%13.3e" "B" t :binary)
>       => "   1.023e+003 B"
>
>    whereas I'd expect "1023 B" instead.
>
>  . It always produces results with a fixed number of digits after the
>    decimal, determined by the value of *normal-format*.  Thus, with a
>    format of "%.1f" it will always produce 1 digit after the decimal,
>    even if that digit is zero:
>
>     (format-human-readable-big-number 900  "%.1f" "%13.3e" "B" t :binary)
>       => "900.0 B"
>
>    which is IMO ugly; "ls -lh" produces just "900" in this case.  This
>    cannot be remedied by using "%.0f" as the normal format, because
>    then it will always round to the nearest integral value, and the
>    fractions will never be shown; again, this is different from "ls -lh".

Thanks for the bug report.
Contributions are welcome.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-09  9:33     ` Pascal J. Bourguignon
@ 2011-04-09 12:13       ` Eli Zaretskii
       [not found]       ` <mailman.0.1302351322.29796.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-09 12:13 UTC (permalink / raw)
  To: help-gnu-emacs

> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
> Date: Sat, 09 Apr 2011 11:33:29 +0200
> 
> Thanks for the bug report.
> Contributions are welcome.

Well, my contribution is now in files.el ;-)



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
       [not found]       ` <mailman.0.1302351322.29796.help-gnu-emacs@gnu.org>
@ 2011-04-11 15:31         ` Ted Zlatanov
  2011-04-11 16:57           ` Eli Zaretskii
  2011-05-12 19:30           ` Ted Zlatanov
  0 siblings, 2 replies; 27+ messages in thread
From: Ted Zlatanov @ 2011-04-11 15:31 UTC (permalink / raw)
  To: help-gnu-emacs

On Sat, 09 Apr 2011 15:13:16 +0300 Eli Zaretskii <eliz@gnu.org> wrote: 

EZ> Well, my contribution is now in files.el ;-)

Could you add the corresponding ERT tests, using the ad-hoc tests Pascal
already provided but converting to should/should-not as needed?

Let me know if you can't and I'll work on it.

Thanks
Ted


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-11 15:31         ` Ted Zlatanov
@ 2011-04-11 16:57           ` Eli Zaretskii
  2011-05-12 19:30           ` Ted Zlatanov
  1 sibling, 0 replies; 27+ messages in thread
From: Eli Zaretskii @ 2011-04-11 16:57 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Ted Zlatanov <tzz@lifelogs.com>
> Date: Mon, 11 Apr 2011 10:31:32 -0500
> 
> On Sat, 09 Apr 2011 15:13:16 +0300 Eli Zaretskii <eliz@gnu.org> wrote: 
> 
> EZ> Well, my contribution is now in files.el ;-)
> 
> Could you add the corresponding ERT tests, using the ad-hoc tests Pascal
> already provided but converting to should/should-not as needed?
> 
> Let me know if you can't and I'll work on it.

Feel free.  It's not that I can't, but I have too many things on my
plate now.

TIA



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: Converting an Integer into Human Readable String
  2011-04-11 15:31         ` Ted Zlatanov
  2011-04-11 16:57           ` Eli Zaretskii
@ 2011-05-12 19:30           ` Ted Zlatanov
  1 sibling, 0 replies; 27+ messages in thread
From: Ted Zlatanov @ 2011-05-12 19:30 UTC (permalink / raw)
  To: help-gnu-emacs

On Mon, 11 Apr 2011 10:31:32 -0500 Ted Zlatanov <tzz@lifelogs.com> wrote: 

TZ> On Sat, 09 Apr 2011 15:13:16 +0300 Eli Zaretskii <eliz@gnu.org> wrote: 
EZ> Well, my contribution is now in files.el ;-)

TZ> Could you add the corresponding ERT tests, using the ad-hoc tests Pascal
TZ> already provided but converting to should/should-not as needed?

Eli, here's a test you can put in test/automated/ if you like it.

Please look at the 'si output for 1024000000 vs. 1000000000.  Is is
supposed to be 1G for both?  It's correct according to the code but it
seems like if there's just one significant figure before the decimal
point we want to show 1.02G or 1.024G.

Ted

#+begin_src lisp
(require 'ert)
(require 'files)

(ert-deftest files-file-size-human-readable ()
  "Test the `file-size-human-readable' function in files.el"
  (dolist (test '((123456789012 "115.0G" "123.5G" "115.0GiB")
                  (1024000000 "976.6M" "1G" "976.6MiB")
                  (1000000000 "953.7M" "1G" "953.7MiB")
                  (102400000 "97.7M" "102.4M" "97.7MiB")
                  (100000000 "95.4M" "100M" "95.4MiB")
                  (10240000 "9.8M" "10.2M" "9.8MiB")
                  (10000000 "9.5M" "10M" "9.5MiB")
                  (1024000 "1000k" "1M" "1000KiB")
                  (1000000 "976.6k" "1M" "976.6KiB")
                  (100000 "97.7k" "100k" "97.7KiB")
                  (65536 "64k" "65.5k" "64KiB")
                  (0 "0" "0" "0iB")
                  (900 "900" "900" "900iB")
                  (1023 "1023" "1k" "1023iB")))
    (let ((n (nth 0 test))
          (s (nth 1 test))
          (si (nth 2 test))
          (iec (nth 3 test)))
      (message "testing regular conversion of %s" n)
      (should (string-equal s (file-size-human-readable n)))
      (message "testing SI conversion of %s" n)
      (should (string-equal si (file-size-human-readable n 'si)))
      (message "testing IEC conversion of %s" n)
      (should (string-equal iec (file-size-human-readable n 'iec))))))

#+end_src


^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2011-05-12 19:30 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-07 10:02 Converting an Integer into Human Readable String Nordlöw
2011-04-07 11:34 ` Deniz Dogan
2011-04-07 12:04   ` Eli Zaretskii
2011-04-07 11:52 ` Eli Zaretskii
2011-04-07 12:51 ` Drew Adams
2011-04-07 17:43   ` Eli Zaretskii
2011-04-07 18:13     ` Drew Adams
2011-04-07 18:35       ` Eli Zaretskii
2011-04-07 18:51         ` Drew Adams
2011-04-07 20:10           ` Eli Zaretskii
2011-04-07 18:44       ` Eli Zaretskii
2011-04-07 19:01         ` Thierry Volpiatto
     [not found]         ` <mailman.17.1302204476.27822.help-gnu-emacs@gnu.org>
2011-04-08  6:35           ` Klaus Straubinger
2011-04-08  7:07             ` Thierry Volpiatto
2011-04-08  8:29               ` Eli Zaretskii
2011-04-08  9:38                 ` Thierry Volpiatto
2011-04-08 11:41 ` Pascal J. Bourguignon
2011-04-08 15:19   ` Eli Zaretskii
2011-04-08 19:12     ` Thierry Volpiatto
2011-04-08 21:36       ` Eli Zaretskii
2011-04-09  5:24         ` Thierry Volpiatto
2011-04-09  8:46   ` Eli Zaretskii
     [not found]   ` <mailman.1.1302338931.20547.help-gnu-emacs@gnu.org>
2011-04-09  9:33     ` Pascal J. Bourguignon
2011-04-09 12:13       ` Eli Zaretskii
     [not found]       ` <mailman.0.1302351322.29796.help-gnu-emacs@gnu.org>
2011-04-11 15:31         ` Ted Zlatanov
2011-04-11 16:57           ` Eli Zaretskii
2011-05-12 19:30           ` Ted Zlatanov

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.