At 10:04 PM +0100 26-02-00, Chuck Atwood wrote:
>And K&R disagrees with you, see page 187:
>
>An expression preceded by the parenthesized name of a data type causes
>conversion of the value of the     expression to the named
>typed.  The construction is call a cast.

I've had lots of trouble with this sort of thing as well.  Different
compilers seem to do different unpredictable things.  (Possibly because
they try to optimize out the multiplication and just compile a constant
into your application.)

The best and most portable workaround I've found is to just create a new
constant.

e.g. instead of
        long result;
        result = 10L * 3600L;

Just do:
        result = 36000;

If you're not dealing with constants, then splitting the math into multiple
lines guarantees sizing conversions happen when you want.

e.g. instead of

        int a, b;
        long result;
        result = (long)a * (long)b;     // should always work
                                        // but doesn't really in my experience
        // or
        result = (long)a * b;           // sometimes works
        // or
        result = (long) (a * b);        // wrong!

just do:
        result = a;
        result *= b;

When I disassemble the result, I've always found that the multiple line
version generates exactly the same code as the best optimizations of the
single line version.  And there's no funny casting required, it's implicit
in the assignment.

                                --Bob



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to