Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35834
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: double == double |
| Date | 2016-10-19 21:19 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <nu963h$drl$2@gioia.aioe.org> (permalink) |
| References | (3 earlier) <nu7vq8$qnj$3@jstuckle.eternal-september.org> <nu80cv$un8$1@dont-email.me> <nu8m9u$i6g$1@jstuckle.eternal-september.org> <nu8uce$chj$1@dont-email.me> <nu95u9$drl$1@gioia.aioe.org> |
On 10/19/2016 9:16 PM, Arne Vajhøj wrote:
> On 10/19/2016 7:08 PM, Jake Jarvis wrote:
>> 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:
>>>>>>> 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.
>
> And just for completeness.
>
> IEEE 754 2008 section 5.11 says:
>
> <quote>
> Comparisons shall ignore the sign of zero (so +0 = −0).
> </quote>
It is also relative easy to verify:
public class ZeroZero {
public static void main(String[] args) {
double x1 = 0.0;
double x2 = -0.0;
System.out.println(Double.doubleToLongBits(x1) ==
Double.doubleToLongBits(x2));
System.out.println(x1 == x2);
}
}
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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