Re: Mathematica bugs?
- To: mathgroup at smc.vnet.net
- Subject: [mg67336] Re: Mathematica bugs?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 18 Jun 2006 05:13:45 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e70f29$rf4$1@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? No bug here, just numerical imprecision. Have a look at the *Numerical-Precision Tracking (NPT)* entry from the _Technology Guide_ at http://wolfram.com/technology/guide/precisiontracking.html To get the right answer, increase the required precision [1] as in In[27]:= x = Pi/4; For[i = 0, i < 56, i += 1, x = 2*Abs[x - 1/2]]; N[x, 20] Out[28]= 0.79387245267980382900 The following expressions may help you to better understand what's going on (machine precision vs. arbitrary precision): In[29]:= NestList[2Abs[#-1/2]&, Pi/4, 56]//N Out[29]= {0.785398, 0.570796, 0.141593, 0.716815, 0.433629, 0.132741, 0.734518, 0.469035, 0.0619298, 0.87614, 0.752281, 0.504561, 0.00912272, 0.981755, 0.963509, 0.927018, 0.854036, 0.708073, 0.416146, 0.167709, 0.664583, 0.329165, 0.341669, 0.316661, 0.366678, 0.266645, 0.466711, 0.0665783, 0.866843, 0.733687, 0.467374, 0.0652523, 0.869495, 0.738991, 0.477982, 0.0440369, 0.911926, 0.823853, 0.647705, 0.29541, 0.40918, 0.181641, 0.636719, 0.273438, 0.453125, 0.09375, 0.8125, 0.625, 0.25, 0.5, 0., 1., 1., 1., 1., -1., 3.} In[30]:= NestList[2Abs[#-1/2]&, Pi/4, 56]//N[#,20]& Out[30]= {0.78539816339744830962, 0.57079632679489661923, 0.14159265358979323846, 0.71681469282041352307, 0.43362938564082704615, 0.13274122871834590770, 0.73451754256330818460, 0.46903508512661636920, 0.061929829746767261609, 0.87614034050646547678, 0.75228068101293095356, 0.50456136202586190713, 0.0091227240517238142532, 0.98175455189655237149, 0.96350910379310474299, 0.92701820758620948597, 0.85403641517241897195, 0.70807283034483794390, 0.41614566068967588780, 0.16770867862064822441, 0.66458264275870355119, 0.32916528551740710237, 0.34166942896518579525, 0.31666114206962840950, 0.36667771586074318101, 0.26664456827851363799, 0.46671086344297272403, 0.066578273114054551945, 0.86684345377189089611, 0.73368690754378179222, 0.46737381508756358444, 0.065252369824872831112, 0.86949526035025433778, 0.73899052070050867555, 0.47798104140101735110, 0.044037917197965297793, 0.91192416560406940441, 0.82384833120813880883, 0.64769666241627761766, 0.29539332483255523532, 0.40921335033488952936, 0.18157329933022094128, 0.63685340133955811744, 0.27370680267911623488, 0.45258639464176753023, 0.094827210716464939537, 0.81034557856707012093, 0.62069115713414024185, 0.24138231426828048371, 0.51723537146343903259, 0.034470742926878065172, 0.93105851414624386966, 0.86211702829248773931, 0.72423405658497547863, 0.44846811316995095725, 0.10306377366009808550, 0.79387245267980382900} > > 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[34]:= x = Pi/4; For[i = 0, i < 50, i += 1, x = 2*Abs[x - 1/2]]; N[Log[x],20] Out[36]= -3.3676443458316025909 Regards, Jean-Marc 1. http://documents.wolfram.com/mathematica/functions/N