Re: mixing types promotion etc..



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
.



Relevant Pages

  • Re: Rewriting this magic sword - changes
    ... Confusion in 3.x isn't so good against spellcasters since they all ... this weapon: Contagion (mindfire). ... impacting the victim's INT and ...
    (rec.games.frp.dnd)
  • Re: Bit-related question
    ... Subtraction is addition of a number that has been negated. ... complement signed ints, negation consists of adding one to the one's ... int main ... Copyright Microsoft Corporation 1984-2002. ...
    (microsoft.public.vc.language)
  • Re: Rewriting this magic sword - changes
    ... Confusion in 3.x isn't so good against spellcasters since they all have ... this weapon: Contagion (mindfire). ... impacting the victim's INT and ...
    (rec.games.frp.dnd)
  • Re: implicit type template warning
    ... spreading more confusion than knowledge! ... > template parameter and is thus a dependent name. ... i.e. there is no need to wait until instantiation to ... int foo = 0; ...
    (comp.lang.cpp)
  • Re: Decreasing order of address within main
    ... int main ... But, from check_1, it confirms that subtraction of a constant ... Don't ignore warning messages. ... The result of subtracting two pointer values is a signed integer ...
    (comp.lang.c)