简体   繁体   English

C++ 浮点数差异

[英]C++ Floating point difference

I am trying to get exact same results on 2 different platform ie Solaris and Linux(sun-studio vs GCC).我试图在 2 个不同的平台上获得完全相同的结果,即 Solaris 和 Linux(sun-studio vs GCC)。 The entire code uses double datatype.整个代码使用双数据类型。 But when i print the output i see differences in the floating point(like in the 20th decimal place).但是当我打印输出时,我看到浮点数的差异(如小数点后 20 位)。

What i need to know is whether i can set some compiler flag to make both GCC and sun-studio compiler behave equally.我需要知道的是我是否可以设置一些编译器标志来使 GCC 和 sun-studio 编译器的行为相同。 Attached Image shows difference in Double datatype.附加图像显示了 Double 数据类型的差异。 Left side is the Output from GCC and other one is from sun-studio.左侧是 GCC 的输出,另一侧是 sun-studio。

在此处输入图片说明

The main issue comes from this :主要问题来自于此:

I am trying to get exact same results on 2 different platform我试图在 2 个不同的平台上获得完全相同的结果

You are not guaranteed that you will have the same results on two different platforms.您不能保证在两个不同的平台上获得相同的结果。 They may differ on so many ways on how they implement floating operations.他们在实施浮动操作的方式上可能有很多不同。

One solution to your problem: fixed-point arithmetic.您的问题的一种解决方案:定点算术。

Also, take a look at these answers :另外,看看这些答案:

Cross Platform Floating Point Consistency 跨平台浮点一致性

Are IEEE float and double guaranteed to be the same size on any OS? IEEE float 和 double 是否保证在任何操作系统上都具有相同的大小?

They are a quite good start for you to understanding what is going on.它们是您了解正在发生的事情的一个很好的开始。

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

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