[英]C++ declare platform independent 32-bit float
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 for floats(qreal根据平台改变大小)。
You're using Qt. 你正在使用Qt。 which is specific to C++, so I'm not sure why the question is tagged C. 这是特定于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位float
的Cray向量机,那么你不会有32位浮点类型(除非你自己在软件中实现它,但我不是看看这将是多么有用)。
There is no <stdfloat.h>
/ <cstdfloat>
corresponding to <stdint.h>
/ <cstdint>
. 没有<stdfloat.h>
/ <cstdfloat>
对应于<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. 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. 您可能想重新考虑是否确实需要32位浮点类型。 If your code were running on a system with 64-bit float
, how would that fail to meet your requirements? 如果您的代码在具有64位float
的系统上运行,那么它将如何满足您的要求?
There isn't such a sized floating point type, but you can always statically assert that the size is 32 bits. 没有这种大小的浮点类型,但您可以始终静态断言大小为32位。 Maybe even something as simple as a global char array: 甚至可能像全局char数组一样简单:
#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位,则通过声明负大小的数组将无法编译。
Most common implementations of C and C++ will use 32 bit float
types. C和C ++的最常见实现将使用32位float
类型。 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: 如果你真的需要捕获任何不会出现这种情况的平台,你可以使用以下命令在程序的早期抛出错误:
#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. 不幸的是我不知道在编译时检测它的方法,我的早期答案是有缺陷的。
On a platfrom that supports IEEE-754 float
it's going to be 32 bit. 在支持IEEE-754 float
的平台上,它将是32位。 On platforms that don't, different width will probably be the least of your problems. 在没有的平台上,不同宽度可能是您遇到的问题中最小的。 Bottom line - use float
and don't worry. 底线 - 使用float
,不用担心。
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. 当且仅当float
符合IEC 559 / IEEE 754浮点标准时,布尔静态成员常量std::numeric_limits<float>::is_iec559
为真。 You'll need to #include
the header <limits>
. 你需要#include
标题<limits>
。
Note that this alone doesn't tell you whether or not float
is specifically 32-bit IEEE 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浮点。
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 binary32 which is commonly implemented. IEEE 758定义了通常实现的众所周知的32位二进制 32。 But IEEE 758 also defines a 32-bit decimal32 , which is primarily use for storage and not computation. 但IEEE 758还定义了一个32位十进制 32,主要用于存储而不是计算。 There also exists other non-IEEE 758 32-bit floating point implementations, though certainly not as common. 还存在其他非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. 即使已知float
是32位IEEE 758-2008 binary32 ,给定环境遵守其严格定义(Nan,subnormals,舍入模式)的程度也可能不同。 This I suspect is the greatest source of subtle variation of 32-float implementations. 我怀疑这是32-float实现的微妙变化的最大来源。
Thus the goal of knowing precisely the floating point model used, likely to gain insure consistency across platforms, may be very challenging to detect at compile time. 因此, 精确地了解所使用的浮点模型的目标,可能获得跨平台的一致性,在编译时检测可能非常具有挑战性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.