是static_cast< int>(static_cast< double>(a))== a? [英] Is static_cast<int>(static_cast<double>(a)) == a?

查看:107
本文介绍了是static_cast< int>(static_cast< double>(a))== a?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的C ++专家,


我需要存储和检索可以为int或

double的元信息。如果我可以统一处理两种

类型,那么该程序将更加简单。现在,我使用的是单一界面:


void setInfo(double); //商店信息

double info(); //检索信息


并使用

setInfo(static_cast< double>(a))



static_cast< int>(info())


保存和检索整数。我还没有看到任何问题,但是我很担心,有时可能有12个可以保存为11.99999999而

被检索为11个。


非常感谢提前。

Bo

Dear C++ experts,

I need to store and retrieve a meta information that can be int or
double. The program would be significantly simpler if I can handle two
types uniformly. Right now, I am using a single interface:

void setInfo(double); // store info
double info(); // retrieve info

and use
setInfo(static_cast<double>(a))
and
static_cast<int>(info())

to save and retrieve integers. I have not seen any problem yet but I am
worried that maybe sometimes, 12 can be saved as 11.99999999 and
retrieved as 11.

Many thanks in advance.
Bo

推荐答案

2006年10月19日星期四11 :55:55 -0400,Bo Peng写道:
On Thu, 19 Oct 2006 11:55:55 -0400, Bo Peng wrote:

亲爱的C ++专家,


我需要存储和检索一个元信息可以是int或

double。如果我可以统一处理两种

类型,那么该程序将更加简单。现在,我使用的是单一界面:


void setInfo(double); //商店信息

double info(); //检索信息


并使用

setInfo(static_cast< double>(a))



static_cast< int>(info())


保存和检索整数。我还没有看到任何问题,但是我很担心,有时可能有12个可以保存为11.99999999而

被检索为11个。


非常感谢提前。

Bo
Dear C++ experts,

I need to store and retrieve a meta information that can be int or
double. The program would be significantly simpler if I can handle two
types uniformly. Right now, I am using a single interface:

void setInfo(double); // store info
double info(); // retrieve info

and use
setInfo(static_cast<double>(a))
and
static_cast<int>(info())

to save and retrieve integers. I have not seen any problem yet but I am
worried that maybe sometimes, 12 can be saved as 11.99999999 and
retrieved as 11.

Many thanks in advance.
Bo






表示C ++中的double是完全实现的定义

,所以是从double到int的转换。


如果你需要统一处理类型,但你只需要一个一次输入

使用变体。 boost :: variant可以在这里提供帮助。

Hi,

the representation of double in C++ is completly implementation defined
and so is the conversion from double to int.

If you need to handle types uniformly but you need just one type at a time
use a variant. boost::variant can help here.


Bo Peng写道:
Bo Peng wrote:

我需要存储和检索一个元信息可以是int或

double。如果我可以统一处理两种

类型,那么该程序将更加简单。现在,我使用的是单一界面:


void setInfo(double); //商店信息

double info(); //检索信息


并使用

setInfo(static_cast< double>(a))
I need to store and retrieve a meta information that can be int or
double. The program would be significantly simpler if I can handle two
types uniformly. Right now, I am using a single interface:

void setInfo(double); // store info
double info(); // retrieve info

and use
setInfo(static_cast<double>(a))



这里不需要static_cast。

There is no need for the static_cast here.




static_cast< int>(info())


来保存和检索整数。我还没有看到任何问题但是我

担心有时候,12可以保存为11.99999999并且

检索为11.
and
static_cast<int>(info())

to save and retrieve integers. I have not seen any problem yet but I
am worried that maybe sometimes, 12 can be saved as 11.99999999 and
retrieved as 11.



除非你的整数有多于你的双数所代表的数字,否则你无需担心。在现今常见的系统中,''double''

(精度为16位)可代表_any_''int''(

只有10位数。如果你想检查一下,你可以编写一个简单的

程序,它计算从INT_MIN到INT_MAX的所有整数,如果不满足你放入主题行的

条件,报告回来。


V

-

请在通过电子邮件回复时删除资金'A' br />
我没有回复最热门的回复,请不要问

Unless your integers have more digits than your double can represent,
you have nothing to worry about. In a usual system nowadays, ''double''
(which has 16 digits of precision) can represent _any_ ''int'' (which
has only 10 digits. If you want to check, you can write a simple
program that counts all integers from INT_MIN to INT_MAX and if the
condition you put in the subject line is not satisfied, report back.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Philipp Reh< se ** @ sefi.dewrote:
Philipp Reh <se**@s-e-f-i.dewrote:

> C ++中double的表示是完全实现定义的
,从double到int的转换。
>the representation of double in C++ is completly implementation defined
and so is the conversion from double to int.



我认为这有点误导,特别是第二个

一半的声明。一个双重值代表一个

的整数必须是转换任何实现中的那个整数。


双精度表示是依赖于实现的,因为你是
状态,但是如果你能确保IEEE浮点双精度

正在被使用,然后精确地表示了大范围的整数。


这是足够可靠的,所以某些语言(matlab,perl)
不需要单独的整数类型。


Steve

I think this is a little misleading, especially the second
half of the statement. A double value which represents an
exact integer must be converted to that integer in any implementation.

The representation of doubles is implementation-dependent as you
state, but if you can ensure that IEEE floating point double precision
is being used, then a large range of integers are precisely represented.

This is reliable enough such that some languages (matlab, perl)
do not bother to have a separate integer type.

Steve


这篇关于是static_cast&lt; int&gt;(static_cast&lt; double&gt;(a))== a?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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