简体   繁体   English

cos(x) 是否需要在使用 IEEE-754 的不同 C++ 实现中返回相同的值?

[英]Is cos(x) required to return identical values in different C++ implementations that use IEEE-754?

Is there any sort of guarantee - either in the C++ standard or in some other document - that C++ code computing cos(x) will produce identical values when compiled with g++, clang, MSVC, etc., assuming those implementations are using IEEE-754 64-bit double s and the input value x is exactly equal?是否有任何形式的保证 - 无论是在cos(x)标准还是在其他一些文件中 - 假设这些实现使用 IEEE-754 64位double s和输入值x正好相等? My assumption is "yes," but I'd like to confirm that before relying on this behavior.我的假设是“是”,但我想在依赖此行为之前确认这一点。

Context: I'm teaching a course in which students may need to compute trigonometric functions of inputs.背景:我正在教授一门课程,学生可能需要计算输入的三角函数。 I can assure that those inputs are identical when fed into the functions.我可以保证这些输入在输入函数时是相同的。 I'm aware that equality-testing double s is not a good idea, but in this specific case I was wondering if it was safe to do so.我知道相等测试double s 不是一个好主意,但在这种特定情况下,我想知道这样做是否安全。

cos is a transcendental function. Transcendental functions are subject to the table-maker's dilemma . cos是一个先验的 function。先验函数受制表者困境的影响。 Informally, what this means is: let's say you come up with some iterative algorithm for approximating the cosine of an input value: for example, a Taylor series.通俗地说,这意味着:假设您提出了一些迭代算法来近似输入值的余弦值:例如,泰勒级数。 When you run this iterative algorithm, you have to decide how much extra precision to keep at the intermediate stages (rounding too early may reduce the accuracy of the final result).当您运行此迭代算法时,您必须决定在中间阶段保留多少额外精度(过早舍入可能会降低最终结果的准确性)。 But because the function is transcendental, it's very difficult to determine how many extra bits must be carried during the calculation in order to yield a correctly rounded final result, and for some input values, the number of extra bits required might be very large.但由于 function 是超越数,因此很难确定在计算过程中必须携带多少额外位才能产生正确舍入的最终结果,并且对于某些输入值,所需的额外位数可能非常大。

For this reason, it is generally not practical to design hardware that guarantees correctly rounded results for transcendental functions such as cos (where "correctly rounded" means that the resulting floating point value is the one that's closest to the true real value of the function).出于这个原因,设计硬件来保证cos等超越函数的正确舍入结果通常是不切实际的(其中“正确舍入”意味着得到的浮点值是最接近函数真实实数值的值) . Instead, the hardware designers will implement a calculation technique that performs reasonably well and that, for most practical input values, will yield a result that is within 1 bit of the exact real result.取而代之的是,硬件设计人员将实施一种性能相当不错的计算技术,对于大多数实际输入值,该技术将产生与实际结果相差 1 位以内的结果。 (If you absolutely need a cosine function that always yields a correctly rounded result, then apparently it's possible to implement one: GNU MPFR claims to have done it. But this will perform much worse than hardware.) (如果您绝对需要始终产生正确舍入结果的余弦 function,那么显然可以实现一个:GNU MPFR 声称已经做到了。但这比硬件性能差得多。)

IEEE 754 (2008) lists cos as one of the "recommended correctly rounded functions", which means that if you implement IEEE 754's version of cos , then you have to yield a correctly rounded result. IEEE 754 (2008) 将cos列为“推荐的正确舍入函数”之一,这意味着如果您实施 IEEE 754 版本的cos ,则必须产生正确舍入的结果。 But these functions are only "recommended" to be provided, and not required.但这些功能只是“建议”提供,并非必需。 Therefore, a conforming implementation of IEEE 754 might not provide a correctly rounded cos function, and might instead provide a "practical" cos function as described in the previous paragraph.因此,符合 IEEE 754 标准的实现可能无法提供正确舍入的cos function,而是可能会提供上一段中所述的“实用” cos function。 Therefore, in practice, two implementations of C++ which both claim to be IEEE 754 compliant may not yield the exact same value for a transcendental function such as cos when applied to the same argument.因此,在实践中,两个声称符合 IEEE 754 标准的 C++ 的实现可能不会为先验 function 产生完全相同的值,例如cos当应用于相同的参数时。

(Note that IEEE 754 requires implementations to provide a square root function that is correctly rounded. This is not a transcendental function, so correctly rounding it is not nearly as difficult.) (请注意,IEEE 754要求实现提供正确四舍五入的平方根 function。这不是先验的 function,因此正确四舍五入并不难。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM