关于长双
,草案说:
3.9.1基本类型
8 There are three floating point types: float, double, and long double. The type double provides at least
as much precision as float, and the type long double provides at least as much precision as double.
The set of values of the type float is a subset of the set of values of the type double; the set of values
of the type double is a subset of the set of values of the type long double. The value representation of
floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic
types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum
and minimum values of each arithmetic type for an implementation.
至于促销,这是下一个最有趣的部分:
4.6浮点推广
1 A prvalue of type float can be converted to a prvalue of type
double. The value is unchanged.
2 This conversion is called floating
point promotion.
请注意,没有关于double
到long double
的任何说明。不过,我冒着滑倒的风险。
接下来是转换,当你从long double
转换为double
时,我们感兴趣的是:
4.8浮点转换
1 A prvalue of floating point type can be converted to a prvalue of
another floating point type. If the source value can be exactly
represented in the destination type, the result of the conversion is
that exact representation. If the source value is between two adjacent
destination values, the result of the conversion is an
implementation-defined choice of either of those values. Otherwise,
the behavior is undefined.
2 The conversions allowed as floating point
promotions are excluded from the set of floating point conversions.
现在,让我们看看变窄的效果:
6。窄化转换是一种隐式转换
[...]
- from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after
conversion is within the range of values that can be represented (even
if it cannot be represented exactly)
从所有这些标准中可以得出两个结论:
- Combining the bit about narrowing with the bit about implementation defined conversions there may be changes in your results across platforms.
- If your intermediate results (considering multiple such results) in
long double
are in a range that cannot be represented accurately by a double
(high or low), these can accumulate to return a different final result which you will want to return back as a double
.
至于哪个更准确,我认为这完全取决于你的应用程序。