boost::units::information::byte_base_unit off by a factor of 8

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

boost::units::information::byte_base_unit off by a factor of 8

Boost - Users mailing list
I am trying to build a program that will display file sizes in human
readable form. The file size is in bytes and I am using boost::units
to convert the bytes to a binary prefixed unit.

Using the example code found here[1], I build a small program
to test the library and the output is off by a factor of 8:

    boost::units::quantity<boost::units::information::byte_base_unit::unit_type> b = 2048. * boost::units::information::byte_base_unit::unit_type();
    std::cout << boost::units::engineering_prefix << b << std::endl;  // expected 2.048 kb
    std::cout << boost::units::symbol_format << boost::units::binary_prefix << b << std::endl; //  "expected 2 Kib"

but the output on stdout is:

16.384 kb
16 Kib

which is 8 times the expected result. The whole example code
can be found here[2].

I have tested this code both on Archlinux (boost 1.66.0) as well
as Ubuntu Xenial Server (libboost-dev 1.58.0.1ubuntu1).

Do you have any idea on how to get the correct output (other
than divide the number of bytes by 8 on the input) ?

Best regards

Simon Doppler

[1] http://www.boost.org/doc/libs/1_65_0/doc/html/boost_units/Examples.html#boost_units.Examples.autoscale
[2] https://dopsi.ch/tmp/main.cpp
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: boost::units::information::byte_base_unit off by a factor of 8

Boost - Users mailing list
AMDG

On 03/21/2018 03:01 AM, Simon Doppler via Boost-users wrote:

> I am trying to build a program that will display file sizes in human
> readable form. The file size is in bytes and I am using boost::units
> to convert the bytes to a binary prefixed unit.
>
> Using the example code found here[1], I build a small program
> to test the library and the output is off by a factor of 8:
>
>     boost::units::quantity<boost::units::information::byte_base_unit::unit_type> b = 2048. * boost::units::information::byte_base_unit::unit_type();
>     std::cout << boost::units::engineering_prefix << b << std::endl;  // expected 2.048 kb
>     std::cout << boost::units::symbol_format << boost::units::binary_prefix << b << std::endl; //  "expected 2 Kib"
>
> but the output on stdout is:
>
> 16.384 kb
> 16 Kib
>
> which is 8 times the expected result. The whole example code
> can be found here[2].
>

The output is "correct" in some sense.  The problem is
that the output code is printing the value in terms
of bits rather than bytes (note that it uses 'b' instead
of 'B').  I'll see if I can solve this, but the
formatting code is already pretty messy.

> I have tested this code both on Archlinux (boost 1.66.0) as well
> as Ubuntu Xenial Server (libboost-dev 1.58.0.1ubuntu1).
>
> Do you have any idea on how to get the correct output (other
> than divide the number of bytes by 8 on the input) ?
>

In Christ,
Steven Watanabe
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users