6
$\begingroup$

Where $n \in \mathbb{R}$ and $m \in \mathbb{R}$, what is the function $f(n, m)$ that can achieve rounding behavior of money where the smallest denomination is not an power of ten?

For instance, if a 5¢ coin is the smallest denomination (like in Canada):

  • $f(1.02, 0.05) = 1.00$
  • $f(1.03, 0.05) = 1.05$
  • $f(1.29, 0.05) = 1.30$
  • $f(1.30, 0.05) = 1.30$

Or if $20 is the cutoff:

  • $f(9.99, 20) = 0$
  • $f(10, 20) = 20$
  • $f(39.99, 20) = 40$
  • $f(40, 20) = 40$
$\endgroup$
6
  • $\begingroup$ Also, if you could help me tag this, it'd be much appreciated! ^^ $\endgroup$ – Ben Leggiero May 25 '16 at 1:53
  • 11
    $\begingroup$ Nitpick. Obviously such a function exists because you just described it and given any two values we'll have output. What you mean is not does such a function exists, but can such a function be formulaically defined simply. $\endgroup$ – fleablood May 25 '16 at 3:21
  • $\begingroup$ @fleablood Fixed! Thanks for recognizing it was a nitpick, too :P $\endgroup$ – Ben Leggiero May 25 '16 at 3:28
  • 3
    $\begingroup$ It's interesting that three answers used the floor function, but none thought to use any version of the round-nearest function. $\endgroup$ – user14972 May 25 '16 at 4:49
  • 2
    $\begingroup$ @Hurkyl I suspect that’s because floor and ceiling are well-defined and there is standard mathematical notation for them. Round-nearest could mean a number of different things — e.g. does 0.5 round up or down? How about -0.5? To be rigorous, you’d normally end up writing the exact variant of round-nearest that you mean as an expression involving floor and/or ceiling. $\endgroup$ – alastair May 25 '16 at 12:14
7
$\begingroup$

Are you familiar with the floor function? For $x \in\mathbb{R}$, $\lfloor x\rfloor$ is the greatest integer less than or equal to $x$. That is, there exists an integer $n$ such that $n \leq x <n+1$, and we define $\lfloor x \rfloor =n$. So the floor function rounds to integers, though not to the nearest integer, as, say, $\lfloor 0.9\rfloor = 0$. We can remedy this by taking $f(x)=\lfloor x+0.5\rfloor$. Then $f$ rounds $x$ to the nearest integer to $x$ (rounding $0.5$ up to $1$, etc.).

If you want to round by larger increments, you can scale the input before and after putting it into the floor function. For example, say we want to round $123$ to the tens digit. Then we first have to scale $127$ down by a factor of $10$, round to the ones digit, and scale it back up: $$\left\lfloor \frac{127}{10}\right\rfloor\cdot 10=\lfloor 12.7\rfloor\cdot 10=12\cdot 10=120.$$ If we want to round to the nearest ten, then again we need to add $0.5$ inside the floor: $$\left\lfloor \frac{127}{10}+0.5\right\rfloor \cdot 10=\lfloor 12.7+0.5\rfloor \cdot 10=\lfloor 13.2\rfloor \cdot 10=13\cdot 10=130.$$

Hopefully you see that the formula you want is $$f(n,m)=\left\lfloor \frac{n}{m}+0.5\right\rfloor \cdot m.$$

$\endgroup$
7
  • 1
    $\begingroup$ These all come to the same conclusion, and it works wonderfully for me, but this one says it in a way I understand best. I'm accepting this one but voting them all up! $\endgroup$ – Ben Leggiero May 25 '16 at 3:27
  • 1
    $\begingroup$ to be noted that rounding "half up" (as suggested in this answer) carries some tiny, but possibly not negligible, amount of bias (en.wikipedia.org/wiki/Rounding#Tie-breaking) $\endgroup$ – Federico May 25 '16 at 8:42
  • $\begingroup$ @Federico how could that be counteracted? $\endgroup$ – Ben Leggiero May 25 '16 at 11:59
  • $\begingroup$ @Supuhstar the wiki article offers some unbiased alternatives, up to you to use your favourite. Your context will also dictate if it makes sense thinking about the problem. $\endgroup$ – Federico May 25 '16 at 12:04
  • 1
    $\begingroup$ @Federico you will if you're the one calculating taxes and discounts ;) $\endgroup$ – Ben Leggiero May 25 '16 at 12:21
6
$\begingroup$

Well, as with all things in math, if you need such a function, there's nothing stopping you from saying $f(n,m)$ is $n$ rounded to the nearest $m$, where the higher option is chosen for $n$ precisely between two consecutive multiples of $m$. If you need this a lot, it's easier to read than having a formula each time you invoke this.

However, one can build this out of the floor function, where $\lfloor x\rfloor$ is defined as the greatest integer less than or equal to $x$. So $\lfloor .5 \rfloor = 0$ for instance. Then, you have $$f(m,n)=m\left\lfloor \frac{n}m+\frac{1}2\right\rfloor.$$ This just works by taking the function $\lfloor x + 1/2\rfloor$, which rounds to the nearest integer (with half-integers rounding up), and appropriately scaling.

$\endgroup$
4
$\begingroup$

You didn't indicate what you wanted to do with negative values. The below rounds toward the nearest denominated value. There are other choices.

Let $a$ be the amount and $g$ be the minimum denomination. Your $f$ is $$ f(a,g) = g\left\lfloor \frac{a}{g} + \frac{1}{2} \right\rfloor $$ where the "$\lfloor$" and "$\rfloor$" indicate the floor function.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.