对于某些正整数n,m,(int)pow(n,m)会出错吗? [英] Will (int)pow(n,m) be wrong for some positive integers n,m?

查看:107
本文介绍了对于某些正整数n,m,(int)pow(n,m)会出错吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设nm是正整数,并且n m 在整数范围内,(int)pow(n,m)会给出错误的答案吗?

Assuming n and m are positive integers, and nm is within the range of an integer, will (int)pow(n,m) ever give a wrong answer?

我为m=2尝试了许多n,到目前为止还没有得到任何错误的答案.

I have tried many n for m=2 and have not gotten any wrong answers so far.

推荐答案

C标准对浮点算术的准确性没有任何要求.准确性是实施定义的,这意味着需要实施来对其进行记录.但是,在实现过程中会留下大量的空白":(&5.2.4.2.2第6段,增加了重点.)

The C standard does not impose any requirements on the accuracy of floating point arithmetic. The accuracy is implementation-defined which means that implementations are required to document it. However, implementations are left with a significant "out": (§5.2.4.2.2 paragraph 6, emphasis added.)

浮点运算(+-*/)以及<math.h><complex.h>中返回浮点结果的库函数的精度是实现-定义,由库函数在<stdio.h><stdlib.h><wchar.h>中执行的浮点内部表示和字符串表示之间的转换精度. 实施可能会指出 准确性未知.

The accuracy of the floating-point operations (+, -, *, /) and of the library functions in <math.h> and <complex.h> that return floating-point results is implementation-defined, as is the accuracy of the conversion between floating-point internal representations and string representations performed by the library functions in <stdio.h>, <stdlib.h>, and <wchar.h>. The implementation may state that the accuracy is unknown.

事实上,gcc通过指定准确性未知.尽管如此,即使不能保证,glibc计算的准确性也非常好.

And, indeed, gcc takes advantage of this by specifying that the accuracy is unknown. Nonetheless, the accuracy of glibc computations is pretty good, even if not guaranteed.

已知MS libc实现有时会为带整数参数的pow函数产生1ULP错误,如果将pow操作的结果简单地截断为int,则会导致错误的值. (我在Visual Studio文档中找不到有关浮点精度的任何规范,但我相信下面的SO问题列表为我的断言提供了证据.)

The MS libc implementation is known to occasionally produce an error of 1ULP for the pow function with integer arguments, resulting in an incorrect value if the result of the pow operation is simply truncated to an int. (I couldn't find any specification in the Visual Studio documentation about floating point accuracy, but I believe that the list of SO questions below provides evidence of my assertion.)

在x86架构上,由于本机浮点表示符合标准,因此大多数实现都进行了一些尝试来实现IEEE 754.但是,直到2008年修订版为止,IEEE-754仅要求从+-*/sqrt正确舍入的结果.自修订版以来,它建议许多其他函数返回正确取整的结果,但是所有这些建议都是可选的,很少有数学库可以实现所有建议.

On x86 architectures, most implementations make some attempt to implement IEEE 754, since the native floating point representation conforms. However, until the 2008 revision, IEEE-754 only required correctly-rounded results from +, -, *, / and sqrt. Since the revision, it recommends that a number of other functions return correctly-rounded results, but all of these recommendations are optional, and few math libraries implement all of them.

如果您真的想使用pow来计算整数的整数幂,建议(并且很容易)使用lround(pow(n, m))而不是(long)(pow(n, m)),这会将结果四舍五入到最接近的整数,而不是乐观地依靠错误为正.如果pow在1ULP之内,则应该为结果提供正确的整数值,直到2 52 (IEEE-754加倍).在2 52 和2 53 之间,1ULP错误将为0.5,有时会四舍五入为错误的整数.除了2 53 之外,并非所有整数都可以表示为双精度.

If you really want to use pow to compute integer powers of integers, it is advisable (and easy) to use lround(pow(n, m)) instead of (long)(pow(n, m)), which will round the result to the nearest integer, rather than optimistically relying on the error being positive. That should give the correct integer value for results up to 252 (with IEEE-754 doubles) if pow is within 1ULP. Between 252 and 253, a 1ULP error would be 0.5, which will sometimes round to the wrong integer. Beyond 253 not all integers are representable as doubles.

SO实际上充满了由该特定问题引起的问题.参见:

SO is actually full of questions resulting from this particular problem. See:

  • Reversing a five digit number with POW function in C
  • C's pow() function as per header file <math.h> not working properly
  • Strange behaviour of the pow function
  • Why am I getting unexpected output when using floor with pow?
  • Why pow(10,5) = 9,999 in C++
  • return value of pow() gets rounded down if assigned to an integer (found by Lưu Vĩnh Phúc)
  • wrong output by power function - C (also)

无疑还有更多.

这篇关于对于某些正整数n,m,(int)pow(n,m)会出错吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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