Hi All,
I am having a strange issue. Below is the code snippet. If I print
fraction * (double)::pow((double)10,scalingFactor)
which is a double I am getting 154 when I type cast that to
int
as
(int)( ((fraction) * ((double)::pow((double)10,scalingFactor))))
it is becoming 153. Not sure why casting get me different output. Also for another strange thing is if store the
fraction * (double)::pow((double)10,scalingFactor)
into a double and then type cast it returning me 154.
////////////////////////////////////////////////////////////////////////////////
std::string floatToString(double fvalue, int valueLength, int scalingFactor)
{
double i = 0;
double fraction = modf(fvalue, &i);
int intpart = (int)i;
double returnValue = fraction * (double)::pow((double)10,scalingFactor);
std::stringstream ss;
ss << std::setfill('0')
<< std::setw(valueLength-scalingFactor)
<< intpart;
ss << std::setfill('0')
<< std::setw(scalingFactor)
<< (int)( ((fraction) * ((double)::pow((double)10,scalingFactor))));
std::cout << " VALUE = " << fvalue << " | scalingFactor = " << scalingFactor
<< " | fraction= " << fraction << " | ss.str() = " << ss.str()
<< " | ::pow((double)10.0,scalingFactor) = "<< ::pow((double)10.0, scalingFactor)
<<" | fraction * (double)::pow((double)10.0,scalingFactor) = "<< fraction * (double)::pow((double)10.0, scalingFactor)
<<" | (int)(fraction * (double)::pow((double)10.0,scalingFactor)) = "<< (int)(fraction * (double)::pow((double)10.0, scalingFactor))
<< " | returnValue = "<< (int)returnValue
<< " | TESTOUTPUT =" << (int)( ((fraction) * ((double)::pow((double)10,scalingFactor))))
<< std::endl;
return ss.str();
}
OUTPUT:
VALUE = 0.154 |
scalingFactor = 3 |
fraction= 0.154 |
ss.str() = 000153 |
::pow((double)10.0,scalingFactor) = 1000 |
fraction * (double)::pow((double)10.0,scalingFactor) = 154 |
(int)(fraction * (double)::pow((double)10.0,scalingFactor)) = 153 |
returnValue = 154 | TESTOUTPUT=153