Express "if true, then 1 else 0" in a formula suitable for Desmos calculator

24k Views Asked by At

In programming, often the value of True is also 1, and False is 0.

This means that:

(x>5)*4

will return 4 if x is greater than 5 (because (x>5)==1), else 0.

I need to accomplish a similar thing using mathematical operators (no piecewise functions, this has to be typed into Desmos calculator.)

Specifically, I need $$ f(x)=\begin{cases} 1&&\text{if}~ x\leq n \\ 0&&\text{otherwise} \end{cases} $$ without having to use piecewise notation. Here $n$ is a positive integer, as is $x$.

3

There are 3 best solutions below

2
On BEST ANSWER

(Posted by Dave Radcliffe but then deleted):

The Desmos calculator supports piecewise functions such as {x<5 : 0, 1} or {x<=5 : 0, 1}

0
On

$$ x\mapsto \frac{|2n+1-2x| + (2n+1-2x)}{2|2n+1-2x|} = \begin{cases} 1 & \text{if } x\le n, \\ 0 & \text{if } x>n. \end{cases} $$

0
On

To simplify Hardy's answer, a function I commonly use is $$\frac{1}{2} + \frac{n-x}{2|n-x|}$$ If you want to make calculations less taxing by working only with integers during calculations, this is obviously the same as
$$\frac{1+\frac{n-x}{|n-x|}}{2}$$
This solution immediately arises from the fact that $\frac{n-x}{|n-x|}$ is either $1$ when $x<n$ and $-1$ when $x>n$
This is obviously undefined for equality of the two variables, although you can add a factor to the top and bottom to change the undefined point, such as $$\frac{1}{2} + \frac{n-x+1}{2|n-x+1|}$$ which is undefined not at equality, but when $x=n+1$
This is not needed however, as Desmos has native support for piecewise functions... see this link for an explanation by the Desmos devs