如果两种语言都遵循IEEE 754,那么使用两种语言进行的计算是否会得出相同的答案? [英] If two languages follow IEEE 754, will calculations in both languages result in the same answers?

查看:83
本文介绍了如果两种语言都遵循IEEE 754,那么使用两种语言进行的计算是否会得出相同的答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将程序从Scilab代码转换为C ++.尤其是一个循环所产生的结果与原始Scilab代码略有不同(这是一长段代码,因此我不会在问题中包含它,但我会尽力在下面总结一下问题).

问题是,循环的每个步骤都使用上一步的计算.另外,计算之间的差异仅在第100,000次迭代(大约300,000次迭代)中才变得明显.

注意:我正在使用"format(25);"将C ++程序的输出与Scilab 5.5.2的输出进行比较.命令.意思是我正在比较25个有效数字.我还要指出的是,我理解在一定数量的位数之后如何无法保证精度,但是在评论之前请先阅读以下部分.到目前为止,两种语言之间的所有计算都相同,最多25位.

到目前为止,我尝试了以下尝试:

  1. 检查正在使用的数据类型:

我已经设法确认Scilab使用的是IEEE 754 double(根据语言文档).另外,根据Wikipedia的介绍,C ++不需要将IEEE 754用于double,但是据我所知,在C ++中使用double的任何地方,它都与Scilab的结果完全匹配.

  1. 检查先验功能的使用:

我还阅读了每位计算机科学家应该了解的有关浮动的知识,Point Arithmetic 表示IEEE不需要先验函数是完全舍入的.考虑到这一点,我比较了两种语言中这些函数(sin(),cos(),exp())的结果,结果也似乎是相同的(最多25位).

  1. 其他功能和预定义值的使用:

我重复了上述步骤,以使用sqrt()和pow().以及Pi的值(我在C ++中使用M_PI,在Scilab中使用%pi).再次,结果是相同的.

  1. 最后,我非常仔细地重写了循环,以确保两种语言之间的代码相同.


注:有趣的是,我注意到,对于所有上述计算,两种语言之间的结果匹配都比计算的实际结果远(浮点算术之外).例如:

使用Wolfram Alpha的sin(x)值= 0.123456789 .....

使用Scilab& amp;的sin(x)值C ++ = 0.12345yyyyy .....

即使使用Scilab或C ++计算的值一旦开始与实际结果(来自Wolfram)不同.每种语言的结果仍然相互匹配.这使我相信,大多数值是以相同的方式计算的(在两种语言之间).即使它们不是IEEE 754所要求的.


我最初的想法是上述前三点之一,两种语言的实现方式有所不同.但是据我所知,一切似乎都能产生相同的结果.

即使这些循环的所有输入都相同,结果是否也可能不同?可能是因为随着时间的推移发生了一个很小的错误(过去我只能看到25位数字)吗?如果是这样,我该如何解决此问题?

解决方案

不,编号系统的格式不能保证使用不同语言的功能提供的等效答案.

函数,例如 sin(x),可以使用相同的语言(以及不同的语言)以不同的方式实现. sin(x)函数就是一个很好的例子.许多实现将使用具有内插的查找表或查找表.这具有速度优势.但是,某些实现可能使用泰勒级数来评估函数.一些实现可以使用多项式来得出近似值.

具有相同的数字格式是解决语言之间的一个障碍.函数实现是另一个.

请记住,您还需要考虑平台.使用80位浮点处理器的程序与使用64位浮点软件实现的程序会有不同的结果.

I'm in the process of converting a program from Scilab code to C++. One loop in particular is producing a slightly different result than the original Scilab code (it's a long piece of code so I'm not going to include it in the question but I'll try my best to summarise the issue below).

The problem is, each step of the loop uses calculations from the previous step. Additionally, the difference between calculations only becomes apparent around the 100,000th iteration (out of approximately 300,000).

Note: I'm comparing the output of my C++ program with the outputs of Scilab 5.5.2 using the "format(25);" command. Meaning I'm comparing 25 significant digits. I'd also like to point out I understand how precision cannot be guaranteed after a certain number of bits but read the sections below before commenting. So far, all calculations have been identical up to 25 digits between the two languages.

In attempts to get to the bottom of this issue, so far I've tried:

  1. Examining the data type being used:

I've managed to confirm that Scilab is using IEEE 754 doubles (according to the language documentation). Also, according to Wikipedia, C++ isn't required to use IEEE 754 for doubles, but from what I can tell, everywhere I use a double in C++ it has perfectly match Scilab's results.

  1. Examining the use of transcendental functions:

I've also read from What Every Computer Scientist Should Know About Floating-Point Arithmetic that IEEE does not require transcendental functions to be exactly rounded. With that in mind, I've compared the results of these functions (sin(), cos(), exp()) in both languages and again, the results appear to be the same (up to 25 digits).

  1. The use of other functions and predefined values:

I repeated the above steps for the use of sqrt() and pow(). As well as the value of Pi (I'm using M_PI in C++ and %pi in Scilab). Again, the results were the same.

  1. Lastly, I've rewritten the loop (very carefully) in order to ensure that the code is identical between the two languages.


Note: Interestingly, I noticed that for all the above calculations the results between the two languages match farther than the actual result of the calculations (outside of floating point arithmetic). For example:

Value of sin(x) using Wolfram Alpha = 0.123456789.....

Value of sin(x) using Scilab & C++ = 0.12345yyyyy.....

Where even once the value computed using Scilab or C++ started to differ from the actual result (from Wolfram). Each language's result still matched each other. This leads me to believe that most of the values are being calculated (between the two languages) in the same way. Even though they're not required to by IEEE 754.


My original thinking was one of the first three points above are implemented differently between the two languages. But from what I can tell everything seems to produce identical results.

Is it possible that even though all the inputs to these loops are identical, the results can be different? Possibly because a very small error (past what I can see with 25 digits) is occurring that accumulates over time? If so, how can I go about fixing this issue?

解决方案

No, the format of the numbering system does not guarantee equivalent answers from functions in different languages.

Functions, such as sin(x), can be implemented in different ways, using the same language (as well as different languages). The sin(x) function is an excellent example. Many implementations will use a look-up table or look-up table with interpolation. This has speed advantages. However, some implementations may use a Taylor Series to evaluate the function. Some implementations may use polynomials to come up with a close approximation.

Having the same numeric format is one hurdle to solve between languages. Function implementation is another.

Remember, you need to consider the platform as well. A program that uses an 80-bit floating point processor will have different results than a program that uses a 64-bit floating point software implementation.

这篇关于如果两种语言都遵循IEEE 754,那么使用两种语言进行的计算是否会得出相同的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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