libstdc++/8135: fixed and scientfic output with different precisions is inconsistent with standard

a.b.taylor@sympatico.ca a.b.taylor@sympatico.ca
Thu Oct 3 12:36:00 GMT 2002


>Number:         8135
>Category:       libstdc++
>Synopsis:       fixed and scientfic output with different precisions is inconsistent with standard
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Oct 03 12:36:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Allen B. Taylor
>Release:        3.2
>Organization:
>Environment:
DJGPP: djdev203.zip, gcc32b.zip, gpp32d.zip, bnu213b.zip.
>Description:
The following program generates incorrect output for floating-point output when setting various values for floatfield flags (fixed and scientific) and precision. When fixed alone or scientific alone are set, the precision is interpreted correctly (number of digits to the right of the decimal point). When both fixed and scientific are not set (the default state of cout), precision is misinterpreted (see output). Similar, but not identical output results from having both fixed and scientific set at the same time. The standard states in section 22.2.2.2 para 8 (table 58)that if neither flag is set, or if both flags are set, then floating-point numbers should print according to printf specifier %g (or %G if uppercase is set), but I think something else is happening.
>How-To-Repeat:
#include <iostream>
#include <iomanip>

using namespace std;

const double pi = 3.1415926535,
             sm = 0.00004321;

void experiment();

int main()
{
    experiment();
    cout.setf(ios::fixed, ios::floatfield);
    experiment();
    cout.setf(ios::scientific, ios::floatfield);
    experiment();
    cout.setf(ios::fixed | ios::scientific, ios::floatfield);
    experiment();

    return 0;
}

void experiment()
{
    cout << "floatfield:";
    ios::fmtflags f = cout.flags();

    if (f & ios::fixed)
        cout << " fixed";
    if (f & ios::scientific)
        cout << " scientific";

    cout << endl;

    for (int x = 0; x < 10; x++)
        cout << x << ". " << setprecision(x)
             << setw(20) << pi
             << setw(20) << sm
             << endl;
}
>Fix:
None.
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list