Purpose of a “.f” appended to a number?

前端 未结 5 1531
余生分开走
余生分开走 2020-12-02 16:24

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

#include
using namespace std;
int main()
{
              


        
5条回答
  •  鱼传尺愫
    2020-12-02 17:21

    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"<

    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!

提交回复
热议问题