Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > using Double.compare()

Reply
Thread Tools

using Double.compare()

 
 
Prime
Guest
Posts: n/a
 
      03-13-2007
Is there any particular reason(s) why I should use the
Double.compare() method?


(i.e. Double.compare(<double1>, <double2>) == 0, instead of just
saying double1 == double2 ??)

Thanks

Patrick

 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-13-2007
Prime wrote:
> Is there any particular reason(s) why I should use the
> Double.compare() method?
>
>
> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
> saying double1 == double2 ??)


It's useful for building compareTo (interface Comparable) and compare
(interface Comparator) methods when the comparison criterion is the
value of a double.

Patricia

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      03-13-2007
On Mar 13, 10:57 am, "Prime" <(E-Mail Removed)> wrote:
> Is there any particular reason(s) why I should use the
> Double.compare() method?
>
> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
> saying double1 == double2 ??)
>
> Thanks
>
> Patrick


Its generally a bad idea to compare floating point numbers for
equality, due to rounding errors, you will find situations where your
comparison fails unexpectedly.


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-13-2007
[Didn't see the original post, so replying to Daniel's post]

"Daniel Pitts" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Mar 13, 10:57 am, "Prime" <(E-Mail Removed)> wrote:
>> Is there any particular reason(s) why I should use the
>> Double.compare() method?
>>
>> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>> saying double1 == double2 ??)
>>


From the Javadocs:
http://java.sun.com/javase/6/docs/ap...uble,%20double)
<quote>
There are two ways in which comparisons performed by this method differ
from those performed by the Java language numerical comparison operators
(<, <=, ==, >= >) when applied to primitive double values:

* Double.NaN is considered by this method to be equal to itself and
greater than all other double values (including Double.POSITIVE_INFINITY).
* 0.0d is considered by this method to be greater than -0.0d.
</quote>

- Oliver


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-13-2007
On 13 Mar 2007 10:57:59 -0700, "Prime" <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>(i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>saying double1 == double2 ??)


They do different things. == ensures you are pointing to the same
object. Compare ensures two different objects contain the same value.
--
Canadian Mind Products, Roedy Green, http://mindprod.com
Priorities: Prevent global climate destabilisation. End both wars. Prepare for oil shortages.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-13-2007
Roedy Green wrote:
> On 13 Mar 2007 10:57:59 -0700, "Prime" <(E-Mail Removed)> wrote,
> quoted or indirectly quoted someone who said :
>
>> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>> saying double1 == double2 ??)

>
> They do different things. == ensures you are pointing to the same
> object. Compare ensures two different objects contain the same value.


I don't understand this comment. Double.compare takes two arguments of
type double. If double1 and double2 are suitable arguments for
Double.compare then they are primitives, and == compares their values.

Patricia
 
Reply With Quote
 
trstag75@yahoo.fr
Guest
Posts: n/a
 
      03-13-2007

On Mar 13, 5:57 pm, "Prime" <(E-Mail Removed)> wrote:
> Is there any particular reason(s) why I should use the
> Double.compare() method?


I'll answer that but for a start: you probably, as Daniel noted,
do *not* want to directly compare double. At least, if you ask
the question, you probably do not know all the details involved.

You probably want to read this (and want to learn what an
epsilon, in this context, is):

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Now to answer your question...

>
> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
> saying double1 == double2 ??)


What are double1 and double2? Objects? Primitives? What Java version?
Can auto-boxing take place?

final Double a = new Double( 0.1d );
final Double b = new Double( 0.1d );
System.out.println( "a == b ? " + (a == b) );
System.out.println( "Double.compare(a,b) == 0: " +
(Double.compare(a,b) == 0) );
final double c = 0.1d;
final double d = 0.1d;
System.out.println( "c == d ? " + (c == d) );
System.out.println( "Double.compare(c,d) == 0: " +
(Double.compare(c,d) == 0) );

Will output:

a == b ? false
Double.compare(a,b) == 0: true
c == d ? true
Double.compare(c,d) == 0: true

When you compare a and b with == you're comparing the
references of the two objects. As I specifically used
"new" to construct them, the JVM MUST (by the specs)
create two different objects. Hence a == b returns false
(the references are not pointing to the same object).

When you compare c and d with ==, you're comparing
primitives.

Now, a little more fun:

final double e = 0.1d;
final double f = 0.3d;
final double g = 0.2d;
final double h = f - g; // we're substracting 0.2 to 0.3 or
are we?
System.out.println( "h == e ? " + (h == e) );
System.out.println( "e is " + e );
System.out.println( "h is " + h );

will output:

h == e ? false
e is 0.1
h is 0.09999999999999998

And the fact Java actually does output "e is 0.1" is really
interesting (hint: just as most decimal numbers, it's
impossible to represent 0.1 using Java's floating point numbers).





 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-15-2007

"Patricia Shanahan" <(E-Mail Removed)> wrote in message
news:et6rk0$2ubd$(E-Mail Removed)...
> Roedy Green wrote:
>> On 13 Mar 2007 10:57:59 -0700, "Prime" <(E-Mail Removed)> wrote,
>> quoted or indirectly quoted someone who said :
>>
>>> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>>> saying double1 == double2 ??)

>>
>> They do different things. == ensures you are pointing to the same
>> object. Compare ensures two different objects contain the same value.

>
> I don't understand this comment. Double.compare takes two arguments of
> type double. If double1 and double2 are suitable arguments for
> Double.compare then they are primitives, and == compares their values.


double1 and double2 may be instances of Double, and get auto-unboxed
when passed to Double.compare() (which might then say they are equal), and
yet be different objects (and thus == says they are not equal).

- Oliver


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments