Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes -0.0 is what makes java very different  :) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "-0.0 is what makes java very different  :)" Watch "-0.0 is what makes java very different  :)" New topic
Author

-0.0 is what makes java very different :)

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
Hi Ranchers,
Why the output is not 0.0? Why do we have negative zero in the float and the double. Can u tell me any practical problem where –ve zero is needed?
class Funda{
public static void main(String args[]){
System.out.println(Math.ceil(-0.5));//-0.0
}
}


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Because float and double datatypes support -ve infinity and +ve infinity.
That is why you don't get devision by zero error when trying to execute this:
double d = 10.0 / -0.0;


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Harvinder,
I am not sure the need of having -0.0. But somewhere i read that range of float values are like this,
-Infinity,....(Negative numbers)...., -0.0, +0.0, ... (Positive Numbers).., +Infinity.
Because of this you are getting the answers as -0.0, instead of +0.0.

Narasimha.


Narasimha
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
Hi Narshimha and Vicken,
I think I should consider here(execute ) Narshimha answer first because he was more specific in the reply. I was knowing the range but I just wanted to know why the java creators where in the need of -0.0 when no other language(I think... ) has it.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
I can't tell you why rules are the way they are. Personally I consider any language that has a feature that other languages don't have as an advantage for that language.
Perhaps James Gosling felt that representing error conditions such as Arithmetic overflow, taking the square root of a -ve number and division by zero; with a floating-point value would differentiate the JPL from the other languages. Well guess what... it had.
All mathematicians agrees that
1.0/ 0.0 = infinity
Now why do you want to restrict this rule and throw a runtime error!!! On the other hand you will always have the choice to compare this value with other values. Do you consider this as a plus or what!?
Why do you compare JAVA with other programming languages but not the other way around?
Java Rules and that is a fact.
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hello all,
According to the study notes which I am refering (Downloaded from javaranch.com) the Math classes in Java considers -0.0 is smaller than +0.0. So the ceil(-0.5) returns the smallest double value which is not less than the aruguement and equal to whole number.
-1.0,-0.5,-0.0,+ 0.0,+1.0 is the line up of the numbers, so in this case what is the smallest double value which is not less than -0.5 and which is equal to a whole number, it is -0.0. Not +0.0. Hope the logic is clear and correct. Please inform if I am wrong.
Thank you very much.


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by ransika desilva:
Math classes in Java considers -0.0 is smaller than +0.0

, See code
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
Ahaaa!!
Try this out...

[ February 16, 2004: Message edited by: Bijesh Krishnadas ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Bijesh, You may want to recheck your example.
double posZ = Math.ceil(0.5); equals 1.0 NOT 0.0
System.out.println("0.0 < -0.0 = " + (negZ < posZ));
should be
System.out.println("0.0 < -0.0 = " + (posZ < negZ));
I edited your code, a little bit... Check the results.

[ February 16, 2004: Message edited by: Vicken Karaoghlanian ]
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
Sorry, for the huge fowl-up....
Thanx for correcting me. Sometimes in the heat of the moment, u miss out universal truths
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by Bijesh Krishnadas:
Sorry, for the huge fowl-up....
Thanx for correcting me. Sometimes in the heat of the moment, u miss out universal truths

No problemo.
 
Don't get me started about those stupid light bulbs.
 
subject: -0.0 is what makes java very different :)
 
Similar Threads
Math.min and Math. max
Math.ceil problem
-0.0==0.0 why true??
[about zero]...
(0.0==-0.0) true or false