Why minus operator on uint64_t gives me wrong results?

271 Views Asked by At

in my super advanced code below I try subtract next values from a big num. I can't understand why this gives me wrong results. I am using cygwin gcc. $ gcc --version gives me gcc (GCC) 6.4.0

    uint64_t val = 0xFFFFFFFFFFFFFFFFULL; // 0xFF FF FF FF FF FF FF FF ULL = 8 bytes
    printf("[ 0]  %ju\n",  val);
    printf("[ 1]  %ju\n",  val -= 1e19);
    printf("[ 2]  %ju\n",  val -= 8e18);
    printf("[ 3]  %ju\n",  val -= 4e17);
    printf("[ 4]  %ju\n",  val -= 4e16);
    printf("[ 5]  %ju\n",  val -= 6e15);
    printf("[ 6]  %ju\n",  val -= 7e14);

gcc -Wall main.c -o main.exe && ./main.exe gives me results as below:

[ 0]  18446744073709551615
[ 1]  8446744073709551616 <- here and below should be 5
[ 2]  446744073709551616
[ 3]  46744073709551616
[ 4]  6744073709551616
[ 5]  744073709551616
[ 6]  44073709551616

Did you notice last digit? Similar things happened for other numbers, but I don't know the reason...

0

There are 0 best solutions below