Thread: Zero divided by minus one

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Zero divided by minus one

    Hi, everyone! The following code would produce -0.00000. But I want it be 0.00000. How can I do that?

    Code:
    int main (void) {
      double a=0.0, b=-1.0;
      printf("%10.5f\n", a/b);
      return 0;
    }
    Thank you in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Use fabs() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    put -(a/b) instead of a/b..

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    0 is neither positive nor negative..so dont take it serious buddy

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    0 compares equal to -0 so it doesn't matter, well until you go and print it that is, in which case -> fabs.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by iMalc View Post
    0 compares equal to -0 so it doesn't matter, ...
    It depends on how the compiler compares them!...
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Sipher View Post
    It depends on how the compiler compares them!...
    Annex F of the C standard says the floating point support for IEC 60559 encompasses the IEEE 754 standard. My IEEE membership doesn't give me access to the standard and I'm too cheap to pay for it, but according to Wikipedia, IEEE 754 guarantees that 0 and -0 compare equal. So unless you have a non-conforming compiler in this regard (which you should probably toss), you should be fine with that comparison.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It doesn't bother anybody that there's such thing as a negative zero?
    I can possibly see where an "almost zero" could be rounded up to zero but a sloppy binary-to-decimal converter could keep the negative sign.
    I can also be possibly persuaded that
    Code:
    printf("%10.5f\n", a/b);
    can get an obscure number on the stack, when it's exported from the floating point registers... but I'm certain that if the result were saved to a variable, that its negative sign would not be retained.
    I highly doubt one could force a variable test = -0 no matter how hard one tried.
    Overall it sounds like a compiler bug.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by anduril462 View Post
    Annex F of the C standard says the floating point support for IEC 60559 encompasses the IEEE 754 standard. My IEEE membership doesn't give me access to the standard and I'm too cheap to pay for it, but according to Wikipedia, IEEE 754 guarantees that 0 and -0 compare equal. So unless you have a non-conforming compiler in this regard (which you should probably toss), you should be fine with that comparison.
    A compiler need not support IEC 60559, though. From Annex F: "An implementation that defines __STDC_IEC_559__ shall conform to the specifications in this annex."

    Even stickier is the fact that the whole IEEE floating point thing is C99 only. Not that C89 can't support IEEE floats (most implementations do, I suspect), but you can't check a macro for conformance like you can in C99.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nonoob View Post
    It doesn't bother anybody that there's such thing as a negative zero?
    I can possibly see where an "almost zero" could be rounded up to zero but a sloppy binary-to-decimal converter could keep the negative sign.
    I can also be possibly persuaded that
    Code:
    printf("%10.5f\n", a/b);
    can get an obscure number on the stack, when it's exported from the floating point registers... but I'm certain that if the result were saved to a variable, that its negative sign would not be retained.
    I highly doubt one could force a variable test = -0 no matter how hard one tried.
    Overall it sounds like a compiler bug.
    Technically C does not required floats to distinguish -0 from +0, but it's pretty universal for floats to be stored in IEEE form, which has an explicit sign bit, along with mantissa and exponent. IEEE also requires zero/negative to be stored as -0, and zero/positive to be stored as +0. The difference also comes into play with discontinuous functions with branch cut.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by anduril462 View Post
    Annex F of the C standard says the floating point support for IEC 60559 encompasses the IEEE 754 standard. My IEEE membership doesn't give me access to the standard and I'm too cheap to pay for it, but according to Wikipedia, IEEE 754 guarantees that 0 and -0 compare equal. So unless you have a non-conforming compiler in this regard (which you should probably toss), you should be fine with that comparison.
    Some compilers compare for equality with 'CMP' instead of 'FCMP'!

    EDIT: I mean that some compilers compare two floats as integers for equality
    Last edited by GReaper; 01-20-2011 at 06:31 AM.
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Sipher View Post
    Some compilers compare for equality with 'CMP' instead of 'FCMP'!

    EDIT: I mean that some compilers compare two floats as integers for equality
    Are we talking any current, major compilers here like GCC or Visual C++? I was thinking that the lack of support for IEC 60559 would be confined to second-rate compilers and really old stuff, not something that your casual C programmer would have to worry about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plus minus ? need help
    By zidangus in forum C Programming
    Replies: 4
    Last Post: 06-17-2010, 04:25 AM
  2. Replies: 2
    Last Post: 08-23-2009, 08:52 AM
  3. Divided my chat into classes and whoops..
    By eXeCuTeR in forum C++ Programming
    Replies: 7
    Last Post: 07-30-2008, 09:35 AM
  4. ASCII code goes into minus
    By Degenko in forum C Programming
    Replies: 4
    Last Post: 05-26-2007, 12:35 PM
  5. Problem with unary minus overloading
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 01-14-2005, 02:42 PM