4
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Range[999997, 1000003, 1]/10000 // N - How it is seen depends on your settings of displayed precision. See your preferences/documentation. $\endgroup$ – ciao Feb 22 '17 at 9:59
  • 1
    $\begingroup$ A common question, e.g. (5580), (14511), (65298), (66477), (91420) $\endgroup$ – Mr.Wizard Feb 22 '17 at 10:04
  • $\begingroup$ Try converting to strings before exporting: ExportString[ToString /@ Table[x, {x, 99.9997, 100.0003, 0.0001}], "Text"]. Use NumberForm before ToString for more control. $\endgroup$ – Mr.Wizard Feb 22 '17 at 10:05
  • $\begingroup$ @ciao I understand the display part (at least a bit ;-)), but here its more important what ends up in the text file because I use it to feed another program. Thanks anyway. $\endgroup$ – Kab Feb 22 '17 at 11:43
  • $\begingroup$ @Mr.Wizard Thanks for the links. ToString@NumberForm[...] does the job (see answer below). I'm not sure why I would profit from ExportString, though. It works without it. $\endgroup$ – Kab Feb 22 '17 at 11:47
3
$\begingroup$

Thanks to Mr.Wizard for bringing me on the right track. Here is the shortest workaround I found using NumberForm:

Export["test.txt",Table[ToString@NumberForm[x, 7], {x, 99.9997, 100.0003, 0.0001}]];

This gives:

99.9997
99.9998
99.9999
100.
100.0001
100.0002
100.0003

Or a more general solution:

min = 99.9997; max = 100.0003; step = 0.0001;
Export["test.txt", Table[ToString@NumberForm[x, StringLength@ToString[step, InputForm] + 1] , {x, min, max, step}]];

wich gives the same but determines the precision of step automatically.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Yes, this is what I was suggesting. +1 for writing it up yourself. ExportString was only as an example to keep from having to actually write a file. I do that by default without thinking. Sorry for the confusion. $\endgroup$ – Mr.Wizard Feb 22 '17 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.