是否有一个情况下,当铸造增加一倍时整失去了precision? [英] Is there a case when an integer loses its precision when casted to double?

查看:146
本文介绍了是否有一个情况下,当铸造增加一倍时整失去了precision?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

int i=25;
double j=(double)i;

有一个机会,Ĵ将值 24.9999999..upto_allowed 25.00000000 ..._ upto_allowed_minus_one_and_then_1 。我记得读这样的东西,但somehere不能够正确地回忆。

Is there a chance that j will have values 24.9999999..upto_allowed or 25.00000000..._upto_allowed_minus_one_and_then_1. I remember reading such stuff somehere but not able to recall properly.

换句话说:

有一个情况下,当铸造增加一倍时整失去了precision?

Is there a case when an integer loses its precision when casted to double?

推荐答案

对于像 25 小的数字,你是好人。对于 INT S于架构的非常大的(绝对)值,其中 INT 是64位(其值不重presentable在53位)或以上,您将松散的precision。

For small numbers like 25, you are good. For very large (absolute) values of ints on architecture where int is 64 bit (having a value not representable in 53 bits) or more, you will loose the precision.

有53位precision浮点数precision其中最显著位(隐含),通常 1

Double precision floating point number has 53 bits of precision of which Most significant bit is (implicitly) usually 1.

在哪里平台浮点再presentation不符合IEEE-754,答案可能有点不同。有关详细信息,你可以参考章节 5.2.4.2.2 C99 / C11规格的

On Platforms where floating point representation is not IEEE-754, answer may be a little different. For more details you can refer chapter 5.2.4.2.2 of C99/C11 specs

这篇关于是否有一个情况下,当铸造增加一倍时整失去了precision?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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