2

Please explain output of the below given code.I m getting different values of c for both the cases i.e,

Case 1 : Value of n is taken from the standard input. Case 2 : Value of n is directly written in the code. link : http://www.ideone.com/UjYFQd

#include <iostream>
#include <cstdio>
#include <math.h>

using namespace std;

int main()
{
    int c;
    int n;

    scanf("%d", &n);    //n = 64
    c = (log(n) / log(2));
    cout << c << endl;  //OUTPUT = 5

    n = 64;
    c = (log(n) / log(2));
    cout << c << endl;  //OUTPUT = 6

    return 0;
}
10
  • I just ran your code and I get 6 in both cases. Cannot reproduce your problem. Try running it couple of times and see if anything changes.
    – Roman
    Nov 5, 2013 at 16:53
  • btw, cin is a simpler way than using scanf(). cin >> n;
    – Roman
    Nov 5, 2013 at 16:55
  • Add the following debugging lines after the scanf line: if (n == 64) { cout << "64 as expected!"; } else { cout << "Wtf?! " << n; } Nov 5, 2013 at 16:56
  • Use cmath instead of math.h and cin instead of scanf if you are using C++. Nov 5, 2013 at 16:56
  • Does scanf("%d", &n) even succeed? Nov 5, 2013 at 16:58

3 Answers 3

4

You may see this because of how the floating point number is stored:

double result = log(n) / log(2); // where you input n as 64
int c = (int)result; // this will truncate result.  If result is 5.99999999999999, you will get 5

When you hardcode the value, the compiler will optimize it for you:

double result = log(64) / log(2); // which is the same as 6 * log(2) / log(2)
int c = (int)result;

Will more than likely be replaced entirely with:

int c = 6;

Because the compiler will see that you are using a bunch of compile-time constants to store the value in a variable (it will go ahead and crunch the value at compile time).

If you want to get the integer result for the operation, you should use std::round instead of just casting to an int.

int c = std::round(log(n) / log(2));
4

The first time around, log(n)/log(2) is computed and the result is very close to 6 but slightly less. This is just how floating point computation works: neither log(64) nor log(2) have an infinitely precise representation in binary floating point, so you can expect the result of dividing one by the other to be slightly off from the true mathematical value. Depending on the implementation you can expect to get 5 or 6.

In the second computation:

n = 64;
c = (log(n) / log(2));

The value assigned to c can be inferred to be a compile-time constant and can be computed by the compiler. The compiler does the computation in a different environment than the program while it runs, so you can expect to get slightly different results from computations performed at compile-time and at runtime.

For example, a compiler generating code for x86 may choose to use x87 floating point instructions that use 80bit floating point arithmetic, while the compiler itself uses standard 64bit floating point arithmetic to compute compile-time constants.

Check the assembler output from your compiler to confirm this. Using GCC 4.8 I get 6 from both computations.

1

The difference in output can be explained by the fact that gcc is optimizing out the calls to log in the constant cases for example, in this case:

n = 64;
c = (log(n) / log(2));

both calls to log are being done at compile time, these compile time evaluation can cause different results. This is documented in the gcc manual in the Other Built-in Functions Provided by GCC section which says:

GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with _builtin are always treated as having the same meaning as the C library function even if you specify the -fno-builtin option. (see C Dialect Options) Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.

and log is one of the many functions that has builtin versions. If I build using -fno-builtin all four calls to log are made but without that only one call to log is emitted you can check this by building with the -S flag which will output the assembly which gcc generate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.