ZYX
ZYX

Reputation: 101

Unsigned long long diffrent results

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

Answers (1)

Weather Vane
Weather Vane

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

Related Questions