【发布时间】:2022-01-04 10:15:00
【问题描述】:
考虑以下 sn-p1(可测试 here):
#include <fmt/core.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
// Let's see how many digits we can print
void test(auto value, char const* fmt_str, auto std_manip, int precision)
{
std::ostringstream oss;
oss << std_manip << std::setprecision(precision) << value;
auto const std_out { oss.str() };
auto const fmt_out { fmt::format(fmt_str, value, precision) };
std::cout << std_out.size() << '\n' << std_out << '\n'
<< fmt_out.size() << '\n' << fmt_out << '\n';
}
int main()
{
auto const precision{ 1074 };
auto const denorm_min{ -0x0.0000000000001p-1022 };
// This is fine
test(denorm_min, "{:.{}g}", std::defaultfloat, precision);
// Here {fmt} stops at 770 chars
test(denorm_min, "{:.{}f}", std::fixed, precision);
}
根据{fmt}库的documentation:
precision 是一个十进制数,表示对于格式为
'f'和'F'的浮点值,或小数点前后应显示多少位数对于格式为'g'或'G'的浮点值。
这个值有限制吗?
在我发布的角落案例中,std::setprecision 似乎能够输出所有
请求的数字,而{fmt} 似乎停止在 770(公平地说,在大多数情况下,这是一个“合理”足够大的值)。有没有我们可以设置的参数来修改这个限制?
编辑
(1) 如果您想知道这些特定值的来源,我正在玩这个问答:
What is the maximum length in chars needed to represent any double value?
【问题讨论】: