C中的浮点数据类型 [英] Float data type in c

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

问题描述

我的疑问是:
我们写

My doubt is :
we write

float a = 25.33f;


当然,这是初始化float变量的正常方法...

现在我们也可以用这种方式做同样的事情


which is of course a normal way of initializing a float variable...

now we can also do the same thing in this way

float a = 25.33;



现在我的疑问是,据我所知,这两个语句之间是否有区别,因为据我所知,这两个语句将执行相同的操作,但是在第一个语句中,它们是25.33末尾写的"f",那么这是什么意思? 'f'表示我知道它表明所存储的数据将是浮点型的,但是第二条语句也具有相同的含义,所以为什么我们在任何浮点数的末尾都写'f'

我尝试过这段代码



now my doubt is whether their is a difference between the above two statements as far as i know both are going to perform the same thing but in the first statement their is ''f'' written at the end of 25.33 so what does that ''f'' indicate i know it indicates that the data stored will be of floating type but the second statement also means the same so why do we write ''f'' at the end of any float number

i tried this piece of code

void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
} 


上面程序的输出是c.为什么?所以
我用谷歌搜索后发现0.7是一个常数,并且将float 0.7舍入为0.699999988,如何将其舍入为0.699999988,如果是0.8
a = 0.800000011和
常数0.8是0.8000000000000000这背后的实际逻辑是什么,请有人帮助我,我会很感激

谢谢&问候
基数:rose:


Output of the above program is c. Why? so
i googled it i found that 0.7 is a constant and the float 0.7 is rounded to 0.699999988 how it is rounded to 0.699999988 and in case of 0.8
a=0.800000011 and
constant 0.8 is 0.8000000000000000 whats the actual logic behind that, please if anyone can help me out i would be thankful

Thanks & Regards
Radix :rose:

推荐答案

计算机上的浮点数可以精确地表示为2的幂的和.您可以使用这么多的二进制位数来接近任意十进制数,但您可能无法准确获得它.

因此,如果我们的目标是用二进制表示十进制数0.7:

0.1(其中.是二进制点)=> 1/2小数=> 0.5
0.11 => 3/4十进制=> 0.75
0.101 => 5/8十进制=> 0.625
0.1011 => 11/16十进制=> 0.6875
0.10111 => 23/32十进制=> 0.71875
0.101101 => 45/64十进制=> 0.703125

...

0.1011001101 => 717/1024十进制=> 0.7001953125(10位编码)

直到您用完为止.每增加3或4位可能会给您另一个精度的小数点,因此当您达到20位左右时,您只有7位左右的精度.

(数学能力更强的人可能想证明这一点,我不能!:-))

干杯,

Ash
floating point numbers on computers can represent numbers exactly that are sums of powers of two. You can get close to an arbitrary decimal number with so many binary bits but you might not be able to get it exactly.

So, if we''re aiming to represent 0.7 in decimal in binary:

0.1 (where the . is the binary point) => 1 / 2 decimal => 0.5
0.11 => 3 / 4 decimal => 0.75
0.101 => 5 / 8 decimal => 0.625
0.1011 => 11 / 16 decimal => 0.6875
0.10111 => 23 / 32 decimal => 0.71875
0.101101 => 45 / 64 decimal => 0.703125

...

0.1011001101 => 717 / 1024 decimal => 0.7001953125 (10 bit encoding)

ad nauseaum until you run out of bits. Each 3 or 4 extra bits might give you another decimal point of accuracy so by the time you get to 20 or so bits you''ve only got 7 or so digits of accuracy.

(someone better at maths might like to prove it, I can''t! :-) )

Cheers,

Ash


radix3写道:

float a = 25.33;

float a = 25.33;



这会将double隐式转换为float.



This implicitly converts the double to a float.

radix3写道:

上述程序的输出为c.为什么?

Output of the above program is c. Why?



C中的浮点数不精确.浮动精度不如两倍精度.



Floating point numbers in C are not precise. Float is less precise than double.


这篇关于C中的浮点数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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