I get strange rounding errors from MachinePrecision values from Export. From this:
InputForm[Range[99.9997, 100.0003, 0.0001]]
I get:
{99.9997, 99.99980000000001, 99.99990000000001, 100., 100.0001, 100.0002,
100.0003}
Or (and more importantly) the same happens when I want to write the values in a file (Range and Table produce the same result):
Export["test.txt", Table[x, {x, 99.9997, 100.0003, 0.0001}]]
The file test.txt contains:
99.9997
99.99980000000001
99.99990000000001
100.
100.0001
100.0002
100.00030000000001
If I try to fix this using
Export["test.txt", SetPrecision[99.99980000000001, 6]]
I get:
99.9998000000000075715433922596275806427`6.
and finally this:
Export["test.txt", N[99.99980000000001, 4]]
gives:
99.99980000000001
It sounds like a simple task but I can't figure out an elegant solution (that is not starting to build a string for example). Can you reproduce this behaviour? I'm using Mathematica 10.0.2.0. How can I get only numbers around 100 with 4 digit precision?
Range[999997, 1000003, 1]/10000 // N
- How it is seen depends on your settings of displayed precision. See your preferences/documentation. $\endgroup$ExportString[ToString /@ Table[x, {x, 99.9997, 100.0003, 0.0001}], "Text"]
. UseNumberForm
beforeToString
for more control. $\endgroup$