C++ 为什么没有无符号浮点类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3589663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why no unsigned floating point types?
提问by Chubsdad
Possible Duplicate:
Why doesn't C have unsigned floats?
可能的重复:
为什么 C 没有无符号浮点数?
The question is probably very basic and probably answered many time earlier, but I want to understand why C++ does not have unsigned floating point types, even though floating point literals can be signed or unsigned.
这个问题可能是非常基本的,并且可能已经回答了很多次,但我想了解为什么 C++ 没有无符号浮点类型,即使浮点文字可以是有符号或无符号的。
$3.9.1/8- "There are three floating point types: float, double, and long double."
$3.9.1/8- “有三种浮点类型:float、double 和 long double。”
回答by AnT
Unsigned integer types have two important properties that differentiate them from signed integer types: "shifted" range (no negative subrange, but positive subrange twice as wide) and modulo arithmetic. For integer types these properties are important enough to justify the existence of unsigned types.
无符号整数类型有两个重要的属性,可以将它们与有符号整数类型区分开来:“移位”范围(没有负子范围,但正子范围宽两倍)和模运算。对于整数类型,这些属性非常重要,足以证明无符号类型的存在。
With floating-types neither of these properties are immediately applicable. With floating-point types the main issue is not in their range (for many purposes it can be thought of as virtually infinite), but rather in precision. And modulo arithmetic is not naturally applicable to non-integer types. For this reason, it didn't make much sense to introduce unsigned floating-point types, i.e. it didn't make much sense to flip-flop the role of just one bit in the floating-point representation.
对于浮动类型,这些属性都不是立即适用的。对于浮点类型,主要问题不在于它们的范围(出于许多目的,它可以被认为几乎是无限的),而在于精度。并且模运算自然不适用于非整数类型。出于这个原因,引入无符号浮点类型没有多大意义,即触发浮点表示中只有一位的角色没有多大意义。
It should also be noted that the above reasoning should probably be used as rationale behind introducing unsigned integer types (and not introducing unsigned floating-point types) in popular hardware and corresponding hardware-derived standards. What we have in C and C++ was essentially inherited from the hardware capabilities and these standards.
还应该注意的是,上述推理可能应该用作在流行的硬件和相应的硬件派生标准中引入无符号整数类型(而不是引入无符号浮点类型)的基本原理。我们在 C 和 C++ 中所拥有的本质上是从硬件功能和这些标准中继承而来的。
Of course, from the conceptual point if view, it would be quite logical to have unsigned floating-point types in the language, just for the sake of consistency. But, alas they are not there.
当然,从概念的角度来看,在语言中使用无符号浮点类型是非常合乎逻辑的,只是为了一致性。但是,唉,他们不在那里。
回答by Kornel Kisielewicz
All floating points are signed. C++ follows the IEEE 754standard, which is the most common hardware implementation and following it, floats are always signed.
所有浮点数都是有符号的。C++ 遵循IEEE 754标准,这是最常见的硬件实现,并且遵循它,浮点数总是有符号的。
As the floats already take up at least 32 bits, the gain of having a software implementation that would regain that 1 bit is insignificant compared to the usefullness of such an implementation.
由于浮点数已经占用了至少 32 位,与这种实现的有用性相比,拥有重新获得 1 位的软件实现的增益是微不足道的。