What is Sign of 0?
-
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)In Python, copysign returns a floating point value, so they use the sign bit. There is no integer representation of -0. If you try
a = -0
, thenprint a
shows 0. MarcImperative 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
-
I don't see a problem? -0 == -1*0 == 0
it ain’t broke, it doesn’t have enough features yet.
But in Python (note the floating point):
-1.0 * 0.0
-0.0 :rolleyes: MarcImperative 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
-
Being annoyingly boring: Signed zero - Wikipedia, the free encyclopedia[^] Sorry!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
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. MarcImperative 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
-
So they decided to look at C/C++ and implement the copysign - C++ Reference[^] but not the signbit - C++ Reference[^]? That is indeed half-baked.
Marc Clifton wrote:
Anyways, I found that weird / interesting. How can 0 be negative?
This can occur when an operation underflows. If you have for example two very small number where one is negative and multiply them, the result can underflow and will be set to zero but the sign bit is preserved.
Jochen Arndt wrote:
but not the signbit - C++ Reference[^]?
In the numpy[^] library, there is a signbit. 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
-
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 aSign
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 createdcopysign
> 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? MarcImperative 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
-
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 aSign
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 createdcopysign
> 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? MarcImperative 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
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
-
But in Python (note the floating point):
-1.0 * 0.0
-0.0 :rolleyes: MarcImperative 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
-
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 aSign
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 createdcopysign
> 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? MarcImperative 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
You're obviously not pessimistic enough.
I wanna be a eunuchs developer! Pass me a bread knife!
-
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 aSign
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 createdcopysign
> 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? MarcImperative 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
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.
-
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. MarcImperative 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
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,00So 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
-
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. MarcImperative 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
-
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 aSign
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 createdcopysign
> 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? MarcImperative 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
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.
-
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
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 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
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
-
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 aSign
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 createdcopysign
> 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? MarcImperative 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
-
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.
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
-
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.
-
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)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. -
Actually, I believe the correct answer is Gemini! (I know I married them)
-
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 aSign
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 createdcopysign
> 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? MarcImperative 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