Quantcast
Channel: Tools
Viewing all articles
Browse latest Browse all 91752

Forum Post: RE: Suggested new compiler warning: explicite cast from real to unsigned int.

$
0
0

The C standard says that casting a negative float value to an unsigned integer is undefined behavior.  Consider the case that f contains an integer value equal to UINT32_MAX.  In that case, you would not want the expression ((uint32_t)f) to narrow the value down to int32_t first, because you won't preserve the correct value.  The C standard leaves it as the programmer's responsibility to make sure any cast of a float to an integer uses an integer type that can correctly represent the integer part of the float value. 

If we were to introduce a warning every time a float value were cast to an unsigned integer value, there could potentially be a lot of expressions that were written correctly and work just fine to suddenly start getting a warning.


Viewing all articles
Browse latest Browse all 91752

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>