Re: Convert short to float



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.


.



Relevant Pages

  • Get/SetWindowLongPtr
    ... I have to cast it from something to avoid ... This is happening because on non-win64, SetWindowLongPtr is actually ... compiler warning me the LONG_PTR -> LONG is a possible loss of data while ... So while this will work great on win64, there's no way to avoid a warning on ...
    (microsoft.public.win2000.developer)
  • Re: Whats wrong with this sentence?
    ... The intention behind using generics is to avoid surprises for the JVM. ... You should use generics to avoid runtime(class cast) exceptions. ... So in this case you are ought to get this warning in eclipse. ... class ItemInfoMap extends HashMapimplements ...
    (comp.lang.java.programmer)
  • Re: [PATCH] [0/9] Use 64bit x86 machine check code for 32bit too
    ... warning: passing argument 2 of ‘strict_strtoull’ makes integer from pointer without a cast ...
    (Linux-Kernel)
  • DBD::ODBC compile problem from v1.00 to 1.09 on RH73
    ... Warning: LD_LIBRARY_PATH doesn't include /usr ... dbdimp.c:162: warning: passing arg 1 of `fprintf' makes pointer from ... integer without a cast ...
    (perl.dbi.users)
  • Re: 2.6.9-rc1-mm5
    ... drivers/net/starfire.c:924: warning: passing arg 1 of `readb' makes pointer from integer without a cast ...
    (Linux-Kernel)