Standard library - floating point with higher precision?

So, I have problems with precision in Python.

I would like to compute such functions:

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

Where x and y can be> 1000. Python math.exp (-1000) (at least 2.6!) Does not have enough floating point precision to handle this.

  • this form looks like logistic / logit / log -odds, but it isn’t, right? Is there any algebraic simplification that I'm missing here?
  • I know about the decimal value, but I'm not sure if it is applicable here.
  • Looks like homework, but it’s not, I promise!

(Also, I'm open to titles! I could not come up with a good question for this question!)

+4
source share
4 answers

you can split the top and bottom by exp(-x)

 P(x,y) = 1/(1 + exp(xy)) 
+8
source
 >>> import decimal >>> decimal.Decimal(-1000).exp() Decimal('5.075958897549456765291809480E-435') >>> decimal.getcontext().prec = 60 >>> decimal.Decimal(-1000).exp() Decimal('5.07595889754945676529180947957433691930559928289283736183239E-435') 
+8
source
 P(x,y) = exp(-x)/(exp(-x) + exp(-y)) 

is equivalent to:

 P(x,y) = 1 / (1 + exp(xy)) 

Perhaps the second works without using more accuracy.

+4
source

I think you're looking for bigfloat for arbitrary robust floating point arithmetic.

+3
source

All Articles