1

I am helping a friend of mine make of game of his design as a mobile app. Doing this process I have found pure mathematical functions to be helpful rather than just use if statements in all scenarios. I haven't studied math beyond high school level and thus I was hoping someone would be able to help me as I haven't been able to find the type of function that I require on the internet.

The function I am looking for takes two inputs, x and y. I would like this function to return the sum of x and y, however, if x is 0 I need that sum to be zero. x is never going to be negative but y can be negative.

I would appreciate it a lot if someone could help me with my problem. I apologies if my question is confusing or unclear and I would be happy to help you understand if the above text does not clearly formulate my problem.

Doro-HD
  • 31
  • 4
    Does $(x+y)×(x\gt0)$ work? Often, 'True' takes the value 1 and 'False' is zero. – Empy2 Jun 22 '21 at 11:44
  • I agree that if statements can be appropriate and sometimes there are no easy way around it even if I dont want to use one. But I am trying to find use for algebraic functions in the programs I make when I see a possibility for it. Like in a case where I needed a functions that returned 0 if x was negative and x if x was positive. At first I tried using an if statement but I eventually discovered a function that went like this f(x)=(|x|+x)/2 – Doro-HD Jun 23 '21 at 05:34

2 Answers2

2

It depends on what you mean by "pure mathematical functions".

You could use an approximation of the Heaviside step function but it doesn't really solves the problem because this function requires the use of an "if statement" to compute it's values.

I think that what you mean by "pure mathematical functions" is "algebraic functions", meaning you only allow yourself to use sums, products, substractions, divisions, power functions and eventually square roots.

Consider a very small value $\epsilon$ (The closest positive value to $0$ that the machine can encode in this case).

Now suppose we found the algrebraic function you're looking for and call it $f(x,y)$

Computing $f(\epsilon,y)$ should give you $y+\epsilon \approx y$ but computing $f(0,y)$ should give you $0$. Which means that if you were to plot the function you'd see a gap along the y axis.

It's a major issu because algebraic functions do not produce this kind of gap.

However, if you allow some approximation to "force continuity" of the function you can make use of the $\epsilon$ value we used earlier and define $f$ this way : $$f(x,y)= \frac{x}{x+\epsilon}.(x+y)$$

When $x=0$ $f$ will return $0$, when $x\neq0$ and $x+y>0$ it will return a fairly good approximation of $x+y$ (the farthest $x$ is from $0$ the better is gets). The overall quality of the approximation increases as $\epsilon$ gets smaller.

Hope it helps, tried not to dive too much into mathematical details.

  • Thank you for the responce. "I think that what you mean by "pure mathematical functions" is "algebraic functions", meaning you only allow yourself to use sums, products, substractions, divisions, power functions and eventually square roots." That is exactly what I meant :) Your answer was easy enough for me to understand and I will definitely try out the function you purpose – Doro-HD Jun 23 '21 at 05:28
1

You want to define a function

$$g(x,y)=f(x)(x+y), \quad\text{where}\quad f(x)=\begin{cases} 0, \space\text{if $x=0$}\\1, \space \text{otherwise}\end{cases}$$

and since we are considering when $x\geq 0$, we don't need to know how the function $f(x)$ behaves for $x<0$.


Here are some useful functions:

  • The Kronecker delta function. It is a function of two variables which is $1$ if the variables are equal and $0$ otherwise.
  • The sign function, sgn(x), which extracts the sign of a real number.
  • The ceiling function, denoted by $\lceil x\rceil$, which maps $x$ to the smallest integer greater than or equal to $x$.

Then some possibilities for $f(x)$ are:

$\text{sgn}(x),\space\text{sgn}(x)^2, \space 1-\delta_{x,0},\space \big\lceil\frac{|x|}{|x|+1}\big\rceil, \space \big\lceil\frac{|x|}{|x-1|+1}\big\rceil,\space \big\lceil\frac{x^2}{x^2+1}\big\rceil, \space$ where $|x|$ is the absolute value of $x$.

Or if we define the Heaviside step function as $$H(x)=\begin{cases} 0, \space\text{if $x\leq 0$} \\ 1, \space \text{if $x>0$}\end{cases},$$

then we can have $f(x)=H(x)$, but more often the function $H(x)$ is defined to be $\frac{1}{2}$ at $x=0$.

Alessio K
  • 10,599