+0.0 and -0.0

This is a discussion on +0.0 and -0.0 within the A Brief History of Cprogramming.com forums, part of the Community Boards category; IEEE standards allow for a +0.0 and a -0.0. If you compared the two values in C++ they would come ...

  1. #1
    Unregistered
    Guest

    Lightbulb +0.0 and -0.0

    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...

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    sticking my neck out...

    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.
    PHP and XML
    Let's talk about SAX

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    e

    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?

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >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.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >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.

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    > 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.
    Last edited by Davros; 07-19-2002 at 06:31 AM.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL question
    By WinteRx182 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 09:51 AM
  2. String and Vectors Problem
    By Matt3000 in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2007, 09:32 PM
  3. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 09:27 PM
  4. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 07:47 PM
  5. the effects of textures on my frame rate
    By DavidP in forum Game Programming
    Replies: 37
    Last Post: 10-03-2003, 12:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21