View Full Version : +0.0 and -0.0

Unregistered

07-18-2002, 12:00 AM

IEEE standards allow for a +0.0 and a -0.0. If you compared the two values in C++ they would come out equal. I've looked into Java and this isn't the case. When you define a hashtable in Java +0.0 hashes to a different value than -0.0. Why would you want to have two values, which are essentialy the same, to hash to different values????????? This is more a theoretical question...

Waldo2k2

07-18-2002, 08:09 AM

well, mathmatically, C++ compiles right on this one. With a zero value there is no plus or minus, so it's pretty much ignored. Because zero has no sign, therefore it isn't positive or negative....neutral perhaps.... anyhow, I think that the whole thing with Java getting different values is (dare i say it) possibly a bug in java's core logic. Possibly how +'s and -'s are interpreted they didn't put in catches for someone sticking a + or - in front of the zero...as to how it comes out with a different value I have no clue, maybe it's actually the answer to -1 and +1 i don't know (I don't do java) But thinking about how programming works that's as logical of a thought i can come up with.

Shiro

07-18-2002, 10:55 AM

From a mathematical view, 0.0 is not necessary equal to 0. The value 0 is an integer value. The value 0.0 is a floating point value, which could be 0.01, but is rounded off to 0.0. The variable should have value 0.0000000000.... to be equal to 0. So if the theoretical value 0.0 can in practice be 0.01 and -0.01 is not equal to +0.01.

I don't know if this is the reason why Java gives a different answer then C++, just indicating that it could have to do with how values are stored internally.

Shiro's right - the more decimal points you have the more accurate it is, which is why e (2.71828, or 1 + 1/1 + 1/2 + 1/3 + 1/4. etc...) is a good number for natural logarithms - they have it analogously, so it's infinitely accurate, and if you have it to the power of x, it's differential coefficient is itself.

Another thing (going back to the original question) in some situations -0.0 and +0.0 sould be treated as different things (although usually only in integration).

Another somewhat related question. If square negative infinity, you should get positive infinity, so would that count as two different degrees of infinity?

Davros

07-18-2002, 11:04 AM

>From a mathematical view, 0.0 is not necessary equal to 0

Form a mathematical view, I would disagree. Of course 0.0 is equal to 0. This is like saying 010 is not equal 10.

If, however, you were to say 0.1 is not equal 0.0, I would agree with you. If 0.1 has been rounded to 0.0, then it is a rounding error.

Shiro

07-18-2002, 11:12 AM

>Of course 0.0 is equal to 0.

What I meant was that 0.0 is not a precise number, it could be 0.01 or smaller. That's why I said that 0.0 is not necessarily equal to 0.

>This is like saying 010 is not equal 10.

That's not the same. What I meant was that 10.0 is not necessary equal to 10.

Well, maybe it's because I'm not a mathematician, but an electrical engineer.

Davros

07-19-2002, 05:23 AM

> What I meant was that 0.0 is not a precise number

While I can see what you are trying to say, but I am a afraid 0.0 is a precise number. Here's another example,

3.14159

is a precise number also. However, it is not a precise representation of PI. When you say that 0.0 could be equal 0.1, you are implicitly refering to the representations used by computers and the problems of those representations.

As regards -0 & +0, technically these are different. Although the magnitude of the values are both zero, the sign direction is different. For most applications the difference is trivial, however, for certain [mathmatical] problems is could be important, even for a value of zero.

Personally, I would say the Java implementation is corrent one, however, I can see why this may cause problems for [most] programs which don't care about the difference.

Here's a simple example with strings, where perhaps the convention is more widely understood:

'Hello' = 'hello'

equal or not?

It depends on whether case is important. Technically, they are not equal, and that's fine as along as everyone understands the convention.

http://www.serve.net/buz/hash.adt/java.006.html

Since -0.0 and 0.0 are bitwise different, java's hash function will

probably hash them to different values.

Powered by vBulletin® Version 4.2.5 Copyright © 2019 vBulletin Solutions Inc. All rights reserved.