Re: Printing double value in a fixed size buffer
On Mar 10, 2:23 pm, mathieu <mathieu.malate...@gmail.com> wrote:
I am trying to find a solution for the following problem: how
do I print in ASCII a double value in a fixed size buffer of
16 bytes ?
My first attempt was:
double data = 0.960000000000662;
std::cout << std::dec << std::setprecision(15) << data << std::endl;
Because it does not work for number matching "0.[0]*", I was
suggested:
std::cout << std::dec << std::setprecision(15) << std::fixed << data
<< std::endl;
This still does not work for:
double data = 1000000.960000000000662;
and std::fixed is actually loosing all the precision for
number such as:
double data = 0.0000000000009654321;
Could someone please let me know how I can use the iomanip functions
to solve my issue ? Requirements are:
- Up to a maximum of 16 bytes
- Only [0-9], 'e'/'E', '+'/'-' and '.' character can be used
- Minimize loss of information/precision
It depends on what you really want and need, but if the values
can occupy a large range, you need to use scientific format (and
even then, you have to watch out for values greater than 1E100
between 0 and 1E-100). Of course, this means that you won't get
15 significant digits in a 16 byte buffer; you have to allow for
the sign, the decimal point, and the exponant part (4
characters). So you end up with something like:
std::cout << EFmt( 16, 9 ) << value ;
with ten digits precision (9 after the decimal point). If you
need more precision over a large range, you need more
characters. (EFmt( 16, 9 ) will generate something matching the
regular expression "[ -][0-9][.,][0-9]{9}e[+-][0-9]{2-}". If
the exponent is less than 100 and greater than -100, that last
{} becomes {2}, and you have a constant width. And whether . or
, is used for the third character depends on the locale. (My
own implementation of EFmt allows adding flags, e.g.
std::ios::uppercase for E instead of e, std::ios::showsign for
[+-] instead of the first [ -], etc. Typically, however, you'll
create a manipulator specifically for the context you're
interested in, with a name designating that context, and use
it.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34