Re: Puzzled by IntegerPart

*To*: mathgroup at smc.vnet.net*Subject*: [mg114641] Re: Puzzled by IntegerPart*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Sat, 11 Dec 2010 01:54:41 -0500 (EST)

On 12/10/10 at 2:29 AM, sebhofer at gmail.com (Sebastian) wrote: >On Dec 9, 12:00 pm, Themis Matsoukas <tmatsou... at me.com> wrote: >>IntegerPart[100*1.15] >>114 >>??? >I was going to say: Mathematica seems to floor numeric quantities >(which would kindof make sense to me), e.g. In[86]:= >In[86]:= IntegerPart[100*1.13] >IntegerPart[100*(1.13 + $MachineEpsilon)] >Out[86]= 112 >Out[87]= 113 >In[88]:= Floor[100*1.13] >Floor[100*(1.13 + $MachineEpsilon)] >Out[88]= 112 >Out[89]= 113 >On the other hand... >In[90]:= IntegerPart[100*1.12] >IntegerPart[100*(1.12 + $MachineEpsilon)] > >Out[90]= 112 >Out[91]= 112 >In[92]:= Floor[100*1.12] >Floor[100*(1.12 + $MachineEpsilon)] >Out[92]= 112 >Out[93]= 112 ... so I'm also puzzled. The puzzle is explained by looking at how these values are represented and understanding what the functions act on. That is In[1]:= RealDigits[1.12] == RealDigits[1.12 + $MachineEpsilon] Out[1]= True So, any function working on the decimal digits clearly gets the same result for both 1.12 and 1.12+$MachineEpsilon But note, In[2]:= RealDigits[1.12, 2] == RealDigits[1.12 + $MachineEpsilon, 2] Out[2]= False That is adding $MachineEpsilon does change the binary representation but does so at bits that not used in the decimal representation for this particular value. However In[3]:= RealDigits[1.13] == RealDigits[1.13 + $MachineEpsilon] Out[3]= False So, functions working on the decimal representation for 1.13 will yield a different result when $MachineEpsilon is added. The behavior depends on the precise value of the nearest machine number to whatever decimal input you make. There are a several tutorials in the documentation that expound on this further. If you want to research this further a starting point would be: tutorial/MachinePrecisionNumbers ComputerArithmetic/tutorial/ComputerArithmetic