1. Hey! Guest! The 31st GMC Jam will take place between Nov 16th, 12:00 UTC (Friday noon) and Nov 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

dividing by zero (and comparisons)

Discussion in 'Programming' started by flyingsaucerinvasion, Nov 8, 2018.

  1. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,749
    I'm not having any problems doing this, but I'm curious if there is a reason not to.

    say I am making a comparison: (a/b) > c, where b is zero

    According to my tests, a/b will be (what I interpret to represent) + or - infinity or NaN, depending on the sign of a.

    +infinity appears to be greater than any real number.
    -infinity appears to be less than any real number.
    NaN appears to be neither greater than nor less than any number.

    I just want to make sure these resutls will always be consistent (on windows).
     
    Last edited: Nov 8, 2018
  2. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    448
    I am not aware that GameMaker allows you to divide by 0. If it's working for you, my guess is that it's probably an undocumented feature, and you should avoid using it.
     
  3. Posh Indie

    Posh Indie Member

    Joined:
    Dec 5, 2016
    Posts:
    158
    Please stop conducting these sorts of tests. The last thing we need is to tear a hole in the space-time contin-
     
    Last edited: Nov 8, 2018
    dannyjenn likes this.
  4. jaydee

    jaydee Member

    Joined:
    Dec 27, 2016
    Posts:
    70
    Dividing by zero is not equivalent to infinity. It is undefined. The limit of the function a/b as b approaches 0 is infinite, and that is where the misconception comes from.

    That GM is not raising an error would imply that you are not dividing by 0, but more likely by 0.0000002 (as an example) as a result of using floats. Therefore, I would assume it is possible for b to equal -0.0000002 as well. Which would make your function inconsistent.

    Why do you want to divide by zero anyway?
     
    zbox likes this.
  5. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,749
    I'm not too concerned about the definition of division by zero in mathematics, just how and if it is implemented consistently in gamemaker. I'm just taking advantage of a/0 = infinity (in gamemaker) to streamline some algorithms, and it is working fine, I'd just like to know that the implementation isn't going to be inconsistent. I'm only using them in comparisons.
     
  6. NeoShade

    NeoShade Member

    Joined:
    Jun 21, 2016
    Posts:
    136
    But... it's not. In normal mathematics, or in gamemaker. I just tested it with show_debug_message(8/0) and I got a division by 0 error. The answer shouldn't be and isn't infinity.
    More to the point, gamemaker can't even express infinity, so what exactly are you trying to do?
     
  7. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,749
    you got a compile error

    try this instead:

    b = 0;

    show_message(5/b)
     
  8. NeoShade

    NeoShade Member

    Joined:
    Jun 21, 2016
    Posts:
    136
    I apologize, you're right. I had no idea that was possible.
    Regardless, I'm still curious about why you're trying to make use of this. I can't think of a single situation where dividing my zero is actually useful.
     
  9. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,749
    The goal is not to divide by zero, but to sidestep the consequences. I might show what I'm doing just as soon as I figure out for sure whether or not what I'm doing isn't completely foolish. :)
     
  10. NeoShade

    NeoShade Member

    Joined:
    Jun 21, 2016
    Posts:
    136
    If you truly want to sidestep the consequences, surely something like the following would be better:

    Code:
    if (b==0)
        {
        if (a > 0)  { aa = 999999999; }
        else if (a < 0)  { aa = -999999999; }
        }
    
    if (aa > c)
        {
        // Whatever your goal is...
        }
    
    This way, you're avoiding funky divide by 0 maths entirely.
    I don't know what your (a == 0) case needs to be, because I don't know the context, but to me, this kind of solution seems safer.
     
  11. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    448
    Yeah, as I said earlier, dividing by 0 probably isn't safe. Even if it does work reliably, you have no idea of whether it'll still work several months down the road when new updates are released.
     
  12. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,749
    Yeah, that's just exactly what I'm trying to find out.

    As it turns out, my algorithm might have been flawed anyway, but for other reasons. I'd still like to know what standard exactly gamemaker is using, and whether it will remain consistent (at least on the windows platform).
     
  13. Online Handle

    Online Handle Member

    Joined:
    Oct 11, 2016
    Posts:
    238
    Just have b default to some low number and you won't ever have a problem.
    Code:
    
    (a / (abs(b) < 0.000001 ? sign(b)*0.000001 : b) ) > c
    
    EDIT: fixed a mistake made while tired
     
    Last edited: Nov 9, 2018
    dannyjenn likes this.
  14. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    448
    Shouldn't that be:
    Code:
    (a / (abs(b) < 0.000001 ? sign(b)*0.000001 : b) ) > c
    ?

    This should probably work too:
    Code:
    (a / (b + ((b==0) * 0.000000000000000000001)) > c
    edit - maybe not
     
    Last edited: Nov 8, 2018
    Online Handle likes this.
  15. Online Handle

    Online Handle Member

    Joined:
    Oct 11, 2016
    Posts:
    238
    Yea, my bad. I was tired when I wrote the response.
    It should be
    Code:
    (a / (abs(b) < 0.000001 ? sign(b)*0.000001 : b) ) > c
    
     
  16. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    576

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice