c++ - 为什么mpfr_printf与printf的十六进制 float (%a转换说明符)不同?

标签 c++ floating-point mpfr

我正在比较常规浮点算法的值,并使用高精度MPFR数作为基准。打印时,我很困惑为什么以下代码输出不同。
MPFR documentation说输出说明符是

‘a’ ‘A’ hex float, C99 style"


因此,我认为它的输出与printf相同。然而,这种情况并非如此:
#include <boost/multiprecision/mpfr.hpp>

#include <mpfr.h>

using namespace boost::multiprecision;

int main (int argc, char* argv[])
{
    double               a = 254.5;
    mpfr_float_1000 mpfr_a = 254.5;

    mpfr_t t;
    mpfr_init2(t, 3324); // 1000 decimal precision
    mpfr_set_d(t, a, MPFR_RNDN);

    printf("double:\t%a\n", a);
    mpfr_printf("mpfr_t:\t%RNa\n", t);
    mpfr_printf("boost:\t%RNa\n", mpfr_a);
}
给出输出:
double: 0x1.fdp+7
mpfr_t: 0xf.e8p+4
boost:  0xf.e8p+4
这不是什么大问题,因为sscanf将它们解析为相同的值,但是我找不到任何说明它们为何不同的文档。
哪一个是规范的?

最佳答案

没有规范形式。 C标准不规范第一位数字,只是对于正常数字它必须为非零。 C 2018 7.21.6.1 8说了aA转换说明符:

A double argument representing a floating-point number is converted in the style [-]0xh.hhhhp±d, where there is one hexadecimal digit (which is nonzero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character and the number of hexadecimal digits after it is equal to the precision;…

关于c++ - 为什么mpfr_printf与printf的十六进制 float (%a转换说明符)不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62828959/

相关文章:

c++ - Typedef C++ 中的枚举值

c++ - avr-gcc:(看似)简单功能中不需要的序言/结尾

c++ - 在资源 DLL 之间切换

python - 如何从 Python 中字符串的前 8 个字节计算 double float ?

c++ - 来自 std::set insert() 和 find() 的写-写数据竞争?

c++ - C++中如何定义从整数到 float 的精度损失?

math - float 学有问题吗?

linux - 使用 linuxbrew 在 Ubuntu 中安装 gcc49 时出错

c++ - MSYS2 MinGW64 在 Windows 上构建 GMP/MPFR 作为静态库,并将它们链接到用 CL 编译的 MSVC 项目中

compilation - 安装MPFR(3.1.0)时出错