Re: Output Precision Exploration
- To: mathgroup at smc.vnet.net
- Subject: [mg118426] Re: Output Precision Exploration
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Thu, 28 Apr 2011 06:32:46 -0400 (EDT)
Log[173.5/173.5] == 0 False To see why this occurs use Trace Trace[Log[173.5/173.5] == 0] {{{{HoldForm[1/173.5], HoldForm[0.005763688760806916]}, HoldForm[173.5*0.005763688760806916], HoldForm[0.9999999999999999]}, HoldForm[Log[0.9999999999999999]], HoldForm[-1.1102230246251565*^-16]}, HoldForm[-1.1102230246251565*^-16 == 0], HoldForm[False]} Which is to say that it evaluates the quotient x/y without any prior analysis to conclude that in this case it is x/x. Use more precision Log[173.5`20/173.5`20] == 0 True Log[Rationalize[173.5/173.5]] == 0 True Or define functions that compensate for failing to use appropriate precision myLog[x_?NumericQ] := Log[Rationalize[x, 0]] // N myLog[173.5/173.5] == 0 True Bob Hanlon ---- Rafael Dunn <worthless.trash.junk at gmail.com> wrote: ============= Mathematica 8.0.1.0, Mac OSX x86 In:= Log[173.5/173.5] Out:= -1.11022*10^-16 I expect an output of exactly 0. Although 10^-16 is small, it turned out to be the largest factor in a chemical equation I was attempting to compute. I discovered this is because Mathematica does not actually evaluate 173.5/173.5 = 1. The output is actually some number 0.9999999999... However, for most decimal constants x/x produces an exact output of 1. By entering a few decimals off the top of my head I also found 1733.5, 26.44, and 27.44 do not produce an output of 1 when divided by themselves. Why? I understand Mathematica's algorithms for working with decimals must make approximations, but why is there so much variance among decimal calculations? 173.49/173.49 = 1, while 173.5/173.5 != 1. Furthermore, I find: x=173.49999999999999 x/x = 173.5/173.5, with infinite precision. If you add or remove a single 9 to the end of x, this ceases to be true. Furthermore, this looks like a contradiction to me: In:= 173.5/173.5 = 1 Log[1] = 0 Log[173.5/173.5] = 0 Out:= True True False I have learned a lot about Mathematica's precision and approximation through the help documentation, but I still can not explain this or see how I can expect Log[x/x] = 0 for the sake of calculations on the 10^-16 scale.