1
$\begingroup$

I'm trying to figure out a way of handling numbers larger than $1.79 \times 10^{308}$ in Javascript. The method I'm attempting is to store numbers as their base-10 logarithms, as that would allow me to write them as

Math.pow(10, num % 1) + "e+" + Math.floor(num)

However, I can't seem to find a way to correctly add and subtract numbers that are stored this way. Multiplication and division aren't a problem, because

$\log_{10}(xy) = \log_{10}(x) + \log_{10}(y) \\ \log_{10}(\frac xy)=\log_{10}(x)-\log_{10}(y)$

Neither is exponentiation, as

$x^y = 10^{y(log_{10}(x))}$

Addition and subtraction are the two operations I can't figure out a concrete solution for. I did some calculations to help myself out with this (plugging in two random numbers for the variables), and I found out

$log_{10}(14)+log_{10}(\frac {14+5}{14})=log_{10}(14+5)\Rightarrow log_{10}(19)$

Though, I'm not sure whether or not this will work with any two values, mainly because it requires taking 10 to the power of the logarithms, which will cause the function to return Infinity if either of them is greater than 308.25.

Is there any other method I can use to get a value $n$ such that $log_{10}(x)+n=log_{10}(x+y)$?

$\endgroup$
1
$\begingroup$

After digging around and trying all sorts of methods, I found one that worked.

$\text{let } d = \log_{10}(y) - \log_{10}(x) \\ \log_{10}(x+y)=\log_{10}(x) + \log_{10}(1+10^{d})$

$\endgroup$
1
$\begingroup$

Indeed, the logarithmic representation is not quite amenable to additions and subtractions (that's the price you pay for easy multiplications and divisions).

$$\log_B(x+y)=\log_B(x)+\log_B\left(1+\frac yx\right)=\log_B(x)+\log_B\left(1+B^{\log(y/x)}\right).$$

As the log-ratio is easy to compute, you can focus on the special 1D function

$$f(t)=\log_B(1+B^t),$$

very close to two straight lines.

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.