Re: mixing types promotion etc..
- From: Jens.Toerring@xxxxxxxxxxxxxxxxxxx
- Date: 25 May 2005 18:27:00 GMT
Stef <Stefano@xxxxxxxxxxx> wrote:
> I have been programming C now for a couple of months but at this
> moment I am a bit confused with types like double float etc..
> My question wat type does the result have when a double value is mixed
> with an int... In the snippet below I substract a double with an int
> but the results are.. unpredictable for me.
> Can I mix ints with doubles doing floating point calculations ?
Yes, you can - you just have to figure out what makes something a
double and what an int;-)
> #include <stdio.h>
> int main(int argc, char *argv[]) {
> double C, M, Y;
> int R, G, B;
> C = M = Y = 0;
> R = 128;
> G = 211;
> B = 87;
> printf("type is INT: red:%d, green:%d, blue:%d\n", R, G, B);
> printf("type is DOUBLE: red:%g, green:%g, blue:%g\n",
> (double)R / 255, (double)G / 255, (double)B / 255);
Here you force 'R' etc. to be converted to a double before
the division is done, so it probably does what you expect.
> C = 1.0 - (R / 255);
> M = 1.0 - (G / 255);
> Y = 1.0 - (B / 255);
I guess you have trouble with these line since 'C', 'M' and 'Y'
are always 1.0 or 0.0. The problem is that e.g. in
R / 255
both 'R' and 255 are integers, so the division is an "integer
division", which results in another integer with the decimal
part of the result truncated. And since 'R' is probably never
larger than 255 you get for all values (except for R == 255)
a result of 1.0 (and 0.0 for R == 255).
Only after that calculation (for which you don't even need
parentheses since division has a higher "precedence" than
addition or subtraction) the result is converted to a double
since the other operand of the subtraction is a double.
If you want some probably more useful results you should write
instead
C = 1 - R / 255.0;
Now 'R' gets converted to a double before the division is done
since the other operand is a double. As you will note you can
use the integer 1 instead of a double here for the subtraction
bcause now the result of the division is already a double and
the 1 will then get converted automatically to a double.
> printf("ONE - C: %g, M: %g, Y: %g\n", C, M, Y);
> printf("TWO - C: %g, M: %g, Y: %g\n", (double)C, (double)M,
> (double)Y);
> printf("THREE - C: %g, M: %g, Y: %g\n",
> (1.0 - (R / 255)), (1.0 - (G / 255)), (1.0 - (Y / 255)));
Shouldn't you have 'C' instead of 'R' and 'M' instead of 'G' in the
second and third argument to printf()?
> printf("FOUR - C: %g, M: %g, Y: %g\n",
> (double)(1.0 - (R / 255)), (double)(1.0 - (G / 255)), (double)(1.0
> - (Y / 255)));
And here again? Perhaps that contributes to the confusion...
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@xxxxxxxxxxxxxxxxxxx
\__________________________ http://www.toerring.de
.
- Follow-Ups:
- Re: mixing types promotion etc..
- From: Dave Thompson
- Re: mixing types promotion etc..
- From: Jens . Toerring
- Re: mixing types promotion etc..
- References:
- mixing types promotion etc..
- From: Stef
- mixing types promotion etc..
- Prev by Date: Re: Port code from C++ to C?
- Next by Date: Re: mixing types promotion etc..
- Previous by thread: mixing types promotion etc..
- Next by thread: Re: mixing types promotion etc..
- Index(es):
Relevant Pages
|