pow和powf有什么区别

00r*_*rhn 0 c floating-accuracy

我尝试解决数学问题,输出灵敏度有点不同,如0.07.然后我比较pow()powf()在我的代码中看到这种敏感性.代码如下:

int main()
{

    int terms, sign=-1;
    double x, operation=0.0;

    printf("Please enter the number of terms : ");
    scanf_s("%d", &terms);

    while (terms <= 0)
    {
        printf("Please re-enter the number of terms :");
        scanf_s("%d", &terms);
    }

    printf("Please enter a value for x :");
    scanf_s("%lf", &x);

    for (int i = 1; i <= terms; i++)
    {
    sign = sign * (-1);
    operation = operation + sign * powf(x + i / 10.0, (2 * i) - 1) / (2 * i);
    }
    printf("The result is : %.2lf\n", operation);

    system("pause");
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

样品:

terms  : 10

x : 1.1

output  : `-59783.61` with `powf`

output  : `-59783.67` with `pow`
Run Code Online (Sandbox Code Playgroud)

这些功能有什么区别?

Mad*_*ist 5

powdoubles上运作.powffloats上运作.这是在C,一个相当标准表示法,其中的功能的基本名称将是操作数(和返回值)的默认类型(如的intdouble),而前缀和后缀的版本是用于其它类型(如long,float等).这是一个参考:http://pubs.opengroup.org/onlinepubs/9699919799/.

数据类型的这种差异完全解释了您在结果中看到的差异.

doubles在尾数中包含53位精度,转换为精度的~16位十进制数.这超出了显示结果的精度,因此可能足以满足您的需要.

float另一方面,s在尾数中只有24位,转换为~7个十进制数字.任何操作组合都会导致舍入错误几乎立即蔓延到显示精度范围内.