Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: [OT] Re: missing import java.lang.Comparable Date: Mon, 11 Jul 2016 13:56:20 -0400 Organization: A noiseless patient Spider Lines: 50 Message-ID: References: <128e7e20-d311-413d-abbc-e885b34ae402@googlegroups.com> <87dc5bee-f689-4f2e-90a8-a43a2de9fe49@googlegroups.com> <638a1d64-f125-4456-a20b-4dbf45f49da2@googlegroups.com> <60a5ccde-2657-48d6-b3dd-04ec1a0973ab@googlegroups.com> <6f69c451-849d-4672-9025-7880a62bd72d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 11 Jul 2016 17:56:23 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="20581"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SZAvT9K4aOkh5+W1VJTZp" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: Cancel-Lock: sha1:iLzzG/vGX3EbRMHTeBBoMdQRFps= Xref: csiph.com comp.lang.java.programmer:34851 On 7/11/2016 12:26 PM, Martin Gregorie wrote: > On Mon, 11 Jul 2016 05:30:50 -0700, Eric Douglas wrote: > >> On Friday, July 8, 2016 at 8:32:14 PM UTC-4, Arne Vajhøj wrote: >>> I don't think this example shows what you think it shows. >>> >>> It really shows that String.valueOf has an impact on the result. >>> >>> Arne >> >> Converting to a String is what makes it work. >> > Disagree: all you've shown is that integer arithmetic is exact regardless > of how the value is stored, while floating point representations are > inexact. This is why you (should have been|were) taught that you should > never compare floating point numbers for equality, instead you check that > the two values differ by less than the least significant digit, i.e. > > float a = 0.002; > float b = 0.005 - 0.003; > if (Math.abs(a - b) < 0.0005) > System.out.println("a and b differ by less than 0.0005"); > > As others have said, use floats or doubles for science and engineering > and int, long, BigInteger or BigDecimal for financials. I suspect his misunderstanding comes not so much from being unaware of computational approximations (rounding, etc.) as from matters of representation. He's disturbed that 0.0001 + 0.0002 is not exactly 0.0003, but this is less from inaccuracy of computation than from inaccuracy of representation. Neither the addend nor the augend is exactly representable as a `double', so Java substitutes the best approximations it can ("close enough for jazz"). The `+' thus adds two values that are not quite what he thinks they are, and gets a sum that is not quite what he thinks it should be (another value, as it happens, that a `double' cannot represent). I sometimes try to help people understand this by asking them to use ordinary decimal arithmetic to compute the fraction of a week that is a weekend. That is, I ask them to compute two-sevenths as a decimal fraction -- and I invite them to carry the computation out to as many places as they please, until they're satisfied that the answer is exact. Then I tell them that binary arithmetic has the same problem with any denominator that is not a power of two, and they usually get the point. Maybe Eric Douglas will, too (although since he proffered the specious claim that 1 + 1 yields 1.99999999999998, this is far from certain). -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM