C (88/207)

From:Steven Dobbs
Date:12 Dec 99 at 17:35:14
Subject:Re: Overflow

From: Steven Dobbs <steven.dobbs@cableinet.co.uk>

Hello Lee

> if( ( T1 + ( (T2-T3)*11/4 ) ) > 205)
> {
> do whatever
> }

> Am I right in thinking that the fact that T1,T2 and T3 are unsigned chars
> doesnt matter if say T3 is bigger than T2. What size of storage will the
> compiler use to calculate the expression to compare to 205? Will there be
> any problems with the expression over/under flowing?

>From a course I did in c, I was told by the lecturer that in those sorts of
circumstances, c will take the type on the left and apply that. so I
_think_ its like this:

float f; unsigned char c;

(f*c) returns a float, but (c*f) will return an unsighned char. To force the
latter to return a float, you do this, (float)c*f

in the same way, f*c can be made to return an unsigned char with (unsighned
char)f*c

in your equation

T1 + ( (T2-T3)*11/4 ) ) > 205)

11 and 4 are considered integers so will probably return for 11/4 the value
2. if you want fractional values for 11/4, perhaps this will work 11.0/4.0
or definately

T1 + ( (T2-T3)*(float)11/4 ) ) > 205)

I hope thats correct and that it helps.

Regards



Steven Dobbs