C ++: what is the printf() format specification for float ? (Visual C ++)
I used to use %g for float and %lg for double .
It looks like the spec has changed, and float is undefined and double is %g .
I have a bit in memory that I print, so casting is not an option.
Is there a way to print float values ββusing printf() ?
Update:
This code was written for unit testing of common C ++ libraries used in the embedded system. Here is what I need to do to get the float to work. The code is in the template function:
template <typename T,typename TTYP,typename Ttyp,int bits,bool IsSigned> Error testMatrixT() { ...
Here is the code snippet:
if (typeid(Ttyp) == typeid(float)) { float64 c = *(float32*)&Tp(row,col); float64 a1 = *(float32*)&Arg1(row,col); float64 a2 = *(float32*)&Arg2(row,col); float64 e = *(float32*)&Exp(row,col); m_b = (c == e); _snprintf(m_acDiag, sizeof(m_acDiag)-1 , "add(Arg1,Arg2): arg1=%g, arg2=%g, Expected=%g, Actual=%g, Result: %s" , a1, a2, e, c, BOOL_PF(m_b)); } else { ...
Pretty ugly, isn't it? Using float as args gives a bad result. Perhaps due to the use of _snprintf() ? A few years ago I would use %lg and everything would be fine. No more.