# dividing by zero (and comparisons)

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

1. ### flyingsaucerinvasionMember

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. ### dannyjennMember

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 IndieMember

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. ### jaydeeMember

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. ### flyingsaucerinvasionMember

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.

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. ### flyingsaucerinvasionMember

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

b = 0;

show_message(5/b)

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. ### flyingsaucerinvasionMember

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.

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. ### dannyjennMember

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. ### flyingsaucerinvasionMember

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 HandleMember

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. ### dannyjennMember

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 HandleMember

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
```

Joined:
Aug 30, 2016
Posts:
576