简体   繁体   English

编译器的不同结果:(gcc 4.8.1)和gcc(4.3.2)

[英]Different results for compilers: (gcc 4.8.1) and gcc( 4.3.2)

I submitted same solution for problem on an online judge on different compilers for C++.On gcc(4.3.2),I got WA, while same solution when submitted on gcc(4.8.1) got TLE. 我通过在线法官针对C ++的不同编译器提交了相同的问题解决方案。在gcc(4.3.2)上,我得到了WA,而在gcc(4.8.1)上提交时,相同的解决方案得到了TLE。

Is it that 4.3.2 is faster but I think performance wise latest versions should outperform previous ones or is it the floating point anomalies in two compilers because problem require calculation of nth root of a 64-bit number and I am using long double and long long data types with pow function.I used something like: 是4.3.2速度更快,但我认为性能明智的最新版本应优于以前的版本,还是两个编译器中的浮点异常,因为问题需要计算64位数字的nth根,并且我使用的是long double和long具有pow函数的长数据类型。我使用了类似的东西:

    long long root,n;
    long double rad,rcnd;
    root = (long long)pow(rad,rcnd); where rcnd = 1.0/n;

Two versions of a same elementary function such as pow() only need each to be accurate to >0.5ULP to sometimes produce different results with the same arguments. 相同基本函数的两个版本(例如pow()每个都需要精确到> 0.5ULP才能有时用相同的参数产生不同的结果。

The C and C++ standards do not place any constraints on the accuracy on pow() . C和C ++标准对pow()的准确性没有任何限制。 A proper implementation will try to be accurate to 1ULP , but that still leaves the possibility of the answer not being the best one and being different from the answer of another 1-ULP-accurate pow() function. 适当的实现将尝试精确到1ULP ,但这仍然留下答案不是最佳答案且与另一个1-ULP准确的pow()函数的答案不同的可能性。 Actually, several questions on this site are caused by pow() functions that are (in)accurate to more than 1ULP , however ugly it may be and despite the ginormous means available to the provider of the inaccurate function. 实际上,此站点上的几个问题是由pow()函数引起的,这些函数的准确性不超过1ULP ,但是尽管提供不准确的函数的方法繁多 ,但可能很丑陋。

So, in short: if you use pow() , the computations may differ between two compilers, although the compilers have the same implementation-defined characteristics (size of int, endianness, ...). 因此,简而言之:如果使用pow() ,则两个编译器之间的计算可能会有所不同,尽管这些编译器具有相同的实现定义的特征(int的大小,字节序,...)。 If you use pow() in a numerically instable computation, the end results may differ arbitrarily. 如果在数值不稳定的计算中使用pow() ,则最终结果可能会有所不同。

The usual solution for reproducible results is to provide your own pow() function. 可重现结果的通常解决方案是提供您自己的pow()函数。 However, if the difference is caused by numerically instable computations, this does not fix the root problem: the result, now reproducible, may still be meaningless. 但是,如果差异是由数值不稳定的计算引起的,则不能解决根本问题:现在可以重现的结果可能仍然没有意义。

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

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