# Rounding Errors in simple Range/Table output

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.99980000000000757154339225962758064276.


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.
– ciao
Commented Feb 22, 2017 at 9:59
• A common question, e.g. (5580), (14511), (65298), (66477), (91420) Commented Feb 22, 2017 at 10:04
• 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. Commented Feb 22, 2017 at 10:05
• @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.
– Kab
Commented Feb 22, 2017 at 11:43
• @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.
– Kab
Commented Feb 22, 2017 at 11:47

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.

• 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. Commented Feb 22, 2017 at 11:49