Operations with large numbers

I have some a_i numbers (for i = 1 to 10000). I need to calculate exp (a_i) / sum (exp (a_j)) using matlab.

Of course, it is impossible to calculate immediately. I found a few tricks, the most interesting:

“Suppose we want to find exp (7.0873e002). It will be a large number, but still hardly available in the matte ability of direct calculation. However, we can find a separate exponent and mantissa without calling“ exp ”as it should;

a = 7.0873e2; x = a/log(10); D = floor(x); % D will be an integer F = 10^(xD); % F will lie in 1 <= F < 10 Then D will be the power of ten and F the mantissa F = 6.27376373225551 % The mantissa D = 307 % The exponent (power of ten) Compare that with the direct answer: exp(a) = 6.273763732256170e+307" 

I tried something like this, but the result could be Inf:

  a = 7.0873e5; x = a/log(10); D = floor(x); F = 10^(xD); exp(a) = Inf 

Does anyone have an idea?

+4
source share
1 answer

Your answer is in F and D Since your a much larger than the example of a (i.e. e5 vs e2 ), which they say is only within the Matlab range, yours should be well out of range and thus becomes inf . But it doesn’t matter, because D and F hold your answer, you should not check it for exp(a) , in the example only exp(a) calculated to demonstrate the proof of the concept. But the whole point of this code is to give you a way to find exp giant numbers.

In your case you will receive

 D = 307797 

and

 F = 3.374110424643062 % Use format long 

so your answer is 3.374110424643062e+307797

+3
source

Source: https://habr.com/ru/post/1492046/


All Articles