posty68
July 5th, 2002, 04:46 AM
what does this mean
lf.lfHeight = (int) ((float)m_tecData.pFont->lfHeight * stretchfactor);
thanks
:confused:
lf.lfHeight = (int) ((float)m_tecData.pFont->lfHeight * stretchfactor);
thanks
:confused:
|
Click to See Complete Forum and Search --> : Newbie code needs explantion posty68 July 5th, 2002, 04:46 AM what does this mean lf.lfHeight = (int) ((float)m_tecData.pFont->lfHeight * stretchfactor); thanks :confused: cup July 5th, 2002, 05:14 AM Which part don't you understand? Is it 1) the (int) cast 2) the (float) cast 3) why they bother casting at all Basically the multiplication has to be done as a float otherwise it loses its resolution. When it is assigned to an int, the compiler moans. As a result, the author has forced the float result to an int to stop the compiler moaning. Practical example int x = 2 * 1.7F This will give you x=2 because it converts the 1.7 to 1. 1.7F is the 1.7 as a float otherwise it defaults to double. This is based on the first item in the expression, which, in this case is the 2. If you would like it to do a floating point multiplication before the assignment, change it to int x = (float) 2 * 1.7F it will give you x =3 but the compiler will warn you that you may lose some resolution. If you want to tell the compiler, "I know about this but what I really, really want (Spice Girls?) is an int and I don't want you to moan about it" then you change it to int x = (int)((float) 2 * 1.7F) Alternatively, if you don't want two casts, you could code it as int x = (int)(1.7F * 2) This will force a floating point multiplication. Graham July 5th, 2002, 05:21 AM It means that someone doesn't really know what they're doing. Assuming that "lfHeight" is of type int in both types that it's a member of, then at least one of the casts is unnecessary. If "stretchFactor" is a float then casting "m_tecData.pFont->lfHeight" is unnecessary, since it will be promoted to float by the normal rules. The (int) cast then truncates the resulting float value to put it back in lf.lfHeight. If stretchFactor is an int, then the casts are wasteful, since strectfactor will be promoted to float and the resulting value truncated back to int, even though the whole statement is naturally int. If stretchfactor is double, then the float cast is pointless because it would still get promoted to double. Graham July 5th, 2002, 05:27 AM Are you sure about that cup? I'm not aware of any rule that will demote the type of a variable in an expression. AFAICR the expressions "2*1.7" and "1.7*2" are exactly equivalent and have type double. Similarly 'a' + 2 has type int, not char. posty68 July 5th, 2002, 05:36 AM does this make a difference LOGFONT lf(*(m_tecData.pFont)); float stretchfactor(300/(float)OutputTextExtent.cx); thanks cup July 5th, 2002, 07:38 AM Graham It is the case on some compilers: not all. Appendix A Sect 6.3 K&R says that float to int for -ve numbers is machine dependent but it does not mention +ve numbers. So whether 1.7F*2 is the same as 2*1.7F depends on the implementation. I have used compilers where 1.7F*2 is not the same as 2*1.7F and there is no end of casting to get it right. Appendix A Sect 6.6 K&R does not specify int to float: only char/short to int, float to double and if either operand is double, it is a double. This is largely left to the vendor. Presumably, some promote the float to a double and int to float to double and therefore it follows the double rule. If all the calculations are done in double, it is not a problem. Things only start going wrong when float is used. BTW I'm referring to the original K&R: not the ANSI version so if things have changed, I stand corrected. Graham July 5th, 2002, 08:23 AM I think that there would be a fairly major outcry if the arithmetic operators were non-commutative. If 2*1.7F came to 1 but 1.7F*2 came to 3.4F the effects could well be calamitous. Many people (including me) prefer to leave casts out of the equation if the promotion rules do the right thing. Anyways, I think the promotion rules are much more rigorous these days covering everything from char up to long double with a side order of unsigned (from which we get the common gotcha: "if (u > -1)" where u is an unsigned int). codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |