First of all, this is standard C.
I have a statement in my code like this:
After much confusion I found that x was assigned 2. This is not what I want though. I know that log(8)/log(2) equals 3.000000... in double and I want it to convert that to the integer 3 and save it into x. For some reason it will not do this though. I have tested by just putting
double x = 3;
int a = x;
printf("a: %d", a); and it prints out a 3 so I do know that it works in that case. When I use the log() function for some reason it floors the 3.000000... and makes it a 2. Is there any way to fix this so int x equals 3?
I also tried this:
double x = log(8)/log(2);
int a = ceil(x);
printf("a: %d", a); which gave me the right number 3. I don't know how the ceiling of 3.000000... would be 3 and the floor of 3.000000... would be 2.
The actual use of the statement in my code is in a function:
arrayInsert(int n, int *array){
int a = log(n)/log(2);
int b = a - 1;
int j = n + pow(2, b);
.
.
.
} a is assigned the right value everytime except when n = 8. log(2)/log(2) works, log(4)/log(2) works, log(16)/log(2) works, but log(8)/log(2) never works. I don't know how one value of it would cause problems. I'm using Visual Studio, could it have a bug in it?