floating point comparison
- To: mathgroup at smc.vnet.net
- Subject: [mg95220] floating point comparison
- From: "Bert Aerts (rm X)" <bert.aertsX at advalvas.beX>
- Date: Sun, 11 Jan 2009 06:37:13 -0500 (EST)
In the code shown below, the result of iterFixedPointList[Cos,0.0] in 94 iterations is equal to FixedPointList[Cos,0.0]. The while loop comparison happens as newV - oldV != 0.0. However if the comparison is written as newV != oldV, then the there are only 82 iterations and the result is not the final one. Why are these 2 comparisons not equal? Remark: machine precision numbers are best shown in 16 digits precision: Global Mathematica definition in Edit / Preferences : Appearance / Numbers / Formatting / Displayed Precision = 16 in Mathematica 7.0.0 x86_64 on Linux Clear[iterFixedPointList]; iterFixedPointList[function_, initValue_] := Module[{i, oldV, newV, listV}, i = 1; oldV = initValue; newV = function[initValue]; listV = {oldV, newV}; While[newV - oldV != 0.0, i++; oldV = newV; newV = function[newV]; listV = Append[listV, newV]; (* Appending newV here already before it is tested makes that the last 2 values in listV will be equal *) ]; Print["Number of iterations is ", i]; listV ]