Huh, I hadn’t thought about units as quotients. That’s neat.
My personal thoughts on units: any quantity is a combination of both a number representing the magnitude and a representation of the ‘exponent’ of each base unit, called their dimensions. So for example, 2.5 kg*m^2/s^2 has a magnitude of 2.5 and dimensions of [mass: 1, length: 2, time: -2]. “Raw” numbers just have all their dimensions set to 0.
You can apply a function f to an arbitrary quantity with dimensions only if it’s homogenous: there’s some k such that for all numbers a, f(ax) = a^k*f(x). Otherwise you’re not scale invariant: sin(1 m) vs sin(100 cm) vs sin(1 cm), that sort of thing.
This is why you can multiply unitary quantities but not add them: multiplication is linear, but addition isn’t (since 2x + y != 2(x + y)).
And in this system we can add e.g. 2m + 3s but the result is just 2m + 3s, nothing happens to it. So that is fine really.
I suppose this makes the most sense mathematically, but practically adding two units with different dimensions should be an error. There isn’t really a use for adding two incompatible units like this.
adding two units with different dimensions should be an error. There isn’t really a use for adding two incompatible units like this.
I don’t think it’s an error; you just get a complex number. Consider the cost of getting something shipped to you, which includes a price and a wait time, measured in dollars plus days. While those are distinct properties, they’re not separate, but related — lowering one drives up the other.
Whether or not it should be an error in a programming language is an economics question. Either is theoretically justifiable but I think in practice you’d have simpler, less surprising error messages if 1s+1m was an error but (1s, 0m)+(0s,1m)=(1s,1m).
Huh, I hadn’t thought about units as quotients. That’s neat.
My personal thoughts on units: any quantity is a combination of both a number representing the magnitude and a representation of the ‘exponent’ of each base unit, called their dimensions. So for example, 2.5 kg*m^2/s^2 has a magnitude of 2.5 and dimensions of [mass: 1, length: 2, time: -2]. “Raw” numbers just have all their dimensions set to 0.
You can apply a function f to an arbitrary quantity with dimensions only if it’s homogenous: there’s some k such that for all numbers a, f(ax) = a^k*f(x). Otherwise you’re not scale invariant: sin(1 m) vs sin(100 cm) vs sin(1 cm), that sort of thing.
This is why you can multiply unitary quantities but not add them: multiplication is linear, but addition isn’t (since 2x + y != 2(x + y)).
I believe this is how F# does it. The Rust crate uom also does this with its
Dimension
trait.Interesting concept!
I suppose this makes the most sense mathematically, but practically adding two units with different dimensions should be an error. There isn’t really a use for adding two incompatible units like this.
I don’t think it’s an error; you just get a complex number. Consider the cost of getting something shipped to you, which includes a price and a wait time, measured in dollars plus days. While those are distinct properties, they’re not separate, but related — lowering one drives up the other.
Whether or not it should be an error in a programming language is an economics question. Either is theoretically justifiable but I think in practice you’d have simpler, less surprising error messages if
1s+1m
was an error but(1s, 0m)+(0s,1m)=(1s,1m)
.