Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard library - higher-precision floating point?

So, I'm having some precision issues in Python.

I would like to calculate functions like this:

P(x,y) = exp(-x)/(exp(-x) + exp(-y))

Where x and y might be >1000. Python's math.exp(-1000) (in 2.6 at least!) doesn't have enough floating point precision to handle this.

  1. this form looks like logistic / logit / log-odds, but it's not, right? Is there some algebraic simplification I'm missing here?
  2. I know about Decimal, but am not sure if it applies here
  3. looks like homework, but it's not, I promise!

(Also, I'm open to titles! I couldn't think of a good one for this question!)

like image 338
Gregg Lind Avatar asked Nov 29 '22 20:11

Gregg Lind


1 Answers

you could divide the top and bottom by exp(-x)

P(x,y) = 1/(1 + exp(x-y))
like image 169
newacct Avatar answered Dec 05 '22 13:12

newacct