Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. What is Sign of 0?

What is Sign of 0?

Scheduled Pinned Locked Moved The Lounge
questioncsharppythoncomfunctional
33 Posts 24 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

    Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

    9 Offline
    9 Offline
    9082365
    wrote on last edited by
    #12

    Marc Clifton wrote:

    How can 0 be negative?

    Well it certainly isn't positive when it's the amount in your glass or your bank account!

    I am not a number. I am a ... no, wait!

    1 Reply Last reply
    0
    • M Marc Clifton

      In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

      Sander RosselS Offline
      Sander RosselS Offline
      Sander Rossel
      wrote on last edited by
      #13

      Dear Mr. Clifton, Please stop belittling me. Signed, 0

      Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

      Regards, Sander

      M 1 Reply Last reply
      0
      • M Marc Clifton

        But in Python (note the floating point): -1.0 * 0.0 -0.0 :rolleyes: Marc

        Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #14

        And in C printf("%f", -1.0 * 0.0) prints -0.000000 see Signed zero[^] (Wikipedia) Edit: should have read the rest of the thread before hitting post .. :(

        M 1 Reply Last reply
        0
        • M Marc Clifton

          In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

          Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

          M Offline
          M Offline
          Mark_Wallace
          wrote on last edited by
          #15

          You're obviously not pessimistic enough.

          I wanna be a eunuchs developer! Pass me a bread knife!

          1 Reply Last reply
          0
          • M Marc Clifton

            In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

            Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu Peter
            wrote on last edited by
            #16

            Problems with Zero - Numberphile - YouTube[^] I can see why in .NET, 0 has no sign - it solves a lot of problems (and it is perfectly good for common arithmetic)

            Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

            1 Reply Last reply
            0
            • M Marc Clifton

              OriginalGriff wrote:

              Being annoyingly boring:

              An interesting read actually. Starts to make sense. Interestingly, in C#: double a = -0.0; a is "0". C# makes no distinction between positive and negative 0. Marc

              Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

              M Offline
              M Offline
              Matt T Heffron
              wrote on last edited by
              #17

              But for which reason? Is it the parser in the compiler? Is it the output routine you used? (The VS debugger?) So I tried the following:

                double a = -0.0;
                Console.WriteLine("a = {0}, 1.0/a = {1}", a, 1.0 / a);
                double b = double.Parse("-0.0");
                Console.WriteLine("b = {0}, 1.0/b = {1}", b, 1.0 / b);
                byte\[\] abytes = BitConverter.GetBytes(a);
                Console.WriteLine("a bytes=" + string.Join(",", abytes.Select(z => z.ToString("X2"))));
                byte\[\] bbytes = BitConverter.GetBytes(b);
                Console.WriteLine("b bytes=" + string.Join(",", bbytes.Select(z => z.ToString("X2"))));
              

              The output was surprising:

              a = 0, 1.0/a = -Infinity
              b = 0, 1.0/b = Infinity
              a bytes=00,00,00,00,00,00,00,80
              b bytes=00,00,00,00,00,00,00,00

              So the compiler handles the -0.0 correctly, but double.Parse doesn't and the output is converting the negative 0.0 to "0.0" with no sign.

              "Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton

              C 1 Reply Last reply
              0
              • M Marc Clifton

                OriginalGriff wrote:

                Being annoyingly boring:

                An interesting read actually. Starts to make sense. Interestingly, in C#: double a = -0.0; a is "0". C# makes no distinction between positive and negative 0. Marc

                Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #18

                It does, just not that way. It is still the case that 1.0 / -0.0 = -Infinity

                1 Reply Last reply
                0
                • M Marc Clifton

                  In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

                  Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                  P Offline
                  P Offline
                  patbob
                  wrote on last edited by
                  #19

                  The sign of 0? I thought everyone knew that was positive because of twos complement math. Of course, with IEEE floating point, -0.0 is representable, but allowing it would be inconsistent, and very, very confusing.

                  We can program with only 1's, but if all you've got are zeros, you've got nothing.

                  D B 2 Replies Last reply
                  0
                  • Sander RosselS Sander Rossel

                    Dear Mr. Clifton, Please stop belittling me. Signed, 0

                    Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                    Regards, Sander

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #20

                    Sander Rossel wrote:

                    Please stop belittling me.

                    Dear Mr. Zero (or is it Mrs. Zero, there is some ambiguity here!), I'm sorry, I had no intention of being n-aught-y. In fact, you are very important! Without you, nothing could not be expressed mathematically. You fulfill a central role in mathematics as the additive identity of the integers, real numbers, and many other algebraic structures. And besides, you are at least 3756 years old, and in ancient Egypt you were given a designation that means "beautiful", which is much better than what the Babylonian's did, which was to represent you with a space, a blank, a nothing! Of course, the Greeks weren't sure about you -- how could nothing be something! Well, I know that you are something indeed, and certainly, based on your age, much wiser than I. Your humble servant, Marc

                    Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                    Sander RosselS 1 Reply Last reply
                    0
                    • M Marc Clifton

                      Sander Rossel wrote:

                      Please stop belittling me.

                      Dear Mr. Zero (or is it Mrs. Zero, there is some ambiguity here!), I'm sorry, I had no intention of being n-aught-y. In fact, you are very important! Without you, nothing could not be expressed mathematically. You fulfill a central role in mathematics as the additive identity of the integers, real numbers, and many other algebraic structures. And besides, you are at least 3756 years old, and in ancient Egypt you were given a designation that means "beautiful", which is much better than what the Babylonian's did, which was to represent you with a space, a blank, a nothing! Of course, the Greeks weren't sure about you -- how could nothing be something! Well, I know that you are something indeed, and certainly, based on your age, much wiser than I. Your humble servant, Marc

                      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                      Sander RosselS Offline
                      Sander RosselS Offline
                      Sander Rossel
                      wrote on last edited by
                      #21

                      Hah! It was I, Sander Rossel, disguised as 0 all along! You're my servant now :D Or was I 0 disguised as Sander...? Anyway, don't divide by me just in case :~

                      Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                      Regards, Sander

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

                        Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                        K Offline
                        K Offline
                        Kiriander
                        wrote on last edited by
                        #22

                        Quite easy: lim(-1/x,x->INF) makes a negative 0. You have to understand that the limit 0 is an entirely different beast from the 0 you get in your wallet when you spend all your money.

                        1 Reply Last reply
                        0
                        • P patbob

                          The sign of 0? I thought everyone knew that was positive because of twos complement math. Of course, with IEEE floating point, -0.0 is representable, but allowing it would be inconsistent, and very, very confusing.

                          We can program with only 1's, but if all you've got are zeros, you've got nothing.

                          D Offline
                          D Offline
                          Daniel Pfeffer
                          wrote on last edited by
                          #23

                          patbob wrote:

                          Of course, with IEEE floating point, -0.0 is representable, but allowing it would be inconsistent, and very, very confusing.

                          Actually, negative zero is essential for correct solution of certain problems involving branch cuts for complex elementary functions. See W. Kahan's Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit[^]. Note that a reference to the final article is given on Kahan's home page, but I don't have access to that publication.

                          If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                          1 Reply Last reply
                          0
                          • P patbob

                            The sign of 0? I thought everyone knew that was positive because of twos complement math. Of course, with IEEE floating point, -0.0 is representable, but allowing it would be inconsistent, and very, very confusing.

                            We can program with only 1's, but if all you've got are zeros, you've got nothing.

                            B Offline
                            B Offline
                            benf2
                            wrote on last edited by
                            #24

                            Typically, one takes the ones compliment and then increment to find the inverse, so that when the numbers added together is "0". So the inverse of 0 is 0. This means that -0 == 0. So, zero has the ability to have both signs.

                            1 Reply Last reply
                            0
                            • I Ian Shlasko

                              I'd like to know how they represent -0 internally... I mean, either they're cutting one number off the max range of the numeric type (e.g. -127->127 instead of -128->127, so 11111111 could represent -0 instead of -1), and changing all of the low-level arithmetic to compensate (unlikely), or... They're wasting a whole byte on the sign, just so they can represent something that, 99.999% of the time, doesn't matter... If they're going to go that route, I think they should figure out 254 more ways to represent zero, just so they're not wasting bits :)

                              Proud to have finally moved to the A-Ark. Which one are you in?
                              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                              P Offline
                              P Offline
                              Plamen Dragiyski
                              wrote on last edited by
                              #25

                              Ian Shlasko wrote:

                              I'd like to know how they represent -0 internally...

                              That's exactly where is the problem. I don't know C# (I'm currently a javascript programmer), but a Number is internally stored as IEEE754 floating point, which have a bit for sign. It is not an integer. Zero can be represented without the sign bit, therefore when sign bit is 0, you get +0.0, when it is 1, you get -0.0. It is just a bit interpretation problem. If you are interesting why -0.0 work as it is, there is very good mathematical theory for infinitesimals. But as a rule of a thumb, never use == on floating-point number. It returns false more often than you expect to.

                              1 Reply Last reply
                              0
                              • B Bassam Abdul Baki

                                The Taurus would be the closest sign.

                                Web - BM - RSS - Math - LinkedIn

                                K Offline
                                K Offline
                                Kirk 10389821
                                wrote on last edited by
                                #26

                                Actually, I believe the correct answer is Gemini! (I know I married them)

                                1 Reply Last reply
                                0
                                • M Marc Clifton

                                  In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

                                  Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                                  Y Offline
                                  Y Offline
                                  Ygnaiih
                                  wrote on last edited by
                                  #27

                                  Damn you people. I started looking into this then I wrote the following: if (-0 == +0) { Console.WriteLine(Math.Sign(0)); } that was C# it returned 0 as expected. Now I am wondering if math is real.

                                  1 Reply Last reply
                                  0
                                  • K k5054

                                    And in C printf("%f", -1.0 * 0.0) prints -0.000000 see Signed zero[^] (Wikipedia) Edit: should have read the rest of the thread before hitting post .. :(

                                    M Offline
                                    M Offline
                                    Member 10267903
                                    wrote on last edited by
                                    #28

                                    Not only Python. Try it in SQL Server 2012:

                                    declare @A as float;
                                    declare @B as float;
                                    declare @C as float;

                                    set @A = 0.0;
                                    set @B = -1.0;
                                    set @C = @A * @B;

                                    print @C;

                                    or PHP:

                                    $A = -1.0 * 0.0;
                                    echo $A . "\n";

                                    1 Reply Last reply
                                    0
                                    • M Marc Clifton

                                      In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

                                      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                                      O Offline
                                      O Offline
                                      ormonds
                                      wrote on last edited by
                                      #29

                                      If 0 represents the contents of my glass then zero is very definitely negative.

                                      1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

                                        Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                                        C Offline
                                        C Offline
                                        Charles Programmer
                                        wrote on last edited by
                                        #30

                                        I find 0.0 to be negative when it involves the quantity of money in my control. ;)

                                        1 Reply Last reply
                                        0
                                        • M Marc Clifton

                                          In C#, Math.Sign(0) is 0. OK, that's one way to solve that. But I was amused by this comment in SO regarding why there isn't a Sign function in Python: > Indeed there was a patch which included sign() in math, but it wasn't accepted, because they didn't agree on what it should return in all the edge cases (+/-0, +/-nan, etc) Instead, they created copysign > math.copysign(x, y) Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. So if you do: math.copysign(15, -313) you get -15.0. Or more amusingly: math.copysign(0, -313) Answer: -0.0 Then try this: -0.0 < 0 Answer: False -0.0 == 0 Answer: True Anyways, I found that weird / interesting. How can 0 be negative? Thoughts, on this Monday morning? Marc

                                          Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                                          C Offline
                                          C Offline
                                          Charles Programmer
                                          wrote on last edited by
                                          #31

                                          I find 0.0 to be negative when it involves the quantity of money in my control. ;)

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups