C program giving incorrect output for simple math!
Posted
by DuffDuff
on Stack Overflow
See other posts from Stack Overflow
or by DuffDuff
Published on 2010-05-02T22:45:23Z
Indexed on
2010/05/02
22:47 UTC
Read the original article
Hit count: 168
c
(all are declared as ints, none are initialized to anything beforehand. I have included math.h and am compiling with -lm)
cachesize = atoi(argv[1]);
blocksize = atoi(argv[3]);
setnumber = (cachesize/blocksize);
printf("setnumber: %d\n", setnumber);
setbits = (log(setnumber))/(log(2));
printf("sbits: %d\n", setbits);
when given cachesize as 1024 and blocksize as 16 the output is as follows:
setnumber: 64
sbits: 5
but log(64)/log(2) = 6 !
It works correctly when given cachesize 512 and blocksize 32. I can't seem to win.
I'm really hoping that it's a stupid mistake on my part, and I'd be grateful if anyone could point out what it is! Thank you!
PS: I posted this in Yahoo Answers first but that was probably silly. Won't be doing that again.
© Stack Overflow or respective owner