C++ 声明平台无关的 32 位浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18705192/
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
C++ declare platform independent 32-bit float
提问by Ilya Kobelevskiy
Is there a way to declare 32-bit floating point value in C++ - ensuring that it will always be 32 bits regardless of platform/compiler?
有没有办法在 C++ 中声明 32 位浮点值 - 确保无论平台/编译器如何,它始终是 32 位?
I can do that for integers like that:
我可以为这样的整数做到这一点:
#include <stdint.h>
uint32_t var; //32 bit unsigned integer
uint64_t var1; //64 bit unsigned integer
is there a way to do something like that for floats? As far as I know,
有没有办法为浮动做类似的事情?据我所知,
float var; //Usually is 32 bit, but NOT GUARANTEED to be 32 bit
is implementation specific, and is not necessarily 32 bit.. (Correct me if I am wrong).
是特定于实现的,不一定是 32 位..(如果我错了,请纠正我)。
I am using qt, so if there is any solution using it I would accept it - I couldn't find anything like quint16 for floats (qreal changes size depending on platform).
我正在使用 qt,所以如果有任何使用它的解决方案,我会接受它 - 我找不到像 quint16 这样的浮点数(qreal 根据平台改变大小)。
采纳答案by Keith Thompson
You're using Qt. which is specific to C++, so I'm not sure why the question is tagged C.
您正在使用 Qt。这是特定于 C++ 的,所以我不确定为什么这个问题被标记为 C。
As far as I know, on all platforms where Qt is supported, type float
is 32-bit IEEE.
据我所知,在所有支持 Qt 的平台上,类型float
都是 32 位 IEEE。
If somebody decides to port Qt to, say, a Cray vector machine with 64-bit float
, you're not going to have a 32-bit floating-point type anyway (unless you implement it yourself in software, but I don't see how that would be useful).
如果有人决定将 Qt 移植到具有 64 位的 Cray 向量机float
,那么无论如何您都不会拥有 32 位浮点类型(除非您自己在软件中实现它,但我看不到这将如何有用)。
There is no <stdfloat.h>
/ <cstdfloat>
corresponding to <stdint.h>
/ <cstdint>
. C and C++ provide float
, double
, and long double
, and imposes minimal requirements on them, but doesn't give you a way to ask for a floating-point type of any specific size.
没有<stdfloat.h>
/<cstdfloat>
对应于<stdint.h>
/ <cstdint>
。C 和 C++ 提供float
、double
、 和long double
,并对它们提出了最低要求,但没有提供一种方法来要求任何特定大小的浮点类型。
Your best bet is probably to add something like this in main
, or in some function that's guaranteed to be called during program startup:
您最好的选择可能是在main
中或在保证在程序启动期间调用的某些函数中添加类似的内容:
assert(CHAR_BIT * sizeof (float) == 32);
and perhaps CHAR_BIT == 8
as well if your code implicitly depends on that.
CHAR_BIT == 8
如果您的代码隐含地依赖于它,也许也是如此。
It's unlikely that the assert
will ever fire -- and if it does, you've got bigger problems.
不太可能assert
会发生火灾——如果真的发生了,你就会遇到更大的问题。
You might want to reconsider whether you really need a 32-bit floating type. If your code were running on a system with 64-bit float
, how would that fail to meet your requirements?
您可能需要重新考虑是否真的需要 32 位浮点类型。如果您的代码在 64 位的系统上运行float
,怎么会无法满足您的要求?
回答by Mark B
There isn't such a sized floating point type, but you can always statically assert that the size is 32 bits. Maybe even something as simple as a global char array:
没有这样大小的浮点类型,但您始终可以静态断言大小为 32 位。甚至可能像全局字符数组一样简单:
#include <climits>
char static_assert_float32[1 - (2 * ((sizeof(float) * CHAR_BIT) != 32))];
This will fail to compile if float
is not 32 bits by declaring an array of negative size.
如果float
通过声明负大小的数组不是 32 位,这将无法编译。
回答by Mark Ransom
Most common implementations of C and C++ will use 32 bit float
types. If you really need to catch any platforms where that won't be the case you can use the following to throw an error early in the program:
C 和 C++ 的最常见实现将使用 32 位float
类型。如果您真的需要捕获任何不会出现这种情况的平台,您可以使用以下内容在程序早期抛出错误:
#include <limits.h>
if (sizeof(float) * CHAR_BIT != 32)
// error code here
Unfortunately I don't know of a way to detect it at compile time, my earlier answer was flawed.
不幸的是,我不知道在编译时检测它的方法,我之前的回答是有缺陷的。
回答by Violet Giraffe
On a platfrom that supports IEEE-754 float
it's going to be 32 bit. On platforms that don't, different width will probably be the least of your problems. Bottom line - use float
and don't worry.
在支持 IEEE-754 的平台上,float
它将是 32 位的。在没有的平台上,不同的宽度可能是你的问题中最少的。底线 - 使用float
,别担心。
回答by user3405743
The Boolean static member constant std::numeric_limits<float>::is_iec559
is true if and only if float
conforms to the IEC 559/IEEE 754 floating-point standard. You'll need to #include
the header <limits>
.
std::numeric_limits<float>::is_iec559
当且仅当float
符合 IEC 559/IEEE 754 浮点标准时,布尔静态成员常量才为真。你需要#include
标题<limits>
。
Note that this alone doesn't tell you whether or not float
is specifically 32-bitIEEE floating point, just that it is IEEE floating point -- some implementation without 32-bit floating point hardware might, for example, choose float
be 64-bit IEEE floating point.
请注意,仅凭这一点并不能告诉您是否float
特别是32 位IEEE 浮点,只是它是 IEEE 浮点——例如,某些没有 32 位浮点硬件的实现可能会选择float
64 位 IEEE浮点。
回答by chux - Reinstate Monica
Knowing that the sizeof(float) is 32 bits may be sufficient, in general, but insufficient in all cases.
一般来说,知道 sizeof(float) 是 32 位可能就足够了,但在所有情况下都不够。
IEEE 758 defines the well known 32-bit binary32which is commonly implemented. But IEEE 758 also defines a 32-bit decimal32, which is primarily use for storage and not computation. There also exists other non-IEEE 758 32-bit floating point implementations, though certainly not as common.
IEEE 758 定义了众所周知的 32 位二进制32 ,它通常被实现。但是 IEEE 758 还定义了一个 32 位的decimal32,它主要用于存储而不是计算。还有其他非 IEEE 758 32 位浮点实现,尽管肯定不常见。
Even if it is known that float
is 32-bit IEEE 758-2008 binary32, the degree which a given environment adheres to its strict definitions (Nan, subnormals, rounding modes) may vary. This I suspect is the greatest source of subtle variation of 32-float implementations.
即使已知它float
是 32 位 IEEE 758-2008 binary32,给定环境遵守其严格定义(Nan、次正规、舍入模式)的程度可能会有所不同。我怀疑这是 32-float 实现细微变化的最大来源。
Thus the goal of knowing preciselythe floating point model used, likely to gain insure consistency across platforms, may be very challenging to detect at compile time.
因此,准确了解所使用的浮点模型的目标可能会确保跨平台的一致性,但在编译时检测可能非常具有挑战性。