r/cpp • u/mateusz_pusz • Apr 20 '26
Preventing Integer Overflow in Physical Computations - mp-units
https://mpusz.github.io/mp-units/HEAD/blog/2026/04/11/preventing-integer-overflow-in-physical-computations/Integers overflow. That is not a controversial statement. What is surprising is how easily overflow can hide behind the abstraction of a units library.
Most developers immediately think of explicit or implicit scaling operations — calling .in(unit) to convert a quantity, constructing a quantity from a different unit, or assigning between quantities with different units. These are indeed places where overflow can occur, and the library cannot prevent it at compile time when the values are only known at runtime. But at least these operations are visible in your code: you wrote the conversion, you asked for the scaling, and you can reason about whether the multiplication or division might overflow your integer type.
The far more insidious problem is what happens when you don't ask for a conversion.
When you write 1 * m + 1 * ft, the library must automatically convert both operands to a common unit before performing the addition. That conversion — which you never explicitly requested — involves multiplication or division by scaling factors. With integer representations, those scaling operations can overflow silently, producing garbage results that propagate through your calculations undetected.
No compile-time programming can prevent this. The values are only known at runtime. But very few libraries provide proper tools to detect it.
This article explains why that limitation is real, how other libraries have tried to work around it, and what mp-units provides to close the gap as tightly as the language allows.
8
u/artisan_templateer Apr 20 '26
Coming from a physics background, I was always taught to use dimensionless quantities where possible, e.g. https://en.wikipedia.org/wiki/Nondimensionalization
If you employ that practice, is this library still useful?
2
u/_Noreturn Apr 20 '26
what's the compile times of this library
0
u/PrestonBannister Apr 20 '26
If with your approach, you have to be a language-lawyer to understand a bit, odds of error are high. Even if you do not err, someone else will. (I have been guilty of this, in the far past.)
Reading your bit, what comes to mind:
When C++ came out, I thought implicit conversion and operator overloading was pretty cool. Though there were folk with all the language experiments in the 1980s (heard of SIGPLAN?) who explicitly disallowed such, I did not get why (then). Since then, I have come to regard both as a huge mistake.
I use thin wrapper classes for typed units, with no implicit conversion to/from native integers, and no operator overloads. Incoming values are range checked before conversion. Named methods for conversion mean you can easily find where in your code range checks may be needed.
Long ago, using 8-bit or 16-bit integers in live code could be more efficient, but not in this century. Using 32-bit or 64-bit integers helps keep you away from overflow in intermediate results, at no loss of efficiency. Seeing code with assignment to an 8 or 16 bit integer variable makes my hair stand on end. This usually means the author does not understand the machine, and the odds of out-of-range operations in the code are high.
Also, have to note, if you can tolerate some loss of efficiency, then floating point types have in-built support for overflow detection. Modern CPUs are quite efficient at floating-point.
Should also note there are older machines where the CPU would catch integer overflow without runtime cost (Burroughs was one), but that design choice for x86 is long past.
That said, while I have used explicit unit-types in code where critical, for me the result have always been more application-specific, and not so much suitable as a general purpose library.
0
u/matthieum Apr 20 '26
After reading about the (wonderful!) work you've done to get units & quantities in the C++ standards, I started playing around with the problem space, and I realized...
... that the above problem is entirely self-inflicted.
This led me to taking a step back, and wonder whether it made sense at all. And honestly, so far, I would argue it doesn't.
I mean, sure you can somehow make
1 * m + 1 * ft"work", for some value of "work", but so far I would argue the costs/benefits analysis is strictly in "not worth it" territory.Furthermore, I would argue that the problem is not limited to integers. Fixed points may also overflow. BigNums may become unwieldy.
In the end, it seems that the user is best equipped, based on their additional knowledge of the dynamic range of the values they use, to judge which units make the most sense, and pick them explicitly:
(1 * m).in(mm) + 1 * mm.No magic. No confusion.