Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# -0.0 is what makes java very different :)

Harvinder Singh
Ranch Hand
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
}
}

Vicken Karaoghlanian
Ranch Hand
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;

Narasimha Rao B.
Ranch Hand
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.

Harvinder Singh
Ranch Hand
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
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
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.

Vicken Karaoghlanian
Ranch Hand
Posts: 522
Originally posted by ransika desilva:
Math classes in Java considers -0.0 is smaller than +0.0

, See code

Ranch Hand
Posts: 31
Ahaaa!!
Try this out...

[ February 16, 2004: Message edited by: Bijesh Krishnadas ]

Vicken Karaoghlanian
Ranch Hand
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 ]

Ranch Hand
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
Posts: 522