1
$\begingroup$

Is it possible to calculate the remainder of two given values with merely addition, subtraction, multiplication, and division? Is there an algorithm or formula if it is even possible?

For instance, say we have two values: 100 and 30. If we divide 100 by 30, we get 3.333.... But is there any way to reach 0.333... (without knowing from the get go how many wholes are included)?

To further illustrate what I mean, and sticking with the example above, one way to find the remainder would be the following formula: 100 / 30 / 10 * 30 = 0.333... BUT this obviously does not work for any two given numbers.

Some more examples (The values in the brackets are the values I am after):

100 / 40 = 2.5 (0.5) 450 / 50 = 9 (0) 11 / 4 = 2.75 (75)

Sorry, if this question is not clear. It is based on a programming challenge I have encountered. I want to use only CSS to calculate the remainder of a text's line height, given a specific vertical offset of the text. For instance, the height of the window might be 100, whereas the line height would be 30. But CSS has no function to easily determine the remainder, and I cannot use loops or recursion (i.e. 100 - 30 - 30 - 30 - 30 < 0), nor conditional (i.e. if N < 0, do...).

My math is awful. But I am wondering whether or not it is mathematically possible?

$\endgroup$
11
  • 1
    $\begingroup$ I'm not sure what you think you mean by remainder. $100\div30$ has a remainder of $10$, not $0.333\ldots$. $\endgroup$ – Andrew Chin Oct 28 '20 at 19:02
  • 1
    $\begingroup$ This article suggests a different route: use :nth-child() and :first-child to perform the modular arithmetic. $\endgroup$ – Eric Towers Oct 28 '20 at 19:05
  • $\begingroup$ I don't know if this meets your requirements, but you could take the quotient of the two numbers, and then find the value of this quotient in modulo $1$. So, for example, if we have $37 \div 3 = 12.333\ldots$, then $12.333\ldots \equiv 0.333\ldots \text{(mod 1)}$. $\endgroup$ – Joe Oct 28 '20 at 19:06
  • $\begingroup$ @AndrewChin Sorry, what i meant was that the remainder of the product (is this the correct terminology)? $\endgroup$ – oldboy Oct 28 '20 at 19:26
  • $\begingroup$ @Joe to my knowledge i do not have access to "modulo 1". tbh, im not exactly sure what that even is? $\endgroup$ – oldboy Oct 28 '20 at 19:27
1
$\begingroup$

I don't know whether it is possible to make the "remainder in integral division" in CSS using "thinking out of the box" somehow, but mathematically it cannot be done using just addition, subtraction, multiplication and division.

Namely, a function $f(x,y)$ made up of addition, subtraction, multiplication and division is a rational function (a quotient of two real polynomials in two variables $x$ and $y$).

Now suppose that $\text{remainder}(x,y)=\frac{P(x,y)}{Q(x,y)}$ where $P$ and $Q$ are polynomials. Fix $y=2$ and then we would have $\text{remainder}(x,2)=\frac{P(x,2)}{Q(x,2)}=\frac{p(x)}{q(x)}$ where $p(x)=P(x,2)$ and $q(x)=Q(x,2)$ - polynomials in one variable. Knowing that:

$$\frac{p(n)}{q(n)}=0$$

for each even $n$, we can conclude that $p=0$ (zero polynomial). However, this is then inconsistent with the other requirement, which is that:

$$\frac{p(n)}{q(n)}=1$$

for each odd $n$.

Note: I can see that CSS spec for $\text{calc}()$ says that, ultimately, when the result of CSS calculation is assigned to an attribute, it may be rounded if that attribute requires an integer. Rounding is similar to truncation ($\text{round}(x)=\text{ceil}(x+0.5)$) and truncation can be used for integral division ($\text{remainder}(x, y)=x-y\times\text{ceil}(x/y)$) so maybe this can be all cobbled together somehow - but I wouldn't know myself how to do that, as I am not a CSS expert...

$\endgroup$
2
  • $\begingroup$ thanks for the definitive answer. i dont understand quite a bit of it, but i understood enough :) $\endgroup$ – oldboy Oct 29 '20 at 6:04
  • $\begingroup$ @oldboy Thanks for convincing me that this is a mathematical question after all. I've retracted my close vote. $\endgroup$ – Stinking Bishop Oct 29 '20 at 9:57
1
$\begingroup$

You are looking for the fractional part of a number.

To make use of this, define a function of two variables that gives one output: $$t=f(x,y)=\frac{x}y-\left\lfloor\frac{x}y\right\rfloor.$$

Using your examples above, we have the following: \begin{align} f(100,30)&=\frac{100}{30}-\left\lfloor\frac{100}{30}\right\rfloor=\frac{10}3-3=\frac13\\ f(100,40)&=\frac{100}{40}-\left\lfloor\frac{100}{40}\right\rfloor=\frac52-2=\frac12\\ f(450,50)&=\frac{450}{50}-\left\lfloor\frac{450}{50}\right\rfloor=9-9=0\\ f(11,4)&=\frac{11}{4}-\left\lfloor\frac{11}{4}\right\rfloor=\frac{11}{4}-2=\frac34 \end{align}

$\endgroup$
4
  • $\begingroup$ what does it mean when the division is wrapped in the "L" brackets? $\endgroup$ – oldboy Oct 28 '20 at 20:35
  • $\begingroup$ is there a way to write the function as a sequence of arithmetic operations (i.e. N / X * Y - Z...)? $\endgroup$ – oldboy Oct 28 '20 at 20:36
  • $\begingroup$ Do you have access to the floor function? $\endgroup$ – Andrew Chin Oct 28 '20 at 20:38
  • $\begingroup$ no floor function in CSS :( CSS is limited to basically addition, subtraction, multiplication, and division, in addition to being able to detect the live dimensions of the window at any given size $\endgroup$ – oldboy Oct 28 '20 at 20:40

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.