6

I've written an equation in a programming language which I need to express using maths:

slots = (elements_count - (elements_count % slot_size) + slot_size) / slot_size

In Excel, you can also write (replaced cell names):

=(FLOOR.PRECISE(elements_count;slot_size)+slot_size)/slot_size

I've changed the story and variable names for the purpose of this question, but let's say the equation is used to figure out how many slots of size slot_size we need to fit elements_count elements.

The modulus is used to round up to the nearest whole number divisible with slot_size. The division then converts this to number of slots. It is correct that the result is 1 when there are zero elements.

How can I express this in mathematical notation? Is there a syntax to round up or down to the nearest number divisible with another number?

Atle
  • 165

1 Answers1

7

Usually, in mathematical notation we express rounding down with the floor function so $\lfloor a \rfloor$ is the largest integer less than or equal to $a$. Similarly, for rounding up, we use the ceiling function, so $\lceil a \rceil$ is the smallest integer greater than or equal to $a$. So for the rounded-down division of $a$ by some $b$, we would just write $\lfloor\frac{a}{b}\rfloor$. Similarly for rounding up you get $\lceil\frac{a}{b}\rceil$.


Edit: For the expression in the question, we can denote it as $\frac{e - e\%s + s}{s} = \frac{e - e\%s}{s} + 1 = \lfloor\frac{e}{s}\rfloor + 1$.

Colm Bhandal
  • 4,649
  • Wouldn't that just mean that you divide a by b and then round up or down? – Atle Aug 14 '15 at 16:47
  • Yes exactly. I see you actually have something a bit different: you just add 1 to the floor function. – Colm Bhandal Aug 14 '15 at 17:12
  • So there's no quick magic way to express this without explicitly writing out the original calculation? :) – Atle Aug 15 '15 at 08:10
  • Notationally, not that I know of. At least I think what I have above is probably the most standard way of expressing it. Computationally, I think your expression could be simplified if you are allowed assume the computer does integer division. E.g. in C++ you can just write $a/b +1$ instead of $(a - a%b)/b + 1$, because the computer automatically trunctates integer division. – Colm Bhandal Aug 15 '15 at 11:58