[cfp-interest 3946] Weird Casting Error

Damian McGuckin damianm at esi.com.au
Tue Jun 9 17:14:06 PDT 2026


Because of my fat fingers and a jumbled brain, my casting of a signed 
floating point number to an unsigned integer showed a logic problem. E.g.

 	const uint32_t k = (uint32_t) x;

produces rubbish if x is negative. I really meant to type

 	const int32_t k = (int32_t) x;

Silly me.

Mind you, if x can be guaranteed positive, it is perfectly legitimate. But 
if not, it is cause of a subtle bug.

Why does C allow a cast from a signed floating point datum to an unsigned 
integer datum when that primitive does not exist in IEEE 754. Is it purely 
historical. Just curious.

Thanks - Damian


More information about the cfp-interest mailing list