RE: Re: bug in IntegerPart ?
- To: mathgroup at smc.vnet.net
- Subject: [mg47799] RE: [mg47769] Re: bug in IntegerPart ?
- From: "DrBob" <drbob at bigfoot.com>
- Date: Tue, 27 Apr 2004 04:46:51 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
BaseForm doesn't show or control the representation of a number; it formats it for display. RealDigits shows the actual bits. 1.65 can't be exact in binary, even if we had a million bits available. Neither can 0.3; they round up or down in decimal depending on whether the last represented bit is 0 or 1. Rational numbers can be represented exactly only if the denominator is a power of 2. RealDigits[1.65, 2] {{1, 1, 0, 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} RealDigits[0.3, 2] {{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, 0, 1, 1}, -1} x=2^^0.010110011001100110011 Precision@x RealDigits[x,2] Length@First@% Log[2, 10^Precision[x]] 0.350000 5.8657 {{1,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,1,0},-1} 19 19.48542643406699 InputForm[x] 0.3499999046325683594`5.865697834958012 0.350000 is displayed that way because it's not a machine precision number, as it would be if you just entered 0.35. Instead, it's an arbitrary-precision number, with precision determined by the way you input it. In this case, about 19.5 binary digits, rather than the 20 you entered. Mathematica assumes the last input digit is uncertain, and it rounded up, as RealDigits shows. Put another zero out there, and you'll get: x=2^^0.0101100110011001100110 Precision@x RealDigits[x,2] Length@First@% Log[2, 10^Precision[x]] 0.350000 6.16673 {{1,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1},-1} 20 20.48542643406699 InputForm[x] 0.3499999046325683594`6.1667278306219915 DrBob www.eclecticdreams.net -----Original Message----- From: AC [mailto:ancow65 at yahoo.com] To: mathgroup at smc.vnet.net Subject: [mg47799] [mg47769] Re: bug in IntegerPart ? "DrBob" <drbob at bigfoot.com> wrote in message news:<c6g015$4lk$1 at smc.vnet.net>... > There's NO reason to be puzzled. 1.65 and 1.3 can't be represented exactly > in binary, so of course their difference may not be exact, either. Hence the > division problems have different numerators. Your 'explanation' makes no sense whatsoever. Mathematica's binary representations of 1.65-1.3 and 0.35 are the same. That can be seen by comparing BaseForm[1.65 - 1.3, 2] with BaseForm[0.35,2] The problem occurs because Mathematica changes, what is apparently intended as an exact number, 1.65-1.3 (=0.35) to 0.34999999999999987 but leaves 0.35 unchanged. Here is an additional reason for concerns. {Accuracy[#], Precision[#]} &[0.35] => {16.4105, MachinePrecision} BaseForm[0.35, 2] => 0.010110011001100110011 (n = 2^^0.010110011001100110011 ) => 0.350000 Notice the unussual display of trailing zeros. {Accuracy[#], Precision[#]} &[n] => {6.32163, 5.8657} I admit to be utterly confused by that loss of accuracy and precision. Additionally, a completely legitimate expression 2^^BaseForm[0.35`, 2] produces a syntax error message. > > RealDigits[0.35, 2] > {{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} > RealDigits[1.65 - 1.3, 2] > {{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} > > DrBob > > www.eclecticdreams.net > > > -----Original Message----- > From: Dennis de Lang [mailto:lang.NO at science.uva.nl] To: mathgroup at smc.vnet.net > Subject: [mg47799] [mg47769] 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