Re: Mathematica bugs?

• To: mathgroup at smc.vnet.net
• Subject: [mg67323] Re: [mg67301] Mathematica bugs?
• From: "Carl K. Woll" <carlw at wolfram.com>
• Date: Sun, 18 Jun 2006 05:13:17 -0400 (EDT)
• References: <200606170836.EAA27990@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Yaroslav Bulatov wrote:
> When I run the following line
> x = Pi/4; For[i = 0, i < 56, i += 1, x = 2*Abs[x - 1/2]]; N[x]
> I get
> 3.
>
> But x should always stay between 0 and 1, why do I get 3?
>
> Also
> x = Pi/4; For[i = 0, i < 50, i += 1, x = 2*Abs[x - 1/2]]; N[Log[x]]
> gives me
> Indeterminate
>
> How can I get an indeterminate here?

In[14]:= x = Pi/4; For[i = 0, i < 5, i += 1, x = 2*Abs[x - 1/2]]; {x, N[x]}

Out[14]= {2 (1/2 - 2 (-1/2 + 2 (1/2 - 2 (-1/2 + 2 (-1/2 + Ï?/4))))),
0.132741}

You see that Mathematica is smart enough to figure out that x-1/2 is
positive, so that the Abs is unnecessary. On the other hand, Mathematica
is not expanding x out, so x becomes a bigger and bigger expression as i
increases. Eventually, the expression involves so many multiplications
and subtractions that applying N to x experiences catastrophic numerical
cancellations.

The solution is simple. Use extended precision numbers:

In[16]:= x = Pi/4; For[i = 0, i < 56, i += 1, x = 2*Abs[x - 1/2]]; N[x, 20]

Out[16]= 0.79387245267980382900

You can even use extended precision numbers with only 2 digits of precision:

In[17]:= x = Pi/4; For[i = 0, i < 56, i += 1, x = 2*Abs[x - 1/2]]; N[x, 2]

Out[17]= 0.79

Carl Woll
Wolfram Research

```

• Prev by Date: Re: Problem with derivate of product
• Next by Date: Re: Problem with derivate of product
• Previous by thread: Re: Mathematica bugs?
• Next by thread: Re: Mathematica bugs?