Question

This is perhaps the strangest thing I have ever come across: A number which is simultaneously positive and negative! (And I can prove it to you, because I have the link to my code with outputs/inputs here at ideaone. Basically, my output is a negative number, but even stranger: when I check to see if it is less than zero, it is false (?!). Even more strange: when you multiply it by a number other than one, it switches back to being printed as a positive number.

This error does not happen when I compile on Xcode, but it does when compiled on the internet (or with some other compilers), such as the one in my link.

It's not important to understand exactly what it does, I'm wondering why this value is both negative and positive at the same time.

#include <stdio.h>
#include <cmath>

int main()
{
    unsigned long answer,T,n,N,a,d,b,L1,amount;

    scanf("%ld",&T); // number of test cases to loop through
    while (T--) {
        scanf("%ld",&N); 

        amount = 0;
        answer = 0;
        n=N-N%2;
        for (a = 1; a <= n/2; a++) {
            d = N-a;
            L1 = a*d;          

            for (b = 1; b*b < L1 ; b++) {
                // "amount" is always a positive number
                amount = 2*(((L1-1)/b) - b + 1) - 1;

                if (d==a) answer+=amount;
                else answer+=2*amount;  // we are only adding positive values to this
            }
        }

        if (answer<0) printf("This answer is less than zero %ld\n",answer);
        if (answer>0) printf("This answer is greater than zero %ld\n",answer);
        printf("%ld\n",answer);
        printf("%ld\n",2*answer/2);
    }
}

Input:

1 2500

Output:

Success  time: 0.02 memory: 3300 signal:0
This answer is greater than zero -1842629629
-1842629629
304854019

As you can see, my negative answer is greater than zero. And not to mention, it's different from the answer when I compiled in Xcode, even in positive form.

I am amazed by this. It's printing out a negative number that is "positive"

Proof is here at ideaone.com

Was it helpful?

Solution

answer is unsigned long. %ld is signed long in the format string. Change %ld to %lu and you won't be changing an unsigned into a signed value at print time and it will print correctly.

    if (answer<0) printf("This answer is less than zero %lu\n",answer);
    if (answer>0) printf("This answer is greater than zero %lu\n",answer);

OTHER TIPS

The problem lies in the signing of your data types. You're using an 'unsigned long', which can't be negative, technically. Negative numbers can still be assigned to it and mathematical operations that lead to the negatives (such as 0 - 1) can still be performed. But the processor will always treat these values as positive.

I know that some compiliers will treat unsigned numbers differently depending on compile and link time flags and that is probably what's causing the difference between the two compilers. Or even possibly the website your using my be interpreting the code, for safety reasons.

Either way, change the declarations to just 'long' and problem solved.

i can not add comment,but i executed in Ubuntu,and it show the same with you. FYI,when i input a and 25,it's ok

myqiqiang@ubuntu:~$ ./test
1
2500
This answer is greater than zero -1842629629
-1842629629
304854019
myqiqiang@ubuntu:~$ ./test
1 25
This answer is greater than zero 12722
12722
12722
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top