-
July 1st, 2025, 08:37 PM
#1
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.
-
July 2nd, 2025, 12:05 AM
#2
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.
-
July 2nd, 2025, 06:38 AM
#3
Re: compiler warning C4244
 Originally Posted by salem_c
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|