Boost logo

Boost :

From: frederic.bron_at_[hidden]
Date: 2007-03-14 11:57:35

This is about output of real values (float, double, ...) in exponential
format and output of not a number and infinity.
My concern is that in C++, the output differs from one compiler/machine to
another. It would be very nice if boost::format could garantee the same
output on every computer. For example, the following code gives the
following output on various machines (using boost 1.33.1):

        std::cout << format("%1% ") % 1e12 ;
        std::cout << format("%1% ") %
std::numeric_limits<double>::quiet_NaN() ;
        std::cout << format("%1% ") %
std::numeric_limits<double>::infinity() ;
        std::cout << format("%1%") %
(-std::numeric_limits<double>::infinity()) ;

The output on cygwin (compiled with g++ 3.4.4) is:
1e+12 nan inf -inf

The output on windows (compiled with g++ 3.4.4 on cygwin with flag
-mno-cygwin) is:
1e+012 1.#QNAN 1.#INF -1.#INF

The output on Tru64 (compiled with g++ 3.4.4) is:
1e+12 NaNQ INF -INF

This makes difficult to check if the programs do the same on various
machines as soon as tests include output of numbers.

Here, I cite section The fprintf function of standard
"Programming languages — C" ISO/IEC 9899:1999(E):

The effect of the printf function in C is descibed as:

A double argument representing an infinity is converted in one of the
[-]inf or [-]infinity — which style is implementation-defined. A
double argument representing a NaN is converted in one of the styles
[-]nan or [-]nan(n-char-sequence) — which style, and the meaning of
any n-char-sequence, is implementation-defined.

A double argument representing a floating-point number is converted in the
style [-]d.ddd e±dd, ... The exponent always contains at least two digits,
and only as many more digits as necessary to represent the exponent.

Could we have the same in C++:
1. 2 digit in the exponent and more only if needed
2. a standard format for not a number and infinity: nan and inf. (They are
roughtly implementation defined in C!)

In the C++ standard "Programming languages — C++" ISO/IEC 14882:2003(E),
the only reference to printf is in 27.8.2 C Library files where we can
find the list of functions in cstdio.
Also using cout, there is no description of how to handle NaN, infinity
and the number of digits in the exponents.
The function should be described in the previous C standard "Programming
languages – C" ISO/IEC 9899:1990 which I do not have.


F. Bron

Avis :
Ce message et toute pièce jointe sont la propriété d'Alcan et sont
destinés seulement aux personnes ou à l'entité à qui le message est
adressé. Si vous avez reçu ce message par erreur, veuillez le détruire et
en aviser l'expéditeur par courriel. Si vous n'êtes pas le destinataire du
message, vous n'êtes pas autorisé à utiliser, à copier ou à divulguer le
contenu du message ou ses pièces jointes en tout ou en partie.

This message and any attachments are the property of Alcan and are
intended solely for the named recipients or entity to whom this message is
addressed. If you have received this message in error please inform the
sender via e-mail and destroy the message. If you are not the intended
recipient you are not allowed to use, copy or disclose the contents or
attachments in whole or in part.

Boost list run by bdawes at, gregod at, cpdaniel at, john at