MathGroup Archive 2009

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

Search the Archive

Re: erroneous result when adding reals

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100907] Re: [mg100853] erroneous result when adding reals
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Thu, 18 Jun 2009 04:51:06 -0400 (EDT)
  • References: <200906170151.VAA25825@smc.vnet.net>

On Jun 16, 2009, at 9:51 PM, Vlad Seghete wrote:

> 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 result of mixing machine precision values with infinite precision  
values is a machine precision value, adding 0. to an infinite  
precision value effectively means converting that value to a machine  
precision value, which is what you observe.

> 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 ->
> 0]
>
> 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
> expected.

tcur + h requires converting h to machine precision and doing the sum  
at that precision the append then stores the machine precision result  
in ts, for some values of x/100 there is no exact machine precision  
representation and whenever you are near such a value your  
calculation will deviate from the exact precision calculation from  
that point on.

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

What are you interested in?  Doing the calculation at machine  
precision or doing it exactly?

Regards,

Ssezi



  • Prev by Date: Re: Help with HoldAll needed
  • Next by Date: Re: Help with Hold
  • Previous by thread: Re: erroneous result when adding reals
  • Next by thread: Re: erroneous result when adding reals