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: double == double Date: Sat, 15 Oct 2016 09:13:54 -0400 Organization: A noiseless patient Spider Lines: 38 Message-ID: References: <8737k0qd38.fsf@golem.phantasia.org> <87zim5es30.fsf@golem.phantasia.org> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 15 Oct 2016 13:13:37 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="414bcd8b93082233ddd2be90cf792575"; logging-data="25497"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19m+ykAo0PYiY3A8qKujypQ" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: <87zim5es30.fsf@golem.phantasia.org> Cancel-Lock: sha1:pbSvAFn/ZGLF8qLBMFBIcbRbSzE= Xref: csiph.com comp.lang.java.programmer:35764 On 10/15/2016 8:04 AM, Michael Jung wrote: > Michael Jung writes: >> Eric Sosman writes: >>> On 10/12/2016 8:15 AM, Stefan Ram wrote: > [...] >>> The problem with comparing double or float values for equality >>> (or inequality) isn't the values themselves, but their "provenance." >>> Calculations that yield identical results when performed with real >>> numbers and their infinite precision may yield different outcomes >>> when finite-precision floating-point values are used instead. We >>> know, for example, that `ln xy = ln x + ln y' for all positive x,y, >>> yet we cannot rely on `Math.log(x*y) == Math.log(x) + Math.log(y)'. >>> A rearrangement that appears innocuous can produce a result that is >>> slightly different (sometimes more than "slightly!") from that of the >>> original, and it's no simple matter to say which, if either, is >>> "right." > [...] > > More pointedly: don't use "==", when doing arithmetic. One of the > applications of using "==" on doubles may be when they are used as > version numbers such as modelling those of (La)TeX in Java. > > (This "argument" does not apply to +0 and -0, though.) The scare quotes around "argument" seem appropriate. I attempted to explain the basis of the simplistic "Don't use ==" advice, hoping that an informed understanding would trump blind obedience to some mysterious rule. (Perhaps I was overly optimistic.) There are instances (admittedly not many) where floating-point == is reliable, appropriate, and "safe when used as directed." It seems to me that someone who can distinguish the safe from the unsafe cases will be a better programmer than someone who closes his mind and simply says "Never use ==, it's sometimes dangerous." -- esosman@comcast-dot-net.invalid "Everybody must get stoned." Looks like the Swedish Academy was.