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

后端 未结 5 952
时光取名叫无心
时光取名叫无心 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:48

    Don't know any special build in solution. Maybe something with using eps function?

    For example as you probably know this will give False (i.e. 0) as a result:

    >> 0.1 + 0.1 + 0.1 == 0.3
    
    ans =
    
         0
    

    But with eps you could do the following and the result is as expected:

    >> (0.1+0.1+0.1) - 0.3  < eps     
    
    ans =
    
         1
    

提交回复
热议问题