“-Weverything” yielding “Comparing floating point with == or != is unsafe”

后端 未结 5 1917
无人及你
无人及你 2020-12-10 00:58

I have a string that I convert to a double like this:

double d = [string doubleValue];

The documentation for doubleValue tells

5条回答
  •  春和景丽
    2020-12-10 01:43

    Probably not necessary for this very simple use case, but here is an example of how you would account for the potential discrepancy if you were trying check if a number is equal to -1:

    #include 
    #include 
    
    int main(void) {
      float sales = -1;
    
      // basically if sales == -1
      if (fabs(1 + sales) < FLT_EPSILON) {
        return 0;
      }
    }
    

提交回复
热议问题