C++浮点数转字符串的精度问题

C++中浮点数转字符串可调用 _gcvt_s 或 _ecvt_s 或 _fcvt_s

就 函数  _gcvt_s 而言

errno_t _gcvt_s( 
   char *buffer,
   size_t sizeInBytes,
   double value,
   int digits 
);
template <size_t cchStr>
errno_t _gcvt_s( 
   char (&buffer)[cchStr],
   double value,
   int digits 
); // C++ only

 参数 digits 表示格式化后的位数(包括小数点前后)

因为浮点数为不精确的表示方式, 所以必须要指定位数, 如

char txt[1024]; auto err = _gcvt_s(txt, 3.1415926, 1000);

txt 的值并不是 "3.1415926" 而是 "3.14159260000000006840537025709636509418487548828125" (Win7x64,VS2015, VC++ v140)

可参考 printf("%f", 3.1415926) 的默认实现, 将 digits 指定为 6(错误: %nf 是指将小数点后面保留n位, 不是整个浮点数的位数)

MSVC库源文件 X:\Program Files (x86)\Windows Kits\10\Source\10.0.10240.0\ucrt\inc\corecrt_internal_stdio_output.h line: 2324

            // The default precision depends on the format specifier used.  For
            // %e, %f, and %g, C specifies that the default precision is 6.  For
            // %a, C specifies that "if the precision is missing and FLT_RADIX
            // is a power of 2, then the precision is sufficient for an exact
            // representation of the value" (C11 7.21.6.1/8).
            //
            // The 64-bit double has 53 bits of precision.  When printing in
            // hexadecimal form, we print one bit of precision to the left of the
            // radix point and the remaining 52 bits of precision to the right.
            // Thus, the default precision is 13 (13 * 4 == 52).
            if (_format_char == 'a' || _format_char == 'A')
            {
                _precision = 13;
            }
            else
            {
                _precision = 6;
            }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值