1

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
BillT
  • 23
  • 3

2 Answers2

1

An alternative to SetAccuracy is the following also

2.60000000000003694822225952521 // SetPrecision[#, 2] &
0

You can use SetAccuracy:

myvalue = 2.60000000000003694822225952521
SetAccuracy[myvalue, 2]

Out[801]= 2.6000000000000369482222595252

Out[802]= 2.6

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Dominic
  • 2,904
  • 1
  • 9
  • 14