开发者

Purpose of a ".f" appended to a number?

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;
}
开发者_如何学Go

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


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

f following a number literal makes it a float.


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


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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜