Finding an $f(x,y,n)$ such that $round[f(x,y,n)] = \lfloor\frac xn \rfloor + \lfloor\frac yn \rfloor$

118 Views Asked by At

Problem:

I have an equation: $$\left\lfloor\frac xn\right\rfloor + \left\lfloor\frac yn\right\rfloor$$ I need to find an equation that does NOT use the floor function, but will take those same two fractions $\frac xn$ and $\frac yn$ and will produce a result that will round to the same answer as the above equation which uses floor. Also $x$ and $y$ will only be integers $0$ or greater. There's also a catch: Only basic arithmetic operations can be used (+, -, * , /).

By way of example: I tried doing something like: $$(\frac x3 - 0.25) + (\frac y3 - 0.25)$$ But this has problems at certain places, like $x=2, y=2$ or at $x=5, y=8$ as well as many others at regular intervals.

Context:

I am designing a series of roll macros for the popular virtual gaming tabletop system, Roll20. The game system that I am working with is the Shadowrun 5th Edition roleplaying game.

In this game you roll a number of six-sided dice when your character does various things. However, the amount of damage that your character has taken will affect the number dice that you roll. Your character can suffer both physical damage or stun damage, and these two types of damages are tracked separately. For every 3 points (it is USUALLY 3 points, but technically can be from 2 to 6) of either physical OR stun damage (calculated separately) that your character takes, you subtract 1 die from the number of dice that you would normally roll.

For example: If your character normally has a dice pool of 12 for a particular skill, but your character has taken 4 points of physical damage and 8 points of stun damage, then instead of rolling 12 dice, you will roll 9 dice. You subtract 1 for having taken 3 points of physical damage, and subtract 2 more for having taken 6 points of stun damage.

The important point is that you DO NOT add the 4 and 8 together before figuring the modified dice pool. You determine those separately. This is expressed in the equation at the top of the page. The number of dice to roll is: $$dice\;to\;roll = base\;dice\;pool-\left(\left\lfloor\frac {phys\,dmg}{3}\right\rfloor + \left\lfloor\frac {stun\,dmg}{3}\right\rfloor\right)$$ Now, for various reasons that involve the way that the Roll20.net macro interpreter deals with macros, I cannot just simply use the floor function to do this. I also cannot use other math functions (like power() or log() or other useful), because these functions are not executed until AFTER the dice are rolled.

However, whenever the macro interpreter encounters a number which is supposed to represent the number of dice to roll, it will round that number to the nearest integer automatically. For this reason, I am trying to come up with a single equation to calculate the number of dice to roll that will round to the correct number.

This problem is similar to the question $f(x,n)$ such that $round[f(x,n)]=\lfloor\frac{x}{n}\rfloor$, which has not been answered. My problem just has more terms.

1

There are 1 best solutions below

0
On

After more though, I've managed to come up with a solution.

I didn't realize until later that the Roll20 macro language allows use of the modulo operator. This means I have been able to come up with an $[f(x,y,n)]$ that is EXACTLY equal to $\left\lfloor\frac xn\right\rfloor + \left\lfloor\frac yn\right\rfloor$.

The equation is: $$\left(\frac xn\right) + \left(\frac yn\right) - \left[\frac{(x\,mod\,n) + (y\,mod\,n)}{n}\right]$$

This seems trivially easy now that I look at it, but, as I said before, I didn't realize that the modulo operator was available to me in the macro language for Roll20. I include this answer for the sake of completion.