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


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

Re: double == double

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: double == double
Date 2016-10-14 22:33 -0400
Organization Aioe.org NNTP Server
Message-ID <nts4hd$11pj$1@gioia.aioe.org> (permalink)
References <double-20161012130409@ram.dialup.fu-berlin.de>

Show all headers | View raw


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 »==«.

Yes.

Good rule of thumb.

It is very rare that == between two doubles are a good solution for
the problem at hand.

>   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?

That is what Math.max does, but I find it difficult to see
any practical usage from that.

>   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.

I think a large part of these cases will turn out not to be good
code.

Arne

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


Thread

Re: double == double Arne Vajhøj <arne@vajhoej.dk> - 2016-10-14 22:33 -0400

csiph-web