Maybe I'm misunderstanding: what would you like the following code to do? int x = 2 / 0; cout << x;
-
-
Replying to @JaneSchwartz100
It's not what I would like it to do, it's what it _does_ do provided you set MXCSR properly (or the equivalent on other CPUs). With div-by-zero/underflow turned off, and flush-to-zero turned on, 2/0 = 0. So your code would print 0.pic.twitter.com/ju2q6BIfv5
1 reply 0 retweets 1 like -
Replying to @cmuratori @JaneSchwartz100
The only reason it _doesn't_ do that by default is because - for some truly bizarre reason - the default setting for MXCSR is to fault on divide by zero. Even though that is basically never what you want, unless you are debugging.
1 reply 0 retweets 1 like -
Replying to @cmuratori
Shouldn't we want to crash hard with errors like these, instead of letting them go unnoticed silently?
3 replies 0 retweets 2 likes -
Replying to @JaneSchwartz100
They're not errors if you wrote the code properly. Most code paths that feature a divide can be written such that they will work properly if the denominator is zero.
1 reply 0 retweets 1 like -
Replying to @cmuratori
Interesting, can you give an example? (Genuine question of course. Always wise to say that on Twitter
)1 reply 0 retweets 0 likes -
Replying to @JaneSchwartz100
Yes, certainly. Simplest is the oft-occurring form: int Step = Width / Count; for(int I = 0; I < Count; ++I) { // ... Step used here ... } With exceptions, this faults. Without exceptions, it runs as expected, since a zero count means the loop is never entered.
1 reply 0 retweets 3 likes -
Replying to @cmuratori @JaneSchwartz100
By enabling divide-by-zero exceptions, we effectively take all loops of this form and turn them from perfectly working code to things that literally crash the application :( There are _many_ other forms that look like this, this is just the easiest to type in Twitter.
1 reply 0 retweets 1 like -
Replying to @cmuratori @JaneSchwartz100
In ASM you can work around some of these. In the previous example, you could presumably move the divide below the top jnz for the for loop, and have the bottom jnz jump back to after the divide, etc., so maybe it "costs you nothing" to do.
1 reply 0 retweets 1 like -
Replying to @cmuratori @JaneSchwartz100
In C, on the other hand, you pretty much just have to manually type out additional if's every time. So you're going to always write if(Count) { int Step = Width / Count; for(int I = 0; I < Count; ++I) { // ... Step used here ... } } for no good reason.
1 reply 0 retweets 1 like
Your other option is to move the Width/Count into the loop body, and count on the compiler to hoist it. Probably not a horrible way to go these days, because it usually will, but, it is not really how you want to express the code as things get more complex.
-
-
Replying to @cmuratori @JaneSchwartz100
But regardless, the point is just, the simple thing that you "just write" would actually work if the exceptions were just turned off. So "the easy code is the right code" without exceptions, which is what you want!
1 reply 0 retweets 2 likes -
Replying to @cmuratori @JaneSchwartz100
i wish for this every division i have written in the past 20 years
0 replies 0 retweets 0 likes
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.