The way you think, Is the only parameter that defines you. Through the time, Mathematics has become the most excellent way to measure and exercise the mind and thinking.
Something that is fascinating about the math, Is it is a set of functions all made of some very obvious ordinary facts. Take the four basic arithmetic operators, Euclid’s Postulates, or the concept of function and sequence limits. They are all some very simple ideas combined together in a right way.
This combination defines a world where everything is made of some simple principles like the way our world is made of some simple flexural strings.
This world is now something very vast and beautiful and thanks to the existence of “Functions” we can simply create whatever we want using the existing formulas and equations.
But when we use these functions. Some of them are like “black boxes”. I’m referring to the idea of black boxes because and only because this article is targeting a “computer science” approach where we are used to be aware of only the input and output of the functions we use.
And that is one giant sadness I’m afraid. That is why in this article we are going to read about the ugliness of the black-box ecosystem.
CMP as short for compare, Is a logical-gate in the CPU’s ALU (Arithmetic Logic Unit). As I’m a fan of creating simple funny math stuff. I have modeled this gate as a function:
This function takes two arguments
b and then compares them to each other. If the given arguments are equal the function’s output will be 0, Else, If the first argument
a was greater it will output 1 and if none of the conditions was true which means argument 2:
b was greater it will result in 2.
Now implementation of this function is absolutely simple and you find it a math hack (Which I hope you do!)
First thing is if the arguments are equal. Well, take it this way. If the two of them be exactly equal, the result will be zero. We have this function in math
sgn which gives us the sign of the number, whether it’s positive, negative or
zero and its result is pretty cool, For positive we get
1, for negative
-1 and for zero we get
As we just wanna determine if it is zero or not, We can make it result only two outputs
0 for if
b are equal and
1 if not. And all we have to do it so put the whole thing inside an absolute:
Now you shall see why using
0 makes sense very soon. What have the need now is to see if they are not equal, Which one is greater?
Well, in order to do so we first need to study the simple divisions. If the numerator is greater than the denominator we can be sure that the number is greater than 1. On the other hand, if the denominator was greater we can state that the result of the division is something between 0 to 1.
What if we put this division on a floor?
You see that once again we have reached where we have
zero and non-zero answers. If numerator is greater, Then the result of the division will be
1 or greater like the way it used to be without the floor. But we see the effect of the floor when we apply it the other case. If the denominator turns out be greater. The result will be zero to
1, So when we apply the floor, It shall be only
And yes just like the previous time we have something with two possible outputs only 0 and non zero. So how if we implement this:
You see, Now if
a be greater it will result
1 otherwise it will result
2, Which if we add both together:
You see that the
1 result of the first part comes to play. Assume that we name the first part
IsEqual and the second one
TheGreaterFinder. You see that when the numbers are equal the
0 which then we times
0 to the whole
TheGreaterFinder and we block its output.
And in the other way, When the numbers are not equal it will activate the
TheGreaterFinder and will not interface with the result because
1 is the neutral component of multiplication.
So that is it. This formula provides you with true and precise result for comparing two numbers.
But why is it stupid?
Everything starts here. You may say: “Well this function works and does have some very awesome hacks and implementations. It’s very tricky so it’s very pretty!”
And yes you are right in some ways. The function is tricky and does have a lot of hacks but it is foolish, You know why? Have we used two
sgn functions in our formula? Have you ever looked at sgn’s definition? Okay now have a look:
See? It’s already a set of
Ifs. And what we have done is also a set of ifs! All this time we could have implemented the function as:
We did all this because we used
sgn blindly as a black-box. All these complexities, All these beautiful hacks. All the tricks we used. All are wasted for something that we could have implemented in less than micro-seconds.
That is why knowing the algorithms, Reading the code of the functions you use and “OpenSourceness” of the code we write does matter. It makes us not to do foolish mistakes like this.
I hope you have learned your lesson!