mardi 6 février 2018

Why does 'scientific' change the meaning of 'precision'?

Here's a MWE:

#include <iostream>
#include <limits>

int main()
{
    // check that it is 17
    std::cout << std::numeric_limits<double>::max_digits10 << '\n';

    std::cout.precision(17);
    std::cout << 1.2345678901234567890 << '\n';
    std::cout.setf(std::ios_base::scientific);
    std::cout.precision(17);
    std::cout << 1.2345678901234567890 << '\n';
}

On my machine this prints:

17
1.2345678901234567
1.23456789012345669e+00

The last output has an additional digit. Why? I find that it surprising behavior, but is it what the standard dictates?

Aucun commentaire:

Enregistrer un commentaire