# How to remove a high accuracy numerical number from a list?

My numerical calculation gives a list of numbers

list = {-0.36, 0.2499999999999992, -0.21, 0.36, 0.36},

I want to delete 0.25 which appears as 0.2499999999999992 in the list due to the numerical process. How can I remove such a number? I tried

DeleteCases[Chop@list, {0.25}]

but did not work.

• It's not really "highly accurate" so much as not showing a more desired value. Do you want to remove it entirely, or just replace it with 0.25? Commented Nov 27, 2023 at 16:59
• What do you want to happen if the list also contains 0.25 or other numbers such as 0.149999999999992? For example, what is the desired output of list2 = {-0.36, 0.2499999999999992, -0.21, 0.36, 0.36, 0.25, 0.149999999999992}? Commented Nov 28, 2023 at 5:02
• @user1066, I want to remove all numerical numbers that are almost 0.25 e.x 0.2499999999999991, 0.2499999999999992, or exactly 0.25. It would be nice also if the new list list2 also showed only a few significant numbers say 4 such that 0.149999999999992 becomes 0.15 Commented Nov 28, 2023 at 6:02
• I think you are Making A Mistake. It is not accuracy that you are removing — the extra digits is a consequence of how floating-point numbers work. In other words, you are arbitrarily tossing data from your list. Most likely the correct way of handling this is simply filtering the numbers by rounding to a certain precision (such as to hundredths), but only for display. Care must be taken that this rounding pass does not propagate elsewhere or you will introduce bias into your dataset! Commented Nov 28, 2023 at 13:47
• One typically does this using a numeric comparison and a threshold. Here are a couple of variants. In[226]:= list = {-0.36, 0.2499999999999992, -0.21, 0.36, 0.36}; DeleteCases[list, vv_ /; Abs[vv - 1/4] < 1/1000] Out[227]= {-0.36, -0.21, 0.36, 0.36} In[229]:= ResourceFunction["Discard"][list, Abs[# - 1/4] < 1/1000 &] Out[229]= {-0.36, -0.21, 0.36, 0.36} Commented Nov 28, 2023 at 15:46

Not very elegant, but since you explicitly mention that you want to remove 0.2499999999999992

list /. (0.2499999999999992 -> Nothing)


or if you want to delete 0.25 explicitly

Round[list, 0.01] /. (0.25 -> Nothing)


and get

{-0.36, -0.21, 0.36, 0.36}

list = {-0.36, 0.2499999999999992, -0.21, 0.36, 0.36}

Delete[list,
Position[Round[list, 0.01] - list, Except[0.], {1, ∞},


{-0.36, -0.21, 0.36, 0.36}

Note

It has the same accuracy and precision as other numbers in the list.

Accuracy /@ list
Precision /@ list


{16.3983, 16.5566, 16.6324, 16.3983, 16.3983} {MachinePrecision, MachinePrecision, MachinePrecision, \ MachinePrecision, MachinePrecision}

• It's a (+1) from me of course, just a comment: you can forget Heads -> False and just use // List at the end. Very minor comment actually
– bmf
Commented Nov 27, 2023 at 8:46

Using Cases and FractionalPart:

Cases[If[Abs[#] < 1, FractionalPart[#], #] & /@ list, x_ /; x != 0.25]

(*{-0.36, -0.21, 0.36, 0.36}*)


Or using Select:

Select[If[Abs[#] < 1, FractionalPart[#], #] & /@ list, # != 0.25 &]

(*{-0.36, -0.21, 0.36, 0.36}*)


Or using DeleteCases:

DeleteCases[list, x_ /; x == 0.25]

(*{-0.36, -0.21, 0.36, 0.36}*)


Or using ReplaceAll:

list /. x_ /; x == 0.25 :> Nothing

(*{-0.36, -0.21, 0.36, 0.36}*)