C ++ / C#与float和double的区别 [英] C++ / C# differences with float and double

查看:452
本文介绍了C ++ / C#与float和double的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将C ++数学库转换为C#。该库混用了float和double的用法(有时在它们之间进行转换),我们试图做同样的事情,以便在C#中获得与在C ++中完全相同的结果,但是事实证明这是非常困难的,即使不是不可能

We are converting a C++ math library to C#. The library mixes the use of floats and doubles (casting between them sometimes) and we are trying to do the same, in order to get the exact same results in C# that we had in C++ but it is proving to be very difficult if not impossible.

我认为问题是以下一项或多项,但我不是专家:

I think the problem is one or more of the following, but I am not an expert:


  1. 将浮点数转换为double并将double转换为float会导致不可预测的结果,并且在C ++和C#中会做不同的操作

  1. Converting floats to double and double to floats is causing unpredictable results and done differently in C++ and C#

C ++和C#处理浮点精度的方式不同,并且它们不能互相模仿。

C++ and C# handle float precision differently, and they can't mimic each other

.NET中有一个设置可以使它像C ++一样运行,但是我可以找不到它(都是32位的)

There is a setting somewhere in .NET to make it perform like C++, but I can't find it (both are 32-bit)

有人可以向我解释可能的问题,也许可以将我链接到我可以使用Microsoft的一些权威文档来解释情况和差异的原因吗?

Can somebody explain to me the possible problems and maybe link me to some authoritative documentation from Microsoft I can use to help explain the situation and the reason for the differences?

EDIT

我们正在使用VC6和.NET4.0

EDIT
We are using VC6 and .NET4.0

由于存在NDA,我无法提供计算示例,但我可以为差异显示一些数字...可能非常无用

I can't give examples of the calculations, because of an NDA, but I can show some numbers for the differences... probably very useless by themselves:

 8.085004000000000 (C#) vs. 
 8.084980000000000 (C++)    

 8.848165000000000 (C#) vs. 
 8.848170000000000 (C++)   

 0.015263214111328 (C#) vs. 
 0.015263900756836 (C++)  

应注意,这些数字包括复杂的问题。这些是计算的结果。

It should be noted that these numbers include compounded problems. These are the results of calculations.

推荐答案

C ++允许程序为临时结果保留比子表达式类型更高的精度。暗示。可能发生的一件事是,中间表达式(或它们的未指定子集)被计算为扩展的80位浮点数。

C++ allows the program to retain a higher precision for temporary results than the type of the subexpressions would imply. One thing that can happen is that intermediate expressions (or an unspecified subset of them) are computed as extended 80-bit floats.

另一方面,如果这适用于C#,但即使适用于此,C#编译器也不必选择相同的表达式子集来计算为80位扩展浮点数。编辑:请参见下面的Eric评论。

I would be surprised on the other hand if this applied to C#, but even if it does, the C# compiler doesn't have to choose the same subset of expression to compute as 80-bit extended floats. See Eric's comment below.

更多详细信息

同一中间精度问题的另一个实例是编译器使用 fmadd 指令执行什么操作时在源代码中进行乘法运算后再进行加法运算(如果目标体系结构具有该乘积,例如PowerPC)。 fmadd 指令可精确计算其中间结果,而正常的加法运算将舍入中间结果。

Another instance of the same intermediate precision problem is when the compiler uses the fmadd instruction for what is a multiplication followed by an addition in the source code (if the target architecture has it—for instance, PowerPC). The fmadd instruction computes its intermediate result exactly, whereas a normal addition would round the intermediate result.

通过C ++编译器,您只需要使用易失性变量将浮点计算编写为三地址代码即可获得中间结果。如果此转换更改了C ++程序的结果,则意味着存在上述问题。但是随后您更改了C ++端的结果。如果不读取生成的程序集,可能无法在C#中获得完全相同的旧C ++结果。

To prevent the C++ compiler from doing that, you should only need to write floating-point computations as three-address code using volatile variables for intermediate results. If this transformation changes the result of the C++ program, it means that the above problem was at play. But then you have changed the C++-side results. There is probably no way to get the exact same old C++ results in C# without reading the generated assembly.

如果它有点旧,则C ++编译器也可能会优化浮动点计算,当它们不相关时,就好像它们是关联的。您对此无能为力。只是不对三地址代码转换将再次阻止编译器应用它,但是仍然没有简单的方法来使C#编译器重现旧的C ++结果。

If it's a little bit old, your C++ compiler may also optimize floating-point computations as if they were associative when they are not. There is not much you can do about that. It's just incorrect. The three-address code transformation would again prevent the compiler from applying it, but again there is no simple way to get the C# compiler to reproduce the old C++ results.

这篇关于C ++ / C#与float和double的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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