Welcome! Log In Create A New Profile

Advanced

Wrong arithmetic results.

Posted by Aruskano 
Wrong arithmetic results.
June 11, 2010 08:46PM
Hi there,

Can anyone explain me why the wii prints the following? I suppose there's a good explanation of why these numbers are conflictive and I wonder if anyone here knows it.

printf("%llX", 0xC8*0x1000000);  //prints 100000000, must be C8000000
printf("%llX", 0xC8*0x100000000);  //prints C800000000

I point out the second result because it's bigger than the other one so it's not a size limit but a conflictive range of values. I can "workaround" this by doing the following (not a real workaround since I know it won't give exact values if the number is not pair)
var = (0xC8/2)*0x1000000;
var += (0xC8/2)*0x1000000;
I know this can be solved with some minor computing but my question is not how to solve it, I want to know why does this happen. Anyone?

I actually don't need this answer, but this caught my attention and I wanted to know if anyone knows the answer.
Re: Wrong arithmetic results.
June 11, 2010 09:57PM
printf("%llX", 0xC8*0x1000000LLU);
should give the right result. 0xC8 and 0x1000000 are assumed to be 32-bit integers and multiplying them results in an overflow. In your second example, one of the numbers is 64-bit so enough space is allocated for the result.
Sorry, only registered users may post in this forum.

Click here to login