destroyer_sam's blog

By destroyer_sam, history, 3 months ago, In English

Sorry if this has been asked before. In a recent online test, I encountered an issue with vector, passed as a parameter. To solve the problem, I needed to find the upper_bound of given queries in the input and store them in a result array to return to the caller function.

The issue came up with the following vector: {0.0, 0.4, 0.9, 1.3}.

When I queried the upper_bound of 0.0, it returned index 0 instead of index 1. This happened during the online test, even though it worked correctly on my machine. This led me to believe that double comparisons might be risky due to precision issues. Perhaps 0.0 in the original vector was imprecisely stored as something like 0.000000011, causing the issue?

Could anyone confirm if this is due to floating-point precision? How to avoid this double trouble in the future!

  • Vote: I like it
  • +1
  • Vote: I do not like it