Re: Convert short to float by Alex
Alex
Tue Jul 26 15:24:21 CDT 2005
Heinz Ozwirk wrote:
> "Alex Blekhman" schrieb
>> [...] As a general rule, I try to
>> avoid redundant casting if code compiles without
>> warnings.
>
> Basically I agree except for the last sentence. If there
> is a warning, examine its cause and fix the problem if
> there really is one. If there really is no possible cause
> for an error, disable the warning (using a pragma, not a
> cast) and comment why you are sure there is no reason for
> that warning. If you use a cast and later change the type
> of the destination variable (from float to double, for
> example), a cast will first convert the initial value to
> the old type and then to the new one. That would cause a
> loss of precision or even an error.
I didn't say that warning should be ignored. I said that if
compiler already knows how to do the job, then there is no
need to intervene with redundant cast. In cases where I do
need to cast I decide each time separately. For example,
many MFC/ATL classes still isn't consistent about
sizes/indexes: CSimpleStringT::GetLength returns `int' while
CAtlArray::GetCount returns `size_t' and CObArray::GetSize
returns `INT_PTR' at all. So, it's quite dificult to avoid
C4245 and C4389 when working with these classes. Silencing
it with #pragma and additional comment each time will
clutter a code even worse. Innocent cast to `int' is much
more readable.
Floating point calculations rules are quite different from
integer ones. As you pointed, casting in floating point
world can be less innocuous. So, it's likely that I'll
redesign a function to avoid dangerous casts altogether.