Best way to handle the ratio which cannot be represented as floating point numbers.

43 Views Asked by At

I need to calculate the ratio of the form:

$s=\sum_1^3q_i$,$\quad$ $p_i=\frac{q_i}{\sum_1^3q_i}$,

where $q_i >0$. One problem is that $q_i$ are too small that they can not represented as floating point numbers, then I can try logarithms $z_i=\mathrm{log}(q_i)$ and $\mathrm{log}(p_i)=\mathrm{log}(q_i)-\mathrm{log}(q_1+q_2+q_3)$, at this moment, I know $\mathrm{log}(q_1)=-2012,\mathrm{log}(q_2)=-2013,\mathrm{log}(q_3)=-2014$, but how to deal with $\mathrm{log}(q_1+q_2+q_3)$, any one could give me some idea?

thanks

1

There are 1 best solutions below

2
On

Hint

Let me suppose $q_1\gt q_2 \gt q_3$. So, $$p_1=\frac{q_1}{q_1+q_2+q_3}=\frac{1}{1+\frac{q_2}{q_1}+\frac{q_3}{q_1}}$$ Now, $$p_2=p_1 \frac{q_2}{q_1}$$ $$p_3=p_2 \frac{q_3}{q_2}$$ I suppose that this will give you your number for almost any accuracy (computing the required ratios from the logarithms).