Strange behavior of doubles in C++, Possible UB?

Revision en2, by ekzhang, 2017-10-03 05:47:37

I recently submitted twice to problem 865C/866C (same problem), Gotta Go Fast.

The first submission got wrong answer on test 1 on Codeforces. But I tested the solution both locally and on Ideone, and the answer was correct.

In the second submission my only change was adding the following one "useless" line of code to a for loop:

if (hi > 2 && hi < 1) cout << "this should never run" << endl;

It got AC.

My question is, why is this happening? I've tried debugging it locally and looking at the assembly, but there are no bugs when it runs on my local machine or on Ideone; it's only acting strangely on Codeforces. I've been staring at this code for many hours now thinking it might be undefined behavior, but I've tried many slightly different submissions (with long double, without the call to min) that work just fine without the extra useless if statement.

Tl;dr: I am utterly confused and would greatly appreciate if you could help me find where my UB is, or if there's some other problem with floating-point precision :-)



  Rev. Lang. By When Δ Comment
en2 English ekzhang 2017-10-03 05:47:37 2 Tiny change: '30954904\nhttp://c' -> '30954904\n\nhttp://c'
en1 English ekzhang 2017-10-03 05:47:21 1283 Initial revision (published)