[Toybox] [PATCH] Add support for 1024 as well as 1000 to human_readable.

Rob Landley rob at landley.net
Sun Aug 23 18:20:15 PDT 2015


On 08/17/2015 03:38 PM, enh wrote:
> On Mon, Aug 17, 2015 at 12:02 PM, Rob Landley <rob at landley.net> wrote:
>> I take a stab at making it work with long long, and if it's nontrivial
>> I'll bite the bullet and merge the double version.
> 
> it would be nice if you could keep the double versions for builds that
> allow floating point. i suspect that coreutils isn't using floating
> point under the hood, because some of its rounding seems wrong. it
> would be sad to pessimize toybox on real hardware to support such a
> minority use case.

It looks like the double version can say "1023M", which isn't right
either. Yes, ubuntu can too:

$ truncate blah -s $((1023*1024))
$ du -h --apparent-size blah
1023K	blah

You're already saying you don't want to copy them getting the rounding
wrong, but this is the wrong number of _digits_. The base 10 one is 3
digits max, either via "9.8" or "321". This one can go to 4 digits. I
thought the reason for the "double" was to take care of that, but
apparently not?

I was halfway through doing an if (CFG_TOYBOX_FLOAT) one with the double
and an else case with the long long, but I _think_ what we need to do is
test against 999 each time and if we overshoot slightly it should round
up as 1.0 of the next unit? So decimal test always, but binary divide
where appropriate.

The ubuntu behavior is...

$ truncate blah -s $((3000*1024))
$ du -h --apparent-size blah
3.0M	blah

That got rounded up (3000 < 3*1024) so 1001KB being 1.0MB is also
legitimately something you can round up? I think?

/me wists for a specification. Oh well. I hate when I have to guess at
what the right behavior _is_...

> (even though they're easy enough to find, i didn't list examples above
> because i didn't want to encourage you to break things :-) )
> 
>> (What would be really
>> nice would be test cases, but I'll see what I can come up with.)
> 
> this is why you need unit tests for the library rather than relying on
> testing the final tools...

I added a test command to toys/examples and a very simple one to tests/
but I need the *.test file waaaay fluffed out. :)

Rob

 1440379215.0


More information about the Toybox mailing list