在c ++中的十六进制浮点文字 [英] Hexadecimal floating point literals in c++
问题描述
类似于:
$ b是否可以使用十六进制浮点数值初始化浮点变量? $ b
'0x011.1'//错!
不,C ++不支持文字,它不是标准的一部分。
不可移植的解决方案是使用一个编译器,将其作为扩展( GCC做到了这一点)。
一个便携式的解决方法是解析他们在运行时使用例如字符串文字 strtof()
或 strtod()
为 double
。
在评论中指出,你也可以选择将常量存储在C文件中。这样做要求您可以访问C99编译器,因为十六进制浮点文字是C99级功能。由于使用新的C ++编译器但没有C99编译器的环境(阅读:Visual Studio)是相当普遍的,所以这可能不是一个可行的解决方案。
(C++) Is it possible to initialize a float variable with a hexademical float point value?
something like so:
'0x011.1' // wrong!
No, C++ doesn't support that for literals, it's not part of the standard.
A non-portable solution is to use a compiler that adds this as an extension (GCC does this).
A portable workaround is to parse them from string literals at runtime using e.g. strtof()
or strtod()
for double
.
As pointed out in a comment, you can also opt to store the constants in a C file. Doing so requires that you have access to a C99 compiler though, since hex float literals is a C99-level feature. Since environments with a new C++ compiler but without a C99 compiler (read: Visual Studio) are quite common, that might not be a workable solution.
这篇关于在c ++中的十六进制浮点文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!