在我之前的问题中,我打印了一个double using cout,当我没有预料到它时,它变得圆润了。我如何才能使cout打印一个双重使用全精度?


当前回答

c++ 20 std::格式

这个伟大的c++库新特性的优点是不会像std::setprecision那样影响std::cout的状态:

#include <format>
#include <string>

int main() {
    std::cout << std::format("{:.2} {:.3}\n", 3.1415, 3.1415);
}

预期的输出:

3.14 3.142

正如https://stackoverflow.com/a/65329803/895245中提到的,如果不显式传递精度,它会打印带有往返保证的最短十进制表示。TODO更详细地了解它与:dbl::max_digits10的比较,如https://stackoverflow.com/a/554134/895245中显示的{:.{}}:

#include <format>
#include <limits>
#include <string>

int main() {
    std::cout << std::format("{:.{}}\n",
        3.1415926535897932384626433, dbl::max_digits10);
}

参见:

返回c++中的默认浮点打印精度,以了解如何恢复pre-c++20中的初始精度 Std::string格式,如sprintf https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification

其他回答

在这个问题中,有一个关于如何无损地将double类型转换为字符串的描述(在Octave中,但它可以很容易地在c++中重现)。我们的想法是对浮点数有一个人类可读的简短描述和一个十六进制形式的无损描述,例如:pi -> 3.14{54442d18400921fb}。

这是一个适用于任何浮点类型的函数,而不仅仅是双精度浮点类型,它还将流放回之后找到它的方式。不幸的是,它不能很好地与线程交互,但这是iostreams的本质。你需要在文件的开头包含以下内容:

#include <limits>
#include <iostream>

这是这个函数,如果你经常使用它,你可以把它放在头文件中:

template <class T>
void printVal(std::ostream& os, T val)
{
    auto oldFlags = os.flags();
    auto oldPrecision = os.precision();

    os.flags(oldFlags & ~std::ios_base::floatfield);
    os.precision(std::numeric_limits<T>::digits10);
    os << val;
    
    os.flags(oldFlags);
    os.precision(oldPrecision);
}

像这样使用它:

double d = foo();
float f = bar();
printVal(std::cout, d);
printVal(std::cout, f);

如果你想使用普通的插入<<操作符,你可以使用额外的包装器代码:

template <class T>
struct PrintValWrapper { T val; };
template <class T>
std::ostream& operator<<(std::ostream& os, PrintValWrapper<T> pvw) {
    printVal(os, pvw.val);
    return os;
}
template <class T>
PrintValWrapper<T> printIt(T val) {
    return PrintValWrapper<T>{val};
}

现在你可以这样使用它:

double d = foo();
float f = bar();
std::cout << "The values are: " << printIt(d) << ", " << printIt(f) << '\n';

c++ 20 std::格式

这个伟大的c++库新特性的优点是不会像std::setprecision那样影响std::cout的状态:

#include <format>
#include <string>

int main() {
    std::cout << std::format("{:.2} {:.3}\n", 3.1415, 3.1415);
}

预期的输出:

3.14 3.142

正如https://stackoverflow.com/a/65329803/895245中提到的,如果不显式传递精度,它会打印带有往返保证的最短十进制表示。TODO更详细地了解它与:dbl::max_digits10的比较,如https://stackoverflow.com/a/554134/895245中显示的{:.{}}:

#include <format>
#include <limits>
#include <string>

int main() {
    std::cout << std::format("{:.{}}\n",
        3.1415926535897932384626433, dbl::max_digits10);
}

参见:

返回c++中的默认浮点打印精度,以了解如何恢复pre-c++20中的初始精度 Std::string格式,如sprintf https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification

上ostream::精密(int)

cout.precision( numeric_limits<double>::digits10 + 1);
cout << M_PI << ", " << M_E << endl;

将产生

3.141592653589793, 2.718281828459045

为什么说“+1”我不知道,但你多出来的数字是正确的。

你可以直接在std::cout上设置精度,并使用std::fixed格式说明符。

double d = 3.14159265358979;
cout.precision(17);
cout << "Pi: " << fixed << d << endl;

您可以#include <limits>来获得浮点数或双精度浮点数的最大精度。

#include <limits>

typedef std::numeric_limits< double > dbl;

double d = 3.14159265358979;
cout.precision(dbl::max_digits10);
cout << "Pi: " << d << endl;