为什么 C++ 和 C# 对双精度类型有不同的最大值的解释?
Explanation of why C++ and C# have a different maximum value for type 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# 中,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.