Casting Values

Oct 25, 2018

I have a scary Halloween story to tell you...

I was running into a bug in my new compiler where large ints where loosing precision. After tracing through my compiler's C++ code for about an hour trying to catch the spot, I came to this function:

int intConstValue() { return _type == kFloat ? _float : _int; }

It took me a bit of starring and then it hit. Something from the deep recesses of C-lore came jumping up.

Do you know the issue?

I won't keep everyone in suspense...

The both sides of trinary operator must return the same type, and in this case I was returning an INT and a FLOAT, so it cast the INT to a FLOAT before returning the value. I didn't notice it until one of my units tests sent large INT's through, larger than a FLOAT can store without loosing precision. Since the function (intConstValue) is returning an INT, I guess I assumed the it would downcast the float, but you know what they say about assuming.

I seem to recall being bitten by a similar bug 20 years ago. I guess I'm good until 2038... just in time for Unix time roll over.