71

I saw 1/3.f in a program and wondered what the .f was for. So tried my own program:

#include <iostream> int main() { std::cout << (float) 1/3 << std::endl; std::cout << 1/3.f << std::endl; std::cout << 1/3 << std::endl; } 

Is the .f used like a cast? Is there a place where I can read more about this interesting syntax?

3
  • 26
    +1 for trying your own program and building a hypothesis. That said, you'll probably want to make sure you're learning from a book, it's the best way to learn a new language. Commented Jan 28, 2011 at 12:41
  • 2
    What was the output of the program? I could do it myself but I think that would have been nice to include too Commented Sep 8, 2013 at 20:51
  • It isn't a .f. It is 3. followed by f. Commented Mar 29, 2020 at 9:23

5 Answers 5

76

3. is equivalent to 3.0, it's a double.

f following a number literal makes it a float.

Sign up to request clarification or add additional context in comments.

Comments

59

Without the .f the number gets interpreted as an integer, hence 1/3 is (int)1/(int)3 => (int)0 instead of the desired (float)0.333333. The .f tells the compiler to interpret the literal as a floating point number of type float. There are other such constructs such as for example 0UL which means a (unsigned long)0, whereas a plain 0 would be an (int)0.

The .f is actually two components, the . which indicates that the literal is a floating point number rather than an integer, and the f suffix which tells the compiler the literal should be of type float rather than the default double type used for floating point literals.

Disclaimer; the "cast construct" used in the above explanation is not an actual cast, but just a way to indicate the type of the literal.

If you want to know all about literals and the suffixes you can use in them, you can read the C++ standard, (1997 draft, C++11 draft, C++14 draft, C++17 draft, C++20 draft, C++23 draft) or alternatively, have a look at a decent textbook, such as Stroustrup's The C++ Programming Language.

As an aside, in your example (float)1/3 the literals 1 and 3 are actually integers, but the 1 is first cast to a float by your cast, then subsequently the 3 gets implicitly cast to a float because it is a righthand operand of a floating point operator. (The operator is floating point because its lefthand operand is floating point.)

3 Comments

@Nav, if you want to know all about literals you can read the standard (1997 draft available at open-std.org/jtc1/sc22/open/n2356/lex.html#lex.literal) Alternatively, have a look at a decent textbook, such as Stroustrup's The C++ Programming Language www2.research.att.com/~bs/3rd.html
The .f is actually 3. followed by f. There is certainly no such thing as .f anywhere in the syntax for C++.
cppreference.com also has a chapter about floating point literals.
24

By default 3.2 is treated as double; so to force the compiler to treat it as float, you need to write f at the end.

Just see this interesting demonstration:

float a = 3.2; if ( a == 3.2 ) cout << "a is equal to 3.2"<<endl; else cout << "a is not equal to 3.2"<<endl; float b = 3.2f; if ( b == 3.2f ) cout << "b is equal to 3.2f"<<endl; else cout << "b is not equal to 3.2f"<<endl; 

Output:

a is not equal to 3.2
b is equal to 3.2f

Do experiment here at ideone: http://www.ideone.com/WS1az

Try changing the type of the variable a from float to double, see the result again!

Comments

11

3.f is short for 3.0f - the number 3.0 as a floating point literal of type float.

Comments

3

The decimal point and the f have a different purpose so it is not really .f

You have to understand that in C and C++ everything is typed, including literals.

3 is a literal integer. 3. is a literal double 3.f is a literal float.

An IEEE float has less precision than a double. float uses only 32 bits, with a 23 bit mantissa and an 8 bit exponent (plus a sign bit).

double gives you more accuracy, but sometimes you do not need such accuracy (e.g. if you are doing calculations on figures that are only estimates in the first place) and that given by float will suffice, and if you are storing large numbers of them (eg processing a lot of time-series data) that can be more important than the accuracy.

Thus float is still a useful type.

You should not confuse this with the notation used by printf and equivalent statements.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.