问题 - 从double转换为unsigned int [英] Problem - casting from double to unsigned int

查看:239
本文介绍了问题 - 从double转换为unsigned int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在VC6代码移植到VC8的项目中工作(VC ++ .Net 2005)


我得到了当我将double值转换为unsigned int时出现问题。问题是我

在铸造之后(明确地/隐含地)得到正确的值。


代码如下:

const double d_a = 100e-9; // 100ns

const double d_b = 20e-9; // 20ns

const unsigned __int32 ui_c =(unsigned __int32)(d_a / d_b)


它将ui_c输出为4,应为5。


它在VC6中完美运行。如果我在VC8中创建一个包含

这3行代码的新控制台项目,它正在工作,我正在获得适当的价值。


但是在PORTED VC8中项目是一个DLL我没有得到正确的价值。


我想知道是否应该应用任何设置来获得正确的值。


请尽快回复此查询。


提前致谢,

Vinod

Hi,

I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005)

I got a problem when I cast a double value to unsigned int. Problem is I
couldna??t get the proper value after casting (explicitly / implicitly).

Code looks as below :
const double d_a = 100e-9; // 100ns
const double d_b = 20e-9; // 20ns
const unsigned __int32 ui_c = (unsigned __int32) (d_a / d_b)

It is giving ui_c output as 4, which should be 5.

It is working perfectly in VC6. If I create a new console project containing
those 3 lines of code in VC8, it is working and I am getting proper value.

But in PORTED VC8 project which is a DLL I am NOT getting the proper value.

I want to know whether any settings should be applied to get the proper value.

Please reply to this query as soon as possible.

Thanks in advance,
Vinod

推荐答案

我在VC6代码移植到VC8的项目中工作(VC ++ .Net
I am working in the project where VC6 code is ported to VC8 (VC++ .Net

2005)


当我将一个double值转换为unsigned int时出现问题。问题是我

在投射后(显式/隐式)无法获得正确的值。


代码如下所示:

const double d_a = 100e-9; // 100ns

const double d_b = 20e-9; // 20ns

const unsigned __int32 ui_c =(unsigned __int32)(d_a / d_b)


它将ui_c输出为4,应为5。


它在VC6中完美运行。如果我创建一个新的控制台项目

包含

VC8中的那3行代码,它正在工作,我正在获得正确的价值



但在PORTED VC8项目中这是一个DLL,我没有得到正确的

值。


我想知道是否应该应用任何设置来获得正确的

值。
2005)

I got a problem when I cast a double value to unsigned int. Problem is I
couldn''t get the proper value after casting (explicitly / implicitly).

Code looks as below :
const double d_a = 100e-9; // 100ns
const double d_b = 20e-9; // 20ns
const unsigned __int32 ui_c = (unsigned __int32) (d_a / d_b)

It is giving ui_c output as 4, which should be 5.

It is working perfectly in VC6. If I create a new console project
containing
those 3 lines of code in VC8, it is working and I am getting proper
value.

But in PORTED VC8 project which is a DLL I am NOT getting the proper
value.

I want to know whether any settings should be applied to get the proper
value.



它在VC6中运行的事实意味着你很幸运。

你真的不应该期望从浮点得到精确的整数结果

操作。


搜索google for

''每个程序员应该知道浮点数''

你会发现一些解释。


解决这个问题你必须处理公差/误差幅度


-


亲切的问候,

Bruno van Dooren
BR ********************** @ hotmail.com

仅删除_nos_pam

The fact that it worked in VC6 means that you got lucky.
You really should not expect to get exact integer result from floating point
operations.

search google for
''what every programmer should know about floating point''
and you will find a number of explanations.

to work around this problem you have to work with tolerances / error margins

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"


你是对的,但它在VC8(控制台)项目中工作正常,


我不知道为什么它没有在PORTED VC8(DLL)

项目中给出正确的价值...


您对项目中的优化设置有任何了解吗? ty?

我认为这可能会导致问题。但我不确切知道。


-Thanks,

Vinod


" Bruno van Dooren" ;写道:
You are correct, But It is working fine in the VC8 (Console) project,

I don''t know why it is not giving the proper value in the PORTED VC8 (DLL)
project...

Do you have any idea about optimization setting in the project property?
I think that might cause the problem. But I don''t know exactly.

-Thanks,
Vinod

"Bruno van Dooren" wrote:

我在VC6代码移植到VC8的项目中工作(VC ++ .Net

2005)


当我将一个double值转换为unsigned int时出现问题。问题是我

在投射后(显式/隐式)无法获得正确的值。


代码如下所示:

const double d_a = 100e-9; // 100ns

const double d_b = 20e-9; // 20ns

const unsigned __int32 ui_c =(unsigned __int32)(d_a / d_b)


它将ui_c输出为4,应为5。


它在VC6中完美运行。如果我创建一个新的控制台项目

包含

VC8中的那3行代码,它正在工作,我正在获得正确的价值



但在PORTED VC8项目中这是一个DLL,我没有得到正确的

值。


我想知道是否应该应用任何设置来获得正确的

值。
I am working in the project where VC6 code is ported to VC8 (VC++ .Net
2005)

I got a problem when I cast a double value to unsigned int. Problem is I
couldn''t get the proper value after casting (explicitly / implicitly).

Code looks as below :
const double d_a = 100e-9; // 100ns
const double d_b = 20e-9; // 20ns
const unsigned __int32 ui_c = (unsigned __int32) (d_a / d_b)

It is giving ui_c output as 4, which should be 5.

It is working perfectly in VC6. If I create a new console project
containing
those 3 lines of code in VC8, it is working and I am getting proper
value.

But in PORTED VC8 project which is a DLL I am NOT getting the proper
value.

I want to know whether any settings should be applied to get the proper
value.



它在VC6中工作的事实意味着你很幸运。

你真的不应该期望从浮点获得精确的整数结果

操作。


搜索google for

''每个程序员应该知道浮点数''

你会发现一些解释。


解决这个问题你必须处理公差/误差幅度


-


亲切的问候,

Bruno van Dooren
BR ********************** @ hotmail.com

仅删除_nos_pam


The fact that it worked in VC6 means that you got lucky.
You really should not expect to get exact integer result from floating point
operations.

search google for
''what every programmer should know about floating point''
and you will find a number of explanations.

to work around this problem you have to work with tolerances / error margins

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"


>您对项目属性中的优化设置有什么了解吗?


尝试将浮点模型开关/ fp更改为适用于控制台测试项目中的任何工作。


Dave
>Do you have any idea about optimization setting in the project property?

Try changing the floating point model switch /fp to whatever works for
you in the console test project.

Dave


这篇关于问题 - 从double转换为unsigned int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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