将float转换为double [英] converting float to double

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

问题描述



最近在我们的代码中,我遇到了一个在一个double中填充

浮点数的情况。由于这个原因,精度自动延长了

。总而言之,这导致了系统另一部分其他地方出现问题,而这个数字被用于某种计算和一些最终的截断导致系统进入

haywire。所以我的问题是,鉴于此代码:


int main()

{

float f = 59.89F;

/ *以下行导致问题* /

双d = f;


/ *他们最终解决了这个问题* /

f * = 1000.0;

double dd =(double)f / 1000.0;


返回0;

}


我看到正在做什么,但为什么后者会成功?

更好?

(考虑现实生活中的''f'以保持股价)


Recently in our code, I ran into a situation where were stuffing a
float inside a double. The precision was extended automatically
because of that. To make a long story short, this caused problems
elsewhere in another part of the system where that figure was used for
some calculation and some eventual truncation led to the system going
haywire. So my question is, given this code:

int main()
{
float f = 59.89F;
/* the following line caused problems */
double d = f;

/* they ended up solving it like this */
f *= 1000.0;
double dd = (double)f / 1000.0;

return 0;
}

I see what is being done but why does the latter make the situation
better?
(consider ''f'' in real life to hold stock prices)

推荐答案



Dilip写道:

Dilip wrote:

最近在我们的代码中,我遇到了一个在一个double中填充

浮点数的情况。由于这个原因,精度自动延长了

。总而言之,这导致了系统另一部分其他地方出现问题,而这个数字被用于某种计算和一些最终的截断导致系统进入

haywire。所以我的问题是,鉴于此代码:


int main()

{

float f = 59.89F;

/ *以下行导致问题* /

双d = f;


/ *他们最终解决了这个问题* /

f * = 1000.0;

double dd =(double)f / 1000.0;


返回0;

}


我看到正在做什么,但为什么后者会成功?

更好?

(在现实生活中考虑''f'来持有股票价格)
Recently in our code, I ran into a situation where were stuffing a
float inside a double. The precision was extended automatically
because of that. To make a long story short, this caused problems
elsewhere in another part of the system where that figure was used for
some calculation and some eventual truncation led to the system going
haywire. So my question is, given this code:

int main()
{
float f = 59.89F;
/* the following line caused problems */
double d = f;

/* they ended up solving it like this */
f *= 1000.0;
double dd = (double)f / 1000.0;

return 0;
}

I see what is being done but why does the latter make the situation
better?
(consider ''f'' in real life to hold stock prices)



不是个好主意...


浮动这一点本质上是不精确的 - 乘以1000简单地

调整不精确的方式对这个具体的b / b
案例有帮助,但在所有情况下都可能没有帮助( AFAIK)。


如果你想安全准确地处理这类数据,来源

http://www2.hursley.ibm.com/decimal/ 可能会有所帮助。

Not a good idea...

Floating point is by its nature imprecise - multiplying by 1000 simply
adjusts the imprecision in a way which was helpful in this specific
case, but may not be helpful in all cases (AFAIK).

If you want to safely and accurately handle this sort of data, sources
such as http://www2.hursley.ibm.com/decimal/ may be helpful.


Dilip写道:
Dilip wrote:

>

最近在我们的代码中,我遇到了一个在double中填充

浮点数的情况。由于这个原因,精度自动延长了

。长话短说,这导致了系统另一部分其他地方出现问题,其中使用了这个数字

用于某些计算,最终截断导致系统

走向干线。所以我的问题是,鉴于此代码:


int main()

{

float f = 59.89F;

/ *以下行导致问题* /

双d = f;


/ *他们最终解决了这个问题* /

f * = 1000.0;

double dd =(double)f / 1000.0;


返回0;

}


我看到正在做什么,但为什么后者使

的情况更好?

(在现实生活中考虑''f'来持有股票价格)
>
Recently in our code, I ran into a situation where were stuffing a
float inside a double. The precision was extended automatically
because of that. To make a long story short, this caused problems
elsewhere in another part of the system where that figure was used
for some calculation and some eventual truncation led to the system
going haywire. So my question is, given this code:

int main()
{
float f = 59.89F;
/* the following line caused problems */
double d = f;

/* they ended up solving it like this */
f *= 1000.0;
double dd = (double)f / 1000.0;

return 0;
}

I see what is being done but why does the latter make the
situation better?
(consider ''f'' in real life to hold stock prices)



它没有。它应该让它出错。要么你不是b $ b显示真实情况,要么你有编译器错误。


或者,如果你依赖浮标的确切值

和/或双打,你的代码坏了。由于股票价格(今天)

是十进制的,只需使用合适大小的标准化整数。

读取Knuth(TAOCP)浮点数和预期错误。


-

Chuck F(cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>

It doesn''t. It should make it dead wrong. Either you are not
showing the real situation or you have a compiler bug.

Alternatively, if you are depending on exact values from floats
and/or doubles, your code is broken. Since stock prices (today)
are decimalized, just use a normalized integer of suitable size.
Read Knuth (TAOCP) on floating point and expected errors.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


Dilip写道:
Dilip wrote:

>

最近在我们的代码中,我遇到了一个在一个double中填充

浮点数的情况。由于这个原因,精度自动延长了

。总而言之,这导致了系统另一部分其他地方出现问题,而这个数字被用于某种计算和一些最终的截断导致系统进入

haywire。所以我的问题是,鉴于此代码:


int main()

{

float f = 59.89F;

/ *以下行导致问题* /

双d = f;


/ *他们最终解决了这个问题* /

f * = 1000.0;

double dd =(double)f / 1000.0;


返回0;

}


我看到正在做什么,但为什么后者会成功?

更好?

(在现实生活中考虑''f'来持有股票价格)
>
Recently in our code, I ran into a situation where were stuffing a
float inside a double. The precision was extended automatically
because of that. To make a long story short, this caused problems
elsewhere in another part of the system where that figure was used for
some calculation and some eventual truncation led to the system going
haywire. So my question is, given this code:

int main()
{
float f = 59.89F;
/* the following line caused problems */
double d = f;

/* they ended up solving it like this */
f *= 1000.0;
double dd = (double)f / 1000.0;

return 0;
}

I see what is being done but why does the latter make the situation
better?
(consider ''f'' in real life to hold stock prices)



我不会开始使用像这样的浮动变量。


当你需要最小的浮点类型时,使用float,




使用long double,

当你需要浮点类型

时最精确和/或范围。


使用双倍,

其余所有时间。


-

pete

I wouldn''t have used a float variable like that to begin with.

Use float,
when you need the smallest floating point type.

Use long double,
when you need the floating point type
with most precision and or range.

Use double,
all the rest of the time.

--
pete


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

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