- Home /

# what is the real value of Mathf.infinity

I want to know what is the real value of mathf.infinity? i want to know what number unity uses to represent infinity. is it possible to assign this number using a numeric literal to a variable?

**Answer** by matyicsapo
·
Apr 18, 2010 at 02:36 PM

visual c# says: 3.402823 * Mathf.Pow(10, 38)

which unity too can handle as a number

unity regards this as inifinity: 3.402823 * Mathf.Pow(10, 39)

(both using javascript or c#)

tried adding and subtracting ones from both but i couldn't get to infinity nor could i get away from it

(3.402823 * Mathf.Pow(10, 39)) - (3.402823 * Mathf.Pow(10, 38))

is still infinity

so i assume you could use this number: 3.402823 * Mathf.Pow(10, 38)

See http://en.wikipedia.org/wiki/Floating_point_number#Infinities for a reference to how floating point numbers represent infinities.

**Answer** by duck
·
Apr 18, 2010 at 04:11 PM

As far as I'm aware, Mathf.Infinity simply *doesn't have* a "normal" numerical equivalent, so no - it's not possible to assign it using a numeric literal.

You can directly assign it to a variable using the Mathf.Infinity constant, or you can have it returned as a result of by dividing by zero, or by exceeding float.MaxValue in an operation, however you can't subsequently subtract from this value to return to normal numeric values.

I can't really understand why you'd want to try and equate it with a real number, because then by definition it would not be infinity, it would be that number! I'm not sure whether you're trying to solve a particular problem, but it doesn't make sense to use anything other than the actual Infinity constant that is already provided.

Mathf.Infinity takes its value directly from float.PositiveInfinity which can be looked up in the MSDN reference.

PositiveInfinity is one of three "special values" that can be assigned to a float variable, in addition to the normal numeric range of values.

The other values are NegativeInfinity and Not a Number (NaN) (these also do not have a real numeric value equivalent!).

i did not try to solve a problem. i know that it can not be assigned but i had a challenging talk with one of my friends about it. i told him that it should be a set of bits like a normal float and a number that is larger than maxvalue of floats. thank you! i will read more in MSDN

If you're just interested in what the underlying 32 bits look like, it's simply:

```
S Exponent Mantissa
--------------------------------------------------------------
0 1111 1111 000 0000 0000 0000 0000 0000 // positive infinity
1 1111 1111 000 0000 0000 0000 0000 0000 // negative infinity
Y 1111 1111 XXX XXXX XXXX XXXX XXXX XXXX // NaN where at least one "X" has to be "1"
```

For more information see wikipedia

**Answer** by Ashkan_gc
·
Apr 19, 2010 at 11:08 AM

ok the answer is the infinity number is a constant representing the infinity and it's defined in the design of the floating point calculation hardware and is one of the many numbers that can be created using 32 bits. it's a normal 32 bit series of number but computer knows about it and knows it's infinity. in some computers infinity related calculations are software emulated but in most machines it's done in hardware. in high level languages compiler itself handles it but in older languages like see it's simply a macro and you really can assign it to a variable in assembly but when you do calculations on it the computer will produce correct results so infinity+1 = infinity. be careful it's not possible to check if (a == float.posetiveInfinity) you should use functions like isInfinity to check for that. when i said compiler handles it i meant that there are constant in the language itself that represent the infinity and -infinity and nan numbers so you don't need to know the set of bits that they mean INF or -INF. for having negative zero you can just type -0. don't worry about infinity because you can not assign it using numeric literals and there is not any way to get infinity as a result from a+b :)

**Answer** by weatxyz
·
Mar 16, 2016 at 05:47 PM

Looks like this post is old, but figured I'd add to this so people know exactly what Infinity is.

Infinity is the highest value that a floating point number can represent. Floating points are a little weird and complicated, but the formulas in the other posts calculate the highest possible value of a floating point number. float.MaxValue should actually be the same as Mathf.Infinity, but someone can correct me if I'm wrong.

No, that's actually wrong. Infinity doesn't have a numerical value at all, not in computer science nor in mathematics. Infinity is a "concept" or an "idea". The IEEE floating point format has several non number states / values which indicate a special value. This includes +inf, -inf and NaN.

float.MaxValue is the actual highest value that can be represented with a float. A mathematical operation that would somehow increase that value is usually "rounded" to infinity. So the standard usually rounds to +/- inf when the number gets too large / small.

The floating point format can work with the infinity concept in a logical way, not really in a mathematical way. For example in mathematics 1/0 is not defined. You can take the limit of 1/x for x approaching 0 either from left(+) or right(-) which will lead to either +inf or -inf. The IEEE standard has solved that "problem" by introducing a "signed zero". So for example (+0)/-1 = (-0) and (-0)/-1 = (+0). However things like (inf / inf) isn't defined and will lead to a NaN (Not a Number) same happens with (0*inf). However this does work: (1/+inf) == (+0).

Just wondering... is comparing a float against infinity more or less efficient than comparing it against a large float.

For instance would this be more efficient if I used say 10000f on the basis that my level is never going to be that big anyway?

```
closestDistance = Mathf.Infinity;
if (enemyDistance < closestDistance)
```

Well, i doubt that it would make any mesurable difference. You actually have to distinguish between PositiveInfinity and NegativeInfinity. I guess that a comparison of a number, that isn't itself infinity, against infinity will be faster, as the hardware doesn't have to compare the values but only the "kind" of the float value. So if one operand is one of the two infinities and the other operand is any number it can immediately tell which one is larger. +inf > x and -inf < x where x is any number except that infinity you compare to.

Comparing float values in general should be quite fast anyways. First they would check the sign of both numbers. If one is positive and the other negative you can already tell which one is larger. Then you would compare the exponents alone. Depending on the signs the number with the larger(+) / smaller(-) exponent is the larger value. If both numbers have the same exponent we have to compare the mantissa. This could all be done with simple integer arithmetic. It might has some special cases for "0" as +0 and -0 actually are treated the same so -0 == +0 if you compare them.

For sorting / search algorithms to find the smallest / largest distance (or value in general) it's usually the best idea to use +inf / -inf as initial value. In your case 10000f might be too small in some cases. What if all values are greater than 10000? One could say you should pick a "reasonable large / small number" but it always depends on context what's reasonable. In a space game you might be dealing with distances way beyond 10000.

Well, in formal mathematics, depending, infinity is a perfectly good numerical value. It's infinity. The version I learned was *countable* infinity (but I had to google to remember this.)

1/0=infinity and infinity+1=infinity are perfectly fine equations. There are even proofs showing that infinity-squared is infinity, but 2 to the power of infinity is a larger, more powerful infinity.

But, in a game or anything even close to a game, I can't think of any possible reason when that would even matter. In practice Mathf.infinity is either an error, or "largest storable number+1."

@owen: In mathematics the result of the equation 1/x for x=0 is not defined as there is a singularity at at this point. The result could be either +inf or -inf. Apart from that infinity does never have an exact value. That's why 1/x for x=+inf is actually "0". For example (+inf) + (-inf) also isn't defined because infinity doesn't have an exact value. Performing that calculation will lead a NaN. As i said above the IEEE format invented the signed "0" to handle the singularity at 0. In mathematics you can inspect the limit of 1/x when approaching 0 from either side:

```
lim (1/x) = +inf
x-->+0
lim (1/x) = -inf
x-->-0
```

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

### Follow this Question

### Related Questions

ForceMode.Acceleration estimated dst != covered dst with a single Addforce in effect 1 Answer

Calculating force 1 Answer

Physic Material friction mathematical formula 0 Answers

Calculating triangle side length based on hypotenuse? 1 Answer

Find position of pixel on sphere given position of pixel on texture 1 Answer