RE: bug in IntegerPart ?
- To: mathgroup at smc.vnet.net
- Subject: [mg47787] RE: [mg47736] bug in IntegerPart ?
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 27 Apr 2004 04:46:26 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Dennis de Lang [mailto:lang.NO at science.uva.nl] To: mathgroup at smc.vnet.net >Sent: Saturday, April 24, 2004 10:16 AM >To: mathgroup at smc.vnet.net >Subject: [mg47787] [mg47736] bug in IntegerPart ? > > >Why does the following happen? > >In[1]:= (1.65 - 1.3)/0.007 >Out[1]:= 50. >In[2]:= IntegerPart[%] >Out[2]:= 49 > >but: > >In[1]:= 0.35/0.007 >Out[1]:= 50. >In[2]:= IntegerPart[%] >Out[2]:= 50 > >I'm puzzled.... >- Dennis > > To answer the why: simply because this is not the same! This is a basic fact of finite machine arithmetic. In[1]:= (1.65 - 1.3)/0.007 Out[1]= 50. In[2]:= {IntegerPart[%], FractionalPart[%]} Out[2]= {49, 1.} In[3]:= %% // FullForm Out[3]//FullForm= 49.999999999999986` In[4]:= 0.35/0.007 Out[4]= 50. In[5]:= {IntegerPart[%], FractionalPart[%]} Out[5]= {50, 0.} In[6]:= %% // FullForm Out[6]//FullForm= 50.` You see the minute difference of the numerators in the binary digits: In[7]:= RealDigits[.35, 2] Out[7]= {{1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0}, -1} In[8]:= RealDigits[(1.65 - 1.3), 2] Out[8]= {{1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0}, -1} (Details may be different on your machine.) -- Hartmut Wolf