看到浮点值额外的precision值 [英] Extra precision values seen for float values

查看:183
本文介绍了看到浮点值额外的precision值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是该解决方案code从KN国王的现代编程方式的锻炼; Tibial。

Below is the solution code for an excercise from KN King's Modern Programming approach.

#include<stdio.h>

int main(void)
{
    int i;
    float j,x;
    scanf("%f%d%f",&x,&i,&j);
    printf("i = %d",i);
    printf("x = %f",x);
    printf("j = %f",j);
}

输入:

12.3 45.6 789

预期结果:

i = 45
x = 12.3
j = 0.6

实际结果:

i = 45
x = 12.300000
j = 0.600000

问:

为什么有见过花车额外的十进制值?是否有任何默认precision浮法设置,我能怎样改变(仅默认 - 我知道我可以使用格式字符串的printf 来控制precision值)。我使用的Windows7上的 GCC (称为Mingw)。

Why are there extra decimal values seen for floats? Is there any default precision set for float and how can I change it (Only default - I know I can use formatting strings to control precision values in printf). I am using gcc (Mingw) on windows7.

推荐答案

浮点格式记录的的数学值。他们不记录值的多少precision扫描时已经(或以其它方式获得)。 (对于这个问题,这是整数格式真了。)

The floating-point formats record only a mathematical value. They do not record how much precision the value had when scanned (or otherwise obtained). (For that matter, this is true of the integer formats too.)

当你显示一个浮点值,必须指定多少位数字显示。 C标准定义的六位数%F 说明默认: 1

When you display a floating-point value, you must specify how many digits to display. The C standard defines a default of six digits for the %f specifier:1

A 双击参数重新presenting一个浮点数转换为十进制表示在风格上的 [ - ] ddd.ddd 的,其中位数在小数点后的字符等于precision规范。如果precision丢失,它被当作6;如果precision是零,在没有指定标志,不出现小数点字符。如果出现小数点字符,至少有一个数字之前出现。该值将四舍五入到数字的适当数量。

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.

您通过在格式规范,如%一段时间后写的位数指定precision 2F 2

You specify the precision by writing the number of digits after a period in the format specifications, such as %.2f:2

在precision采用句的形式,随后要么通过一个星号的 * (后述),或通过一个可选的十进制整数( );如果只指定时间,则precision算作零。

The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero.

(星号用于指定的传递给的printf A INT 参数位数。)

(The asterisk is used to specify the number of digits in a int parameter passed to printf.)

1 Ç2011(N1570)7.21.6.1 8。

1 C 2011 (N1570) 7.21.6.1 8.

2 Ç2011(N1570)7.21.6.1 4。

2 C 2011 (N1570) 7.21.6.1 4.

这篇关于看到浮点值额外的precision值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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