4
$\begingroup$

How to avoid the rounding error, when exporting data like this:

ra = Range[0.004, 0.074, 0.0005];
Export["range.txt", ra]

The result is this: enter image description here

$\endgroup$
6
  • $\begingroup$ Why? When you read this data with another program that uses floating point, the error will grow back anyway. $\endgroup$
    – John Doty
    Commented Jun 28, 2018 at 11:24
  • $\begingroup$ This is just an example, but it inflates the size of my files unnecessarily. As I am handling a huge amount of data I have to figure out some efficiency. $\endgroup$
    – Kay
    Commented Jun 28, 2018 at 11:56
  • $\begingroup$ Try something like ToString@NumberForm[#, {10, 4}] & /@ ra before exporting. Does that help? $\endgroup$
    – halirutan
    Commented Jun 28, 2018 at 12:02
  • 1
    $\begingroup$ Alternatively to @halirutan's suggestion, you can use Export["range.txt", NumberForm[ra, {10, 4}], "List"] $\endgroup$
    – Lukas Lang
    Commented Jun 28, 2018 at 12:39
  • 1
    $\begingroup$ but it inflates the size of my files unnecessarily If size is an issue why are you not writing binary files ? Not only will that ensmallen your files, it will circumvent the problem which prompted your question, and make reading/writing faster. Win win win. $\endgroup$ Commented Jun 29, 2018 at 5:57

1 Answer 1

5
$\begingroup$

You may use SetPrecision on your list, ra.

ExportString[SetPrecision[ra, 6], "Table"]
0.004
0.0045
0.005
0.0055
0.006
0.0065
0.007
0.0075
0.008
0.0085
0.009
0.0095
0.01
0.0105
...

Hope this helps.

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