Logum中的灾难性取消
我正在尝试在相对精度较低的双精度浮点中实现以下功能: logsum(x,y)=log(exp(x)+exp(y))logsum(x,y)=log(exp(x)+exp(y))\mathrm{logsum}(x,y) = \log(\exp(x) + \exp(y)) 这在统计应用程序中被广泛使用以添加在对数空间中表示的概率或概率密度。当然,或都很容易溢出或下溢,这很不好,因为日志空间最初是用来避免下溢的。这是典型的解决方案:exp(x)exp(x)\exp(x)exp(y)exp(y)\exp(y) logsum(x,y)=x+log1p(exp(y−x))logsum(x,y)=x+log1p(exp(y−x))\mathrm{logsum}(x,y) = x + \mathrm{log1p}(\exp(y - x)) Cancellation from y−xy−xy-x does happen, but is mitigated by expexp\exp. Worse by far is when xxx and log1p(exp(y−x))log1p(exp(y−x))\mathrm{log1p}(\exp(y - x)) are close. Here's a relative error plot: The plot is cut off at 10−1410−1410^{-14} to emphasize …