为什么最终输出自行舍入?
在下面的代码中,
#include< stdio.h> 浮子区域(浮动VAR) { 返回(var*var); } int main() { 浮动一侧; printf(“ \ nenter在广场的侧面:”); scanf(“%f”,& side); ** printf(“区域为:%.1f”,区域(侧)); ** 返回0; }
输入为15.5,如果使用格式“%.1F”,则输出为:
240.3
以及何时,
输入为15.5,格式指定符“%.2F”,然后输出为:
240.25(实际上是正确的值)
Why does the value gets rounded off when format specifier is "%.1f", instead of just printing upto the first decimal place and the output being 240.2 ?
In the code below,
#include <stdio.h> float area(float var) { return (var*var); } int main() { float side; printf("\nEnter the side of square : "); scanf("%f",&side); **printf("The area is : %.1f",area(side));** return 0; }
Input is 15.5, if format specifier "%.1f" is used then the output is:
240.3
and when,
Input is 15.5, with format specifier "%.2f" then the output is :
240.25 (which is actually the correct value)
Why does the value gets rounded off when format specifier is "%.1f", instead of just printing upto the first decimal place and the output being 240.2 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
规格要求四舍五入。
f
格式指定符,C17规格说注意最后一句话。
现在,有多种舍入的方式。我认为
printf
被指示旨在纪念fesetround
,这在我的系统上也不例外。demo 在编译器资源管理器上,
我的系统默认值default to found to found。当它需要选择两个数字中的哪个最接近(例如0.5)时,它圆满到最接近的数字。
demo 在编译器资源管理器
上因为5/10可以由浮点数准确表示。)
我不知道规格是否要求默认为四舍五入到最接近,但这是自然而然的事情数字中的十进制位置。
如果不这样做,它也会产生很多奇怪的结果。您是否期望
printf(“%.1f \ n”,0.3);
打印0.2
?好吧,如果您舍入而不是四舍五入到最近。二进制中的数字是周期性的,包括1/10、2/10、3/10、4/10、6/10、7/10、7/10、8/10和9/10。这些不能完全使用浮点号来准确表示。理想情况下,编译器使用最近的代表数字,有时该数字有时更高,有时稍低一些。
demo 在编译器资源管理器上,
如果
printf
是truncate,printf(
printf)(
printf(printf)( “%.1f \ n”,0.3);
将打印0.2
。demo 在编译器资源管理器上
,最后我找不到有关如何四舍五入到最近的规格中的任何东西。甚至可以回合的决定似乎是编译器的决定。
这条打题规则没有正/负偏见,也没有偏见/远离零的偏见,这使其自然而然。它甚至是“ IEEE 754操作中使用的默认舍入模式,用于二进制浮点格式的结果”。
The spec mandates rounding. Of the
f
format specifier, the C17 spec saysNote the last sentence.
Now, there are many ways of rounding. I think
printf
is directed to honour the "directed rounding mode" set byfesetround
, and it's no exception on my system.Demo on Compiler Explorer
My system defaults to rounding to nearest. When it needs to choose which of two numbers are nearest (e.g. for 0.5), it rounds to nearest even number.
Demo on Compiler Explorer
(I used
.0f
and.5
since 5/10 can be represented exactly by a floating point number.)I don't know if the spec mandates defaulting to round to nearest or not, but that's the natural thing to do when one wants to reduce the number of decimal places in a number.
It would also produce a lot of weird results if it didn't do this. Would you expect
printf( "%.1f\n", 0.3 );
to print0.2
? Well, it would if you rounded down instead of rounding to nearest.So many number are periodic in binary, including 1/10, 2/10, 3/10, 4/10, 6/10, 7/10, 8/10 and 9/10. These can't be represented exactly using floating point numbers. Ideally, the compiler uses the nearest representable number instead, and this number is sometimes a little higher, sometimes a little lower.
Demo on Compiler Explorer
If
printf
were to truncate,printf( "%.1f\n", 0.3 );
would print0.2
.Demo on Compiler Explorer
Finally, I don't find anything in the spec about how to round to nearest. The decision to round to even appears to be the compiler's.
This tie-breaking rule has no positive/negative bias and no bias toward/away from zero, making it a natural choice. It's even "the default rounding mode used in IEEE 754 operations for results in binary floating-point formats."