MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

erroneous result when adding reals

  • To: mathgroup at
  • Subject: [mg100853] erroneous result when adding reals
  • From: Vlad Seghete <vlad.seghete at>
  • Date: Tue, 16 Jun 2009 21:51:42 -0400 (EDT)

Hi all,

I'm fairly new to Mathematica and today I ran into an issue that
confuses me endlessly. It has to do with simple addition of real
numbers. It seems like adding x (anything) to 0. (the real) produces a
result different from x, within something close to machine precision.
The problem becomes more serious when I do the addition in a Do loop,
like below:

Module[{tnew, tcur = 0., h = 1/100},
  Clear[ts]; ts = {tcur};     (* ts is a list *)
  Do[tcur = ts[[-1]];
   tnew = tcur + h;          (* add h to the last element of the list
   AppendTo[ts, tnew],   (* and then push it at the end of the list *)
   {step, 1, 300}             (* repead 300 times *)
ListLinePlot[Table[h, {h, 0, 3, 1/100}] - ts, InterpolationOrder ->

The plot I get is NOT constant, and the error introduced through the
"real addition" done in the Do loop is systematic and adds up to
something relatively large. Notice that if I substitute 0. (the real)
with 0 (the integer or rational), then the result is exactly like

Do any of you know why this happens and how I could avoid it, other
than working with *only* rational numbers? Thank you!

  • Prev by Date: Re: Combined Set, SetDelayed
  • Next by Date: Re: What should be a simple task....
  • Previous by thread: Re: Matrices, TraditionalForm and Two Equal Signs
  • Next by thread: Re: erroneous result when adding reals