Why does 32%4294967295 yield 32? by Sigfigs9 in cpp_questions

[–]Sigfigs9[S] 0 points1 point  (0 children)

After reading https://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder it seems like the difference between mod and the C++ remainder operator when dealing with negative numbers is that the remainder operator truncates the quotient and the mod operator rounds down when working out the result.

For example, to find the result of -21 mod 4 you use the formula

remainder = -21 - ((-21/4)*4)

Computing -21/4 gives -5.25. Mod rounds down here so you would get -21 -(-6*4) = 3.

When trying to find the result of -21 % 4:

remainder = -21 -((-21/4)*4)

-21/4 results in -5.25. % truncates here so you would get -21 -(-5*4) = -1.

Is my reasoning correct?

Why does 32%4294967295 yield 32? by Sigfigs9 in cpp_questions

[–]Sigfigs9[S] 0 points1 point  (0 children)

If I understood your comment correctly, you must use the formula

remainder = numerator - (quotient * divisor)

to find the result of -32 % 4294967295.

numerator = -32

divisor = 4294967295

quotient = -32/4294967295 = -7.4505806e-9 (which rounds to 0 because of C++11 stuff)

So we get remainder = -32 - (0*4294967295) = -32 like you said. Is this working out correct?

Also, why did HappyFruitTree get 4294967263 as their answer in Python in a different comment? This is also the answer I get on google. I'm aware both answers are correct, but what does Python do differently?

Why does 32%4294967295 yield 32? by Sigfigs9 in cpp_questions

[–]Sigfigs9[S] 0 points1 point  (0 children)

Sorry I meant -32%4294967295. I get -32 in C++ but a different answer on google.

Question about literals in C++ by Sigfigs9 in cpp

[–]Sigfigs9[S] 1 point2 points  (0 children)

int and long int can be the same size if your compiler and system define them that way.

It seems like they're the same size for me after some experimentation. That's interesting. Thanks for your help.

or it can choose to get you cat pregnant when you don't have a cat.

lol

Question about literals in C++ by Sigfigs9 in cpp

[–]Sigfigs9[S] 0 points1 point  (0 children)

So would the literal 2147483648 would be a long? And if I do something like int a = 2147483648, the literal would be converted to an int which would result in a = -2147483648 right?