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


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

Re: double == double

Started byArne Vajhøj <arne@vajhoej.dk>
First post2016-10-14 22:33 -0400
Last post2016-10-14 22:33 -0400
Articles 1 — 1 participant

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 Arne Vajhøj <arne@vajhoej.dk> - 2016-10-14 22:33 -0400

#35760 — Re: double == double

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-10-14 22:33 -0400
SubjectRe: double == double
Message-ID<nts4hd$11pj$1@gioia.aioe.org>
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

[toc] | [standalone]


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


csiph-web