r/cpp • • 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.

16 Upvotes

32 comments sorted by

View all comments

Show parent comment

1

u/mateusz_pusz Apr 20 '26

Thanks for sharing your perspective. I actually agree with your core philosophy—unchecked implicit conversions and "clever" operator overloading have caused a lot of grief in C++ history. However, I think there are a few misconceptions about how modern units libraries (and this article) address those concerns:

  1. Why "Converting at the Edges" isn't enough

You're right that we should check inputs. But the point of this article is that even with "safe" inputs, overflow can happen silently during simple math. If you add 1 * m + 1 * ft, the library must scale these to a common unit. That scaling involves multiplication that the user didn't explicitly write. A "thin wrapper" doesn't help here; it just produces a garbage result. This library is designed to make those "hidden" operations as safe as the "visible" ones.

2. The "Language-Lawyer" Paradox

The library uses complex C++ machinery "under the hood" specifically so the user doesn't have to be a language lawyer. The goal is to let a novice write distance / time and have the compiler handle the dimensional analysis and scaling safety automatically. The complexity is the "cost" paid by the library author to provide a simple, safe interface for the user.

3. The Reality of Embedded Systems

While 8-bit and 16-bit integers might seem like a relic in desktop/server dev, they are the daily reality in the embedded and automotive worlds (where mp-units is often used). In these domains, we can't always "just use a 64-bit int" due to hardware constraints, yet the code is often mission-critical. That's why having a library that catches these overflows is a safety requirement, not just a preference.

4. Floating Point isn't a Silver Bullet

We use floating point for many things, but many of our users require the absolute precision and deterministic behavior of integers. My article is specifically for those who must use integers and want to do so without the silent failures that usually come with them.

If you're interested in how we've moved past the "implicit conversion" mess of the 80s/90s, I’d love for you to look at the implementation. It’s built on the idea that the machine should work for us, not the other way around.