- Colorful Computing: Exploring XR, Real-Time, and More
- Posts
- "See How A Programmer Pisses Off Mathematicians with a Simple Trick!"
"See How A Programmer Pisses Off Mathematicians with a Simple Trick!"
(And let me try to piss you off, too š¤)
Writing down x = x + 1 would enrage a mathematician because it violates the basic principles of algebra. It is a contradiction, as it implies that a number is equal to itself plus 1, which is impossible.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/ef5bad01-5171-4379-9e8c-371824319c41/IMG_7847.jpg?t=1703782011)
In algebra, the equals sign denotes that the expressions on either side are identical in value. For the equation, it claims that x is the same as x increased by one unit, which is a contradiction.
No number is equal to itself plus another nonzero amount.
It violates the fundamental property of equality in maths.
In programming howeverā¦
ā¦the expression āx = x + 1ā is a common operation. It is not interpreted as an algebraic equation but as an assignment statement. It means ātake the current value of x, add 1 to it, and then update x with this new value.ā Itās a way to increment the value of x by one. Just in short.
This is a fundamental concept in programming for iterating or counting, and programmers are very familiar with it.
In a longer version, with a temporary variable this would look like this:
x = 10 # Original value of x
temp = x + 1 # Increment x by 1 using temp variable
x = temp # Assign the value of temp back to x
Thatās why we have == and = in programming to differentiate between comparing and assigning.
= is the assignment operator. Itās used to assign a value to a variable. For example, x = 1 sets the value of x to 1.
== however is the equality comparison operator. Itās used to compare two values to see if they are the same. For example, x == 1 checks whether the value of x is equal to 1 and results in either true or false.
Now you might ask: Is there an option to piss off both, mathematicians AND programmers?
Of course there is š:
x + 1 = x
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/aae72562-8d1d-416f-a640-5d4c8f2e7fea/IMG_7849.jpg?t=1703782011)
x+1=x would be incorrect from both, a mathematical AND programming standpoint.
In math, x+1=x still is a contradictory statement, as the left side still isnāt (and never will be!) equal to the right side.
And in programming x+1=x tries to assign the value of x to x + 1, which is not meaningful in programming at all.
WHY? you askā¦
Now that you came so far, let me try to piss you off too =-D
BECAUSE! Pointersā¦
To be absolutely correct let me introduce you to every beginning programmers greatest cause of anxiety: Pointers
In C and C++ x+1 on the left side of the = is not only an invalid value; its is an invalid L-value, or Left value.
In simple terms, an lvalue is something that can appear on the left side of an assignment (=) operator, because it represents an address in memory that can be filled with a value.
An rvalue - to name the opposite - is an expression that represents a data value that is stored at this address in memory. They are temporary values and cannot have values assigned to them. Rvalues appear on the right side of an assignment statement. For instance, in the expression x=5 the number 5 is an rvalue. In x=5+1 the expression 5+1 is also an rvalue.
Again, WHYYYYYY!?
In c++ we donāt shuffle the whole variable around in code, we only point to the address where the value is stored.
Like in this example:
#include <stdio.h>
int main() {
int x = 42; // Declare an integer x and assign it 42
int* ptr = &x; // Declare a pointer with address of x
printf("Value: %d\n", x); //42
printf("Address in memory: %p\n", (void*)ptr); //0x7ffeebf5b8ac
return 0;
}
And for some VERY good reasons!
Pointers are used to pass large structures or arrays to functions without copying the entire data structure. This is much more efficient in terms of memory and performance.
And most important for me, who is doing lots of hardware programming, pointers are needed to communicate with most sensors and hardware in embedded systems. This is almost always done by grabbing or setting values directly from or to addresses in memory.
Now, I hope you forgive me for the spike in adrenaline when mentioning pointers.
So long
Basti