Path: csiph.com!news.mixmin.net!news.albasani.net!.POSTED!not-for-mail From: Michael Jung Newsgroups: comp.lang.java.programmer Subject: Re: double == double Date: Thu, 13 Oct 2016 21:10:51 +0200 Organization: albasani.net Lines: 50 Message-ID: <8737k0qd38.fsf@golem.phantasia.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net WT81bOT9tFBnS7ygvca7JBpSSq1bGgmDJmUMjFUvIvXbBdXBQr05Mw6wsAaRlqwjY75/nmS/gy9/nWGTpSV3oA== NNTP-Posting-Date: Thu, 13 Oct 2016 19:10:53 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="i2WWHUQnzpN7x7NlVAr5NC4LadRDHhFgbkIV9P+Xsvmjp0JmRYm7EkvXh+UsyMmTqmq5RTXAJnhTOJcOXzIAzR/ROuEWsHdt82VeUkmuAs3WQ4JaVNXdhgN97cunqbgX"; mail-complaints-to="abuse@albasani.net" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:JBU+/YN4BFEzuoljuVOupUK0lrc= sha1:ZM/7tFkGFm+4EnXMZleFxnmR3Zc= Xref: csiph.com comp.lang.java.programmer:35752 Eric Sosman writes: > On 10/12/2016 8:15 AM, Stefan Ram wrote: >> Sometimes it is taught that it makes no sense to compare >> two double values with »==«. >> But what about this function? >> public static double max( final double a, final double b) >> { return >> a == 0.0 && b == 0.0 && java.lang.Double.doubleToRawLongBits( a ) >> == Double.doubleToRawLongBits( -0.0d )? b : a >= b ? a : b; } >> >> which tries to return positive zero if one argument is >> positive zero and the other one negative zero? The situations, where you'd want max(+0,-0) = +0 and not -0 are extremely limited, considering that +0 = -0, even for (Java) doubles. >> It seems that there are special cases when a comparison >> of double values with »==« is appropriate. I have observed >> that this is usually the case when comparing with »0.0« - >> another example would be avoiding to divide by 0.0. In fact, one of the extremely rare applications of +0 and -0 is division by them: 1/+0 = +Infinity , 1/-0 = -Infinity. And, of course, "a == b iff a - b == 0.0" intuitively; allowing equality on 0.0 opens it for all numbers. Put in other words, using +0 and -0 as different turns doubles into something they were not meant to be. > > 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." Maybe it is easiest to see how fast you get there by evaluating the following code: public static void main(String[] args) { System.out.println(0.1 + 0.2 == 0.3); System.out.println(0.1 + 0.2 - 0.3 == 0); }