How to properly compare an integer and a floating-point value?

前端 未结 6 1776
遥遥无期
遥遥无期 2021-01-12 12:02

How do I compare an integer and a floating-point value the right way™?

The builtin comparsion operators give incorrect results in some edge cases, for examp

6条回答
  •  失恋的感觉
    2021-01-12 12:49

    Use double, not float. Take the double value + 0.5. Truncate it by static cast to long long. Now compare the two long longs.

提交回复
热议问题