<分区>
在 C# 中,double 类型的最大值为:1.79769313486232E+308。
但是,在 C++ 中,double 类型的最大值为:1.79769e+308。
这意味着 C++ 程序(使用 strtod 函数)不能总是解析 C# 输出的 double 类型值。
是否有针对此行为的任何解释以及处理该问题的任何好的解决方案?
<分区>
在 C# 中,double 类型的最大值为:1.79769313486232E+308。
但是,在 C++ 中,double 类型的最大值为:1.79769e+308。
这意味着 C++ 程序(使用 strtod 函数)不能总是解析 C# 输出的 double 类型值。
是否有针对此行为的任何解释以及处理该问题的任何好的解决方案?
最佳答案
来自 C++ / C# differences with float and double
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.
更多信息 Precision and Accuracy in Floating-Point Calculations
引用 Eric Lippert
section 4.1.6 of the C# specification, which begins **Floating-point operations may be performed with higher precision than the result type of the operation. For example, some hardware architectures support an “extended” or “long double” floating-point type with greater range and precision than the double type, and implicitly perform all floating-point operations using this higher precision type. ... ** See the spec for more details.
关于c# - 为什么 C++ 和 C# 对 double 类型有不同的最大值的解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49187441/