Re: An arithmetic puzzle, equality of numbers.
- To: mathgroup at smc.vnet.net
- Subject: [mg103114] Re: An arithmetic puzzle, equality of numbers.
- From: Emu <samuel.thomas.blake at gmail.com>
- Date: Tue, 8 Sep 2009 05:59:22 -0400 (EDT)
- References: <h829m8$3ue$1@smc.vnet.net>
On Sep 7, 4:36 pm, Richard Fateman <fate... at cs.berkeley.edu> wrote:
> For[i = 1.11111111111111111111, i > 0, Print[i = 2*i - i]]
>
> This came to my attention (again), and I thought it might amuse
> current readers of this newsgroup.
>
> What looks like an infinite loop terminates with i==0 being true. also
> i==2 is true. (Tested in Mathematica 6.0; I don't have access to 7 yet.)
This is just due to the Precision of the calculation decreasing with
every iteration.
In[225]:= NestWhileList[2#-#&,1.11111111111111111111,#>0&]
Out[225]=
{1.1111111111111111111,1.1111111111111111111,1.1111111111111111111,1.111111111111111111,1.111111111111111111,1.11111111111111111,1.11111111111111111,1.1111111111111111,1.1111111111111111,1.111111111111111,1.111111111111111,1. 11111111111111,1.11111111111111,1.1111111111111,1.1111111111111,1.111111111111,1.111111111111,1.11111111111,1.11111111111,1.1111111111,1.1111111111,1. 111111111,1.111111111,1.111111111,1.11111111,1.11111111,1.1111111,1.1111111,1.111111,1.111111,1.11111,1.11111,1.1111,1.1111,1.111,1.111,1.11,1.11,1.1,1.1,1.,1.,0.,0.}
In[226]:= Precision/@%
Out[226]=
{20.0458,19.5686,19.0915,18.6144,18.1373,17.6602,17.183,16.7059,16.2288,15. 7517,15.2745,14.7974,14.3203,13.8432,13.3661,12.8889,12.4118,11.9347,11.4576,10.9805,10.5033,10.0262,9.54909,9.07197,8.59485,8.11773,7.6406,7.16348,6. 68636,6.20924,5.73212,5.255,4.77788,4.30076,3.82363,3.34651,2.86939,2.39227,1.91515,1.43803,0.960907,0.483786,0.00666479,0.}
Here's one simple way around it:
In[227]:= Block[{$MinPrecision=MachinePrecision,
$MaxPrecision=MachinePrecision},NestList[2#-#&,
1.11111111111111111111,100]]
Out[227]=
{1.1111111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.11111111111
1!
111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.11111 1111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111,1.111111111111111}
By the way, haven't you written about similar problems in the past?
http://forums.wolfram.com/mathgroup/archive/2001/Nov/msg00119.html
Sam