sqlpostgresqlunderflow

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?


Solution

  • 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