Click to See Complete Forum and Search --> : m_floatValue = m_nValue * 0.5 >> Warning!


Shahzad
April 10th, 1999, 01:55 AM
float m_floatValue;
int m_nValue = 5;
m_floatValue = m_nValue * .5 ; // it give warning message.

: warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

I am not dealing with 'double' at all, and the operation I am doing is quite reasonable. Why does the compiler give this error and how to correct it.

Paul McKenzie
April 10th, 1999, 04:48 AM
You may not be dealing with "double", but the
compiler converts the 0.5 to double. One of the
things that you should be aware of are the type
conversions that C or C++ has to do with
intermediate values.

You probably need to cast the 0.5 to float (and
maybe the m_nValue also).

Regards,

Paul McKenzie

Rail Jon Rogut
April 10th, 1999, 11:38 AM
The compiler believes that the .5 is a double, without having to use a cast like (float).5, you may use 0.5F:

m_floatValue = m_nValue * 0.5F;

Rail

Recording Engineer/Software Developer
Rail Jon Rogut Software
railro@earthlink.net
http://home.earthlink.net/~railro/

Fraser
April 12th, 1999, 06:27 AM
float m_floatValue;
int m_nValue = 5;
m_floatValue = m_nValue * (float).5 ; // should do the trick

Since float is of lower precision than double (and the compiler assumes _all_ decimal literals are double) it is warning you.

It won't matter for most purposes, but if the software you're authoring is to control nuclear or surgical equipment (for example) it may be important.