Re: Mathematica bugs?
- To: mathgroup at smc.vnet.net
- Subject: [mg67335] Re: Mathematica bugs?
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sun, 18 Jun 2006 05:13:43 -0400 (EDT)
- Organization: Customer of PlusNet plc (http://www.plus.net)
- References: <email@example.com>
- 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? > Hello, First, I wonder if you noticed how slowly that loop executed? The reason is, of course, that the loop is building up a huge expression - which is a very expensive thing to do. You probably meant to write x=N[Pi/4] which would start off the calculation with machine real numbers and avoid this problem. However, let us return to the original calculation, which is exact until the final application of N. You can see what is happening here by adding a Print[N[x]] inside your loop. This shows you that at some point you get x=0.0 - because even though x can never be mathematically zero, when you do floating point arithmetic on it the round-off produced zero in this case (which is also why you get Indeterminate in your second example). Try this: x = Pi/4; For[i = 0, i < 56, i += 1, x = 2*Abs[x - 1/2];Print[x];Print[N[x]];Print[N[x,50]]]; N[x] Now by comparing the high precision calculation N[x,50] with the machine precision N[x] you can see how the accuracy of the answer is slowly eroded away. I do wish people would wait before announcing yet another bug in Mathematica - they do exist, but they are moderately rare! David Bailey http://www.dbaileyconsultancy.co.uk