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

前端 未结 4 1469
梦谈多话
梦谈多话 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:56

    In the past when I have had to use an epsilon value it's been very much bigger than the machine epsilon value.

    Although it was for 32 bit doubles (rather than 64 bit doubles) we found that an epsilon value of 10-6 was needed for most (if not all) calculated values in our particular application.

    The value of epsilon you choose depends on the scale of your numbers. If you are dealing with the very large (10+10 say) then you might need a larger value of epsilon as your significant digits don't stretch very far into the fractional part (if at all). If you are dealing with the very small (10-10 say) then obviously you need an epsilon value that's smaller than this.

    You need to do some experimentation, performing your calculations and checking the differences between your output values. Only when you know the range of your potential answers will you be able to decide on a suitable value for your application.

提交回复
热议问题