This site uses strictly necessary cookies. More Information

X# Unity Infinity Math Result

```
float stunResist = 0;
void Start(){
GetHit(0.80f);
}
void GetHit(float stunChance){
stunChance = stunChance / stunResist;
}
```

now i am no math genius but isn't the result supposed to be 0.80 and unity is giving me infinity result. All i want to know is why?

Edit also i should note yes you can not divide by 0 but what if the default is 0 how do you pass this then?

i should not have to write extra logic for this to be accepted this should already be in the basics of the game engine to notice the 0 result and to just pass the division in that case so that no matter what the result comes out 0,80 even if the divide number is 0.

Edit #2: Well obviously nobody cares enough about this to answer or comment so a mod can close this, I just don't know why something as basic as making a division be forgotten if it is 0 is not in the unity engine already i mean the game engine know what a division is already so its seems easy to do something like.

```
if(dividing number is 0){
keep result and forget the division
}
```

so in my case unity would know the dividing number is 0 and just keep the 0.80 as the result with out touching it until the divided number was changed in the future by future code.

The problem with doing the check every single time is that takes processing power. Consider you in the midst of some nested loops that runs very often: in this case even a single comparison operation can add significantly to the total processing time. This is the same reason you need to check that your objects are not equal to null, before you use them. Personally, I'm surprised that didn't generate a run time error: Division by zero is a very low level CPU exception/interrupt.

Edit: you asked about default values- If a number is used as a denominator, I usually set the default value to 1.0f (aka, do nothing). It is also possible to store the number x as 1/x, and do a multiplication rather than division at operation-time. (this second option makes those divide by zero's more obvious.)

Stun resist should be 1 by default then. And above 1 when you want to add resistance. And yeah X/0 = infinity in math...

Not really of practical programming importance, but:

Technically, in math, division by zero yields an UNDEFINED value, not infinity: https://en.wikipedia.org/wiki/Division_by_zero However, it is accurate to state that: for Y=Z/X, as X approaches 0, Y approaches infinity. (sorry to be nit-picky)

True :=) I just did not want to introduce the idea limits. But to be even more precise a decimal float is never really zero so my guess is that unity engine can't handle the result of the division of such a small number close to 0.In that case ins$$anonymous$$d of throwing a stack overflow it says infinity (pure hypothesis). I also think that the engine behind the scene is working with double (that actually supports infinity).

do you agree?Stun resist should be 1 by default then. And above 1 when you want to add resistance.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.