【发布时间】:2019-04-04 04:42:52
【问题描述】:
我尝试解决一个数学问题,但我的输出灵敏度略有不同,例如 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;
}
示例:
terms : 10
x : 1.1
output : `-59783.61` with `powf`
output : `-59783.67` with `pow`
这两个函数有什么区别?
【问题讨论】:
-
man pow:
double pow(double x, double y)和float powf(float x, float y) -
无关:请不要使用 _s 函数,它们并不比便携式对应物更安全。
-
与其考虑 0.07 的差异,不如考虑 1,000,000 中 1 部分的差异 - 大约是典型
float的精度。 -
同
.1和.1f之间。
标签: c floating-accuracy