VC rejecting hexadecimal floating-point constant
Asked Answered
E

2

5

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?

Etch answered 12/8, 2013 at 5:32 Comment(0)
F
8

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

Fortalice answered 12/8, 2013 at 5:36 Comment(1)
Thank you for the confirmation; I was afraid it was something like that. I guess I'll have to add a switch to output decimal scientific notation for pre-C99 compilers.Etch
T
1

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.

Tyrelltyrian answered 16/6, 2017 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.