r/C_Programming • u/Krotti83 • Sep 18 '26
Question Casting float to integer (UB or not UB)
I'm working on an own libc for the purpose of education and for an own Kernel. Currently implementing the various output formats for as an example printf. Converting normal integers to strings I have already implemented. Now I want add support for floating-point data types.
Normally at least with GCC the following works:
#include <stdio.h>
int main(int argc, char *argv[])
{
float f = 42.83f;
int i;
i = (int) f;
printf("float: %f, integer: %d\n", f, i);
return 0;
}But I'm not sure if the above can lead to undefined behavior with other compilers and architectures, or not.
Can somebody clarify that?
Thanks in advance!
30
Upvotes
44
u/gnolex Sep 18 '26
The standard is fairly clear on float-to-integer conversions. If the truncated floating-point value fits in the target integer type, that truncated value is used. Otherwise the behavior is undefined. So 42.83 cast to an int would give you 42, while 42.83e+20 cast to an int (assuming 32-bit int) results in undefined behavior.
If you want to safely handle float-to-integer conversions, you probably want to add range comparisons in your standard library and handle out-of-range values in a well-defined way. A common way is saturation: values above INT_MAX are clamped to INT_MAX, values below INT_MIN are clamped to INT_MIN.
Do note that kernel code (such as in Linux) tends to completely avoid using floating-point types for various safety reasons.
18
u/der_pudel Sep 18 '26
Sort of. Casting anything that fits in
intis well-defined. Casting NaN, infinities, or extremely large numbers (e.g. 3.402823466E+38) is UB.10
u/sciencekm Sep 18 '26
From the 1st Ed. K&R
When a value of floating type is converted to integral type, the fractional part is discarded; if the resulting value cannot be represented in the integral type, the behavior is undefined. In particular, the result of converting negative floating values to unsigned integral types is not specified.
4
u/ffd9k Sep 18 '26
check
isfinite(f) && f >= (float)INT_MIN && f <= (float)INT_MAXfirst, then(int) fwon't be UB.2
u/Shot-Industry-4914 Sep 18 '26
Looks like I'm the only one missing here something. You're implementing printf() not type conversion (i.e. compiler). Just print the float and the int you're given. Regardless of UB, there will be an integer for the function to print.
2
u/21Ali-ANinja69 Sep 18 '26
Why is this relevant to implementing %f in printf?
1
u/TheChief275 Sep 18 '26
huhhh well operations on floats are defined differently but casting to an int will always just truncate the value. one thing you might need to be careful with is that the value of a float might not fit into an int. considering you're probably on an LP64 or LLP64 architecture, you'll be fine with values from -2,147,483,648 up to 2,147,483,647 (though you may want to round/floor/ceil the float first as well), but technically an int is only guaranteed to have a capability of -32,767 up to 32,767 (it's at least 16 bits with no guaranteed sign representation until C23), so another integer type might be more fit for the situation
1
u/burlingk Sep 18 '26
Explicitly casting float to int has been a normal C thing for a very long time.
You can get away with implicite casting at time, but it's better to do it explicitly so people know it's not an accident.
1
u/Classic-Rate-5104 Sep 18 '26
No, this isn't UB, even in the very first C-version it wat already will defined. Conversion from float or double to int is just truncation. Only in case the value is outside the valid range of int, it's UB
1
u/[deleted] Sep 18 '26
This is something no one needs to worry about, the standard library has all the type conversions you need. I can't think of the exact names of the functions off the top of my head, but it's safer (and less annoying) just to trust those instead of re-inventing the wheel, there's too many edge scenarios, and it's an old problem. It's fun to do it all yourself sometimes, but i found making my own type conversions was fairly complicated if you want to cover every use case.
1
u/aioeu Sep 18 '26 edited Sep 18 '26
What makes you think it wouldn't have defined behaviour? How would you do it in a different way?
Anyway, take a look at C23 §6.3.1.4/1. It is carefully worded to say exactly what is and what is not permitted.