3
$\begingroup$

I want to know summation of some small numbers, such as {e^-1000, -e^1001, e^1002...}

If all numbers are positive, I can use log-sum-exp algorithm. But unfortunately, negative numbers are also included in numbers.

Is there any method to calculate the sum of such numbers?

$\endgroup$
3
  • $\begingroup$ You can (sort of) take the log of a negative number: $\log(-1) = i\pi$ (sort of) $\endgroup$
    – wlad
    Dec 11, 2020 at 20:35
  • 2
    $\begingroup$ Maybe first all positive numbers can be summed up separately, and all negative summed up separately? $\endgroup$ Dec 11, 2020 at 20:38
  • $\begingroup$ Defining $\log(-x) = \log(x) + i\pi$ and $\log(0)= \infty$ should present one solution to your problem $\endgroup$
    – wlad
    Dec 11, 2020 at 20:41

1 Answer 1

1
$\begingroup$

In terms of Python, use

from cmath import log, exp

def clog(x):
    if  x == 0:
        return -float('inf')
    else:
        return log(x)

For a negative value of $x$, this gives $\log(x) = \log(-x) + i \pi$, and for $\log(0)$, this gives $\infty$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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