Little pupil
Little pupil

Reputation: 41

Mod problems in VS2010

there is a long long number 11111111111111111 , and a int number 99, when I using mod operator that it suddenly change 99 to 0.

After this,it becomes 99 again. this is my code :

    if(cur.sum % N == 0 && cur.sum > 0){
        printf("%I64d  %d  %d  %d",cur.sum,N,cur.sum%N,N);
        return cur.t;
    }

I never change the value of N beyond it except getting data. ![my ide is vs2010][1]

Upvotes: 0

Views: 56

Answers (1)

The Dark
The Dark

Reputation: 8514

cur.sum%N is still a long long and needs to be printfed with %I64d otherwise your first %d will print the first half of the long long value last %d will be printing the second half of the long long value*.

To fix it, change your printf:

printf("%I64d  %d  %I64d  %d",cur.sum,N,cur.sum%N,N);

or better yet, use the << operator on C++ standard library streams which mean you don't need to specify format parameters.

Note*: First and second half of the value may be different on different machines depending on your computer's endianness.

Upvotes: 1

Related Questions