浮法VS双 [英] Float vs Double

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

问题描述

是否有过两个浮点数值之间,其中一个比较(等于())的情况,将返回如果你比较他们为 DOUBLE ,但返回真正如果您比较它们作为FLOAT?

Is there ever a case where a comparison (equals()) between two floating point values would return false if you compare them as DOUBLE but return true if you compare them as FLOAT?

我写了一些程序,作为我的组项目的一部分,来比较任何类型的两个数值。这里是4种我不得不应付干脆:双击浮动 INT 。所以我想组双击浮动成一个功能,那就是我刚刚施放任何浮动双击并做比较。

I'm writing some procedure, as part of my group project, to compare two numeric values of any given types. There're 4 types I'd have to deal with altogether : double, float, int and long. So I'd like to group double and float into one function, that is, I'd just cast any float to double and do the comparison.

这会导致任何不正确的结果?

Would this lead to any incorrect results?

感谢。

推荐答案

如果你正在转换双打花车和它们之间的区别是超出了float类型的precision,你可以遇到麻烦。

If you're converting doubles to floats and the difference between them is beyond the precision of the float type, you can run into trouble.

举例来说,假设你有两个double值:

For example, say you have the two double values:

9.876543210
9.876543211

和一个浮动的precision只有六个小数位数。这将意味着,无论浮动值将是 9.87654 ,因此平等的,即使双值本身是不相等的。

and that the precision of a float was only six decimal digits. That would mean that both float values would be 9.87654, hence equal, even though the double values themselves are not equal.

不过,如果你在谈论的花车被转换为双打,那么相同的花车应该给你相同的双打。如果彩车是不同的,额外的precision将确保双打是截然不同的为好。

However, if you're talking about floats being cast to doubles, then identical floats should give you identical doubles. If the floats are different, the extra precision will ensure the doubles are distinct as well.

这篇关于浮法VS双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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