Convert piecewise function with ranges to a single mathematical formula

70 Views Asked by At

For a programming task (an SQL expression) I need a function made of mathematical operators ($+$, $-$, $*$, $floor$, $ceil$, and comparison operators returning value $0$ (false) or $1$ (true)...) that maps a real value into 4 discrete values depending on 3 boundary values $a$, $b$, $c$ where $a < b < c$ and $x > 0$:

$$f(x, a, b, c) = \begin{cases} 0, & \text{if $x \lt a$}\\ 1, & \text{if $a \le x \lt b$}\\ 2, & \text{if $b \le x \lt c$}\\ 3, & \text{if $x \ge c$} \end{cases} $$

In SQL a CASE operator exists but but I want to avoid if because one constraint I have is the $x$ is in itself a complex expression, so the function should have $x$ mentioned just once.

As an example of the thing I need but with the simpler case of a single boundary value, for $$ f(x, a) = \begin{cases} 0, & \text{if $x \lt a$}\\ 1, & \text{if $x \ge a$} \end{cases} $$ the formula is: $f(x, a) = x \lt a$