4
$\begingroup$

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.

$\endgroup$
6
  • 1
    $\begingroup$ 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? $\endgroup$ Nov 27, 2023 at 16:59
  • $\begingroup$ 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}? $\endgroup$
    – user1066
    Nov 28, 2023 at 5:02
  • $\begingroup$ @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 $\endgroup$
    – MMA13
    Nov 28, 2023 at 6:02
  • 1
    $\begingroup$ 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! $\endgroup$
    – Dúthomhas
    Nov 28, 2023 at 13:47
  • 2
    $\begingroup$ 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} $\endgroup$ Nov 28, 2023 at 15:46

3 Answers 3

6
$\begingroup$

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}

$\endgroup$
6
$\begingroup$
list = {-0.36, 0.2499999999999992, -0.21, 0.36, 0.36}

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

{-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}

$\endgroup$
1
  • 1
    $\begingroup$ 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 $\endgroup$
    – bmf
    Nov 27, 2023 at 8:46
3
$\begingroup$

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}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.