Note To Self: Division By Zero Is Not A Good Idea

Makes the game crash you know.

Dangerous thing to do when you have variables used in dividing something. Some programmer guru can now comment and tell people how this “possibility to divide by zero” (when using variables) thing should be handled properly. (I code a small IF check for each variable that I use in dividing something).

Juuso Hietalahti


  1. The operators are a built in function syntax. You write the function and can make it handle anything a normal function can handle, because it IS a function. I know that through experience and all the material I’ve ever come across on it agrees. Since I’ve done what you say can not be done, I am sure it is possible, not necessarily the best answer, but possible.

  2. Steven I think you are barking up the wrong tree looking at operator overloading which can not be done for basic/integral/builtin types in either C, C++ or C# (I am assuming Juuso is using one of these languages).
    If windows and C or C++:

    If Linux

    As for C# I have never set a trap in that language so I can not help, but if IRC that would not cause a programme crash.

  3. Juuso,
    When you devide by Zero in windows, there is a “windows exception”. As far as I remember. You can catch this exception, with se_translator or something like this. If you don’t catch this exception, then it will cause a windows dialog to pop up.
    What you can do is handle this(and other) windows exceptions and just add an assert, so when you run on debug it will break in that point.

    Bottom line, I can’t think of any situation you need to devide by 0, any time there is a devision by zero, its a bug. That means you need to make one solution to all devisions in your program, and not put an ‘if’ only on certain places. Its also a lot more work to put an ‘if’ every place you devide.

  4. Hmm my comment has been eaten twice, maybe because it contains links yet there is no feed back to tell me it is waiting moderation.

  5. @Israel: heheee :D

    @Brian: yeh, something like that was in my mind.

    @Steven & Liam: could you give some example…?

  6. Coding Padawan here to help. The nifty answer I know about is to “overload” the division operator for the specific case of dividing by zero. Then you can give a “safe” fail situation rather than triggering a crash. How to do that depends on languages and other such things.

    Of course then there are fun little things you can do like one calculator assignment I did, where if a person divided by zero five times in a row the program told off the user and closed itself. It was just command line, but I thought it was funny.

  7. If you’re programming in C/C++, you can use this type of statement if you suspect you could be dividing by zero:

    (divisor ? dividend/divisor : 0)

    This checks if the divisor evaluates to true (non-zero) before trying to divide by it. The last part. “0”, could be whatever makes sense.

    Another thing to watch out for is variable initialization. If you initialize a variable to 0 and don’t add anything to it before you try to divide by the variable, then you’ll run into problems.

    Hope that helps. :)

  8. If you are using an IEEE 754 implementation of floating points you can set a trap handler for the division by zero operation.

Comments are closed.