Monday, June 13, 2011

Modular Arithmetic

More a note to self than anything else:

Given x, return a value which is a multiple of y and "still sufficiently close to x". Surely there's a way of phrasing that second part more eloquently?

In C# code:

decimal TransformXToMultipleOfY(decimal x, decimal y)
{
decimal modResult = x % y;

if (modResult == 0)
{
return x;
}

return x+ (y - (x % y));
}

No comments:

Post a Comment