ezb.io Go back Open original

# EZB - Functions

Somewhere in my senior year of high school, two friends and I were wondering how one could write min(x,y) in math notation using only binary operations and elementary functions. It took us a little while to come up with something, but eventually we got this:

$$\min(x, y) = \frac{x + y}{2} - \frac{\lvert x - y \rvert}{2}$$

This first part of this function finds the midpoint between $x$ and $y$. Then, we travel half the distance between $x$ and $y$ toward $-\infty$, thus arriving at the smaller of the two numbers. It's pretty clear that max(x,y) can be achieved by travelling toward $+\infty$ instead of toward $-\infty$, like so:

$$\max(x, y) = \frac{x + y}{2} + \frac{\lvert x - y \rvert}{2}$$

Curious to perform this "mathematical translation" on other functions commonly used in programing, we wondered how this could be done for equality, $\text{eq}(x,y)$. Since we wanted to stick to functions on the reals, we opted for true being $1$, and false being $0$. Here's what one of us came up with,

$$\text{eq}(x, y) = 1 - \left\lceil \frac{\lvert x - y \rvert}{\lvert x - y \rvert + 1} \right\rceil$$

Using the celing function might seem like "cheating", but it's important to remember that we were mostly trying to sidestep functions defined in a piecewise/pattern-matching fashion. Thus, this one-line arithmetic definition met our needs.

We kept going. Another useful function is sign(x), also known as signum(x), that returns $-1$ if $x$ is negative, $1$ if $x$ is positive, and $0$ if $x$ is $0$. Here's what we came up with,

$$\text{sign}(x) = \left\lfloor \frac{x}{\lvert x \rvert + 1} \right\rfloor + \left\lceil \frac{x}{\lvert x \rvert + 1} \right\rceil$$

Now, what's the point of doing all of this? Well, for me it was the fact that in programming, arguments to a function (even as numbers) are data, stored sequentially in memory. This mindset allows for easier conceptualization of some operations over others. For example, how would you write some code to reverse a number? Well, if the number was stored in base 10 in memory, and not in binary, you could just copy the data but in reverse order. Now, as a preview of what is to come, how would you write a math function to reverse an integer?

When I was first working on this stuff, the idea of treating math functions in this way was pretty foreign and made me very curious about what was possible. Control flow, time and space complexity, a sequence of instructions, all of that was gone when writing using just arithmetic operations. In math, things just are.

I understand now that this really isn't any ground-breaking math nor does it connect two fields in any novel way, but it was what I thought was math, and that's what made me thoroughly enjoy the journey. I hope you enjoy it too.

Let's talk about reversing an integer next.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9712953,"math_prob":0.9952879,"size":1961,"snap":"2019-51-2020-05","text_gpt3_token_len":432,"char_repetition_ratio":0.10781809,"word_repetition_ratio":0.0,"special_character_ratio":0.21315655,"punctuation_ratio":0.11311054,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9992812,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-17T23:00:25Z\",\"WARC-Record-ID\":\"<urn:uuid:64d56449-2c6a-43f5-8076-be104cf08cb7>\",\"Content-Length\":\"7061\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a99d5b94-63a1-42a2-8855-3c8d8d2cd2fe>\",\"WARC-Concurrent-To\":\"<urn:uuid:d14ad25e-ac1a-48aa-b812-bc938ac05f0c>\",\"WARC-IP-Address\":\"34.227.168.133\",\"WARC-Target-URI\":\"https://www.tefter.io/bookmarks/46266/readable\",\"WARC-Payload-Digest\":\"sha1:V5JASBSLHNHWZFNFLFQHYHJHHHSPNYSI\",\"WARC-Block-Digest\":\"sha1:P7XFG7PI3RVEXHDQI6SDMWPLWPBLNWK5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250591234.15_warc_CC-MAIN-20200117205732-20200117233732-00463.warc.gz\"}"}