c++ - 为什么 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 标准不规范第一个数字,除非它对于普通数字必须是非零的。C 2018 7.21.6.1 8 说,a
和A
转换说明符:
表示浮点数的双精度参数以 [-]0xh.hhhhp±d 样式转换,其中在小数点字符及其后的十六进制位数等于精度;…</p>
推荐阅读
- javascript - 将样式添加到 javascript 变量
- java - 使用按钮滚动后如何返回活动顶部?
- node.js - 在nodejs中逐字获取命令行参数
- c - C:指针分配错误
- c# - 如何使用 Azure 函数或逻辑应用将数据保存到 sql 数据库中?
- reactjs - axios 不会将 post 数据发送到后端
- python - 有没有办法从 django 的 TemplateView 中的同一类中的另一个函数访问函数内部的变量
- c# - 用于 asp.net mvc 5 的 HtmlHelpr 和 asp.net core mvc 的 IHtmlHelper 的 .NET 标准扩展库
- bash - 被 bash 中的 globbing、变量替换和 find 命令的组合弄糊涂了
- git - 为什么我的第二个私有存储库没有使用我的 GitHub 部署密钥?