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


Groups > comp.lang.java.programmer > #35818 > unrolled thread

Re: double == double

Started byMike Amling <mamling@chaff.us>
First post2016-10-19 08:03 -0500
Last post2016-10-22 07:15 -0500
Articles 12 — 6 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#35818 — Re: double == double

FromMike Amling <mamling@chaff.us>
Date2016-10-19 08:03 -0500
SubjectRe: double == double
Message-ID<nu7quv$aur$1@dont-email.me>
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);
}

--Mike Amling
V2h5IHVzZSBvbmUgJ2phdmEubGFuZy5Eb3VibGUnIGFuZCBvbmUgJ0RvdWJsZSc/

[toc] | [next] | [standalone]


#35820

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-10-19 09:32 -0400
Message-ID<nu7sl4$h0a$1@dont-email.me>
In reply to#35818
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).

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#35822

FromJerry Stuckle <jstucklex@attglobal.net>
Date2016-10-19 10:26 -0400
Message-ID<nu7vq8$qnj$3@jstuckle.eternal-september.org>
In reply to#35820
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, so it
would evaluate a>=b.  Since both are -0.0, it would return that value.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#35823

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2016-10-19 16:36 +0200
Message-ID<nu80cv$un8$1@dont-email.me>
In reply to#35822
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.

, so it
> would evaluate a>=b.  Since both are -0.0, it would return that value.
>

[toc] | [prev] | [next] | [standalone]


#35827

FromJerry Stuckle <jstucklex@attglobal.net>
Date2016-10-19 16:50 -0400
Message-ID<nu8m9u$i6g$1@jstuckle.eternal-september.org>
In reply to#35823
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.

> , so it
>> would evaluate a>=b.  Since both are -0.0, it would return that value.
>>
> 



-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#35832

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2016-10-20 01:08 +0200
Message-ID<nu8uce$chj$1@dont-email.me>
In reply to#35827
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.

[toc] | [prev] | [next] | [standalone]


#35833

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-10-19 21:16 -0400
Message-ID<nu95u9$drl$1@gioia.aioe.org>
In reply to#35832
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>

Arne

[toc] | [prev] | [next] | [standalone]


#35834

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-10-19 21:19 -0400
Message-ID<nu963h$drl$2@gioia.aioe.org>
In reply to#35833
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

[toc] | [prev] | [next] | [standalone]


#35840

FromJerry Stuckle <jstucklex@attglobal.net>
Date2016-10-19 21:47 -0400
Message-ID<nu97nm$q3q$1@jstuckle.eternal-september.org>
In reply to#35833
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>
> 
> Arne
> 
> 

So, if that's the case, what does it matter if the operation returns
+0.0 or -0.0?


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#35842

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-10-19 21:52 -0400
Message-ID<nu980i$fs1$3@gioia.aioe.org>
In reply to#35840
On 10/19/2016 9:47 PM, Jerry Stuckle wrote:
> 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>
>
> So, if that's the case, what does it matter if the operation returns
> +0.0 or -0.0?

Somebody defined the contract for Math.max so that it have to return
-0.0 for (-0.0,-0.0).

It has to fulfill that contract.

I think they could have made the contract different. But somebody at
SUN 20 years ago did not agree.

Arne



[toc] | [prev] | [next] | [standalone]


#35826

FromMichael Jung <miju@golem.phantasia.org>
Date2016-10-19 19:39 +0200
Message-ID<87d1iw9r1s.fsf@golem.phantasia.org>
In reply to#35820
Eric Sosman <esosman@comcast-dot-net.invalid> writes:
> On 10/19/2016 9:03 AM, Mike Amling wrote:
>> public static double max(final double a, final double b) {
>>   return a==0.0 && b==0.0 ? 0.0 : (a>=b ? a : b);
>> }
or 

public static double max(final double a, final double b) {
  return a>=b ? a : b;
}

>     This would return +0.0 for max(-0.0, -0.0).

Why would that be wrong?

Michael

[toc] | [prev] | [next] | [standalone]


#35860

FromMike Amling <mamling@chaff.us>
Date2016-10-22 07:15 -0500
Message-ID<nufl8l$ogo$1@dont-email.me>
In reply to#35820
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==

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web