为什么C的输出不同? [英] Why is the output differing in C?

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

问题描述

[code]

#include< stdio.h>

#include< conio.h>

void main()

{float a = 19234.84797;

printf(%f,a);

getch();

}

[/ code]



输出为19234.847656或其他更长的数字,如100000.000000或其他东西,而不是输出19234.84797其他。我真的很想知道这是怎么回事。请帮我。谢谢。

解决方案

您是否理解这些浮点类型仅提供近似值?在一般情况下,它们不是需要无限大量内存的实数。从第一眼看,它看起来像这种类型的可接受的准确性。在我看来,类型 float 很少被合理地使用,因为它的精度通常不令人满意,并且内存消耗的经济性并不重要或有时是虚幻的。在大多数情况下, double 更合适。



-SA

[code]
#include<stdio.h>
#include<conio.h>
void main()
{float a=19234.84797;
printf("%f",a);
getch();
}
[/code]

Instead of having 19234.84797 as output, the output is 19234.847656 or some other longer number such as 100000.000000 or something else. I really wonder how this is possible. Please help me. Thanks.

解决方案

Do you understand that those floating-point types provide only approximated values? They are not real numbers which would require infinite amount of memory in general case. From the first glance, it looks like an acceptable accuracy for this type. In my opinion, the type float is rarely reasonably used, as its precision is often unsatisfactory and the economy in memory consumption is not critical or sometimes illusionary. In most cases, double is more suitable.

—SA


这篇关于为什么C的输出不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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