473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

mixing types promotion etc..

Hello

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 ?

#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);

C = 1.0 - (R / 255);
M = 1.0 - (G / 255);
Y = 1.0 - (B / 255);

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)));
printf("FOUR - C: %g, M: %g, Y: %g\n",
(double)(1.0 - (R / 255)), (double)(1.0 - (G / 255)), (double)(1.0
- (Y / 255)));

return 0;
}
Nov 14 '05 #1
5 1320
Stef <St*****@sirtakie.tk> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Je***********@physik.fu-berlin.de wrote:
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).


Make that "a result of 0 (and 1 for R = 255)." Sorry, got that
confused with the final result of the complete expression.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3


Stef wrote:
Hello

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. When an arithmetic operator like `+' has
a double and an int as operands, the int is converted
to double and then the result is computed in double
arithmetic. However, this conversion happens on an
operand-by-operand basis, not just once for an entire
expression involving several operators, and this is
probably what's confusing you. See below.
#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);
This is fine, and presumably you got the expected results.
Since the three expressions are identical in form, I'll just
analyze the first one: `(double)R / 255'.

`(double)' is an operator that takes one operand, in this
case the int value of `R'. The effect is to produce a double
value equivalent to R's int value, and that converted value
becomes the numerator of the fraction.

Next comes the `/' operator. Its two operands are a double
(the converted R) and an int (255). The effect is to convert
the 255 from int to double (255.0) and then to divide the two
double quantities, yielding a double result just a little
larger than one-half.
C = 1.0 - (R / 255);
M = 1.0 - (G / 255);
Y = 1.0 - (B / 255);
printf("ONE - C: %g, M: %g, Y: %g\n", C, M, Y);
This is presumably what confuses you, and here's where
the "operand-by-operand" conversion makes a difference. Again,
I'll just analyze the first expression.

First comes the division, whose operands are `R' and `255'.
Both of these values have int type, so no conversion to a
common type is needed: the program performs an integer division
and yields an integer result. Integers have no fractional parts,
so the result has no fractional part: R / 255 yields zero, with
a largish remainder that's discarded.

Next comes the subtraction, whose operands are `1.0' and
the result of the division. That result is an int, so it's
converted to a double, yielding 0.0. Then the subtraction
happens as 1.0 - 0.0, yielding 1.0.
printf("TWO - C: %g, M: %g, Y: %g\n", (double)C, (double)M,
(double)Y);
Since C, M, and Y are already of type double, converting
them with `(double)' operators does precisely nothing. You
should get the same values as before.
printf("THREE - C: %g, M: %g, Y: %g\n",
(1.0 - (R / 255)), (1.0 - (G / 255)), (1.0 - (Y / 255)));
The first two of these work just as they did when you
computed C,M,Y above, and should give the same answers. The
last is different, since `Y' is a double. Let's work through
`1.0 - (Y / 255)':

First, the division: the numerator is the double `Y' and
the denominator is the `int' 255. The 255 is converted to
255.0, and then the division occurs, yielding a quotient of
about 0.004. Then comes the subtraction; both operands are
double so no conversion is necessary, and the difference
comes out to roughly 0.996.
printf("FOUR - C: %g, M: %g, Y: %g\n",
(double)(1.0 - (R / 255)), (double)(1.0 - (G / 255)), (double)(1.0
- (Y / 255)));


This is just like the preceding statement. Since each of
`1.0 - (x / 255)' produces a double result, applying the `(double)'
operator to it does nothing at all and you should get the same
result as before.

The thing to remember is that each operator takes care of
whatever conversions its own operands needs, but does so the
same way whether it's standing on its own or is part of a
larger expression. In `C = 1.0 - (R / 255)' the `/' operator
has a pair of int operands and performs an integer division;
the fact that the result will later be converted from int to
double doesn't change anything. If you want to do a double
division so you can retain the fractional part, you must cause
the operands to be converted to double before the division
takes place. Here are some rewrites (I've dropped the parens
because they're unnecessary; feel free to re-insert them on
aesthetic grounds if you like):

C = 1.0 - (double)R / (double)255;
C = 1.0 - (double)R / 255;
C = 1.0 - (double)R / 255.0;
C = 1.0 - R / 255.0;

--
Er*********@sun.com

Nov 14 '05 #4
I want to thank yoo all for the great and complete answers. It really
makes a difference. C is a powerfull language, so powerfull that I
will shoot myself in the foot many more times... But it's worth the
effort..

THnkx.. !!
Nov 14 '05 #5
On 25 May 2005 18:27:00 GMT, Je***********@physik.fu-berlin.de wrote:
Stef <St*****@sirtakie.tk> wrote:


<snip: correct answers about float/int arithmetic>
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()?

Probably not. He probably wants C for cyan as the _complement_
computed as 1-x of R=red, M=magenta as the complement of G=green, and
Y=yellow should be the complement of B=blue.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rudolf | last post by:
Is it possible to add a vb.net source code module to a c# project and if so how? Thanks Rudolf
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
2
by: Andy | last post by:
Hi... i'm trying to understand the concept of function name overloading in c++. to understand the resolving system it's important to understand the diffrent levels of typecasting (exact match,...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
3
by: shmartonak | last post by:
I have the following program. Under linux I've compiled it with gcc and in DOS I've compiled it with TURBOC 2.01. I get different outputs when I run it. /* start of program */ #include...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
2
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get...
4
by: Philipp | last post by:
Hello I have a function writeValueXml(,,int) (see below) which converts an int value to a string which is then passed to a overloaded version of itself for printing writeValueXml(,,string) ...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.