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

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?

Answer1:

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

Answer2:

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;

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:How can I prevent/detect an underflow in a Postgresql calculation that uses EXP()