Incorrect behavior of ?: operator

Found a bug? Post information about it here so we can fix it!

Moderator: Moderators

Post Reply
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Incorrect behavior of ?: operator

Post by Rado1 »

I usually use the ?: operator for assignments without problems, but this particular situation surprised me.

Why the ?: expression below returns strange numbers, eg., 1079134336 for Level==2 and NumberOfBonuses > 0? Level and NumberOfBonuses are integer variables, SpawnBonusTimer is a Timer. I tried also "if" version, which works as expected; returns, e.g., 4 for level 2. See the code snippets below.

Code: Select all

// incorrect results
SpawnBonusTimer.Interval = (NumberOfBonuses < 1) ? 0 : random(1, 0.3) * (8.0 / Level);

Code: Select all

// correct results
if(NumberOfBonuses < 1) SpawnBonusTimer.Interval = 0;
else SpawnBonusTimer.Interval = random(1, 0.3) * (8.0 / Level);
Either I overlooked something or there's a bug in the ?: operator.
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

It's both a bug and a known problem with parsing the "?" operator in C-style languages. The bug in this case is that it incorrectly interprets the float as an int and I'll change the compiler so that this won't happen.

The compiler has a problem to determine the type of the "?" expression because the "true" part is an int and the "false" part is a float. If you force both parts to float it should work "? 0f : "... (notice the "f").
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

Thanks Ville. I was not aware of this issue before. Every day we learn something new...
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

:?

Please don't fix this! It's the only way / hack to cast floats to ints ( and vice versa ).

K
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

What about floor() or ceil() functions? It's not real casting, but can be used instead.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

Hi Rado,

That's converting, I'm talking about the re-interpretation of a bit-pattern.

http://emix8.org/forum/viewtopic.php?t=729

K
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

I understand now. So, I do think ?: should be fixed; but can be documented better in help.
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

I had forgotten about that Kjell. Ok I won't fix it for now, but we should have another way of doing the int/float hacks that makes it more obvious just by reading the code what is happening. In C++ there are things like reinterpret_cast for this.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

Hej Ville,

That would be the best solution yes :) Being able to use FileMoveData on a integer variable would help as well ( as that is the primary offender I use the ? hack for ).

K
Post Reply