What are the best practices for floating-point comparisons in Matlab?

后端 未结 5 949
时光取名叫无心
时光取名叫无心 2020-12-03 11:02

Obviously, float comparison is always tricky. I have a lot of assert-check in my (scientific) code, so very often I have to check for equality of sums to one, and similar is

5条回答
  •  悲&欢浪女
    2020-12-03 11:26

    Any fixed tolerance will fail if you put in very large or very small numbers, simplest solution is to use eps to get the double precision:

    abs(A-B)

    The 4 is a totally arbitrary number, which is sufficient in most cases.

提交回复
热议问题