• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

if(0.0 == -0.0) - Sample question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All ,

I found a code mentioned below (http://www.examsguide.com/scjp/freequestions8.html)

if(0.0 == -0.0) {
System.out.println("true");
}
else{
System.out.println("false");
}

this prints true and my understanding is that 0 is neither negative nor positive.

Is there is any other reason also , Pleae explain.


Ashish
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you understand explains the simple result
it is true because sign doesn't matter here

both sides of operator are equal and hence true is returned
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this prints true and my understanding is that 0 is neither negative nor positive.



This is not completely true. The IEEE floating point spec defines a positive zero and a negative zero. So, a zero can be positive or negative.

They are equal because the specification defines that a positive zero is equal to a negative zero.

But you can tell them apart... For example, the inverse of positive zero is infinity, while the inverse of negative zero is negative infinity.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an example of this, take a look at this code...



As you can see, the negative sign attached to the zero is preserved, as the two answers are different.

Henry
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But note that corresponding wrappers are not equal:



will print false false.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashish

This is one of two special cases .

+0.0 = -0.0 is true
Float.Nan = Float.Nan is false

It is specified in Java API.
 
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
For an example of this, take a look at this code...



As you can see, the negative sign attached to the zero is preserved, as the two answers are different.

Henry



Hi henry,

I coded that part with some of my own, and I got exiting results. My program was:



The result printed was:
0
0
0
0
Infinity
-Infinity


Can anyone explain me why the 2nd and 4th zero was printed??

Regards,
Saurabh
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone explain me why the 2nd and 4th zero was printed??



The rules discussed in this topic applies to floating point numbers. Your example uses integers.

Henry
[ July 06, 2008: Message edited by: Henry Wong ]
 
What's brown and sticky? ... a stick. Or a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic