floating-point-comparison

Comparing floats in a pandas column

邮差的信 提交于 2019-12-29 05:03:29
问题 I have the following dataframe: actual_credit min_required_credit 0 0.3 0.4 1 0.5 0.2 2 0.4 0.4 3 0.2 0.3 I need to add a column indicating where actual_credit >= min_required_credit. The result would be: actual_credit min_required_credit result 0 0.3 0.4 False 1 0.5 0.2 True 2 0.4 0.4 True 3 0.1 0.3 False I am doing the following: df['result'] = abs(df['actual_credit']) >= abs(df['min_required_credit']) However the 3rd row (0.4 and 0.4) is constantly resulting in False. After researching

Floating point equality

半城伤御伤魂 提交于 2019-12-03 06:29:01
问题 It is common knowledge that one has to be careful when comparing floating point values. Usually, instead of using == , we use some epsilon or ULP based equality testing. However, I wonder, are there any cases, when using == is perfectly fine? Look at this simple snippet, which cases are guaranteed to succeed? void fn(float a, float b) { float l1 = a/b; float l2 = a/b; if (l1==l1) { } // case a) if (l1==l2) { } // case b) if (l1==a/b) { } // case c) if (l1==5.0f/3.0f) { } // case d) } int main

Floating point equality

陌路散爱 提交于 2019-12-02 20:00:41
It is common knowledge that one has to be careful when comparing floating point values. Usually, instead of using == , we use some epsilon or ULP based equality testing. However, I wonder, are there any cases, when using == is perfectly fine? Look at this simple snippet, which cases are guaranteed to succeed? void fn(float a, float b) { float l1 = a/b; float l2 = a/b; if (l1==l1) { } // case a) if (l1==l2) { } // case b) if (l1==a/b) { } // case c) if (l1==5.0f/3.0f) { } // case d) } int main() { fn(5.0f, 3.0f); } Note: I've checked this and this , but they don't cover (all of) my cases. Note2

Why does Release/Debug have a different result for std::min?

最后都变了- 提交于 2019-11-29 02:48:37
Here is the test program: void testFunc() { double maxValue = DBL_MAX; double slope = std::numeric_limits<double>::quiet_NaN(); std::cout << "slope is " << slope << std::endl; std::cout << "maxThreshold is " << maxValue << std::endl; std::cout << "the_min is " << std::min( slope, maxValue) << std::endl; std::cout << "the_min is " << std::min( DBL_MAX, std::numeric_limits<double>::quiet_NaN()) << std::endl; } int main( int argc, char* argv[] ) { testFunc(); return 0; } In Debug, I get: slope is nan maxThreshold is 1.79769e+308 the_min is nan the_min is 1.79769e+308 In Release, I get: slope is

Why does Release/Debug have a different result for std::min?

老子叫甜甜 提交于 2019-11-27 17:05:39
问题 Here is the test program: void testFunc() { double maxValue = DBL_MAX; double slope = std::numeric_limits<double>::quiet_NaN(); std::cout << "slope is " << slope << std::endl; std::cout << "maxThreshold is " << maxValue << std::endl; std::cout << "the_min is " << std::min( slope, maxValue) << std::endl; std::cout << "the_min is " << std::min( DBL_MAX, std::numeric_limits<double>::quiet_NaN()) << std::endl; } int main( int argc, char* argv[] ) { testFunc(); return 0; } In Debug, I get: slope

What's the difference in R between identical(x, y) and isTRUE(all.equal(x, y))?

↘锁芯ラ 提交于 2019-11-26 15:58:44
问题 Is there any difference between testing isTRUE(all.equal(x, y)) and identical(x, y) ? The help page says: Don't use 'all.equal' directly in 'if' expressions-either use 'isTRUE(all.equal(....))' or 'identical' if appropriate. but that "if appropriate" leaves me in doubt. How do I decide which of the two is appropriate? 回答1: all.equal tests for near equality, while identical is more exact (e.g. it has no tolerance for differences, and it compares storage type). From ?identical: The function

Why can&#39;t I compare reals in Standard ML?

馋奶兔 提交于 2019-11-26 10:02:38
问题 Why doesn\'t 1.0 = 2.0 work? Isn\'t real an equality type? It gives the error: Error: operator and operand don\'t agree [equality type required] operator domain: \'\'Z * \'\'Z operand: real * real in expression: 1.0 = 2.0 Why won\'t reals in patterns work like so? fun fact 0.0 = 1.0 | fact x = x * fact (x - 1.0) It gives the error: Error: syntax error: inserting EQUALOP 回答1: Why doesn't 1.0 = 2.0 work? Isn't real an equality type? No. The type variable ''Z indicates that the operands of =