views:

325

answers:

4

Why isn't 0f treated as a floating point literal in C++?

#include <iostream>

using namespace std;

int main(){
  cout << 0f << endl;

  return 0;
}

Compiling the above gives me

C2509 (syntax error: 'bad suffix on number')

using VS2008.

+14  A: 

Because the 0 is an integer constant.

edit: The error message given by codepad.org (assume g++) may be a little easier to understand. "error: invalid suffix "f" on integer constant". A "0.f" will work because 0. (or 0.0, same thing) is a decimal constant, and asking for a decimal constant to be a float makes more sense than asking for an integer constant to be a float :)

Dakota Hawkins
@Dakota, That's precisely why I added the 'f' suffix in order to force the compiler to treat it as a float.
Vulcan Eager
f can only be applied to a double literal (such as 0.0), and 0 is an int literal.
Etienne de Martel
@Etienne, and the question is why?
Vulcan Eager
Eh, because the standard says so? That's a crappy answer, I know, but I can't think of another reason.
Etienne de Martel
Then I suppose your answer is "because that's the way it works". The compiler treats integer constants and decimal constants differently. A 0.0 by itself is going to be a double, so you can add the 'f' to make it a float. A 0 by itself is going to be an integer, and the floating point suffixes wouldn't apply. Similarly, "0.0UL" probably won't compile.
Dakota Hawkins
@Vulcan Eager: Does it really matter why? it matters in as much as asking why to index an array you use [] and not <> or why the entry point for your program is a function called `main` and not `StartHere`. There is no why beyond *'cos the standard says so*
David Rodríguez - dribeas
+10  A: 

Because you need 0.0f.

Oli Charlesworth
I know that 0.0f silences the compiler. But that still doesn't explain why the 'f' suffix alone won't do the trick.
Vulcan Eager
@Vulcan: Presumably, "just because". Basically, I echo David Thornely's sentiments above.
Oli Charlesworth
+12  A: 

Assuming grammar used by C++ for floating point constants is same as that for C (which I think is true), we have:

Definitions of some shortcuts taken from ANSI C grammar

D      [0-9]
L      [a-zA-Z_]
H      [a-fA-F0-9]
E      [Ee][+-]?{D}+
FS     (f|F|l|L)
IS     (u|U|l|L)*

Now the f or F you see at the end of the floating points is in defined in FS above.

Now lets see the grammar to recognize valid floating point constants:

{D}+{E}{FS}?        
{D}*"."{D}+({E})?{FS}?  
{D}+"."{D}*({E})?{FS}?  

Now if you see carefully there is no rule that would identify 0f.

Using rule1 we can have 0e0f

Using rule2 we can have .0f or 0.0f

Using rule3 we can have 0.f or 0.0f

What actually happen in your case is the 0 of 0f will be consumed by the lexical analyzer as an integer constant D and the f will be consumed as the FS token. Now when the parse sees a D followed by FS for which there is no matching rule, it spits out the error:

error: invalid suffix "f" on integer constant
codaddict
+3  A: 

If there was an explicitly stated reason for this design decision, it would be in the C99 "Rationale" document (C++ copied all this stuff verbatim from C without reconsidering it). But there isn't. This is everything that's said about the 'f' suffix:

§6.4.4.2 Floating constants

Consistent with existing practice, a floating-point constant is defined to have type double. Since C89 allows expressions that contain only float operands to be performed in float arithmetic rather than double, a method of expressing explicit float constants is desirable. The long double type raises similar issues.

The F and L suffixes have been added to convey type information with floating constants, much like the L suffix does for long integers. The default type of floating constants remains double for compatibility with prior practice. Lower-case f and l are also allowed as suffixes.

There is an implied reason, though. Note the wording: "the ... suffixes have been added to convey type information with floating constants." The authors of the standard were thinking of numeric constants as already being unambiguously either integer or floating point by the time you get to the suffix. The suffix is only for extra specificity within the category, it can't flip a number from one category to another. This is backed up by the actual grammar (C99 §6.4.4) which first defines numeric constants as being either integer-constants or floating-constants, and then defines separate classes of suffixes for each.

Zack
+1. This is the only answer that really provides an inkling of the likely rationale behind this decision.
Brian