This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

type casting?

Hello,

a question about how C166 handles type conversions:

I have this:

unsigned long c;
unsigned char i, d


if the following code is executed:

c = 20403776;
i = 0;
d = 3;
c += pow (16, i) * d;


the result should be: 20403776 + 1 * 3 = 20403779.
That's not the case, C166 calculates 20403780.

If I do the calculation like this:
c += (unsigned long) (pow (16, i)) * (unsigned long) d;

I get the correct result.
Now, of course the second calculation is "better", all components are
of the same datatype, but why would C166 not be able to handle these
conversions automatically, and why would it calculate a wrong result?

Thank you for any advice.
Holger

  • I would suspect that the compiler promotes all right hand terms to the type pow() returns, then once the evaluation is complete casts to the required type of the assignment. I don't have the ANSI spec handly.