Reputation: 101
Why I got:
34458730320
3446590374
if I run:
#include <stdio.h>
int main()
{
printf("%llu\n",10248428203192896360LLU/594823321LLU*2LLU);
printf("%llu\n",2LLU*10248428203192896360LLU/594823321LLU);
return 0;
}
I expected that the second is correct (checked in Mathematica, Matlab).
Upvotes: 0
Views: 31
Reputation: 34585
The integer value
2LLU * 10248428203192896360LLU
overflows 64 bits, and the compiler should warn about that. The first is correct
10248428203192896360LLU / 594823321LLU * 2LLU
because the division is exact with no remainder, so it does not matter about doubling after the division was made.
Upvotes: 1