double可以完全代表一个int吗? [英] Can a double always represent an int exactly?

查看:199
本文介绍了double可以完全代表一个int吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用表达式int a = ceil(SomeDouble)。

手册页说ceil返回最小的

整数不低于SomeDouble,代表

为双倍。但是,我的理解是

double在其整个值中具有非均匀的精度

范围。一个double总是能够确切地说
代表int类型的任何值吗?有人可以请

给我一个关于如何确保这个问题的解释,

假设类型实现的细节变化了

与平台?


谢谢。


Fred


PS我并不担心溢出int值

范围,只是保证精确表示

of int by double。

解决方案

在文章< 41 *************** @ doe.carleton.ca>,

Fred Ma< fm *@doe.carleton.ca>写道:

我正在使用表达式int a = ceil(SomeDouble)。
手册页说ceil返回最小的整数少于SomeDouble,表示为双倍。但是,我的理解是,
double在其值范围内具有不均匀的精度。双重总是能够完全表示int类型的任何值吗?有人可以请给我一个关于如何确保这一点的解释,
鉴于类型实现的细节因平台而异吗?



我不知道C标准是否指定了这个效果的
。但这是一个特定于实现的

观察。


在具有64位双精度的机器上遵循IEEE

规范,尾数部分是53位(加上一个隐藏的

位),因此整数大约2到

50次幂应该是完全可以表示的。特别是,如果

机器有32位整数,它们都可以完全代表

双倍。


我的机器,有32位整数和64位双精度,

下面给出了确切的答案:


printf("%30.15f \ n",1.0 + pow(2.0,52.));


但是下面的内容拉得太远而答案

是不准确的:


printf("%30.15f \ n",1.0 + pow(2.0,53。));


-

rr


Rouben Rostamian写道:


我不知道C标准是否指定了<这个效果。但这是一个特定于实现的观察。

在具有64位双精度的机器上,遵循IEEE规范,尾数部分为53位(加上一个隐藏的<因此,比特也是如此)因此,大约2到25的幂的整数应该是可以准确表示的。特别是,如果机器有32位整数,它们都可以完全代表双倍。

在我的机器上,它有32位整数和64位双打,
下面给出了确切的答案:

printf("%30.15f \ n",1.0 + pow(2.0,52.));

printf("%30.15f \ n",1.0 + pow(2.0,53。));




我意识到如果一个double实际上使用了两倍于

整数的位数,那么尾数应该足够大以至于不精确应该

永远不会出现。我只关心这是否可以依赖

。在发现

之后,我对于看似正常的事情的信心已经被动摇了。在某些环境中,长度与int的位数相同。

如果double具有相同的位数,该怎么办?在一些

环境中注册?其中一些位将由

指数占用,并且尾数实际上比

int少。因此,它的价值将低于整个价值范围内的整数。


Fred


< blockquote>>我正在使用表达式int a = ceil(SomeDouble)。

手册页说ceil返回的最小整数不小于SomeDouble,代表
为双倍。但是,我的理解是,
double在其值范围内具有不均匀的精度。双重总是能够完全表示int类型的任何值吗?


No.没有什么禁止实现选择

int = 64位有符号整数,而double = 64位IEEE double,其中

只有53个尾数位。范围外的整数+/- 2 ** 53

可以四舍五入。

有人可以请
指出我如何确保这一点的解释,<鉴于类型实现的细节与平台有所不同?




无法保证。


Gordon L. Burditt


I''m using the expression "int a = ceil( SomeDouble )".
The man page says that ceil returns the smallest
integer that is not less than SomeDouble, represented
as a double. However, my understanding is that a
double has nonuniform precision throughout its value
range. Will a double always be able to exactly
represent any value of type int? Could someone please
point me to an explanation of how this is ensured,
given that the details of a type realization varies
with the platform?

Thanks.

Fred

P.S. I am not worried about overflowing the int value
range, just about the guaranteed precise representation
of int by double.

解决方案

In article <41***************@doe.carleton.ca>,
Fred Ma <fm*@doe.carleton.ca> wrote:

I''m using the expression "int a = ceil( SomeDouble )".
The man page says that ceil returns the smallest
integer that is not less than SomeDouble, represented
as a double. However, my understanding is that a
double has nonuniform precision throughout its value
range. Will a double always be able to exactly
represent any value of type int? Could someone please
point me to an explanation of how this is ensured,
given that the details of a type realization varies
with the platform?



I don''t know whether the C Standard specifies anything to
this effect. But here is an implementation-specific
observation.

On a machine with 64-bit doubles which follow the IEEE
specification, the mantissa part is 53 bits (plus one hidden
bit as well) therefore integers as large as around 2 to the
50th power should be exactly representable. In particular, if
the machine has 32-bit ints, they are all exactly representable
as doubles.

On my machine, which has 32-bit ints and 64-bit doubles,
the following yields the exact answer:

printf("%30.15f\n", 1.0 + pow(2.0, 52.));

However the following stretches it too far and the answer
is inexact:

printf("%30.15f\n", 1.0 + pow(2.0, 53.));

--
rr


Rouben Rostamian wrote:


I don''t know whether the C Standard specifies anything to
this effect. But here is an implementation-specific
observation.

On a machine with 64-bit doubles which follow the IEEE
specification, the mantissa part is 53 bits (plus one hidden
bit as well) therefore integers as large as around 2 to the
50th power should be exactly representable. In particular, if
the machine has 32-bit ints, they are all exactly representable
as doubles.

On my machine, which has 32-bit ints and 64-bit doubles,
the following yields the exact answer:

printf("%30.15f\n", 1.0 + pow(2.0, 52.));

However the following stretches it too far and the answer
is inexact:

printf("%30.15f\n", 1.0 + pow(2.0, 53.));



I realize that if a double actually uses twice as many bits as
ints, the mantissa should be big enough that imprecision should
never arise. I''m just concerned about whether this can be relied
upon. My faith in what seems normal has been shaken after finding
that long has the same number of bits as int in some environments.
What if double has the same number of bits as ints in some
environments? Some of those bits will be taken up by the
exponent, and the mantissa will actually have fewer bits than an
int. Hence, it will be less precise than ints within the value
range of ints.

Fred


>I''m using the expression "int a = ceil( SomeDouble )".

The man page says that ceil returns the smallest
integer that is not less than SomeDouble, represented
as a double. However, my understanding is that a
double has nonuniform precision throughout its value
range. Will a double always be able to exactly
represent any value of type int?
No. There is nothing prohibiting an implementation from choosing
int = 64-bit signed integer, and double = 64-bit IEEE double, which
has only 53 mantissa bits. Integers outside the range +/- 2**53
may be rounded.
Could someone please
point me to an explanation of how this is ensured,
given that the details of a type realization varies
with the platform?



It is NOT ensured.

Gordon L. Burditt


这篇关于double可以完全代表一个int吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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