← Retour au blog
tech 30 July 2026

C++ Float-to-Int Conversion: An Undefined Behavior to Watch

Converting a float to an int in C++ can lead to undefined behavior when the value doesn't fit into the target integer. Learn why and how to avoid this trap in your code.

Article inspired by the original source
C++ float-to-int conversion can be undefined behavior ↗ kttnr.net

Introduction

In C++, converting a float to an int seems straightforward. It's a common operation, yet it hides a potential pitfall: undefined behavior. When the truncated float value doesn't fit into the target integer, the result becomes unpredictable. However, many developers overlook this crucial detail.

The Issue: Undefined Behavior

In C++, when a float is converted to an int, the fractional part is discarded. However, if the integer value exceeds the capacity of the int, the behavior becomes undefined. This means the program might yield different results depending on the hardware or compiler used. For instance, on an x86 processor, an unrepresentable value might be converted to INT_MIN.

Concrete Examples

Consider the following code: ``cpp void foo(float f) { int i0 = f; int i1 = int(f); int i2 = static_cast<int>(f); } `` This code generates no warnings even with -Wall and -Wextra options enabled. Yet, each of these conversions can cause undefined behavior for some inputs.

Why It's Problematic

The variability of results across different hardware is just part of the issue. More importantly, any undefined behavior that executes is dangerous. As Ralf Jung explains, what the hardware does doesn't always reflect what your program does.

Practical Solutions

The right approach is to check bounds before conversion. Here's how:

```cpp bool canRepresentAsInt(float f) { return f >= static_cast<float>(std::numeric_limits<int>::min()) && f <= static_cast<float>(std::numeric_limits<int>::max()); }

int safeFloatToInt(float f) { if (!canRepresentAsInt(f)) { throw std::out_of_range("Float cannot be represented as int"); } return static_cast<int>(f); } ```

Using Detection Tools

Utilize detection tools like Undefined Behavior Sanitizer with -fsanitize=float-cast-overflow to identify these issues in your code.

Conclusion

Ensuring that float-to-int conversions are safe is crucial to avoid hard-to-track bugs. By checking bounds and using detection tools, you can safeguard your code against these undefined behaviors. Let's discuss your project in 15 minutes.

C++ float-to-int conversion undefined behavior programming errors code safety
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call