Win a copy of Getting started with Java programming language this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How is it possible that -0.0 == 0.0?  RSS feed

 
Dana Ucaed
Ranch Hand
Posts: 197
5
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I met in one page of CodeRanch the stuff:

-0.0 == 0.0 is true.

What is the explication for this statement?

Thanks in advance
 
Tim Cooke
Marshal
Posts: 3556
170
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think it wouldn't be true? Is there a different between zero and negative zero? How would you define negative zero?
 
Campbell Ritchie
Sheriff
Posts: 53561
126
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The mathematicians will tell you that there is no such thing as -0. But computer arithmetic does not usually follow strict mathematical notions. It follows IEEE754 for floating‑point numbers. Look it up on Wikipedia or elsewhere. It mandates negative 0 and also that there is no difference between that and ordinary 0.

Note that does not apply strictly to the wrapper classes:-Yes, I know d0 and d_0 are bad names for variables.
 
Campbell Ritchie
Sheriff
Posts: 53561
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . no difference between that and ordinary 0. . . .
I meant, when you subtract one from the other you still get 0.0.
Try:
System.out.printf("1.0 / 0.0:   %f%n"1.0 / -0.0: %f%n", 1.0 / 0.0, 1.0 / -0.0);
 
Stephan van Hulst
Saloon Keeper
Posts: 6841
100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are equivalent, but not identical. The difference becomes clear when you divide something by zero and negative zero:
 
Dana Ucaed
Ranch Hand
Posts: 197
5
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I caught after I posted.

Sorry for my question.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15813
76
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to be sorry! It's not a stupid question and the question and answers might be useful to someone else in the future who is wondering about the same thing.
 
Dana Ucaed
Ranch Hand
Posts: 197
5
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was surprised that this question was presented on this site as a unusual question.

 
Dana Ucaed
Ranch Hand
Posts: 197
5
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The mathematicians will tell you that there is no such thing as -0. But computer arithmetic does not usually follow strict mathematical notions. It follows IEEE754 for floating‑point numbers. Look it up on Wikipedia or elsewhere. It mandates negative 0 and also that there is no difference between that and ordinary 0.

Note that does not apply strictly to the wrapper classes:-Yes, I know d0 and d_0 are bad names for variables.


Thanks Campbell for your nice post.

This post raised me other questions.
:-)


 
Campbell Ritchie
Sheriff
Posts: 53561
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:. . . Thanks Campbell for your nice post.

This post raised me other questions.
:-)
That's a pleasure Please ask the other questions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!