I would like to convert a short value to float. Is the following style
correct? If not, what is the correct way?

/////////////
short value = 123;
float a_float_value = (float) short_value;
/////////////

Re: Convert short to float by Rodrigo

Rodrigo
Tue Jul 26 06:18:41 CDT 2005

That's right, but I prefer static_cast<float> syntax, both versions do the
same thing but c++ casting operators have some advantages. If you want to
know more about this:
http://www.informit.com/articles/article.asp?p=397657&rl=1


--
Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org





Re: Convert short to float by Alex

Alex
Tue Jul 26 08:22:53 CDT 2005

Charles Tam wrote:
> I would like to convert a short value to float. Is the
> following style correct? If not, what is the correct way?
>
> /////////////
> short value = 123;
> float a_float_value = (float) short_value;
> /////////////

Actually, in this particular case I'd omit casting at all
and let the compiler to promote silently short_value to
float type. Semantically, there's no difference, but when
other developer will read the code he won't ask himself why
there's explicit cast. As ageneral rule, I try to avoid
redundant casting if code compiles without warnings.



Re: Convert short to float by Heinz

Heinz
Tue Jul 26 14:21:44 CDT 2005

"Alex Blekhman" <tkfx.N05P4M@yahoo.com> schrieb im Newsbeitrag
news:evnniUekFHA.3336@tk2msftngp13.phx.gbl...
> Charles Tam wrote:
>> I would like to convert a short value to float. Is the
>> following style correct? If not, what is the correct way?
>>
>> /////////////
>> short value = 123;
>> float a_float_value = (float) short_value;
>> /////////////
>
> Actually, in this particular case I'd omit casting at all
> and let the compiler to promote silently short_value to
> float type. Semantically, there's no difference, but when
> other developer will read the code he won't ask himself why
> there's explicit cast. As ageneral 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.

Heinz



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.



RE: Convert short to float by CharlesTam

CharlesTam
Tue Jul 26 17:37:01 CDT 2005

Thanks for the inputs from everyone.

"Charles Tam" wrote:

> I would like to convert a short value to float. Is the following style
> correct? If not, what is the correct way?
>
> /////////////
> short value = 123;
> float a_float_value = (float) short_value;
> /////////////