Is there any way to round numbers in C?
I do not want to use ceil and floor. Is there any other alternative?
I came across this code snippet when I Googled f
Round value x to precision p, where 0 < p < infinite. (f.ex. 0.25, 0.5, 1, 2,…)
float RoundTo(float x, float p)
{
float y = 1/p;
return int((x+(1/(y+y)))*y)/y;
}
float RoundUp(float x, float p)
{
float y = 1/p;
return int((x+(1/y))*y)/y;
}
float RoundDown(float x, float p)
{
float y = 1/p;
return int(x*y)/y;
}