简体   繁体   English

VC拒绝十六进制浮点常数

[英]VC rejecting hexadecimal floating-point constant

I'm writing a program which converts CIL bytecode to C source code for machine consumption. 我正在编写一个程序,它将CIL字节码转换为C源代码以供机器使用。 I was concerned about inaccuracy in floating-point constants due to conversion to and from decimal. 我担心由于转换为十进制和从十进制转换而导致的浮点常量不准确。 After doing some research, I discovered that C (but not C++) is supposed to be able to accept a hexadecimal notation for floating-point constants. 在做了一些研究之后,我发现C(但不是C ++)应该能够接受浮点常量的十六进制表示法。

I decided to try it out, but MS VC9 gives me errors no matter what I try. 我决定尝试一下,但无论我尝试什么,MS VC9都会给我错误。 Here is what I'm trying: 这是我正在尝试的:

// Switches: /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /FD /MDd /Fo"Debug\\" /Fd"Debug\vc90.pdb" /W3 /nologo /c /ZI /TC

#include <tchar.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    double d = 0x1.0p+1;    // error C2059
    //double d = 0x1p+1;    // doesn't work either
    //double d = 0x1p1;     // doesn't work either
    //double d = 0x1.0p1;   // doesn't work either

    printf( "%f\n", d );

    return 0;
}

I expected this to print out 2, from 1x2^1, but instead it gives me this compiler error: 我希望这打印2,从1x2 ^ 1,但它给了我这个编译错误:

error C2059: syntax error : 'bad suffix on number'

I realize C++ doesn't support this syntax (or so I've read,) but notice this is compiled with /TC so it should be straight C, and I used a *.c filename for good measure also. 我意识到C ++不支持这种语法(或者我已经读过了),但是请注意这是用/TC编译的所以它应该是直接的C,并且我也使用了*.c文件名。

Am I doing something wrong here, or is VC9 just not standards compliant? 我在这里做错了,还是VC9不符合标准?

There's nothing wrong with your code. 您的代码没有任何问题。 Floating-point hexadecimal constants were added to C in the C99 standard, but MSVC only supports the older C90 standard (with some extensions, like // single-line comments and the long long type). 在C99标准中,浮点十六进制常量被添加到C中,但MSVC仅支持较旧的C90标准(带有一些扩展,如//单行注释和long long类型)。

C++17 standard added full support of C99 hexadecimal floating-point literals. C ++ 17标准增加了对C99十六进制浮点文字的完全支持。 Visual C++ will have them available in Visual Studio 2017 15.6 release. Visual C ++将在Visual Studio 2017 15.6版本中提供它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM