|
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
|
|
|
|
|
Another trick for C#: use the Ceiling function.
static void Main(string[] args)
{
double a = Math.Ceiling(-0.1);
double b = 1 * 0;
double c = -1 * 0;
Console.WriteLine("a = Math.Ceiling(-0.1), a=" + a.ToString());
byte[] bytes;
bytes = BitConverter.GetBytes(a);
Console.WriteLine("Bytes of a:");
Console.WriteLine(BitConverter.ToString(bytes));
Console.WriteLine("b = 1 * 0, b=" + b.ToString());
bytes = BitConverter.GetBytes(b);
Console.WriteLine("Bytes of b:");
Console.WriteLine(BitConverter.ToString(bytes));
Console.WriteLine("c = -1 * 0, c=" + c.ToString());
bytes = BitConverter.GetBytes(c);
Console.WriteLine("Bytes of c:");
Console.WriteLine(BitConverter.ToString(bytes));
}
Output:
a = Math.Ceiling(-0.1), a=0
Bytes of a:
00-00-00-00-00-00-00-80
b = 1 * 0, b=0
Bytes of b:
00-00-00-00-00-00-00-00
c = -1 * 0, c=0
Bytes of c:
00-00-00-00-00-00-00-00
I wrote about this some years ago for our SAS programming users (What's the difference between 0 and -0?[^]), as a similar IEEE 754 behavior exists there.
|
|
|
|
|
The flaw in this is that b and c are both calculated as integer values (at compile time) then converted to double. Integer doesn't have a negative zero.
"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
|
|
|
|
|
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.
|
|
|
|
|
|
Zero means nothing to me...
|
|
|
|
|
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!
|
|
|
|
|
Dear Mr. Clifton,
Please stop belittling me.
Signed,
0
|
|
|
|
|
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
|
|
|
|
|
Hah! It was I, Sander Rossel, disguised as 0 all along! You're my servant now
Or was I 0 disguised as Sander...?
Anyway, don't divide by me just in case
|
|
|
|
|
You're obviously not pessimistic enough.
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
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.
-- modified 10-May-16 4:33am.
|
|
|
|
|
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.
|
|
|
|
|
If 0 represents the contents of my glass then zero is very definitely negative.
|
|
|
|
|
I find 0.0 to be negative when it involves the quantity of money in my control.
|
|
|
|
|
I find 0.0 to be negative when it involves the quantity of money in my control.
|
|
|
|
|
Should I still continue in this profession or just quit now?
|
|
|
|
|
I would try to find better cartoons first.
it ain’t broke, it doesn’t have enough features yet.
modified 20-Oct-19 21:02pm.
|
|
|
|
|
I usually don't read programming themed cartoons, or cartoons at all, unless someone posts an "obligatory Dilbert."
Marc
|
|
|
|
|
|
With no bio, I can't be sure - but if it is assumed that you're new to this business, then as soon as you get some real experience (and possibly slit the throat of a user and/or manager or two) you'll understand.
If not, there's always a job selling artisinal ice cream treats to grouchy assholes. You need to worry about cartoons there, too. click[^]
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "As far as we know, our computer has never had an undetected error." - Weisert | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|