5 ms·
Units are to calculations as Types are to code. They help prevent a large class of common errors. If your types aren't consistent, you know your code is wrong.
by feoren 5y ago
Units are to calculations as Types are to code. They help prevent a large class of common errors. If your types aren't consistent, you know your code is wrong. If your units aren't consistent, you know your calculation is wrong.
Just like there can be stronger and weaker type systems, there can be stronger and weaker unit systems. Stronger unit systems catch a larger class of errors. I commonly catch errors nobody else sees because I use a stronger unit system than they do, which is amended with generics ("kg of what?"). Just like you said: a unitless quantity is weak; ppm doesn't help you catch errors very well. It could be mg/kg or µL/L, and it matters when you start canceling ratios. But you can take it further: mg<constituent>/kg<soil>. When you start throwing in soil density and molecular mass, it's very easy to come up with examples that "pass" the standard dimensional analysis but "fail" your advanced unit checker. You can look like a wizard by detecting errors in equations completely outside of your field of expertise.
Once you realize that there can be as many unit-systems as type-systems and the only criterion for a good vs. bad unit system is how well it catches errors, the purists who try to enforce their physics textbook's view of units will look really silly to you. The only problem is that you start arguing with them about whether specific gravity is really unitless or not (it's more helpful to say it's not), and they immediately decide you must be a moron because that's not what their textbook says. Oh well. You're getting the right answer, and they're not.
- lordnacho 5y agoThis is the most insightful comment I've read today. Ties up a number of thoughts I've had while explaining this to my kid. You're right that there's a choice about how strongly typed your unit system is. Another useful tidbit is if your formula has a log or an exp, the arg needs to be dimensionless.
- feoren 5y ago> Another useful tidbit is if your formula has a log or an exp, the arg needs to be dimensionless. I have cognitive dissonance about that. I understand that if you look at the Taylor series of Exp(x), you're clearly adding terms containing 1, x, x^2, x^3, etc., and that it doesn't make sense to add a meter to a square meter to a cubic meter and so forth. So therefore Exp(1m) doesn't/shouldn't make sense. But on the other hand, I can come up with examples that seem to make sense, but seem "dimensionful". Is a bit dimensionless? Because it certainly makes sense to consider 2^(32 bits) and 2^(4 bytes). Yet bits are deeply related to entropy and might even be considered a unit of energy. Are we sure they're dimensionless? What is a "square bit" anyway? I can also define "specific meters" to be the length of an object divided by the length of a 1-meter-long stick. This value is now dimensionless, so it should be OK to exponentiate it, correct? Am I cheating? How? Or I should be allowed to take Log(7 mg/kg) because mg/kg is dimensionless. But this should equal Log(7 mg) - Log(1 kg), which I'm not allowed to do. What gives? Again I come back to: is this actually helping me catch errors in my calculation? I'm honestly not sure here.
- fuzzybear3965 5y agoYeah, so the problem is that the units of a function applied to a value is not necessarily the same as the value. Consider the area of a square. It has one argument whose unit is of length type. But, the function returns something of area type - totally fine. If you want to talk about e^(x) then you're totally free to do that but, you're right: if x is not dimensionless then the expression has no physical meaning (as little meaning as one meter added to one second, as an example of another clearly nonsensical physical function). In Physics, your starting point always makes sense dimensionally (F=m*a, E=m*g*h) and the game is to combine expressions that make dimensional (and physical) sense to glean insight into how different physical constraints of the system are related. If you have any Physics problem where you end up with e^(x) or sin(x) and x is not dimensionless then it's a dead giveaway that you performed an algebraic misstep at some point when playing the game. So, in short, you can propose a lot of functions that make no sense from a physical standpoint, but you won't arrive at those if you're working from first principles which are, by nature, dimensionally consistent.
- danuker 5y agoI would treat a Taylor series as "numeric trickery" that happens to produce useful approximations, in which case I wouldn't care about units. As for Log of a unit, if it is useful in some strange circumstance, would require a different unit to keep track of (like logkg), similarly to i (the imaginary unit) being the result of sqrt(-1).
- lordnacho 5y agoI think it's more errors in derivations that you're after. Like the sibling comment mentions, in physics you get a bunch of different units but they have to be consistent, eg despite there being different expressions that describe energy, they end up being the same under the hood. I'm not sure I can think of a case where you can do otherwise. Entropy is J/K and seems to follow the same dimensionality constraints as everything else, though I'm not well versed in information theory so I don't know how they think about it. The log example only works because log only takes a dimensionless parameter. The only way to get that is to cancel the two kinds of meters and shove the dimensionless 0.007 into it. Ie it is a type error to take the log of 7mg or anything with a unit. One interesting thing I came across is in the cpp book by Stroustrup he actually builds a custom type system capable of preventing issues like this and uses physics examples.
- palimpsests 5y agoAre you suggesting that you've developed unit systems that are stronger and superior to what has been established in the field of physics? Can you please share some examples other than adding specifiers about material e.g. 3 mg of plutonium, 5 L of argon, etc? I can think of at least one other criterion for checking unit systems - ease of computation. See, for example, MKS vs Gaussian units.
- feoren 5y agoI'm not suggesting that I have a better unit system for all of physics than what is currently used in physics. I am suggesting that there are lots of different unit systems and the "best" one depends on what problem you're trying to solve (the same as with programming languages). I'm suggesting that concepts from software engineering could benefit many other disciplines, because in some ways software engineering is simply the study of how to solve problems reliably and repeatably. I'm suggesting that we should feel free to tinker and experiment with unit systems as we do with programming languages. I don't have concrete examples other than adding in "of X material" when appropriate (e.g. mass and volume, but not energy), but don't underestimate how useful that really is. One calculation I encounter a lot that is wrong a troublingly large amount of the time, despite passing a traditional dimensional analysis, is this one: Calculate the mass of SO2 produced from burning 1,000 moles of gas with a molecular weight of 24 g/mol containing 2% H2S by weight (and no other sources of sulfur). It's not hugely complicated: there are really only two units to keep track of here (g and mol) but they can be "of" three different materials (whole gas, SO2, and H2S), so it's very easy to cancel terms when you really shouldn't if you analyze it traditionally. With the materials attached to each unit, you're also forced to include a term that is otherwise invisible (the stoichiometric ratio of SO2 to H2S, which is usually omitted because it's 1), which helps guide your intuition on how the calculation relates to the chemistry of what's happening and helps you come up with the correct calculation when more complicated chemical reactions are involved.
- slver 5y agoAnd ironically we have no types for units in most languages. Instead we need to wrap numbers in objects making them extremely cumbersome to work with.
- ctdonath 5y agoStrong units also reveal surprising relationships, such as matter is energy (E=mc^2), or energy consumption is fuel area (an XKCD I can’t find).