Is the use of machine epsilon appropriate for floating-point equality tests?

前端 未结 4 1483
梦谈多话
梦谈多话 2020-12-20 13:11

This is a follow-up to Testing for floating-point value equality: Is there a standard name for the “precision” constant?.
There is a very similar question Double.

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 13:54

    How to choose a value for epsilon?

    Short Answer: You take a small value which fits your applications needs.

    Long Answer: Nobody can know which calculations your application does and how accurate you expect your results to be. Since rounding errors sum up machine epsilon will be almost all times far too big so you have to chose your own value. Depending on your needs, 0.01 be be sufficient, or maybe 0.00000000000001 or less will.

    The question is, do you really want/need to do equality tests on floating point values? Maybe you should redesign your algorithms.

提交回复
热议问题