为什么printf的一轮浮点数? [英] Why printf round floating point numbers?

查看:129
本文介绍了为什么printf的一轮浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打印使用一些浮点数的printf
例如:

I am trying to print some floating point numbers using printf. For example:

int main()
{
    printf("%.1f",76.75); 
    return 0;
}

输出:76.8

THE OUTPUT: 76.8

和我有结果的一些问题。

And I have some questions about the result.

首先,为什么不将其打印76.7?

First of all, why didn't it print 76.7?

其次,是如何做到的轮数?

Second, how did it round the number?

推荐答案

在除了现有的答案,注意,许多C编译器尽量遵循IEEE 754浮点事宜。 IEEE 754建议根据当前舍入模式为二进制浮点转换为十进制四舍五入。默认的舍入模式为就近舍和领带,甚至。按照默认的最近偶数模式从浮点转换为十进制一些编译平台不采取四舍五入方式考虑,总是圆的。

In addition to existing answers, note that many C compilers try to follow IEEE 754 for floating-point matters. IEEE 754 recommends rounding according to the current rounding mode for conversions from binary floating-point to decimal. The default rounding mode is "round to nearest and ties to even". Some compilation platforms do not take the rounding mode into account and always round according to the default nearest-even mode in conversions from floating-point to decimal.

由于 76.75 重新presents数百分之七千六百七十五确切地说,它是76.7和76.8之间pcisely中途$ P $。后者的数字被认为是连申请,如果是就近舍,甚至。这可能是为什么你的编译平台选择生成此十进制重新presentation作为转换到浮点数的小数 76.75

Since 76.75 represents the number 7675/100 exactly, it is precisely halfway between 76.7 and 76.8. The latter number is considered the "even" one when applying "round to nearest-even". This is probably why your compilation platform chose to generate this decimal representation as the conversion to decimal of the floating-point number 76.75.

这篇关于为什么printf的一轮浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆