在我之前的问题中,我打印了一个double using cout,当我没有预料到它时,它变得圆润了。我如何才能使cout打印一个双重使用全精度?
当前回答
最轻松的……
#include <limits>
using std::numeric_limits;
...
cout.precision(numeric_limits<double>::digits10 + 1);
cout << d;
其他回答
在c++ 20中,你可以使用std::format来做到这一点:
std::cout << std::format("{}", M_PI);
输出(假设双IEEE754):
3.141592653589793
默认浮点格式是具有往返保证的最短十进制表示形式。与setprecision I/O操纵符相比,这种方法的优点是它不会打印不必要的数字。
在此期间,您可以使用{fmt}库,std::format是基于。{fmt}还提供了print函数,使这更容易和更有效(godbolt):
fmt::print("{}", M_PI);
免责声明:我是{fmt}和c++ 20 std::format的作者。
利用std::设置精度:
#include <iomanip>
std::cout << std::setprecision (15) << 3.14159265358979 << std::endl;
最轻松的……
#include <limits>
using std::numeric_limits;
...
cout.precision(numeric_limits<double>::digits10 + 1);
cout << d;
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
这是一个适用于任何浮点类型的函数,而不仅仅是双精度浮点类型,它还将流放回之后找到它的方式。不幸的是,它不能很好地与线程交互,但这是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';