How can I prevent/detect an underflow in a Postgresql calculation that uses EXP()

佐手、 提交于 2019-12-10 21:43:08

问题


I am receiving a value out of range: underflow error from pgsql, in a query that uses the EXP(x) function. What values of x trigger this? How do I prevent or detect it?


回答1:


The function exp is called the exponential function, and its inverse is the natural logarithm, or logarithm to base e. The number e is also commonly defined as the base of the natural logarithm

In other words, exp(x) and e^x are the same function. However, since e is a transcendental number, and therefore irrational, its value cannot be given exactly.

The Numerical value of e truncated to 10 decimal places is 2.71828 1828

So, the function exp(x) is technically valid for all values of x, but practically speaking, you can limit them. For example, if you limit them to +/- 700 you should cover all cases covering the range

exp(700) = 1.01423205 × 10^304
exp(-700) = 9.85967654 × 10^-305

More than that depends on your application




回答2:


A workaround I'm using is to figure check range on my arguments using 'CASE' than fall back to a default if it's out of reasonable scope:

SELECT CASE WHEN p > 100 THEN 0
       ELSE .1 ^ p
       END;


来源:https://stackoverflow.com/questions/413940/how-can-i-prevent-detect-an-underflow-in-a-postgresql-calculation-that-uses-exp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!