简体   繁体   English

C ++比较双打

[英]C++ Comparing Doubles

Is there any known problem using < or > with doubles? 将<或>与双精度一起使用是否存在任何已知问题?

d1 > d2

d2 > d1

I cannot find anything that would indicate that this would be a problem. 我找不到任何迹象表明这将是一个问题。

However, I assume this will be a problem due to the issues with equality and I will have to use "almost equal" logic using Epsilon: 但是,由于存在平等问题,我认为这将是一个问题,我将不得不使用Epsilon使用“几乎相等”的逻辑:

d1 >= d2

d2 >= d1

> and < work exactly as expected; ><完全按照预期工作; if the value of d1 is greater than the value of d2 , then d1 > d2 will be true (and vice versa). 如果d1的值大于d2的值,则d1 > d2将为true (反之亦然)。 The problem (if there is one) is that d1 and d2 might not be exactly the values you expected. 问题(如果存在)是d1d2可能与您期望的值不完全相同。

However, replacing > with >= will change the behaviour for exactly one value of d1 (for a fixed value of d2 ). 但是,用> >=替换>会改变d1一个值的行为(对于d2的固定值)。

The only problem is that you may get a 'false positive' where there are two variables d1 and d2 which you deem to be of equal value. 唯一的问题是,在您认为两个变量d1d2具有相等值的情况下,您可能会获得“误报”。 In that case if you do d1 > d2 , there is a chance the expression will evaluate to true. 在这种情况下,如果您执行d1 > d2 ,则该表达式可能会评估为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM