Standard Text Format for Floating Point Values?

  • Follow


I have noticed a great deal of variance between various run time
libraries for C++ compilers in how floating point values (floats and
doubles) get formatted when they represent "invalid" values such as
plus or minus infinity or not a number.   I work with applications
where the presentation of such data is rather important and find
myself frustrated that, even with the same library, I cannot write out
such a value as text to a stream and expect it to be read from that
stream as the same value.  My question is whether there is any
standard for describing these type of values as text.  If there is
such a standard, I would like my output to conform to it.  Failing
that, I suppose that I will use my own formatting rules so that my
programs produce reliable output.

Thanks,

Jon Trauntvein

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Jon 5/5/2004 8:38:36 PM

Jon Trauntvein wrote:
> I have noticed a great deal of variance between various run time
> libraries for C++ compilers in how floating point values (floats and
> doubles) get formatted when they represent "invalid" values such as
> plus or minus infinity or not a number.

So far as I am aware, formatting of non-numbers is either unspecified
or undefined in C++.  (Formatted output is mostly defined in terms of
printf as specified in C90, but it is difficult and expensive to get
hold of that standard now.)

> I work with applications where the presentation of such data is
> rather important and find myself frustrated that, even with the same
> library, I cannot write out such a value as text to a stream and
> expect it to be read from that stream as the same value.

Unfortunately the standard does not allow more liberal parsing of
numbers, probably because that could break code like this:

    int i;
    std::string s;
    if (is >> i)
        process_number(i);
    else if (is >> s)
        process_string(s);

> My question is whether there is any standard for describing these
> type of values as text.  If there is such a standard, I would like
> my output to conform to it.  Failing that, I suppose that I will use
> my own formatting rules so that my programs produce reliable output.

C99 specifies many more aspects of floating-point handling in the
language and library, many of which I think are likely to make it into
a future C++ standard.

Quoting from the final draft of C99 (not the actual standard), on
output through printf(),

   "A double argument representing an infinity is converted in one of
    the styles [-]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."

This goes for e, f and g specifiers; the E, F and G specifiers result
in upper-case letters instead.  On input through fscanf(), all of the
possible implementation-defined output formats must be accepted case-
insensitively.  The optional n-char-sequence following "nan" is an
arbitrary long sequence of characters 0-9, A-Z, a-z and _.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 5/14/2004 9:07:49 PM


1 Replies
113 Views

(page loaded in 0.044 seconds)

Similiar Articles:













7/18/2012 12:19:30 PM


Reply: