Skip to content
Tech News
← Back to articles

Floor and Ceil versus Denormals on CPU and GPU

read original more articles
Why This Matters

This article highlights the importance of understanding floating-point behavior, especially for functions like floor, ceil, trunc, and round, which are fundamental in graphics programming and numerical computations. Recognizing how these functions behave with denormal numbers and negative values is crucial for developers to ensure precision and correctness in their applications, particularly in high-performance computing on CPUs and GPUs.

Key Takeaways

Recently, I dove deep into floating-point numbers and their behavior. Somehow, this topic haunts me in my programming practice since I created Floating-Point Formats Cheatsheet back in 2013 and also released a comprehensive article The Secrets of Floating-Point Numbers in 2024.

This time, I would like to focus on one specific question:

What is the result of floor(-1.175493930432748e-38) ?

Note: Hexadecimal value of our input number is 0x807FFFFD .

Floor, ceil, trunc, round

To recap, floor, ceil, trunc, round are functions available in the standard library of C, C++, as well as shading languages: HLSL and GLSL. Each of them transforms a floating-point number into an integral floating-point value, but using different rounding rules.

Note this is not about a conversion from float to int . The result of these functions is still a float , just having only integral part. When the input is already integral, the value is returned as-is. Otherwise, it gets "snapped" to the nearest integer in a specific direction:

floor - rounding "down" i.e., towards -infinity.

- rounding "down" i.e., towards -infinity. ceil - rounding "up" i.e., towards +infinity.

- rounding "up" i.e., towards +infinity. trunc - rounding towards zero, which we can also explain as truncating the fractional part.

... continue reading