大n的java.lang.Math.pow(x,n)有多精确? [英] How accurate/precise is java.lang.Math.pow(x, n) for large n?

查看:165
本文介绍了大n的java.lang.Math.pow(x,n)有多精确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算(1.0-p)^n,其中p是0到1之间的双精度数(通常非常接近0),n是一个正整数,可能是几百或几千(也许更大;我是还不确定).如果可能的话,我想只使用Java内置的java.lang.Math.pow(1.0-p, n)来实现,但是我有点担心,这样做可能会导致我对所关注的值范围产生巨大的准确性/精度损失.有人对使用Java的实现会有什么样的错误有一个大概的认识吗?我不确定它们的实现到底是什么(对数和/或泰勒近似值?),所以我不能冒险猜测.

I would like to calculate (1.0-p)^n where p is a double between 0 and 1 (often very close to 0) and n is a positive integer that might be on the order of hundreds or thousands (perhaps larger; I'm not sure yet). If possible I would love to just use Java's built in java.lang.Math.pow(1.0-p, n) for this, but I'm slightly concerned that there might be a gigantic loss of accuracy/precision in doing so with the range of values that I'm interested in. Does anybody have a rough idea of what kind of error I might expect using Java's implementation? I'm not sure what goes on under the hood in their implementation (logs and/or Taylor approximations?), so I can't hazard a good guess.

我最担心的是相对误差(即偏离的幅度不超过一个数量级).如果答案是Java的实现会产生太多错误,那么您是否有任何好的库建议(但再次,我希望这不是必需的)?谢谢.

I'm mostly concerned about relative error (i.e. not being off by more than an order of magnitude). If the answer turns out to be that Java's implementation will produce too much error, do you have any good library recommendations (but again, I'm hoping this shouldn't be needed)? Thanks.

推荐答案

计算结果必须在准确结果的1 ulp之内.

The computed result must be within 1 ulp of the exact result.

因此,我认为您不必担心实现的麻烦,而不必担心浮点精度的限制.如果您最关注精度而不是性能,则可能要考虑使用BigDecimal.pow().

So I don't think you need to worry about the implementation so much as about the limits of floating-point accuracy. You may want to consider using BigDecimal.pow() if accuracy rather than performance is your primary concern.

这篇关于大n的java.lang.Math.pow(x,n)有多精确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆