Search code examples
compiler-warnings

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


Why do I get this compiler warning message on lines 2-6?

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

inline float SIGN(const double &a, const float &b)
    {return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);}
inline float pow (float x, double y) {return pow(double(x),y);}
inline float pow (double x, float y) {return pow(x,double(y));}
inline float atan2 (float x, double y) {return atan2(double(x),y);}
inline float atan2 (double x, float y) {return atan2(x,double(y));}

Solution

  • In C and C++, the float type is lower precision than the double type. You have declared your functions as returning floats, but the functions are working with doubles. The rules of mixing numerical types apply here, and some types get converted, silently, as you combine them.

    The results of your functions, as calculated inside them, are doubles which you then return from the functions as floats. The compiler is warning you that it is possible that the value in the double type you are returning from inside your function will not fit in the functions' return type float properly, and some precision may be lost in the conversion. It does not otherwise explicitly warn you about mixing float and double. But it might if you mixed int and double.

    There is little value to using float types on modern (younger than 10 years) desktop or server hardware.