Re: boost::units::information::byte_base_unit off by a factor of 8
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, 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.
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 184.108.40.206ubuntu1).
> Do you have any idea on how to get the correct output (other
> than divide the number of bytes by 8 on the input) ?