type casting

J

john

I thought about this and I just want confirmation on that it's right
If I do like this

double b = 10.6;
double c = 5.0;
double a = (int)b / c;

This will cast b to an int, and then divide it with c, (since
typecasts have higher precedence than /). But when dividing,
does b gets promoted to a double again, to be able to carry out
the division?

Am I right that b first casts to an int (removing fractal part),
and then back to double again?
 
M

milhous.30

no the cast works on the result

b/c ....then cast to (int)...then assignment

you want
a = ((int)b)/c

to force the cast first
 
J

john

no the cast works on the result

b/c ....then cast to (int)...then assignment

you want
a = ((int)b)/c

to force the cast first
Are you sure?
Then how come (typecast) have higher precedence than / ?
 
E

Eric Sosman

(e-mail address removed) wrote On 05/03/06 16:59,:

First, learn how to quote enough context so your
message makes sense. There's a time-honored literary
convention of beginning "in medias res," but Usenet
is not the place for it.

For those just joining in, the question was about
double b = 10.6;
double c = 5.0;
double a = (int)b / c;

.... and whether the expression in the third line means
`((int)b) / c' or `(int)(b / c)'.
no the cast works on the result

b/c ....then cast to (int)...then assignment

you want
a = ((int)b)/c

to force the cast first

Second, after you've learned how to quote context,
learn C. Your answer is flat-out wrong, R-O-N-G, wrong.

A cast operator "binds more tightly" than the division
operator, so `(int)b / c' means `((int)b) / c' and not
`(int)(b / c)'. With the values in the example, `a' will
wind up as 2.0 either way

actual: (int)10.6 -> 10, 10 / 5.0 -> 10.0 / 5.0 -> 2.0

wrong: 10.6 / 5.0 -> 2.3, (int) -> 2, -> 2.0

.... but if you try it with 1.5 and 0.5, say, you'll see

actual: (int)1.5 -> 1, 1 / 0.5 -> 1.0 / 0.5 -> 2.0

wrong: 1.5 / 0.5 -> 3.0, (int) -> 3, -> 3.0
 
F

Fred Kleinschmidt

no the cast works on the result

b/c ....then cast to (int)...then assignment

you want
a = ((int)b)/c

to force the cast first
NO, b will be cast to an int before the division occurs.
Then that result will be promoted to a double before the divide.
Just try it:
b = 10.6;
c = 5.3;
a = (int)b/c;
Answer: a = 1.886792, which is 10.0/5.3
 
M

milhous.30

(e-mail address removed) wrote On 05/03/06 16:59,:
First, learn how to quote enough context so your
message makes sense. There's a time-honored literary
convention of beginning "in medias res," but Usenet
is not the place for it.

For those just joining in, the question was about


... and whether the expression in the third line means
`((int)b) / c' or `(int)(b / c)'.





Second, after you've learned how to quote context,
learn C. Your answer is flat-out wrong, R-O-N-G, wrong.

A cast operator "binds more tightly" than the division
operator, so `(int)b / c' means `((int)b) / c' and not
`(int)(b / c)'. With the values in the example, `a' will
wind up as 2.0 either way

actual: (int)10.6 -> 10, 10 / 5.0 -> 10.0 / 5.0 -> 2.0

wrong: 10.6 / 5.0 -> 2.3, (int) -> 2, -> 2.0

... but if you try it with 1.5 and 0.5, say, you'll see

actual: (int)1.5 -> 1, 1 / 0.5 -> 1.0 / 0.5 -> 2.0

wrong: 1.5 / 0.5 -> 3.0, (int) -> 3, -> 3.0

There I properly quoted it(despite the fact that the quoting is not
built in to the system).

Secondly the cast does NOT bind tighter in all cases
Consider the example

int main()
{
int a = 7;
int b = 8;
double c = 7/8;
double d = (double)7/8;
double e = ((double) 7)/8;
printf("%lf %lf %lf", c,d,e);

}

prints

0.000000 0.875000 0.875000

This example shows that the casting does not ALWAYS bind tighter.

So use parans to ensure the correct binding. The compiler should
optimize correctly.

(e-mail address removed)
 
E

Eric Sosman

(e-mail address removed) wrote On 05/03/06 18:17,:

Thank you for learning to quote context; it makes
things much easier. One tiny addition: remember to
include attributions when you quote, so one can tell
who's being quoted. See the "milhous ... wrote" line
above? That's what's wanted. Read some of the other
threads in this and other newsgroups, and you'll get
an idea of the style.

Back to our story: I wrote
A cast operator "binds more tightly" than the division
operator, so `(int)b / c' means `((int)b) / c' and not
`(int)(b / c)'. [...]

Secondly the cast does NOT bind tighter in all cases
Consider the example

int main()
{
int a = 7;
int b = 8;
double c = 7/8;
double d = (double)7/8;
double e = ((double) 7)/8;
printf("%lf %lf %lf", c,d,e);

}

prints

0.000000 0.875000 0.875000

This example shows that the casting does not ALWAYS bind tighter.

It shows the opposite. Since d and e are identical,
the extra parentheses in e's computation made no difference:
they did not change the meaning of the expression. That
means that in d's computation, the cast binds to the 7 and
not to the quotient 7/8 (which would have been zero).

A thought: Are you using the word "cast" in the same
way the C language does? There are exactly two casts in
the code above; they are the `(double)' operators. The
conversion from int to double that takes place in c's
computation is not a cast. That conversion does, indeed,
apply to the result of the entire r.h.s. Why? Because
the `=' assignment operator[*] "binds less tightly" than
divisions and casts!

[*] Pedantry: The `=' signs above are not actually
assignment operators, but punctuation that introduces the
initializers. However, initialization takes place "as if
by assignment," and the outcome is the same.
 
P

pete

john said:
I thought about this and I just want confirmation on that it's right
If I do like this

double b = 10.6;
double c = 5.0;
double a = (int)b / c;

This will cast b to an int, and then divide it with c, (since
typecasts have higher precedence than /). But when dividing,
does b gets promoted to a double again, to be able to carry out
the division?

Am I right that b first casts to an int (removing fractal part),
and then back to double again?

Yes.
 
P

pete

There I properly quoted it(despite the fact that the quoting is not
built in to the system).

Secondly the cast does NOT bind tighter in all cases
Consider the example

int main()
{
int a = 7;
int b = 8;
double c = 7/8;
double d = (double)7/8;
double e = ((double) 7)/8;
printf("%lf %lf %lf", c,d,e);

}

prints

0.000000 0.875000 0.875000

This example shows that the casting does not ALWAYS bind tighter.

How does it show that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Chatbot 0
Weird Behavior with Rays in C and OpenGL 4
Function is not worked in C 2
Casting one pointer type to another 6
Homework in C - Help Needed 1
TF-IDF 2
type casting 5
casting 23

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,515
Latest member
Harvey7327

Latest Threads

Top