Please consider the following:
zeros={0.,0};
data={1, 0., 0};
DeleteCases[data, #]&/@zeros
Head/@zeros
(*{Real, Integer}*)
(*{{1, 0}, {1, 0.}}*)
For my understanding all integers (e.g. 0
, 2
) are element of the real numbers (e.g. 0.00000
, 2.00000
). So why would Mathematica not delete all zeros form data
.
I know from here that one can solve the DeleteCases
-problem via DeleteCases[Rationalize@data,0]
but this is not the point here.
EDIT
The following test may explain my problem slightly better:
sets={Integers,Reals,Complexes};
test=Table[Element[j, i], {i, sets}, {j, zeros}];
(*{{False, True}, {True, True}, {True, True}}*)
test
returns as expected that 0
is element of Integers
, Reals
and Complexes
for which reason I would expect for DeleteCases[data, #]&/@zeros
the following result:
{{1}, {1, 0.}}
For the latter 0.
can not be deleted from data
because I called delete all 0-Integers whereas in the first case all zeros can be deleted as I called delete all 0-Reals.
Note:
Instead of 0
and 0.
we could use also 2
and 2.
. The value does not matter.
DeleteCases[data, 0 | 0.]
? As far as I can tell your example works as expected. With/@
you are only deleting one type of zero at a time. $\endgroup$Table[Element[j, i], {i, sets}, {j, zeros}]
$\endgroup$0==0.
, you should read up the documentation forEqual
andSameQ
... it's fully covered between the two. $\endgroup$Element
is a mathematical operation, which is why it (correctly) says that0
is an element ofIntegers/Reals/Complexes
. However,0.
is a floating point representation of zero and is not an exact integer, henceFalse
. It certainly is an element ofReals
, and by extension, an element ofComplexes
which is a superset ofReals
, which is why it returnsTrue
.DeleteCases
uses pattern matching and relies on the heads (or theFullForm
) and does not do a mathematical comparison. This is something you need to get comfortable with in order to use Mathematica effectively. $\endgroup$