丢失精度分配浮点值? [英] Loss of precision assigning floating point values?

查看:97
本文介绍了丢失精度分配浮点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查浮点数到双精度(反之亦然)

是否会导致精度损失?

解决方案

BigMan写道:

如何判断浮点数为double(反之亦然)
是否会导致精度损失?




将float赋值给double总是精确的。将

a double分配给一个浮点数会导致精度损失,如果双尾数在其尾数部分超出
的范围内有非零位数。浮动的
尾数。没有平台无关的支票

你可以做,这一切都取决于浮动的表示和

双重类型。


V




Victor Bazarov写道:

BigMan写道:

如何检查float的赋值是否为double(或者反$ b $反之亦然)会导致精度损失?



将float赋值给double总是精确的。如果double在其尾部的部分中具有超过浮点数的尾数大小的非零位,则将双精度分配给浮点将导致精度损失。没有平台无关的检查,你可以做,这一切都取决于浮动和
双重类型的表示。

V




double d = ...;

float f =(float)d;

bool loss =(d!=(double)f );





问候,

Bogdan Sintoma


Bogdan Sintoma写道:

Victor Bazarov写道:

BigMan写道:

我怎样才能检查是否将浮点数赋值为double(或者反之亦然)
会导致精度损失?



将浮点数分配给双精灵总是精确的。如果double在其尾部的部分中具有超过浮点数的尾数大小的非零位,则将双精度分配给浮点将导致精度损失。没有平台无关的检查,你可以做,这一切都取决于浮动和
双重类型的表示。

V



double d = ...;
float f =(float)d;
bool loss =(d!=(double)f);




我认为这可以衡量如果分配_has_resulted_失去了

精度,而不是它_will_结果,你不同意吗?


V


How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?

解决方案

BigMan wrote:

How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?



Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

V



Victor Bazarov wrote:

BigMan wrote:

How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?



Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

V



double d = ...;
float f = (float)d;
bool loss = (d != (double)f);

?

Regards,
Bogdan Sintoma


Bogdan Sintoma wrote:

Victor Bazarov wrote:

BigMan wrote:

How can I check if assignment of a float to a double (or vice
versa)
will result in loss of precision?



Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

V


double d = ...;
float f = (float)d;
bool loss = (d != (double)f);

?



I think this measures if the assignment _has_resulted_ in a loss of
precision, not if it _will_ result, don''t you agree?

V


这篇关于丢失精度分配浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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