I’ve been thinking about units a little bit. Seldom do we come across unitless quantities; they only seem to come up when you’re talking about a ratio of two things. We can always add two quantities of identical units… except in one place.
The following program does not compile:
int main() {
int foo, bar;
int* p = &foo + &bar;
}
If we attach standard physical-style units to these things, there is no reason this should be an invalid operation. We’re adding two int*s, or more generally, two of whatever you get when you take the address of an int together. And yet, it dies. And rightfully so: the operation is meaningless. So what kind of unit is int*? What kind of unit doesn’t allow you to add another unit of the same type?
I’m calling it a logarithmic unit. It makes sense if you define the program in pseudo-C:
int main() {
int foo, bar;
exp(int*) p = exp(&foo) * exp(&bar);
}
Now the error is obvious. We’re assigning a quantity with dimensions exp(int*)2 to a variable with dimensions exp(int*). But we had to multiply instead of add.
So let’s fix it and then take the natural logarithm of the whole expression:
int main() {
int foo, bar;
2 int* + p = &foo + &bar;
}
And now it’s clear what kind of unit a pointer is: it’s an int*+. A logarithmic unit. And look, it works perfectly with adding pointer differences, a perfectly well-defined operation in C:
int main() {
int foo, bar, baz;
int* + p = &foo + (&baz - &bar);
}
With units, the expression is:
int*+ p = (int*+ &foo) + ((int*+ &baz) - (int*+ &bar));
If we consider int* just like any other symbol, the units work out perfectly. And what, then, is the dimensionality of ptrdiff_t? Why, it’s unitless, of course, just like a ratio.
Another case of logarithmic units that we just ignore: the Farenheit and Celcius temperature scales. One that we do pay attention to is the bel, but it is dimensionless. When you add dBs, you’re really multiplying something.
