- Home /
if statement for infinity. How to check if a variable is Infinity?
How do I check for "Infinity"?
Can I make an if statement for checking if the variable is returning infinity?
if ( myVecto.x == infinity ) { ....
You can check if is returning infinity by printing in the console first. For example:
if ( vectorForce.x > 100000f) Debug.Log( vectorForce.ToString());
And it will print in console something like this:
(Infinity, -Infinity, -Infinity)
But I can't make something like this
if (vectorForce.x == "Infinity") { <<<------------ RETURNS ERROR
Because my rigid body force assign attempt for 'myGameObject' is not valid. And the Input force is
And/or transform float or Vector assign attempt for my gameObject is not valid.{ Infinity, -Infinity, -Infinity }
THx!Input localScale is { Infinity, 0.500000, 0.500000 }.
Answer by unidad2pete · Aug 14, 2017 at 06:18 PM
Try:
if (vectorForce.x == Mathf.Infinity)
{
Debug.Log("infinity");
}
or this:
if (vectorForce.x.ToString() == Mathf.Infinity.ToString())
{
Debug.Log("infinity");
}
And if the value is negative Infinity? The other answers options are correct in some way.
Answer by Bunny83 · Aug 15, 2017 at 01:04 AM
If you don't care if the value is PositiveInfinity orNegativeInfinity you can simply use IsInfinity
if(vectorForce.x.IsInfinity())
However if you want to distinguish between positive and negative infinity you would simply use:
if(vectorForce.x == float.PositiveInfinity)
// or
if(vectorForce.x == float.NegativeInfinity)
IsInfinity will return true in both cases. Note: if the infinity value isn't expected by you, you might want to investigate where it actually comes from. Common causes are that you divided your vector by "0". Dividing a value different from 0 by zero doesn't result in an error but in positive or negative infinity.
So in general it's better to handle that situation before you divide. Note that when you divide 0 by 0 it will result in a NaN value instead as 0/0 is not defined.
Answer by tanoshimi · Aug 14, 2017 at 06:19 PM
I don't quite understand the use case, but you can test whether a float value is at its max possible using:
if(vectorForce.x == float.MaxValue)
Note: float.$$anonymous$$axValue is not the same as float.PositiveInfinity. $$anonymous$$axValue is the largest number that can still be represented by a float. Everything above that will yield PositiveInfinity.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Some Boolean "Action" Trouble 2 Answers
How to add a variable to a prebuilt class? 1 Answer
Class Scope Variable 1 Answer