The standard way to round numbers is to choose the nearest whole value, if the initial value is exactly halfway between two values, i.e. there is a tie, then you choose the larger one.
However where I work we round in a different way. Everything is measured in powers of two. So wholes, halves, quarters, eights, sixteenths etc. This means our measurements are always a binary fraction. We also round to binary fractions. However when the value is exactly halfway between, instead of rounding up we round to the "nicer" number.
For example if I measure 5/8 but I need to round it to the nearest fourth, both 2/4 and 3/4 are equally close to 5/8, but 2/4 = 1/2 which is a nicer number so we round to 1/2. If I measured 7/8 and needed to round to the nearest fourth I would round up to 8/8 = 1.
To put it concretely if we express every number as \$x\times2^n\$ where \$x\$ is odd, then we round towards the number with the larger \$n\$.
Going back to the example: I measure 5/8 and I need to round it to the nearest fourth. The values I can choose are \$2/4=1\times2^{-1}\$ and \$3/4=3\times 2^{-2}\$, since -1 is larger than -2 we round towards that.
When both the options are fully reduced fractions you can think of this as rounding towards the fraction with the smaller denominator. However this intuition becomes a little bit strained when the options are whole numbers.
Challenge
In this challenge you will receive 3 numbers. An odd positive integer \$x\$, an integer \$n\$ and an integer \$m\$. You must round \$x\times2^n\$ to the nearest integer multiple of \$2^m\$ using the process described, and output the result as a binary fraction. This can be either a native binary fraction or the \$x\times2^n\$ format used for the input. The input will always be fully reduced so that the numerator, \$x\$, is odd, however you are not required to do so for your output.
This is code-golf so the goal is to minimize the size of your source code as measured in bytes.
Test cases
\$x\$ | \$n\$ | \$m\$ | ⇒ | \$x\$ | \$n\$ |
---|---|---|---|---|---|
5 | -3 | -2 | ⇒ | 1 | -1 |
3 | -1 | -3 | ⇒ | 3 | -1 |
9 | -3 | 0 | ⇒ | 1 | 0 |
1 | 3 | 4 | ⇒ | 0 | 5 |
1 | 4 | 4 | ⇒ | 1 | 4 |
3 | 3 | 4 | ⇒ | 1 | 5 |