Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jake Jarvis Newsgroups: comp.lang.java.programmer Subject: Re: double == double Date: Thu, 20 Oct 2016 01:08:18 +0200 Organization: A noiseless patient Spider Lines: 53 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 19 Oct 2016 23:07:58 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="6af518a73f2b9552469d9401c97afeb5"; logging-data="12851"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZtZYxT4lJV8IhT+9IdLJiTl0tdYUL9Xc=" User-Agent: Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 In-Reply-To: Cancel-Lock: sha1:pEQCAt8bolIBfx9e7it8PbwIagw= Xref: csiph.com comp.lang.java.programmer:35832 Am 19.10.2016 um 22:50 schrieb Jerry Stuckle: > On 10/19/2016 10:36 AM, Jake Jarvis wrote: >> Am 19.10.2016 um 16:26 schrieb Jerry Stuckle: >>> On 10/19/2016 9:32 AM, Eric Sosman wrote: >>>> On 10/19/2016 9:03 AM, Mike Amling wrote: >>>>> On 10/12/16 7: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? >>>>>> >>>>>> 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. >>>>> >>>>> If a==0.0 and b==0.0, there's no need to test whether either is -0.0. >>>>> The function could be simplified to >>>>> >>>>> public static double max(final double a, final double b) { >>>>> return a==0.0 && b==0.0 ? 0.0 : (a>=b ? a : b); >>>>> } >>>> >>>> This would return +0.0 for max(-0.0, -0.0). >>>> >>> >>> PMJI, but how do you figure? a==0.0 && b==0.0 would be false >> >> No. >> > > And why not? -0.0 != 0.0. > But exactly that is not the case https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.21.1 | 15.21.1. Numerical Equality Operators == and != | [...] | Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard: | [...] | Positive zero and negative zero are considered equal.