简体   繁体   English

在R中使用sqrt(x)和x ^ 0.5时结果不同

[英]Different result when using sqrt(x) and x^0.5 in R

I have an issue where there is a difference in the results when I use sqrt(x) rather than x^0.5. 我有一个问题,当我使用sqrt(x)而不是x ^ 0.5时,结果会有差异。

The calculations are being carried out on floating point numbers such as: 计算是针对浮点数进行的,例如:

0.002296438 0.002296438

Trouble is this truncated version as displayed in Rstudio does not replicate the problem. 问题是,Rstudio中显示的此截断版本无法复制该问题。 However the non-truncated version does (any idea how I can get the non-truncated version to display so I can show a working example)? 但是,非截断的版本确实可以吗(您知道如何获取非截断的版本以便可以显示一个有效的示例吗?)

The errors are indeed small of the order of e^-18 which are not so worrying in themselves. 误差的确很小,只有e ^ -18的数量级,本身并不担心。 However over even moderately large data sets (10,000 date points) these errors compound to give errors in the variance estimate at the 4th decimal place which is more concerning! 但是,即使在中等规模的数据集(10,000个日期点)上,这些误差也会加起来,使方差估计的误差在小数点后第4位更令人担忧!

I realize that R is only accurate to 16 decimal places, see answer from nullglob below but these errors seems to be systematic? 我意识到R只能精确到小数点后16位,请参见下面nullglob的答案,但这些错误似乎是系统的? Every time you run sqrt(x) and x^0.5 they both produce the same answer each time. 每次您运行sqrt(x)和x ^ 0.5时,它们每次都会产生相同的答案。 However these answers are still different from each other. 但是,这些答案仍然彼此不同。

Formatting Decimal places in R 格式化R中的小数位数

Is one version considered to be more accurate than the other? 是否认为一个版本比另一个版本更准确?

Baz 巴兹

OK here is the example 好的,这是例子

[1] 0.002296437934635199226707
> test4=sqrt(0.002296437934635199226707)
> test5=0.002296437934635199226707^0.5
> test6=test5-test4
test6
[1] 6.938894e-18

Confirmed (more or less) the suggestion of different C calls with the following C code, which calls the sqrt() and pow() functions from the system math library. 通过以下C代码(或多或少)确认了有关不同C调用的建议,该C代码从系统数学库中调用sqrt()pow()函数。

#include <math.h>
#include <stdio.h>

int main(int argc, char **argv) {
    double x = 0.002296437934635199226707;
    long double y = 0.002296437934635199226707;

    printf("%1.22g\n",sqrt(x)-pow(x,0.5));
    printf("%1.22Lg\n",sqrtl(y)-powl(y,0.5));

    return(0);
}

and prints 和印刷品

-6.938893903907228377648e-18
-3.388131789017201356273e-21

on my system (32-bit Ubuntu 12.04), ie the results for long double are slightly closer than the results for double. 在我的系统(32位Ubuntu 12.04)上,即long double的结果比double的结果稍微接近。 The commenters above are correct, though, that if you're worrying about this level of precision you probably have larger problems; 上面的评论者是正确的,但是,如果您担心这种精度水平,您可能会遇到更大的问题。 is the rest of your algorithm stable to this level of precision? 您算法的其余部分是否稳定到这种精度水平?

In order to dig deeper (which probably isn't worth it except for intellectual curiosity) you'd have to find out more about the implementation of these two functions in the system libraries for your particular OS. 为了更深入地挖掘(除了出于好奇心,这可能不值得),您必须在特定操作系统的系统库中找到有关这两个功能的实现的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM