• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

confusion on (==)

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void check()
2: {
3: System.out.println(Math.min(-0.0,+0.0));
4: System.out.println(Math.max(-0.0,+0.0));
5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
6: }

A) prints -0.0, +0.0 and false.
B) prints -0.0, +0.0 and true.
C) prints 0.0, 0.0 and false.
D) prints 0.0, 0.0 and true.

The answer is B why please explain
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
as far as i think comparing -0.0 ==0.0 always returns true.
coz actually there is nothing like -0.0 and +0.0, both are just 0 and thus the equality test succeeds.

Sandy
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check the API for the min and max methods that take floating-point primitives, you will find...

Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero.


Note that == is a "numerical comparison operator."
[ September 14, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually for floating point numbers in Java, -0.0 and 0.0 are different.
Math.min(-0.0,0.0) outputs -0.0, but
-0.0 == 0.0 is true too..So it is a little wierd, but that is how it is.
The floating point numbers (double and float) are in this order
NEGATIVE_INFINITY --> negative integers/fractions --> -0.0 --> 0.0 --> positive integers/fractions --> POSITIVE_INFINITY
This is what I know.

Roopesh.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roopesh Gulecha:
... The floating point numbers (double and float) are in this order
NEGATIVE_INFINITY --> negative integers/fractions --> -0.0 --> 0.0 --> positive integers/fractions --> POSITIVE_INFINITY...


Well, the "order" of signed zeros depends on the context. Within the min and max methods of Math, this order exists. However, numerical comparison operators like > and < will not recognize an order.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It is mentioned in JLS that in Floating point numbers -0.0 and +0.0 are considered equal when "==" operator is used. Hence true is the answer.
If any thing is to be changed in the answer for perfection, please let me know.

Regards,
Pavan.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic