将double变量处理为布尔值 [英] Treating double variable as boolean

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

问题描述

这是更好的方式(效率/最佳实践明智)测试如果双变量等于0?

Which is the better way(efficiency/best practice wise) to test if a double variable is equal to 0?

    1. if(a_double)
          .....
       else
          .....

    OR

    2. if(a_double == 0)
          .....
       else
          .....


推荐答案

你在做什么)。我通常喜欢 if(a_double == 0.0)虽然。另外请注意,使用浮点数,通常需要进行近似比较,以便计算出四舍五入的可能性(尽管这样做可能非常简单)。

The second is generally better (more explicit about what you're doing). I'd normally prefer if (a_double == 0.0) though. Also note that with floating point, it's often useful to do an approximate comparison to account for the possibility of rounding in the calculations (though doing this well can be non-trivial).

编辑:由于似乎有一些误解,什么数字可以和不能精确地表示:大多数计算机使用二进制浮点。这意味着可以精确地表示分母是二的幂的和的分数。如果(且仅当)分母包含不能被表示为2的幂的和的素因子,则不可能精确地表示该数字。

Since there appears to be some misunderstanding about what numbers can and can't be represented precisely: most computers use binary floating point. This means a fraction in which the denominator is a sum of powers of two can be represented exactly. If (and only if) the denominator contains a prime factor that cannot be represented as a sum of powers of 2 is it impossible to represent that number precisely.

当然,如果你得到太小(或太大),它是不可能表示数字(例如,正常的IEEE浮点不提供一种方式来表示一个数字,如1e + 10000或1e-2000)。此外,当你接近表示的极限时,你放弃一些精度(例如,正常 double 的限制是1e-308 - 在1e-300,你只有得到〜7位的精度,而不是通常〜15)。

Of course, if you get too small (or too large) it can be impossible to represent the number at all (e.g., normal IEEE floating point doesn't provide a way to represent a number like 1e+10000 or 1e-2000). Also, when you approach the limits of representation, you give up some precision (e.g., the limit for a normal double is 1e-308 -- and at 1e-300, you only get ~7 digits of precision instead of the usual ~15).

然而,在一定的限制内,整数可以精确表示(取决于有效位数的大小 - - 通常约为2 53 ),因此分母可以是2的和或幂的分数(例如3.5,1.375)。

Within certainly limits, however, integers can be represented precisely (depends on the size of the significand -- usually around 253), and so can fractions where the denominator is a sum or powers of 2 (e.g., 3.5, 1.375).

也有使用十六进制浮点和/或十进制浮点的计算机。对于我们在这里考虑的,十六进制浮点基本上与二进制浮点相同(即,因为16是2的幂,所以如果分母是2的幂的和,它仍然可以仅精确地表示分数)。十进制浮点允许精确表示数字,其中分母包括5的幂(例如,1.2可以精确地表示为十进制浮点,但不是二进制或十六进制浮点)。这具有明显的优点(再次,在其范围和精度的范围内)以十进制数输入的任何内容都将被精确表示。

There are also computers that use hexadecimal floating point and/or decimal floating point. For what we're considering here, hexadecimal floating point is essentially the same as binary floating point (i.e., since 16 is a power of 2, it can still only represent fractions precisely if the denominator is a sum of powers of 2). Decimal floating point allows precise representation of numbers where the denominator includes powers of 5 as well (e.g., 1.2 can be represented precisely in decimal floating point but not in binary or hexadecimal floating point). This has the obvious advantage that (again, within the limits of its range and precision) anything you enter as a decimal number will be represented precisely.

这篇关于将double变量处理为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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