大n的java.lang.Math.pow(x,n)有多精确? [英] How accurate/precise is java.lang.Math.pow(x, n) for large 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.