Is there a “normal” EqualQ function in Mathematica?

后端 未结 7 665
爱一瞬间的悲伤
爱一瞬间的悲伤 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:35

    I'm not aware of an already defined operator. But you may define for example:

    longEqual[x_, y_] := Block[{$MaxPrecision = 20, $MinPrecision = 20},
                                Equal[x - y, 0.]]  
    

    Such as:

    longEqual[1.00000000000000223, 1.00000000000000223]
    True
    longEqual[1.00000000000000223, 1.00000000000000222]
    False   
    

    Edit

    If you want to generalize for an arbitrary number of digits, you can do for example:

    longEqual[x_, y_] :=
     Block[{
       $MaxPrecision =  Max @@ StringLength /@ ToString /@ {x, y},
       $MinPrecision =  Max @@ StringLength /@ ToString /@ {x, y}},
       Equal[x - y, 0.]]
    

    So that your counterexample in your comment also works.

    HTH!

提交回复
热议问题