67

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?

Nav
  • 19,885
  • 27
  • 92
  • 135
  • 25
    +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](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), it's the best way to learn a new language. – GManNickG Jan 28 '11 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 – andrewtweber Sep 08 '13 at 20:51
  • It isn't a `.f`. It is `3.` followed by `f`. – user207421 Mar 29 '20 at 09:23

5 Answers5

71

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

f following a number literal makes it a float.

peoro
  • 25,562
  • 20
  • 98
  • 150
55

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

wich
  • 16,709
  • 6
  • 47
  • 72
  • 1
    @Nav, if you want to know all about literals you can read the standard (1997 draft available at http://www.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 http://www2.research.att.com/~bs/3rd.html – wich Jan 28 '11 at 13:07
  • The `.f` is actually `3.` followed by `f`. There is certainly no such thing as `.f` anywhere in the syntax for C++. – user207421 Mar 29 '20 at 09:25
  • cppreference.com also has a [chapter about floating point literal](https://en.cppreference.com/w/cpp/language/floating_literal)s. – user5534993 Jul 03 '20 at 12:12
22

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!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
10

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

jcoder
  • 29,554
  • 19
  • 87
  • 130
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.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
CashCow
  • 30,981
  • 5
  • 61
  • 92