How dangerous is it to compare floating point values?

前端 未结 11 799
囚心锁ツ
囚心锁ツ 2020-11-21 06:44

I know UIKit uses CGFloat because of the resolution independent coordinate system.

But every time I want to check if for example fram

相关标签:
11条回答
  • 2020-11-21 07:07

    I am using the following comparison function to compare a number of decimal places:

    bool compare(const double value1, const double value2, const int precision)
    {
        int64_t magnitude = static_cast<int64_t>(std::pow(10, precision));
        int64_t intValue1 = static_cast<int64_t>(value1 * magnitude);
        int64_t intValue2 = static_cast<int64_t>(value2 * magnitude);
        return intValue1 == intValue2;
    }
    
    // Compare 9 decimal places:
    if (compare(theView.frame.origin.x, 0, 9)) {
        // do important operation
    }
    
    0 讨论(0)
  • 2020-11-21 07:09

    Comparing to zero can be a safe operation, as long as the zero wasn't a calculated value (as noted in an above answer). The reason for this is that zero is a perfectly representable number in floating point.

    Talking perfectly representable values, you get 24 bits of range in a power-of-two notion (single precision). So 1, 2, 4 are perfectly representable, as are .5, .25, and .125. As long as all your important bits are in 24-bits, you are golden. So 10.625 can be repsented precisely.

    This is great, but will quickly fall apart under pressure. Two scenarios spring to mind: 1) When a calculation is involved. Don't trust that sqrt(3)*sqrt(3) == 3. It just won't be that way. And it probably won't be within an epsilon, as some of the other answers suggest. 2) When any non-power-of-2 (NPOT) is involved. So it may sound odd, but 0.1 is an infinite series in binary and therefore any calculation involving a number like this will be imprecise from the start.

    (Oh and the original question mentioned comparisons to zero. Don't forget that -0.0 is also a perfectly valid floating-point value.)

    0 讨论(0)
  • 2020-11-21 07:09

    The correct question: how does one compare points in Cocoa Touch?

    The correct answer: CGPointEqualToPoint().

    A different question: Are two calculated values are the same?

    The answer posted here: They are not.

    How to check if they are close? If you want to check if they are close, then don't use CGPointEqualToPoint(). But, don't check to see if they are close. Do something that makes sense in the real world, like checking to see if a point is beyond a line or if a point is inside a sphere.

    0 讨论(0)
  • 2020-11-21 07:11

    Since 0 is exactly representable as an IEEE754 floating-point number (or using any other implementation of f-p numbers I've ever worked with) comparison with 0 is probably safe. You might get bitten, however, if your program computes a value (such as theView.frame.origin.x) which you have reason to believe ought to be 0 but which your computation cannot guarantee to be 0.

    To clarify a little, a computation such as :

    areal = 0.0
    

    will (unless your language or system is broken) create a value such that (areal==0.0) returns true but another computation such as

    areal = 1.386 - 2.1*(0.66)
    

    may not.

    If you can assure yourself that your computations produce values which are 0 (and not just that they produce values which ought to be 0) then you can go ahead and compare f-p values with 0. If you can't assure yourself to the required degree, best stick to the usual approach of 'toleranced equality'.

    In the worst cases the careless comparison of f-p values can be extremely dangerous: think avionics, weapons-guidance, power-plant operations, vehicle navigation, almost any application in which computation meets the real world.

    For Angry Birds, not so dangerous.

    0 讨论(0)
  • 2020-11-21 07:11

    I'd say the right thing is to declare each number as an object, and then define three things in that object: 1) an equality operator. 2) a setAcceptableDifference method. 3)the value itself. The equality operator returns true if the absolute difference of two values is less than the value set as acceptable.

    You can subclass the object to suit the problem. For example, round bars of metal between 1 and 2 inches might be considered of equal diameter if their diameters differed by less than 0.0001 inches. So you'd call setAcceptableDifference with parameter 0.0001, and then use the equality operator with confidence.

    0 讨论(0)
提交回复
热议问题