CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2006
    Posts
    300

    compiler warning C4244

    How would i begin to fix this warning?

    Code:
    #include <iostream>
    using namespace std;
    float lbstokg(float);		// declaration
    
    int main()
    {
    	float lbs;
    	cout << "\nEnter your weight in pounds: ";
    	cin >> lbs;
    	cout << "Your weight in kilograms is " << lbstokg(lbs)
    		<< endl;
    	return 0;
    }
    //---------------------------------------------------------------------------------------------------
    // lbstokg()
    // converts pounds to kilograms
    float lbstokg(float pounds)
    {
    	return 0.453592 * pounds;
    }
    I started this little project and curious how to avoid data loss if i ever used this in a socket type of program.

  2. #2
    Join Date
    Nov 2018
    Posts
    156

    Re: compiler warning C4244

    Is the error "loss of precision"?

    Short answer.
    0.453592 is of type double, which promotes the multiplication to double as well. You then lose precision by returning a float.
    Write 0.453592f to make your floating point constant of type float.

    Long answer.
    Change all your floats to double
    Apart from some niche environments, there's no benefit to using floats.

  3. #3
    Join Date
    Nov 2006
    Posts
    300

    Re: compiler warning C4244

    Quote Originally Posted by salem_c View Post
    Is the error "loss of precision"?

    Short answer.
    0.453592 is of type double, which promotes the multiplication to double as well. You then lose precision by returning a float.
    Write 0.453592f to make your floating point constant of type float.

    Long answer.
    Change all your floats to double
    Apart from some niche environments, there's no benefit to using floats.
    Thanks, I never knew this.

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