Why isn't 0f treated as a floating point literal in C++?
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
typedouble
. Since C89 allows expressions that contain onlyfloat
operands
to be performed infloat
arithmetic rather thandouble
, a method of
expressing explicitfloat
constants is desirable. Thelong double
type
raises similar issues.The
F
andL
suffixes have been added to convey type information with
floating constants, much like theL
suffix does for long integers. The default
type of floating constants remains double for compatibility with prior practice.
Lower-casef
andl
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.
What is the significance of 0.0f when initializing (in C)?
float x = 0 has an implicit typecast from int to float.
float x = 0.0f does not have such a typecast.
float x = 0.0 has an implicit typecast from double to float.
Depending on the compiler, implicit typecast can require the compiler to generate extra code.
The type of a floating point literal with exponent
By default, all floating point literals, with or without an exponent part, have type double
. You can add the f
suffix to make the type float
or L
to make the type long double
.
In the case of float f = 123456e-3;
, you're initializing a float
with a double
constant, so there is the possibility of loss of precision, however this particular constant only has 6 decimal digits of precision so it should be OK.
Why is the f required when declaring floats?
Your declaration of a float contains two parts:
- It declares that the variable
timeRemaining
is of typefloat
. - It assigns the value
0.58
to this variable.
The problem occurs in part 2.
The right-hand side is evaluated on its own. According to the C# specification, a number containing a decimal point that doesn't have a suffix is interpreted as a double
.
So we now have a double
value that we want to assign to a variable of type float
. In order to do this, there must be an implicit conversion from double
to float
. There is no such conversion, because you may (and in this case do) lose information in the conversion.
The reason is that the value used by the compiler isn't really 0.58, but the floating-point value closest to 0.58, which is 0.57999999999999978655962351581366... for double
and exactly 0.579999946057796478271484375 for float
.
Strictly speaking, the f
is not required. You can avoid having to use the f
suffix by casting the value to a float
:
float timeRemaining = (float)0.58;
How can the suffix for numeric literals long int and long double both be l/L?
A literal number needs to have a period or an exponent to be treated as a floating point literal constant. If it doesn't have any of these, it is treated as an integer literal constant.
f after number
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 50.0f);
uses float constants. (The constant 0.0 usually declares a double in Objective-C; putting an f on the end - 0.0f - declares the constant as a (32-bit) float.)
CGRect frame = CGRectMake(0, 0, 320, 50);
uses ints which will be automatically converted to floats.
In this case, there's no (practical) difference between the two.
Why is the letter f used at the end of a float no.?
The f
indicates it's a floating point literal, not a double literal (which it would implicitly be otherwise.) It hasn't got a particular technical name that I know of - I tend to call it the "letter suffix" if I need to refer to it specifically, though that's somewhat arbitrary!
For instance:
float f = 3.14f; //Compiles
float f = 3.14; //Doesn't compile, because you're trying to put a double literal in a float without a cast.
You could of course do:
float f = (float)3.14;
...which accomplishes near enough the same thing, but the F is a neater, more concise way of showing it.
Why was double chosen as the default rather than float? Well, these days the memory requirements of a double over a float aren't an issue in 99% of cases, and the extra accuracy they provide is beneficial in a lot of cases - so you could argue that's the sensible default.
Note that you can explicitly show a decimal literal as a double by putting a d
at the end also:
double d = 3.14d;
...but because it's a double value anyway, this has no effect. Some people might argue for it advocating it's clearer what literal type you mean, but personally I think it just clutters code (unless perhaps you have a lot of float literals hanging around and you want to emphasise that this literal is indeed meant to be a double, and the omission of the f isn't just a bug.)
Related Topics
Getting Gdb to Save a List of Breakpoints
Correct Way to Define C++ Namespace Methods in .Cpp File
Protected Data in Parent Class Not Available in Child Class
Will My iPhone App Take a Performance Hit If I Use Objective-C for Low Level Code
Calling Constructors in C++ Without New
Reasonably Portable Way to Get Top 64-Bits from 64X64 Bit Multiply
Is Using an Union in Place of a Cast Well Defined
How to Declare Std::Unique_Ptr and What Is the Use of It
How to Get the Current Windows System-Wide Timer Resolution
What Does Iterator->Second Mean
Bstr to Std::String (Std::Wstring) and Vice Versa
Simple String Parsing with C++
Regarding C++ Include Another Class
Is It a Good Practice to Always Use Smart Pointers
How to Print Stack Trace for Caught Exceptions in C++ & Code Injection in C++
What Does It Mean to "Poison a Function" in C++