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.
Asked
Active
Viewed 58 times
1
-
a = N[2.60000000000003694822225952521, 6] truncates the actual number. – Bill Watts Jan 30 '20 at 20:34
-
Sorry, I meant f3 where it says f1. – BillT Jan 30 '20 at 20:36
-
This general link may be of use to you. – Roman Jan 30 '20 at 21:33
-
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. – BillT Jan 30 '20 at 22:23
-
Related: (7871), (55292) – Mr.Wizard Jan 30 '20 at 22:39
-
This does seem like a dupe of (7871). – J. M.'s missing motivation Jan 30 '20 at 22:48
-
People have indicated ways to do this. But the bigger issue is that testing equality with approximate numbers is a fragile business. Most commonly, inequalities with modest tolerances are used instead. – Daniel Lichtblau Jan 30 '20 at 22:49