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 15:09:27 -0400 Organization: A noiseless patient Spider Lines: 67 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: 7bit Injection-Date: Mon, 11 Jul 2016 19:09:30 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e905b254ff7ab0d0dda2c4195925adb8"; logging-data="5458"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FCwCtCroQAbH3Mi6eyBzf" 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:GHnov/WcKVoqDtx036FRZBM6KjA= Xref: csiph.com comp.lang.java.programmer:34856 On 7/11/2016 1:59 PM, Eric Douglas wrote: > On Monday, July 11, 2016 at 1:56:35 PM UTC-4, Eric Sosman wrote: >> Maybe Eric Douglas will, too (although since he proffered the specious >> claim that 1 + 1 yields 1.99999999999998, this is far from certain). > > I was certain it did that once with a whole number (using the double primitive) but I don't have the test program offhand, so maybe it is only with the fractional amounts. That's why I suspect you haven't grasped the representational issue. A `double' can represent, exactly, all integers in the range -9007199254740991 through 9007199254740991 (-2**53+1 through 2**53-1). In particular, it can represent both 1's and their sum 2 exactly. Outside that range, `double' starts to skip some integers: For a while it can represent only the even numbers, then only the numbers divisible by four, then by eight, and so on. If you're summing numbers with magnitudes in excess of nine quadrillion or so, expect inaccuracies to creep in. In the other direction, `double' can represent numbers with fractional parts, but only some of the infinitely many that exist. Specifically, the exact values for `double' are fractions P/Q where P is an integer between plus or minus nine quadrillion *and* Q is an integer power of two (within a broad but limited range that includes both positive and negative powers). So, 1.0 is exact 0.5 is exact 0.25 and 0.75 are exact 0.125, 0.375, 0.625, 0.875 are exact ... and so on (to a point) BUT 0.1 is inexact 0.02 is inexact 0.003 is inexact 0.0004 is inexact ... and so on This inexactitude is exactly the same difficulty that afflicts decimal arithmetic in computing 1/3 or 2/7 or 3/11 or any other fraction whose denominator (in lowest terms) has a prime factor that is not a factor of ten. Lest anyone imagine that computing in decimal would somehow solve all problems, let me offer a thought experiment drawn from real life. Interest rates on savings are, at the moment, rather low. In an effort to lure, sorry, "interest" depositors, a bank wants to trumpet the comparatively high rate of 1.1%, but they'd like to pay out as little as possible. "Aha!" says some bright lad, "We'll offer an interest rate that, when compounded monthly, produces a 1.1% *annual* yield! Because compounding makes the number look bigger, we can advertise 1.1% while actually paying out at a rate that's slightly lower." The bank's executives bless the plan, and send the BL off to calculate the monthly rate. Question: What should that monthly rate be? To put it another way, what is the rate R such that (1+R/100)**12 = 1.011 = 1 + 1.1%? Calculate in any integer base you prefer, but answer exactly. (This sort of problem also makes a good rejoinder to people who maintain that *all* financial calculations should use integer math.) -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM