Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35860

Re: double == double

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Mike Amling <mamling@chaff.us>
Newsgroups comp.lang.java.programmer
Subject Re: double == double
Date Sat, 22 Oct 2016 07:15:19 -0500
Organization A noiseless patient Spider
Lines 35
Message-ID <nufl8l$ogo$1@dont-email.me> (permalink)
References <double-20161012130409@ram.dialup.fu-berlin.de> <nu7quv$aur$1@dont-email.me> <nu7sl4$h0a$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
Injection-Date Sat, 22 Oct 2016 12:15:17 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="6222d97a76955177f5b01c30f4b8f62f"; logging-data="25112"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181V3of6Q+qG3LaV1GKNKuN"
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.4.0
In-Reply-To <nu7sl4$h0a$1@dont-email.me>
Cancel-Lock sha1:3m4Sus2GlxvyS5bT9aCdwfGeDa0=
Xref csiph.com comp.lang.java.programmer:35860

Show key headers only | View raw


On 10/19/16 8: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).

D'oh!

--Mike Amling
QWx0aG91Z2ggbXkgZmlyc3QgcmVhY3Rpb24gd2FzICJUaGF0J3MgT0sgd2l0aCBtZS4iLg==

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: double == double Mike Amling <mamling@chaff.us> - 2016-10-19 08:03 -0500
  Re: double == double Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-19 09:32 -0400
    Re: double == double Jerry Stuckle <jstucklex@attglobal.net> - 2016-10-19 10:26 -0400
      Re: double == double Jake Jarvis <pig_in_shoes@yahoo.com> - 2016-10-19 16:36 +0200
        Re: double == double Jerry Stuckle <jstucklex@attglobal.net> - 2016-10-19 16:50 -0400
          Re: double == double Jake Jarvis <pig_in_shoes@yahoo.com> - 2016-10-20 01:08 +0200
            Re: double == double Arne Vajhøj <arne@vajhoej.dk> - 2016-10-19 21:16 -0400
              Re: double == double Arne Vajhøj <arne@vajhoej.dk> - 2016-10-19 21:19 -0400
              Re: double == double Jerry Stuckle <jstucklex@attglobal.net> - 2016-10-19 21:47 -0400
                Re: double == double Arne Vajhøj <arne@vajhoej.dk> - 2016-10-19 21:52 -0400
    Re: double == double Michael Jung <miju@golem.phantasia.org> - 2016-10-19 19:39 +0200
    Re: double == double Mike Amling <mamling@chaff.us> - 2016-10-22 07:15 -0500

csiph-web