CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Newbie code needs explantion

    what does this mean
    lf.lfHeight = (int) ((float)m_tecData.pFont->lfHeight * stretchfactor);

    thanks


  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.
    Succinct is verbose for terse

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    May 2000
    Location
    England
    Posts
    574

    sorry maybe should have included these ?

    does this make a difference
    LOGFONT lf(*(m_tecData.pFont));

    float stretchfactor(300/(float)OutputTextExtent.cx);

    thanks

  6. #6
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.
    Succinct is verbose for terse

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured