I am trying to print some floating point numbers using printf
. For example:
我试图使用printf打印一些浮点数。例如:
int main()
{
printf("%.1f",76.75);
return 0;
}
THE OUTPUT: 76.8
输出:76.8
And I have some questions about the result.
我对结果有一些疑问。
First of all, why didn't it print 76.7?
首先,为什么不打印76.7?
Second, how did it round the number?
第二,它是如何围绕这个数字的呢?
3 个解决方案
#1
10
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.
除了现有的答案,请注意许多C编译器试图遵循IEEE 754的浮点问题。 IEEE 754建议根据当前舍入模式对从二进制浮点到十进制的转换进行舍入。默认的舍入模式是“舍入到最近并且与偶数相关”。某些编译平台不考虑舍入模式,并且在从浮点到十进制的转换中始终根据默认的最近偶数模式进行舍入。
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
.
由于76.75完全代表7675/100,它正好在76.7和76.8之间。当应用“舍入到最接近均匀”时,后一个数字被认为是“偶数”。这可能是您的编译平台选择生成此十进制表示形式作为浮点数76.75的十进制转换的原因。
#2
7
C99 §7.19.6.1 The
fprintf
function
f
,F
A
double
argument representing a floating-point number is converted to decimal notation in the style[−]ddd.ddd
, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the#
flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.表示浮点数的双参数在样式[ - ] ddd.ddd中转换为十进制表示法,其中小数点字符后面的位数等于精度规范。如果精度丢失,则取6;如果精度为零且未指定#flag,则不显示小数点字符。如果出现小数点字符,则在其前面至少出现一个数字。该值四舍五入到适当的位数。
#3
5
First of all, why he didn't print 76.7?
首先,他为什么不打印76.7?
Because the language specification says that the low-order digit will be rounded.
因为语言规范说低位数字将被舍入。
how he rounded the number? (some code would help)
他如何舍入这个数字? (一些代码会有所帮助)
This differs implementation-by-implementation. According to the documentation,
这与实现的实现有所不同。根据文件,
The low-order digit shall be rounded in an implementation-defined manner (emphasis added).
低位数字应以实现定义的方式舍入(强调添加)。
#1
10
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.
除了现有的答案,请注意许多C编译器试图遵循IEEE 754的浮点问题。 IEEE 754建议根据当前舍入模式对从二进制浮点到十进制的转换进行舍入。默认的舍入模式是“舍入到最近并且与偶数相关”。某些编译平台不考虑舍入模式,并且在从浮点到十进制的转换中始终根据默认的最近偶数模式进行舍入。
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
.
由于76.75完全代表7675/100,它正好在76.7和76.8之间。当应用“舍入到最接近均匀”时,后一个数字被认为是“偶数”。这可能是您的编译平台选择生成此十进制表示形式作为浮点数76.75的十进制转换的原因。
#2
7
C99 §7.19.6.1 The
fprintf
function
f
,F
A
double
argument representing a floating-point number is converted to decimal notation in the style[−]ddd.ddd
, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the#
flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.表示浮点数的双参数在样式[ - ] ddd.ddd中转换为十进制表示法,其中小数点字符后面的位数等于精度规范。如果精度丢失,则取6;如果精度为零且未指定#flag,则不显示小数点字符。如果出现小数点字符,则在其前面至少出现一个数字。该值四舍五入到适当的位数。
#3
5
First of all, why he didn't print 76.7?
首先,他为什么不打印76.7?
Because the language specification says that the low-order digit will be rounded.
因为语言规范说低位数字将被舍入。
how he rounded the number? (some code would help)
他如何舍入这个数字? (一些代码会有所帮助)
This differs implementation-by-implementation. According to the documentation,
这与实现的实现有所不同。根据文件,
The low-order digit shall be rounded in an implementation-defined manner (emphasis added).
低位数字应以实现定义的方式舍入(强调添加)。