By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

conversion from 'const double' to 'float'

P: 71
Hello,

The following code:


[HTML]inline float SIGN(const double &a, const float &b)
{return b>=0 ? (a>=0 ? a:-a) : (a>=0 ? -a:a);}[/HTML]

gives me the warning:

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

how can I get rid of this?
Aug 10 '07 #1
Share this Question
Share on Google+
2 Replies


RRick
Expert 100+
P: 463
Have your SIGN function return a double. Then the internal computing of SIGN will be consistent. You will still get warnings if you try to fill a float with a value from SIGN, but that's between your float and what SIGN returns

This is a case of where a template solution is useful.
Expand|Select|Wrap|Line Numbers
  1. template < class T>
  2. T  Sign( T a, T b) {  blah blah blah }
  3.  
Now you have a Sign function that works with any single numeric type. Aslo, I use Sign instead of SIGN because all caps are usually reserved for macros.
Aug 10 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Expanding on RRick:

Do not mix floating point types. Unless you can write the reason on paper, use double. float is a holdover from the days of machines with small memories. Avoid relic functions that use float arguiments or returns.
Aug 11 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.