保证C/C ++中sqrt函数的精度 [英] Guaranteed precision of sqrt function in C/C++
问题描述
每个人都从C/C ++中的math.h
/cmath
知道sqrt
函数-它返回其参数的平方根.当然,这样做必须有一些错误,因为并非每个数字都可以精确存储.但是我可以保证结果有一定精度吗?例如,这是平方根的最佳近似值,可以用所用的浮点类型表示.or
如果计算结果的平方,则使用给定的浮点类型将使其尽可能接近初始参数.
Everyone knows sqrt
function from math.h
/cmath
in C/C++ - it returns square root of its argument. Of course, it has to do it with some error, because not every number can be stored precisely. But am I guaranteed that the result has some precision? For example, 'it's the best approximation of square root that can be represented in the floating point type usedor
if you calculate square of the result, it will be as close to initial argument as possible using the floating point type given`?
C/C ++标准有什么相关之处吗?
Does C/C++ standard have something about it?
推荐答案
对于C99,没有特定要求.但是大多数实现都尝试尽可能地支持附录F:IEC 60559浮点运算.它说:
For C99, there are no specific requirements. But most implementations try to support Annex F: IEC 60559 floating-point arithmetic as good as possible. It says:
定义
__STDC_IEC_559__
的实现应符合本附件中的规范.
An implementation that defines
__STDC_IEC_559__
shall conform to the specifications in this annex.
并且:
<math.h>
中的sqrt
函数提供IEC 60559平方根操作.
The
sqrt
functions in<math.h>
provide the IEC 60559 square root operation.
IEC 60559(等同于IEEE 754)对sqrt
之类的基本操作进行了说明:
IEC 60559 (equivalent to IEEE 754) says about basic operations like sqrt
:
除二进制<->十进制转换外,每个操作都应像首先产生一个无穷大精度且无界的中间结果一样执行,然后强制该中间结果适合目标格式.
Except for binary <-> decimal conversion, each of the operations shall be performed as if it first produced an intermediate result correct to infinite precision and with unbounded range, and then coerced this intermediate result to fit in the destination's format.
最后一步是根据几种舍入模式进行舍入,但是结果必须始终是目标精度中最接近的可表示值.
The final step consists of rounding according to several rounding modes but the result must always be the closest representable value in the target precision.
这篇关于保证C/C ++中sqrt函数的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!