This tutorial uses the symbol $\infty$ in a few places, and it's not at all obvious how it behaves. It is after all bigger than any other number, and that's kind of weird. Mathematicians say that infinity is not a number at all, and that's correct in a way because it cannot always be treated like a finite number. This chapter contains many examples of that.

Basic Calculations

Some math websites and tutorials discard things like $\infty+5$ as nonsense, but I'll instead show you that it is possible to define some $\infty$ calculations if we carefully avoid defining something that breaks stuff. Experienced mathematicians probably want me to mention that I'm talking about the extended real number line. There, I have mentioned it.

$$\begin{align}\infty + 5 = \infty\end{align}$$

Yes, that's correct. If you add any finite number to $\infty$, you'll just get $\infty$. You can think about it like this: if you have $\infty$ lines of code and you write more code, you still have $\infty$ lines of code...

$$\begin{align}\infty - 5 = \infty\end{align}$$

...or if you delete 5 lines of code, you still have lots of code! In general, $\infty + x = \infty$ for any finite $x$, even if $x$ is negative.

Let's think about this a bit. If we have $\infty+3 = \infty$ and $\infty+5 = \infty$, we also have $\infty+3 = \infty+5$ and it seems like $3 = 5$. That's obviously not correct, and the problem is that if we want to get from $\infty+3 = \infty+5$ to $3=5$ we need to substract $\infty$ on both sides. That gives us $\infty-\infty$ on both sides, and $\infty-\infty$ is undefined, just like $0/0$.

>>> float('infinity') - float('infinity')   # nan is short for "not a number"

So, if you used to have $\infty$ lines of code and you tell me that you deleted $\infty$ lines of it, I have no idea how much code you have now; it depends on how you deleted $\infty$ lines of code. If you deleted the whole repository you have no code, but if you deleted lines of code one by one, deleting them did nothing because $\infty-1=\infty$. In both cases, you deleted $\infty$ lines of code.

Here are more examples of things that we can do. Try them out with your favorite programming language.

$$\begin{align}\frac 3 \infty &= 0 \\ \infty \cdot 3 &= \infty+\infty+\infty = \infty \\ \frac \infty 3 &= \infty \cdot \frac 1 3 = \infty \\ \infty \cdot (-3) &= -(\infty \cdot 3) = -\infty \\ \frac{\infty}{-3} &= -\frac \infty 3 = -\infty \\ \infty^3 &= \infty \cdot \infty \cdot \infty = \infty \\ 2^\infty &= 2 \cdot 2 \cdot 2 \cdot ... = \infty \\ \left(\frac 1 2\right)^\infty &= \frac 1 2 \cdot \frac 1 2 \cdot \frac 1 2 \cdot...=0\end{align}$$

The summary page contains a concise list of rules.


What if we want to do something kind of like $\infty-\infty$, but in a controlled way instead of saying that it's undefined? For example, what happens to $x-x^2$ as $x$ gets really big? Maybe we can just plug in $\infty$, the weird thing that is... well, really big:


That didn't work. But what if we just plug in some big numbers?

>>> def f(x):
...     return x - x**2
>>> f(10)
>>> f(100)
>>> f(1000)
>>> f(1000000000)
>>> f(1000000000000000000)

The values get closer and closer to $-\infty$. This is called taking the limit.

$$\begin{align}\lim_{x\to\infty}(x-x^2) = -\infty\end{align}$$

The $x\to\infty$ part means that $x$ approaches $\infty$. It means that $x$ gets bigger and bigger, but it never goes all the way to $\infty$. We don't really care about what $\infty-\infty^2$ is, we just want to know what happens to $x-x^2$ with a huge $x$.

However, sometimes we can calculate limits by plugging in the value:

$$\begin{align}\lim_{x\to\infty}x^2 = \infty^2 = \infty\end{align}$$

Even if we can't, it doesn't mean that we need to plug in big numbers and guess what the limit might be based on them. It's often possible to do something to get rid of $\infty-\infty$:

$$\begin{align}\lim_{x\to\infty}(x-x^2) &= \lim_{x\to\infty}(\blue x-\blue x\cdot x) = \lim_{x\to\infty}\blue x(1-x) \\ &= \infty\cdot(1-\infty) = \infty\cdot(-\infty)=-\infty^2 = -\infty\end{align}$$

Note that $x-x^2=x(1-x)$ is valid for a finite $x$, but $\infty-\infty^2=\infty(1-\infty)$ is wrong because $\infty-\infty^2$ is not defined to begin with. The $x$ was finite because we're talking about a limit, and it doesn't mean just plugging in $x=\infty$.

If you see something like $x\to2^+$, it means that $x$ goes to $2$ from the positive side, and we can plug in values like $2.001$ or $2.0000000001$ to guess what the limit might be. Similarly, $x\to2^-$ means approaching with numbers like $1.9999999$.

A plain $x\to2$ means that we must approach 2 from both sides and get the same value, but as a special case, $x\to\infty$ and $x\to-\infty$ mean approaching from one side only because it's not possible to approach from both sides.

Handy thing: $a^2-b^2=(a-b)(a+b)$

Proof using $(a+b)c=ac+bc$ and $(a-b)c=ac-bc$:

$$\begin{align}& \ \blue{(a-b)}\green{(a+b)} \\ =&\ \blue a \green{(a+b)}-\blue b \green{(a+b)} \\ =&\ (\blue a \green a+\blue a\green b)-(\blue b\green a+\blue b\green b) \\ =&\ aa\rcancel{+ab}\rcancel{-ab}-bb \\ =&\ a^2 - b^2\end{align}$$

Spoiler: plug in $a=x$ and $b=1$.



Can you calculate these limits by hand? Check the answer by plugging in numbers with your favorite programming language or calculator.

$$\begin{align}\lim_{x\to\infty}(x^5-x^4) \qquad \lim_{x\to\infty}(x^4-x^5)\end{align}$$$$\begin{align}\lim_{x\to1^+}\frac{x^2-1}{x-1} \qquad \lim_{x\to1^-}\frac{x^2-1}{x-1} \qquad \lim_{x\to1}\frac{x^2-1}{x-1}\end{align}$$

Common Tricks

There are a few things you can do when working with limits. Here $x$ and $y$ can be anything, and plain $\lim$ without anything under it means that these rules work with all limits.

$$\begin{align}\begin{array}{ll} \lim(x+y) = (\lim x)+(\lim y) &\text{ if }(\lim x)+(\lim y)\text{ is defined} \\ \lim(x-y) = (\lim x)-(\lim y) &\text{ if }(\lim x)-(\lim y)\text{ is defined} \\ \lim(xy) = (\lim x)(\lim y) &\text{ if }(\lim x)(\lim y)\text{ is defined} \\ \lim\frac x y = \frac{\lim x}{\lim y} &\text{ if }\frac{\lim x}{\lim y}\text{ is defined} \\ \lim(x^y) = (\lim x)^{\lim y} &\text{ if }(\lim x)^{\lim y}\text{ is defined} \\ \lim f(x) = f(\lim x) &\text{ if }f\text{ is a continuous function} \\ \end{array}\end{align}$$

I don't recommend memorizing these rules; they are quite self-explanatory and simple. Just don't get confused when we use them.

The "if blablabla is defined" means that the rules cannot be used when they give us something that doesn't make sense. For example:

$$\begin{align}\lim_{x\to\infty}(x^2 - x) = \left(\lim_{x\to\infty} x^2\right)-\left(\lim_{x\to\infty} x\right) = \infty - \infty\end{align}$$

Here we need to do some other stuff before using the rules.

You are probably wondering WTF the last rule is saying. See this thing if you have no idea what a function is. Pretty much all functions you'll come across in this tutorial are continuous, and I'll let you know if something is not continuous. We'll talk more about this later.

For example, $\sqrt{\quad}$ is continuous:

$$\begin{align}\lim\sqrt x=\sqrt{\lim x}\end{align}$$

In the rest of this tutorial, we'll calculate limits like this:

$$\begin{align}\lim_{h\to0} \frac{1}{\sqrt{x+h}+\sqrt x}\end{align}$$

Again, don't get confused; there's nothing unusual here so everything's nice and continuous.

$$\begin{align}\blue{\lim_{h\to0}} \frac{1}{\sqrt{x+\blue h}+\sqrt x} &= \frac{1}{\sqrt{x+\blue{\underset{h\to0}{\lim}h}} + \sqrt x} \\ &= \frac{1}{\sqrt{x+\blue{0}} + \sqrt x}\end{align}$$

A faster way is to just think about what happens to $\frac{1}{\sqrt{x+h}+\sqrt x}$ with a tiny $h$. It obviously gets very close to $\frac{1}{\sqrt{x}+\sqrt x}$.

Indeterminate Forms

As you can see, getting $\infty-\infty$ or $\frac 0 0$ in a limit means that we couldn't just plug in the value that is being approached. We need to do more work to get rid of the $\infty-\infty$ or $\frac 0 0$ situation. In mathy words, $\infty-\infty$ and $\frac 0 0$ are indeterminate forms.

There are many other indeterminate forms too. Here's a concise list:

$$\begin{align}\infty-\infty,\ \frac \infty \infty,\ \frac 0 0,\ 0\cdot\infty,\ 1^\infty,\ 0^0,\ \infty^0\end{align}$$

Let's look at a few examples of these situations in limits giving different values.


You have already seen many $\infty-\infty$ limits with different values in this chapter.


I'll divide top and bottom by $x$ in these examples. If you try to do e.g. $\frac{\infty}{2\infty}$ you get $\frac \infty \infty$.

$$\begin{align}\lim_{x\to\infty} \frac{x}{2x} = \lim_{x\to\infty} \frac 1 2 = \frac 1 2\end{align}$$$$\begin{align}\lim_{x\to\infty} \frac{2x}{x} = \lim_{x\to\infty} \frac 2 1 = \frac 2 1 = 2\end{align}$$

Here's a fun way to think about this, taken from a comment on this question:


$\displaystyle\frac 0 0$

Plugging in $x=0$ gives $\frac 0 0$ in these examples.

$$\begin{align}\lim_{x\to0}\frac{x}{2x} = \lim_{x\to0} \frac 1 2 = \frac 1 2\end{align}$$$$\begin{align}\lim_{x\to0}\frac{2x}{x} = \lim_{x\to0} \frac 2 1 = \frac 2 1 = 2\end{align}$$

See also this video.

$0 \cdot \infty$

This one is interesting because $0$ multiplied by anything finite is $0$, and infinity multiplied by anything nonzero is either $\infty$ or $-\infty$ (see above).

$$\begin{align}\lim_{x\to\infty}(0 \cdot x) = 0\end{align}$$$$\begin{align}\lim_{x\to\infty} \biggl(\ \underbrace{\frac 1 x}_{\to0^+} \cdot \overbrace{x}^{\to\infty}\ \biggr) = \lim_{x\to\infty} \frac x x = \lim_{x\to\infty} 1 = 1\end{align}$$$$\begin{align}\lim_{x\to\infty} \biggl(\ \underbrace{\frac 1 x}_{\to0^+} \cdot \overbrace{x^2}^{\to\infty}\ \biggr) = \lim_{x\to\infty} \frac{x^2}{x} = \lim_{x\to\infty} \frac x 1 = \frac \infty 1 = \infty\end{align}$$


You might be thinking that it makes sense to define $1^\infty = 1$ because $1^x=1$ for any finite $x$ (multiplying 1 by itself $x$ times is still 1). Even though this limit is 1...

$$\begin{align}\lim_{x\to\infty}1^x = 1\end{align}$$ doesn't mean that all $1^\infty$ limits are 1. Here's a famous example that we'll revisit later:

$$\begin{align}e = \lim_{n\to\infty} \left(1+\frac 1 n\right)^n = 2.718281828459...\end{align}$$

This number is called $e$. The inside of the limit goes to 1 because $\displaystyle\lim_{n\to\infty}\textstyle(1+\frac 1 n)=1+0=1$, but the whole thing does not go to 1.

>>> n = 10000000000
>>> (1 + 1/n)**n
>>> import math
>>> math.e

Here are more $1^\infty$ limits:

$$\begin{align}\lim_{n\to\infty} \left(1+\frac 1 n\right)^{2n} = \lim_{n\to\infty} \Biggl(\left(1+\frac 1 n\right)^n\Biggr)^2 = e^2 = 7.389055...\end{align}$$$$\begin{align}\lim_{n\to\infty} \left(1+\frac 1 n\right)^{n\ \cdot\ n} = \lim_{n\to\infty} \Biggl(\left(1+\frac 1 n\right)^n\Biggr)^n = e^\infty = \infty\end{align}$$

Interestingly, Python doesn't know this...

>>> 1**float('infinity')     # python does this WRONG

...but JavaScript does the right thing:

> Math.pow(1, Infinity)         // NaN is the correct answer!


Let's start by thinking about what $x^0$ means for a finite $x$. We know that $x^{a+b}=x^ax^b$ (see the basics page), so we can plug in e.g. $a=1$ and $b=0$:

$$\begin{align}x^{1+0} &= x^1x^0 \\ x &= x \cdot x^0\end{align}$$

This is true if $x=0$, and if $x \ne 0$, we can divide by $x$ on both sides to get $1=x^0$. So here's our conclusion: $x^0=1$ for any finite $x$ except $x=0$ and we know nothing about $0^0$ yet.

On the other hand, multiplying $0$ by itself many times is still $0$, so $0^x=0$ with any positive $x$. We get these limits:

$$\begin{align}\lim_{x\to0^+} 0^x = 0\end{align}$$$$\begin{align}\lim_{x\to0} x^0 = 1\end{align}$$

Most programming languages do $0^0 = 1$ because it's handy in some cases.


We just learned that $x^0=1$ with any finite $x$ except $x=0$.


But we also have $\infty^x=\infty$ for any positive $x$:

$$\begin{align}\lim_{x\to0^+}\infty^x = \infty\end{align}$$

Infinite Fractions and Stuff

This stuff doesn't have anything to do with other things in this chapter, but I added it here because it's fun.


It's an infinite fraction, it never ends! But we can still find its value, and in fact, it's quite simple: we'll say that the value is $x$ and figure things out from there.

$$\begin{align}x &= 1+\frac{1}{2+\frac{1}{2+\frac{1}{2+...}}} \\ &= 1+\frac{1}{1+\underbrace{1+\frac{1}{1+1+\frac{1}{1+1+...}}}_{\text{this part is }x}}\end{align}$$

Handy thing: $(a-b)(a+b)=a^2-b^2$

This was proved earlier in this chapter. Now we need to plug in $a=x$ and $b=1$.

This is correct, the infinite fraction contains itself! Now we get an equation that we can solve easily:

$$\begin{align}x &= 1 + \frac{1}{1+x} \\ x-1 &= \frac{1}{x+1} \\ (x+1)(x-1) &= 1 \\ x^2-1^2 &= 1 \\ x^2 &= 2 \\ x=\sqrt2 \text{ or } & x=-\sqrt2\end{align}$$

The infinite fraction consists of adding and dividing positive numbers, so it must be positive.



Golden ratio is the only positive $x$ value that satisfies $x + x^2 = x^3$. Prove that $\sqrt{1+\sqrt{1+\sqrt{1+\sqrt{1+...}}}}$ is the golden ratio.