Is there a “normal” EqualQ function in Mathematica?

后端 未结 7 668
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 03:49

On the documentation page for Equal we read that

Approximate numbers with machine precision or higher are considered equal if they d

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 04:13

    I propose a strategy that uses RealDigits to compare the actual digits of the numbers. The only tricky bit is stripping out trailing zeroes.

    trunc = {Drop[First@#, Plus @@ First /@ {-Dimensions@First@#, 
             Last@Position[First@#, n_?(# != 0 &)]}], Last@#} &@ RealDigits@# &;
    exactEqual = SameQ @@ trunc /@ {#1, #2} &;
    
    In[1]  := exactEqual[1.000000000000000000000000000000000000000000000000000111,
                         1.000000000000000000000000000000000000000000000000000111000]
    Out[1] := True
    In[2]  := exactEqual[1.000000000000000000000000000000000000000000000000000111,
                         1.000000000000000000000000000000000000000000000000000112000]
    Out[2] := False
    

提交回复
热议问题