I think it is worthwhile to include a table of how different methods compare when deciding about possible zero value. My advice is to use PossibleZeroQ
, but always make sure to handle/be prepared to all extrema. Let me quote the documentation:
The general problem of determining whether an expression has value
zero is undecidable; PossibleZeroQ
provides a quick but not always
accurate test.
ClearAll[x];
expr = {0, 0., 0.*10^-1, 1.`1 - 1, 0.0000000000000000000,
0 + $MachineEpsilon, 0.0001, 0. I, x};
Panel@TableForm[Transpose@{
FullForm /@ expr,
Replace[expr, {0 | 0. -> True, _ -> False}, {1}],
MatchQ[#, 0 | 0.] & /@ expr, (* same as above *)
(# === 0 \[Or] # === 0.) & /@ expr ,(*
not identical to MatchQ above! *)
# == 0 & /@ expr,
(NumericQ@# \[And] # == 0) & /@ expr,
PossibleZeroQ /@ expr (* same as NumericQ && Equal *)
}, TableHeadings -> {expr, {FullForm, "rule matching", MatchQ,
SameQ, Equal, NumericQ && Equal, PossibleZeroQ}}] /.
False -> Item[False, Background -> [email protected]]

Note that returning False
for PossibleZeroQ[x]
is not correct mathematically (e.g. x
could be zero), but sometimes one really only wants to know whether an expression has a numeric value of zero or not (and x
doesn't have a value).
Please feel free to add further methods/corrections to this table!
0.-0
gives the definitive answer:0.
$\endgroup$