我正在比较常规浮点算法的值,并使用高精度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说了a
和A
转换说明符:
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/