1
$\begingroup$

I am working on numerical solutions to PDEs. I get some outputs which I then place back into the original equation to check my answers against expected values which are the original inputs. The results look good if I print check value next to the expected value but can sometimes result in FALSE results when tested as follows (f3==f3chk). f1 is an original input point value like 2.6 for example while f1chk might come back as 2.60000000000003694822225952521 if I ask it to display all these digits. This difference is enough to cause the equality test to fail. My question: Is there a simple way to cut off all the trailing numbers beyond 6, not just in the display but but the memory itself. I've been looking through the documentation but nothing is so obvious. I could take the difference between the two values, test the its magnitude, and if its small enough, subtract it from the check value and then proceed with the equality test. This sound like a lot of rigamarole, and I would hope mathematica would have taken care of it somewhere.

$\endgroup$
7
  • $\begingroup$ a = N[2.60000000000003694822225952521, 6] truncates the actual number. $\endgroup$
    – Bill Watts
    Commented Jan 30, 2020 at 20:34
  • $\begingroup$ Sorry, I meant f3 where it says f1. $\endgroup$
    – BillT
    Commented Jan 30, 2020 at 20:36
  • $\begingroup$ This general link may be of use to you. $\endgroup$
    – Roman
    Commented Jan 30, 2020 at 21:33
  • $\begingroup$ Thanks Bill, This was helpful. I found that I also needed to use SetPrecision[] to get the desired result. I now have it working in a simple test notebook. $\endgroup$
    – BillT
    Commented Jan 30, 2020 at 22:23
  • $\begingroup$ Related: (7871), (55292) $\endgroup$
    – Mr.Wizard
    Commented Jan 30, 2020 at 22:39

2 Answers 2

1
$\begingroup$

An alternative to SetAccuracy is the following also

2.60000000000003694822225952521 // SetPrecision[#, 2] &
$\endgroup$
0
$\begingroup$

You can use SetAccuracy:

myvalue = 2.60000000000003694822225952521
SetAccuracy[myvalue, 2]

Out[801]= 2.6000000000000369482222595252

Out[802]= 2.6

$\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.