Skip to content

Latest commit

 

History

History
44 lines (27 loc) · 2.76 KB

DETAILS.md

File metadata and controls

44 lines (27 loc) · 2.76 KB

How is computed the logarithm of a big number ?

It's not that difficult since we have the Log10 function provided by the math package. So we just have to convert the big number into a float64 (type input required by Log10), and we're good.

But if an overflow happens, it's bad!

So we check if the number is too big to be converted into a float64. If so, we know that:

$log\big(ab\big) = log(a) + log(b)$

So we can do:

$log(x) = log(\sqrt{x}) + log(\sqrt{x})$(let $x$ be a big number)

By computing the square root of $x$ (with the Sqrt function provided by the math/big package), that value will be smaller $(\sqrt{x} < x)$ and may not cause an overflow.
If it doesn't overflow, by adding the logarithm of this value with itself $(log(\sqrt{x}) + log(\sqrt{x}))$, we can compute the logarithm of the big number.
Now if $\sqrt{x}$ still overflows, we just have to compute $\sqrt{\sqrt{x}}$. In other terms we get the fourth root. So to get $log(x)$, we have to multiply $\sqrt{\sqrt{x}} \times 4$.
And if $\sqrt{\sqrt{x}}$ is still too big, we continue the same process by computing its square root, and multiply it by $8$. So the general formula is:

$$log\big(\sqrt[2^{n}]{x}\big) \times 2^{n} = log(x)$$

$(n \in \mathbb{N}$ being the number of times we've computed the square root of the previous square root until we found a decent value that doesn't overflow. Meaning when $\sqrt[2^{n}]{x} \leq$ max number before overflow)

Limitations of Log10 (IntLog10, FloatLog10, RatLog10)

The largest number a float64 (output type of Log10) can handle before overflowing is $2^{1024} - 1$ (see math.MaxFloat64).
And we know that $log\big(10^{p}\big) = p$.
With that, we can conclude that the biggest number we can compute its logarithm is $10^{(2^{1024} - 1)}$.

Also, the variable that can overflows before reaching $10^{(2^{1024} - 1)}$ is $2^{n}$ (which is of type int64 and therefore can handle up to $2^{63} - 1$).
We can know the largest number that can be computed before causing an overflow to this value $(2^{n})$ by resolving this inequation:

$\sqrt[2^{63} - 1]{x} \leq 2^{63} - 1$ for IntLog10 (max value of int64: $2^{63} - 1$)
$\sqrt[2^{63} - 1]{x} \leq 2^{1024} - 1$ for FloatLog10 and RatLog10 (max value of float64: $2^{1024} - 1$)

So for IntLog10: $x \leq 10^{\frac{log\big(2^{63} - 1\big)}{(\frac{1}{2^{63} - 1})}}$ or $x \leq \sqrt[\frac{1}{2^{63} - 1}]{2^{63} - 1}$

$x \leq 10^{10^{20.24284004863006}}$

And for FloatLog10/RatLog10: $x \leq \sqrt[\frac{1}{2^{63} - 1}]{2^{1024} - 1}$

$x \leq 10^{10^{21.45379945581629}}$