原来工作需要移植matlab中Floor函数,用C语言实现扩展
int RoundFloor(double dX)
{
int nRes = 0;
int nTemp = 0;
int nLast = 0;
if(dX > 0)
{
nTemp = dX;
nLast = (int)(dX*10.0) - nTemp*10;
if(nLast > 4)
{
nTemp +=1;
}
nRes = nTemp;
}
if(dX < 0)
{
nTemp = dX;
nLast = (int)(dX*10.0) - nTemp*10;
if(nLast < 0 && nLast < -4)
{
nTemp -=1;
}
nRes = nTemp;
}
if(dX == 0)
{
nRes = floor(dX);
}
return nRes;
}
//@正解
//RoundFloor(30.0) = 30
//RoundFloor(30.1) = 30
//RoundFloor(30.4) = 30
//RoundFloor(30.5) = 31
//RoundFloor(30.9)= 31
//
//RoundFloor(-30.0) = -30
//RoundFloor(-30.1) = -30
//RoundFloor(-30.4) = -30
//RoundFloor(-30.5) = -31
//RoundFloor(-30.9) = -31