Due to rounding error cannot get ratio between two numbers:
Ratio=exp(x)/(exp(x)+exp(y))
such that x=-1.11e4
and y=-1.12e4
.
Any mathematical or computational trick to do?
How about some mathematical manipulation in log-space...
R = exp(x)/[exp(x)+exp(y)]
log(R) = log[exp(x)] - log[exp(x)+exp(y)]
= log[exp(x)] - log[exp(x)*(1+exp(y)/exp(x))]
= log[exp(x)] - log[exp(x)*(1+exp(y-x)]
= log[exp(x)] - log[exp(x)] - log[(1+exp(y-x))]
= - log[(1+exp(y-x))]
Now, exp(y-x)
should be a reasonable number, so you can calculate that easily. Then convert back to normal space using R = exp(log(R))
.
If that still doesn't work, you can actually taylor expand the last line:
log[(1+z)] ~ 1 + z^2/2 - z^3/3 ...
for small z
, in this case z = exp(y-x)
.