首页 > 解决方案 > 为什么 mpfr_printf 与十六进制浮点数的 printf 不同(%a 转换说明符)?

问题描述

我正在比较来自常规浮点运算的值,并使用高精度 MPFR 数字作为基线。打印时,我很困惑为什么下面的代码输出不同。

MPFR 文档说输出说明符是

'a' 'A' 十六进制浮点数,C99 样式"

所以我会假设它的打印结果与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++floating-pointmpfr

解决方案


没有规范的形式。C 标准不规范第一个数字,除非它对于普通数字必须是非零的。C 2018 7.21.6.1 8 说,aA转换说明符:

表示浮点数的双精度参数以 [-]0xh.hhhhp±d 样式转换其中在小数点字符及其后的十六进制位数等于精度;…</p>


推荐阅读