Re: Decimal math bug?

*To*: mathgroup at smc.vnet.net*Subject*: [mg28802] Re: Decimal math bug?*From*: "Paul Lutus" <nospam at nosite.com>*Date*: Mon, 14 May 2001 01:32:55 -0400 (EDT)*References*: <9dlc20$o1r@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

"Jim Freeze" <jim at freeze.org> wrote in message news:9dlc20$o1r at smc.vnet.net... > On Sat, 12 May 2001, Steven M. Christensen wrote: > > > This looks like a bug. > Can someone please explain. > > This is Mathematica 4.1 (just purchased) on Linux. > > In[88]:=(.53+.9) - (1.43) == 0 > Out[88]:=False > ---------^^^^^ > > In[89]:=(.53+.9) - (.43+1.0)== 0 > Out[89]:=False > ---------^^^^^ > > In[100]:=(.53+.9) - (.43+0.8+0.2) == 0 > Out[100]:=False > ----------^^^^^ > > In[102]:=(.53+.9) - (.43+0.8+0.1 +0.1) == 0 > Out[102]:=True > ----------^^^^ > > The above results from some strange decimal math: > In[67]:=Range[.53,4,.9] > Out[103]:={0.53, 1.4300000000000002, 2.33, 3.23} > > where sometimes 1.4300000000000002 == 1.43 and sometimes > it doesn't. > > It is really disconcerting to have that trailing 2. > > Can someone please explain what is going on here? Yes, certainly. First, computers perform arithmetic using binary numbers. What you see are decimal conversions of those numbers. Sometimes a perfectly normal-looking decimal number has no finite representation in binary: 0.1 in base 10 is the repeating series 0.00011001100110011001101... in base 2. There are also numbers in binary that have no finite representation in decimal. So when you say "strange decimal math," this is not quite right. It's binary math, and decimal display. This is true for virtually all computers, all computer programs, everywhere, all the time. You just have to know it and accommodate it. The best way to accommodate it in Mathematica is to use integers where possible and use ratios, not decimals, when integers are not possible. This has the added advantage that Mathematica's algebraic manipulations are more flexible. > In[88]:=(.53+.9) - (1.43) == 0 > Out[88]:=False Second, your example is wrong. 53 + 9 = 62, not 143. 53 + 9 - 62 == 0 always, not sometimes. -- Paul Lutus www.arachnoid.com

**RE: variables versus functions**

**Re: Decimal math bug?**

**Re: Decimal math bug?**

**Re: Decimal math bug?**