Author Topic: checking for -0? <solved>  (Read 1363 times)

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
checking for -0? <solved>
« on: March 13, 2011, 10:14:03 AM »
c programming...

I can't seem to get this one.

The integer 0 doesn't play by the same rules as a number that is not zero. How can I distinguish between pos and neg 0? I can't use unsigned, because my variables can be negative, but 0 can't be negative.



Code: [Select]
#include <stdio.h>

int main  ()

{
    int i=0, j=-2;
    
    if (i==-0)
    printf ("i is a neg 0\n");
    
    if (i==0)
    printf ("i is a positive 0\n");
    
    if (j==-2)
    printf ("j is a neg 2\n");
    
    if (j==2)
    printf ("j is a positive 2\n");
    
    getchar ();
    getchar ();
    
    return 0;
}

Output:

i is a neg 0
i is a positive 0
j is a neg 2




How do I get it to state 0 is pos or neg?
« Last Edit: March 15, 2011, 05:18:24 PM by docnascar »
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: checking for -0?
« Reply #1 on: March 13, 2011, 10:30:56 AM »
Quote
   if (i==-0)
    printf ("i is a neg 0\n");

this is wrong code.
C native integer can't store a negative 0, therefore the above test evaluate to i == (-0) equivalent to i == 0.
« Last Edit: March 13, 2011, 10:38:46 AM by as »

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: checking for -0?
« Reply #2 on: March 13, 2011, 10:57:49 AM »
What if its a double?

I still get the same answer.
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: checking for -0?
« Reply #3 on: March 13, 2011, 11:16:45 AM »
What if its a double?

I still get the same answer.



no C native type can store negative 0, if you need negative zero you should use some math library. Don't know what you are trying to achieve, here some links:
decNumber from IBM: http://speleotrove.com/decimal/
GNU Multiple Precision Arithmetic Library: http://gmplib.org/
GNU math: http://www.gnu.org/software/kawa/api/gnu/math/package-summary.html

AS



Online muungwana

  • Hero Member
  • *****
  • Posts: 6208
Re: checking for -0?
« Reply #4 on: March 13, 2011, 12:18:55 PM »

Does "-0" exist? Where does it exist on a number line?If it does then it must be in advance math classes i never got to privilege to take. As far as i know, "0" is a first positive number and all numbers to its left are negative starting with "-1" if talking about integers.

.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: checking for -0?
« Reply #5 on: March 13, 2011, 01:02:12 PM »

Does "-0" exist? Where does it exist on a number line?If it does then it must be in advance math classes i never got to privilege to take. As far as i know, "0" is a first positive number and all numbers to its left are negative starting with "-1" if talking about integers.




never used, but yes, look like existing: http://en.wikipedia.org/wiki/Signed_zero

AS

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: checking for -0?
« Reply #6 on: March 14, 2011, 03:49:30 PM »
My program is making a data table based on the users input.

If the user types in a negative number, the program errors because the table can not have negative numbers.

However if the user types in -0, then it gets added to my table of positive numbers. My table can not visually contain any negative values.

Good:

Code: [Select]
5   5   6   9   3
4   0   1   4   3
5   1   0   9   3

Bad:
Code: [Select]
5   5   6   9   3
4   0   1   4   3
5   1   -0   9   3
« Last Edit: March 14, 2011, 03:54:16 PM by docnascar »
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: checking for -0?
« Reply #7 on: March 14, 2011, 04:06:14 PM »
Quote
If the user types in a negative number, the program errors because the table can not have negative numbers.
This is what you want from your program, as I understand.

Quote
if the user types in -0, then it gets added to my table of positive numbers
Is OK for you that a -0 is assigned in your table as 0 ?

Quote
My table can not visually contain any negative values.
No. Visually contain means nothing.

One thing is how a value is stored in a variable = memory
One thing is how a value is displayed/printed.

Probably you are displaying/printing the strings the user input to your program, not the values stored in your table.

AS
« Last Edit: March 14, 2011, 04:11:38 PM by as »

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: checking for -0?
« Reply #8 on: March 14, 2011, 04:10:44 PM »
Quote
if the user types in -0, then it gets added to my table of positive numbers
Is OK for you that a -0 is assigned in your table as 0 ?

Hi as.
I want it to error on "-0" but allow "0".
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: checking for -0?
« Reply #9 on: March 14, 2011, 04:14:35 PM »
Quote
if the user types in -0, then it gets added to my table of positive numbers
Is OK for you that a -0 is assigned in your table as 0 ?

Hi as.
I want it to error on "-0" but allow "0".


Can you show the code you are using for input ?

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: checking for -0?
« Reply #10 on: March 14, 2011, 04:34:12 PM »
For simplicity. can we use?


Code: [Select]
int main  ()

{
    double i=0;
       
    while (i!=-999)
    {   
        printf ("Only enter -0, 0, -2, or 2. Enter -999 to exit\n");
        scanf ("%lf",&i);     

        if (i==-999)
        break;
       
        if (i==-0)
        printf ("i is a neg 0\n");
       
        else if (i==0)
        printf ("i is a positive 0\n");
       
        else if (i==-2)
        printf ("j is a neg 2\n");
       
        else if (i==2)
        printf ("j is a positive 2\n");
       
        else
        printf ("Can't obey the rules can you \?\n");
    }
   
    getchar ();
    getchar ();
   
    return 0;
}


0 and -0 output "i is a neg 0"



My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: checking for -0?
« Reply #11 on: March 14, 2011, 04:41:30 PM »
For simplicity. can we use?


Code: [Select]
int main  ()

{
    double i=0;
       
    while (i!=-999)
    {   
        printf ("Only enter -0, 0, -2, or 2. Enter -999 to exit\n");
        scanf ("%lf",&i);     

        if (i==-999)
        break;
       
        if (i==-0)
        printf ("i is a neg 0\n");
       
        else if (i==0)
        printf ("i is a positive 0\n");
       
        else if (i==-2)
        printf ("j is a neg 2\n");
       
        else if (i==2)
        printf ("j is a positive 2\n");
       
        else
        printf ("Can't obey the rules can you \?\n");
    }
   
    getchar ();
    getchar ();
   
    return 0;
}


0 and -0 output "i is a neg 0"



No, as wrote above at the beginning of the thread your test is wrong:
Quote
        if (i==-0)
        printf ("i is a neg 0\n");

Retry the same code without the wrong test:
Code: [Select]
int main ()
{
        double i = 0;

        while (i != -999)
        {
                printf ("Only enter -0, 0, -2, or 2. Enter -999 to exit\n");
                scanf ("%lf", &i);

                if (i == -999)
                        break;

//              if (i == -0)
//                      printf ("i is a neg 0\n");

                else if (i == 0)
                        printf ("i is a positive 0\n");

                else if (i == -2)
                        printf ("j is a neg 2\n");

                else if (i == 2)
                        printf ("j is a positive 2\n");

                else
                        printf ("Can't obey the rules can you \?\n");
        }

        getchar ();
        getchar ();

        return 0;
}

AS

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: checking for -0?
« Reply #12 on: March 14, 2011, 04:54:13 PM »
AS. Thanks for sticking with me on this. I'm trying to understand.  ;D

I think your missing my point and I"m missing yours.

Are you saying that C programming can not distinguish between a 0 or -0 (as a number) and there is no way to make my "wrong test" correct so it will distinguish between 0 and -0?

ie, when you enter "0" the program says "i is a positive 0" and when you enter "-0" the program says "i is a neg 0"

My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: checking for -0?
« Reply #13 on: March 14, 2011, 05:09:24 PM »
AS. Thanks for sticking with me on this. I'm trying to understand.  ;D

I think your missing my point and I"m missing yours.

Are you saying that C programming can not distinguish between a 0 or -0 (as a number) and there is no way to make my "wrong test" correct so it will distinguish between 0 and -0?

ie, when you enter "0" the program says "i is a positive 0" and when you enter "-0" the program says "i is a neg 0"


Yes, a C integer or a C double or a C float cannot store a value equal to "-0". when you write -0 in C this is an expression made
by unary operator minus and a value of zero.

What a user input in your program is a string made from character ascii menus and character ascii zero

when your program perform this statement scanf ("%lf",&i); it really does a format conversion from string to double,
and the string "-0" is converted to a double of value 0.

Of course, it's possible to check for strings (i.e. strcmp(str_value, "-0") and in this case you can differentiate the string "0" from the string "-0" because
they are really different:
Code: [Select]
int main ()
{
        char string_number[80]; // storage for string input
        double i = 0; // storage for numeric value

        printf ("Only enter -0, 0, -2, or 2. Enter \"quit\" to exit\n");

        while (1) // will exit on "quit" test
        {
                scanf ("%s", string_number); // input strings

                if (strncmp(string_number, "quit", 1) == 0) // test "quit" strings
                        break;

                if (strcmp(string_number, "-0") == 0)   // invalid input
                {
                        printf ("i is a neg 0\n");
                        printf ("Invalid value\n");
                        continue;
                }

                else if (strcmp(string_number, "0") == 0)
                        printf ("i is a positive 0\n");

                else if (strcmp(string_number, "-2") == 0)
                        printf ("j is a neg 2\n");

                else if (strcmp(string_number, "2") == 0)
                        printf ("j is a positive 2\n");

                else // invalid input
                {
                        printf ("Can't obey the rules can you \?\n");
                        continue;
                }

                // here your input has been validated against your rules
                sscanf(string_number, "%lf", &i);
                printf("stored number is %lf\n", i);

        }

        return 0;
}

AS
« Last Edit: March 14, 2011, 05:44:19 PM by as »

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: checking for -0?
« Reply #14 on: March 15, 2011, 04:53:57 PM »
thanks for the knowledge. I'll have to try the string thing later.
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p