为什么我的999999999的浮动金额变为10000000000? [英] Why does my float of 999999999 become 10000000000?

查看:68
本文介绍了为什么我的999999999的浮动金额变为10000000000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下代码时,它有一个问题:

When I perform the following code, it has a problem:

float number = 999999999;
printf("%lf", number);

结果是10000000000而不是999999999.

为什么不是999999999?

推荐答案

典型float 可以精确表示大约2 32 个不同的数字,例如1234.0和-0.125. 999999999不是其中之一. 10000000000.0f是最接近float的选择.

Typical float can represent exactly about 232 different numbers like 1234.0 and -0.125. 999999999 is not one of them. 10000000000.0f is the closest float alternative.

近似值是在分配过程中发生的

The approximation occurred during the assignment

float number = 999999999;  // Really assigned 10000000000.0f

这篇关于为什么我的999999999的浮动金额变为10000000000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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