dividing by zero (and comparisons)

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:
D

dannyjenn

Guest
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.
 
J

jaydee

Guest
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 + 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.
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?
 
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?
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.
 

NeoShade

Member
I'm just taking advantage of a/0 = infinity (in gamemaker)
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?
 

NeoShade

Member
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.
 
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. :)
 

NeoShade

Member
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.
 
D

dannyjenn

Guest
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.
 
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.
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).
 
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:
D

dannyjenn

Guest
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 : -sign(b)*0.000001) ) > c
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 by a moderator:
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
 
Looking to find out if there is more information about this subject.

What I'm really interested in finding out is whether dividing by zero (in GMS1.4) produces consistent results from machine to machine or from platform to platform.

Here's an example of some code that presumes that the behavoir will be consistent.

It assumes that dividing by zero will return negative or positive infinity, and that negative infinity will compare smaller than all real numbers, and that positive infinity will compare greater than all real numbers. As long as this assumption is true, I don't need to handle seperately cases where _dx1 or _dy1 are zero.

This code reflects a line off of a rotated rectangle represented by an instance.
Code:
    //start point of line
    var _sx = x0;
    var _sy = y0;
    //direction vector of line
    var _dx = mouse_x - x0;
    var _dy = mouse_y - y0;
    var _c = dcos(image_angle);
    var _s = dsin(image_angle);
    //rotate start point and direction vector into rectangle's space
    var _sx1 = (_sx-x) * _c + (_sy-y) * -_s + sprite_xoffset;
    var _sy1 = (_sx-x) * _s + (_sy-y) *  _c + sprite_yoffset;
    var _dx1 = _dx * _c + _dy * -_s;
    var _dy1 = _dx * _s + _dy *  _c;
    //nearside
    var _bx0 = sprite_width  * (_dx1 < 0);
    var _by0 = sprite_height * (_dy1 < 0);
    //farside
    var _bx1 = sprite_width  - _bx0;
    var _by1 = sprite_height - _by0;
    var _t0 = (_bx0 - _sx1) / _dx1; //time of intersection with near side on x axis
    var _t1 = (_by0 - _sy1) / _dy1; //time of intersection with near side on y axis
    var _t = max(_t0,_t1);  //time of intersection is greatest time of intersection with near sides
    var _h = _t < min((_bx1 - _sx1) / _dx1, (_by1 - _sy1) / _dy1);  //if time of intersection with near side is less than time of intersection of far side, then line doesn't miss rectangle
    var _sh = _t1 > _t0;  //if time of intersection of near side on y axis is greatest, then side hit is top or bottom
    if (_t >= 0) //if not negative time solution
    and (_h) {  //and line doesnt miss rectangle
        //location of intersection point:
        var _intercept_x = _sx + _dx * _t;
        var _intercept_y = _sy + _dy * _t;
        //shorten line in proportion to _t
        _dx -= _dx * _t;
        _dy -= _dy * _t;
        //location of reflected line end point:
        if (_sh) { var _nx = _s; var _ny =  _c; } //hit top or bottom side
        else {           var _nx = _c; var _ny = -_s; } //hit left or right side
        //reflect line direction:
        var _d = 2.0 * (_dx * _nx + _dy * _ny);
        _dx -= _d * _nx;
        _dy -= _d * _ny;
        var _reflect_x = _intercept_x + _dx;
        var _reflect_y = _intercept_y + _dy;   
    }
 

Gamebot

Member
b = 0;

show_message(5/b)
Iv'e converted to GMS2 so that formula gives an "inf" but this is definitely up my ally as I try to find odd entities like this. If I'm getting the actual question right you simply want to know:

If dividing by zero is consistent between all platforms?
and
If there is a way to side step the consequences?

Could you not just check if b = 0 then multiply by the recipricol always putting 0 on top. This should always equal 0 and it would be consistent.
 
Iv'e converted to GMS2 so that formula gives an "inf" but this is definitely up my ally as I try to find odd entities like this. If I'm getting the actual question right you simply want to know:

If dividing by zero is consistent between all platforms?
and
If there is a way to side step the consequences?

Could you not just check if b = 0 then multiply by the recipricol always putting 0 on top. This should always equal 0 and it would be consistent.
Oh, I could easily protect against dividing by zero. But as long as under these circumstances it is safe, and consistent, and allows for simpler, cleaner code, and fewer logical branches, why not take advantage of it?
 
Top