mp-units/latest/blog/2026/06/29/why-a-quantity-has-a-character/ #805
Replies: 6 comments 9 replies
-
|
Sorry I couldn't finish getting my feedback in before you posted! I'll leave it as comments now. I'm assuming each comment gets its own thread, so I'll post them separately. |
Beta Was this translation helpful? Give feedback.
-
|
The force/mass example in the beginning came across as alienating, because you're stating as absolute fact something that actually depends on the system in use. In particular, the "pound" is a unit of force in common US engineering practice. It's why PSI is a measure of pressure, and foot-pounds is a measure of torque (just like newton-meters). I'm not sure it helps your case to lead with an example that will distract a significant portion of your audience. (Although as for myself, I did enjoy the slight rabbit hole of research it motivated for me!) |
Beta Was this translation helpful? Give feedback.
-
|
Now, for scope. I see the value in limiting scope and excluding bivectors, pseudoscalars, and the rest, but I think there's a real downside too: it creates barriers for geometric algebra users to adopt the library. "No ISQ quantity needs the distinction": sure, because the ISQ throws away information about grade in projecting it down to order. Geometric algebra users, whose existing programs are written to restore that lost information, will have a hard time adopting mp-units. To see the problem, note that the grade-to-order mapping strongly depends on the spatial dimension. Consider bivector quantities such as angular velocity, angular momentum, and torque: what "order" are they?
In fact, the "tensor" formulation is the most general, because it works in all spatial dimensions. It's also a very practical formulation, because the antisymmetric matrix representation of angular velocity is a bread-and-butter choice in robotics and rigid body dynamics. But the ISO has locked in the choice of "vector" character, which is only correct in 3D! It's true that the rank-ordered behavior that the blog post describes for the order (i.e., a lower order fills a higher) means that the 2D case would work too, because the scalar representation is automatically valid for vector character. However, that same policy has a worse consequence: it means the tensor representation --- the most generally correct one, and the standard choice in rigid body dynamics --- is the only one we can't use. This is a direct consequence of two decisions: baking "character" into the library, and the (again, reasonable) choice to limit scope to the ISO's named characters. I think limiting scope is a good thing, generally: but when combined with the character feature, it actually prevents some users from migrating to the library. This scope limitation without character wouldn't have that effect. Granted, users can always define their own quantities for angular momentum, etc., and give them tensor character. But they'd have to do that for every bivector quantity they touch, and each one drops out of the built-in ISQ hierarchy, losing interoperability with anyone using the standard quantity specs. It also forces them to redefine derived-quantity relationships that are built in to the library using those specs, such as |
Beta Was this translation helpful? Give feedback.
-
|
I do have a correctness concern about the character traits. Your Thinking more about it: for The order axis is a harder case. For libraries that model a vector as an Nx1 matrix, like Eigen, a vector and a matrix look the same, structurally --- same index operators, same scalar type return value. The only difference is their compile-time extents. So if we stick to the current purely structural probe (based on indexing), it seems to me that we can't detect the order without ODR violation risk. That means we'd need to detect extents, which really just relocates the ODR risk. I think that risk is likely manageable, but I did want to call it out. Oh, and I also wonder whether Eigen's convention that "an Nx1 or 1xN matrix is a vector" is universal, or whether other vector/matrix libraries might be more rigorous in separating the two categories. Overall, I won't say order detection is impossible, but it seems very hard. All I'll do here is clearly state the problem, and leave the solution to your creativity. But in any case, yeah: it seems pretty important to eliminate opportunities for ODR violation, or at least minimize them in the most common practical cases. |
Beta Was this translation helpful? Give feedback.
-
|
Reading about your sequence of "traps" was really interesting and enlightening! I'm sure I won't be the only reader who really enjoyed getting a sneak peek into the evolution of this feature. |
Beta Was this translation helpful? Give feedback.
-
|
Overall, I've changed my thinking on this. That's not to say I now think character is required. I don't, and I don't have any motivation to ever add it to Au (unlike some type of "quantity kind" support, which I would like to add to Au someday). Rather, I now understand that there are different basins in design space, and we've pursued separate ones. Au fundamentally lives in the dimensional analysis only basin (again, with possible future extensions for basic "quantity kind" concepts). You've shown all kinds of mistakes that this basin can't catch. That's the downside. The upside is simplicity and generality. Dimensional analysis always applies. Moreover, it gives the most bang for the buck: we've already seen (CppCon 2023 Au talk) that in terms of actual mistakes people make across all domains, especially when weighted by cost, it is overwhelmingly "right dimension, wrong units". (Even though "wrong dimension" would be "worse", it's hard to find practical examples of it). Again, that's the generic story, aggregated across all domains. Individual domains may be different. The power engineering example is a case in point. Admittedly, I do have some (very preliminary) ideas for how we would handle this with dimensional analysis only, but let's grant for argument's sake that this will turn out to be infeasible. That's where a full quantities library --- characters and all --- will really shine. It seems to me that mp-units does an excellent job of meeting the power engineering domain's needs today, whereas dimension-only libraries like Au leave out really consequential use cases. That's the benefit. The cost of this is all of the extra domain knowledge that we need to build in to the library. It's building all the extra machinery for how characters and quantities interact to produce other kinds of quantities. For example: although the blog post didn't explicitly say this, quantity kinds and characters mean you need to build explicit physics equations into the library, so that we can know the specific quantity type of the result. (Yes, Au wants some "quantity kind" implementation, but I'm hoping I can come up with a dumber one that gives the most bang for the buck; time will tell.) The "full quantity" approach does add real value, and we do users a service by including the most common cases, but the effect on the library's implementation scope is tremendous. And, as we've seen with the bivector case, it also raises strong barriers to real and valid existing use cases. Whereas, dimensions and magnitudes are generic, they are always applicable, and they catch the most important errors. So that's why I end up appreciating all of the work you're doing here, while not at all being tempted to follow in your footsteps. I see C++ units libraries as an ecosystem, each one acting as a laboratory for trying out ideas in practice. All the work you've done, deep-diving into the minutiae of ISO and ISQ quantity concepts, inventing creative and ergonomic new C++ interfaces that take advantage of all the latest modern standards, is a truly mind-boggling amount of work. (I know, because I've been privileged to have a front row seat over the past several years!) All of this work is what it takes for someone to explore the "full quantity definition" design space basin. Somebody had to do it, and I can tell you right now that it was never going to be me! 😅 From the carefully designed and implemented character ideas in this blog post, to the upcoming "absolute quantity" interfaces, to so much more, I can't wait to see how it all plays out in practice. I hope my comments here can help make your offering in this design basin, and the ecosystem overall, as strong as possible (especially for really scary error cases like the ODR violation). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
mp-units/latest/blog/2026/06/29/why-a-quantity-has-a-character/
The quantities and units library for C++
https://mpusz.github.io/mp-units/latest/blog/2026/06/29/why-a-quantity-has-a-character/
Beta Was this translation helpful? Give feedback.
All reactions