#### Archived

This topic is now archived and is closed to further replies.

# Dumb compiler or dumb coder?

This topic is 5274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Take a look at this code..
	const int MaxFrameRate= 0;
const double FramePeriod= MaxFrameRate > 0 ? 1. / MaxFrameRate : 0;

VC 6 (latest patch) gives me a the error C2124: divide or mod by zero. Which is pretty dumb, you would think it could detect that 'MaxFrameRate' is never > 0 and not attempt to compile the expression. Whats up with that? [edited by - willm on May 7, 2004 10:28:36 AM]

##### Share on other sites
Well, you have maxframrate constantly defined as 0, so in your statement, you will always be dividing 1 by 0. It tests if MaxFramRAte is greater than 0, and since it isnt, then it will execute the second part of the ternary statement, which is dividing 1 by maxframerate.

##### Share on other sites
Well, perhaps neither ... or both. :-)

Point taken though. It does seem like you''ve got the compiler in a twist there. Perhaps the compiler just doesn''t like constants to be defined using formulae.

So, why are you defining a constant using a conditional operator anway? Makes no sense to me.

Also, setting your Max Frame Rate to 0 seems counter-productive unless you are implying that 0 means that there is no maximum. In which case it''s probably just as good to set it to 1000 which will always look unlimited anyway and you''ll also have no need for your conditional operator.

Ah, I''ve got it now that I''ve looked at it again. It might be your lack of parentheses (sp?). Try this:

const double FramePeriod = (MaxFrameRate > 0) ? (1.0 / (double)MaxFrameRate) : 0;

There could be a mixup between the ''>'' and the ''?''. If not, then my earlier comments stand.

R

--------------------------------------------------------------------------
There is no point in flaming if you''ve merely poured fuel on your own head

##### Share on other sites
Incidentally, shouldn''t this be a warning? Or are you set to show warnings as errors?

##### Share on other sites
"Why won''t my compiler compile my broken code?????"

Does it matter? Just fix your broken code and be done with it.

##### Share on other sites
I was experimenting with a a frame rate limiter, and I used a MaxFrameRate value of 0 to indicate that there was no maximum value.

Tried adding the parenthesis, but no change..

Found some more wierdness, the code in my original post gives an error, but if I explicitly cast MaxFrameRate to a double when doing the divide I get a 'warning C4723: potential divide by 0'.

None of this is a big problem, its easy to work around. Just seems inconsistent..

[edited by - willm on May 7, 2004 11:11:24 AM]

##### Share on other sites
Code isn''t broken as I stated earlier, sorry about that.

I''m guessing this is a result of the type of parser used by both MSVC and gcc. The parser most likely evaluates the "1. / blah" before it gets to the conditional. Because reducing the division is an optimization it can make (since both are constants) it does make it, even though it results in NaN. If you visualize the parse tree, the division is probably at the lowest branch, and the compiler most likely processes the tree in a bottom-up fashion.

It bit you in this case, but it''s necessary that the compiler works in this way. It should just be a warning though.

##### Share on other sites
Even VC7.1 generates an error(not warning). If you remove the const it compiles just fine though, not even a warning. A #define gives the same error.

It seems very much like a compiler bug, because the division part of the statement shouldn''t happen when MaxFrameRate is zero.

##### Share on other sites
This is dumb code IMHO, and a perfectly valid compiler complaint.

If MaxFrameRate was non-const, then i would see the problem, but why are you testing that the value of it is greater than 0 when you already know the value is 0?

Its just the same as writing

0 > 0 ? 1/0 : 0

doesnt make any sense.

##### Share on other sites
quote:
Original post by Jingo
This is dumb code IMHO, and a perfectly valid compiler complaint.

If MaxFrameRate was non-const, then i would see the problem, but why are you testing that the value of it is greater than 0 when you already know the value is 0?

Its just the same as writing

0 > 0 ? 1/0 : 0

doesnt make any sense.

It does make sense if you want to change the max framerate in one place, without needing to change the FramePeriod too. And both should be constant that doesn''t change during the game.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5

• 13
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632926
• Total Posts
3009239
• ### Who's Online (See full list)

There are no registered users currently online

×