0
$\begingroup$

Roll20 has a dice expression parser which, among many other things, allows you to use a subset of the expression logic to determine the number of dice to roll. For example:

/roll (@{level} / 2)d6

Not everything that the dice engine understands is permitted as an expression for determining the number of dice. The result of the expression is automatically rounded towards positive infinity (because fractional dice don't make sense). However, function calls like floor(x) are among the things not permitted in the number-of-dice expression, and several games want a calculation for the number of dice, and that calculation needs to round down.

For something like @{level} / 2, the solution is pretty simple: subtract 0.5, and when the system rounds up, you get the same result as if you had rounded down instead of subtracting 0.5. The same thing works for @{level} / 3 (subtract $\frac{1}{3}$), but the pattern breaks down as the denominator increases.

I want to find a general function $f(x,n)$ such that $round[f(x,n)]=\lfloor\frac{x}{n}\rfloor$. While that doesn't cover every possible expression use for determining the number of dice, it covers a large portion, especially in Dungeons and Dragons which holds over 71% of the games on the site (between all versions plus Pathfinder)

If it matters for this problem, $x$ and $n$ may be assumed to be natural numbers

$\endgroup$
1
$\begingroup$

I've been working on a similar problem for Roll20, and I've found a solution that does not involve rounding at all. In fact you can express $f(x,n)$ using the modulo operator so that it will exactly equal $\lfloor \frac xn \rfloor$.

The equivalency is: $$\left\lfloor \frac xn \right\rfloor\;=\;\frac xn - \frac{x\,mod\,n}{n}$$

I left it un-simplified for clarity of what is happening.

I found a similar equivalency for the ceiling function:

$$\left\lceil \frac xn \right\rceil\;=\;\frac{x}{n} + \left[ \left(n-\frac{x\,mod\,n}{n}\right)\,mod\,n\right]\,mod\,1$$

This one I couldn't think of a way to simplify. It looks ugly, but it works.

$\endgroup$
0
$\begingroup$

I haven't checked all the details but I'm pretty sure (if I understand the problem correctly) that $$ f(x, n) = \frac {x}{n} -\frac{n-1}{n} $$ will do the trick.

$\endgroup$
  • $\begingroup$ $round[\frac{3}{3}-\frac{3-1}{3}]=0$ while $\lfloor\frac{3}{3}\rfloor=1$ $\endgroup$ – Brian S Jan 11 '15 at 4:06
  • $\begingroup$ Apologies, I was under the impression that 'round' rounded up to the next integer. $\endgroup$ – APGreaves Jan 11 '15 at 17:05
  • $\begingroup$ round will round up at >= 0.5 and down at < 0.5. $\endgroup$ – Brian S Jan 12 '15 at 14:50

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.