Different output when using cout + printf vs printf only

132 views Asked by At

When I'm trying to print a division of a long long integer casted to a float, I get different output when trying to:

  1. use printf().
  2. use std::cout first before printf().

I'm using the long long variable as a sum to a 1000 performance timer results and then average them. I could probably do some recursion average splitting. But I'll worry that later.

Anyways, here's what is happening.

If I do this:

long long sum;

while (j < 1000){
    sum += time_record[j];
    j++;
}

cout << "Average execution time: "<< sum/1000000 << "ms\n";
printf("Average execution time: %f ms\n", (float)sum/1000000);

the output I get is:

Average execution time: 4ms
Average execution time: 4.197688 ms

Now if I try to remove cout, I get this output:

Average execution time: 140736480.000000 ms

What exactly is happening here?

0

There are 0 answers