Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VC rejecting hexadecimal floating-point constant

I'm writing a program which converts CIL bytecode to C source code for machine consumption. 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.

I decided to try it out, but MS VC9 gives me errors no matter what I try. 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:

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.

Am I doing something wrong here, or is VC9 just not standards compliant?

like image 868
Kevin Avatar asked Aug 12 '13 05:08

Kevin


2 Answers

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).

like image 56
caf Avatar answered Nov 05 '22 02:11

caf


C++17 standard added full support of C99 hexadecimal floating-point literals. Visual C++ will have them available in Visual Studio 2017 15.6 release.

like image 25
solodon Avatar answered Nov 05 '22 00:11

solodon