I'm developing a C++ program and I need to find a formula that given a number to reduce and a limit number, get a value between 0 and this limit number.
I don't know if it is allow to put C++ code here, but I want to show you my function:
double Utils::reduceNumber(double numberToReduce, double limitNumber)
{
double factor = 0.0;
double result = 0.0;
factor = std::abs(numberToReduce / limitNumber);
if (factor != (int)factor)
factor = (int)factor + 1;
if (numberToReduce > 0)
result = numberToReduce - (byNumber * factor);
else
result = numberToReduce + (byNumber * factor);
return result;
}
For example, If I want to reduce −465.986246 in a limit between 0 and 24, I have to do this:
−465.986246 + (24 x 20) = 14.013754
What is the formula to obtain that 20?
Let $a$ be a given number. Also, suppose that the limit is between $0$ and $N$.
If you want an integer $b$ such that $$0\le a+Nb\le N\iff -\frac{a}{N}\le b\le \frac{N-a}{N},$$ then $$b=\left\lfloor\frac{N-a}{N}\right\rfloor$$ works where $\lfloor x\rfloor$ is the largest integer not greater than $x$.
For $a=−465.986246$ and $N=24$, we have $b=\left\lfloor\frac{24-(−465.986246)}{24}\right\rfloor=20$.