CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Posts
    388

    m_floatValue = m_nValue * 0.5 >> Warning!

    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.


  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: m_floatValue = m_nValue * 0.5 >> Warning!

    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


  3. #3
    Join Date
    May 1999
    Location
    CA, USA
    Posts
    586

    Re: m_floatValue = m_nValue * 0.5 >> Warning!

    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
    [email protected]
    http://home.earthlink.net/~railro/

  4. #4
    Join Date
    Apr 1999
    Location
    New Zealand
    Posts
    8

    Re: m_floatValue = m_nValue * 0.5 >> Warning!

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured