奇怪的输出.不应该输出 153 而不是 152 [英] BIZZARE OUTPUT. SHOULDN'T OUTPUT BE 153 INSTEAD OF 152

查看:65
本文介绍了奇怪的输出.不应该输出 153 而不是 152的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{

  char arr[3]={'1','5','3'};
  int sum=0;
  for (int i=0;i<3;i++)
  {
     sum+=pow((arr[i]-48),3);

     printf("%d to the power 3 is: %f\n",arr[i]-48,pow((arr[i]-48),3));
  }
 printf("sum is %d\n",sum);
}

预期输出:

1 to the power 3 is: 1.000000
5 to the power 3 is: 125.000000
3 to the power 3 is: 27.000000
sum is 153

实际输出:

1 to the power 3 is: 1.000000
5 to the power 3 is: 125.000000
3 to the power 3 is: 27.000000
sum is 152

这是输出的图像

输出不应该是 153 而不是 152吗?

Shouldn't the output be 153 and NOT 152?

如果代替 sum+=pow((arr[i]-48),3);我用
sum+=(int)(floor(pow((arr[i]-48),3)));

If instead of sum+=pow((arr[i]-48),3); i use
sum+=(int)(floor(pow((arr[i]-48),3)));

输出正确地为 153.所以我不认为 pow 返回较小的整数,或者就此而言,将浮点型转换为整数的类型转换返回较小的值是这里的情况

OUTPUT IS CORRECTLY COMING AS 153. So i don't thing pow returning the smaller integer or for that matter type casting of float to integer returning the smaller value is the case over here

推荐答案

Microsoft pow 例程是出了名的糟糕.正如 macOS pow 所展示的那样,可以为这些和类似的操作数返回正确的结果,但微软要么没有投入工程工作来做到这一点,要么选择不改变他们的 pow 实现.即使对于数学结果可以用浮点表示的小整数操作数,pow 也可能返回比正确值稍大或稍小的结果.当返回的结果小于精确的整数结果时,将其转换为整数会导致截断为下一个较小的整数.

The Microsoft pow routine is notoriously bad. It is possible to return correct results for these and similar operands, as the macOS pow demonstrates, but Microsoft either has not put in the engineering work to do this or has chosen not to change their pow implementation. Even for small integer operands with mathematical results that are representable in floating-point, pow may return results that are slightly larger or slightly smaller than the correct value. When returns a result smaller than the exact integer result, then converting it to an integer results in truncation to the next lower integer.

计算pow 很困难,而且并不是所有的实现都做得很好.对于浮点函数,理论上可能的最佳质量是正确舍入.正确舍入的例程返回可表示为最接近精确数学结果的浮点格式的数字,按所选舍入规则控制的方向舍入.(最常用的四舍五入规则是四舍五入到最接近的值,与偶数较低的数字有关.其他规则包括向 +∞、-∞ 和向零四舍五入.)计算 pow 非常困难 具有正确的四舍五入,并且我知道没有任何商业或常用实现这样做.

Computing pow is difficult, and not all implementations do a good job. For floating-point functions, the best quality theoretically possible is correctly rounded. A correctly rounded routine returns the number representable in the floating-point format that is closest to the exact mathematical result, rounded in a direction governed by a chosen rounding rule. (The most commonly used rounding rule is to round to the nearest value, with ties toward the even low digit. Other rules include rounding toward +∞, toward −∞, and toward zero.) It is very difficult to compute pow with correct rounding, and no commercial or commonly used implementation I am aware of does so.

尽管如此,可以设计 pow 以便在精确结果可以浮点格式表示时返回精确结果.我记得,当前的 macOS pow 实现就是这样做的.因此,问题中的程序在使用 macOS 工具编译和执行时,将产生预期的结果.微软的pow没有这个属性,所以计算pow(x, 3)可能会返回一个略小于x3<的值/sup>,即使 x3 是可表示的.

Nevertheless, it is possible to design pow so that it returns the exact result whenever the exact result is representable in the floating-point format. As I recall, the current macOS pow implementation does this. Thus, the program in the question, when compiled and executed with macOS tools, will produce the expected results. Microsoft’s pow does not have this property, so computing pow(x, 3) may return a value slightly less than x3, even when x3 is representable.

即使使用高质量的 pow 实现,出于速度的原因,通常也不希望使用具有小整数幂的 pow.计算 pow(x, 3) 比计算 x*x*x 慢.

Even if one is using a high-quality pow implementation, it is generally desirable not to use pow with small integer powers for reasons of speed. Computing pow(x, 3) is slower than computing x*x*x.

这篇关于奇怪的输出.不应该输出 153 而不是 152的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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