C++ 附加到数字的“.f”的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4828167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:40:45  来源:igfitidea点击:

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

c++syntaxcasting

提问by Nav

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

我在一个程序中看到了“1/3.f”,想知道“.f”是干什么用的。所以尝试了我自己的程序:

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

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

.f 是否像演员一样使用?我可以在任何地方阅读有关这种有趣语法的更多信息?

采纳答案by wich

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

没有.f数字被解释为整数,因此1/3(int)1/(int)3=>(int)0而不是所需的(float)0.333333。该.f告诉编译器解释的字面类型浮体的浮点数。还有其他这样的结构,例如0UL这意味着 a (unsigned long)0,而普通的0将是 an (int)0

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

The.f实际上是两个组件,.表示文字是浮点数而不是整数,f后缀告诉编译器文字应该是 float 类型,而不是用于浮点文字的默认 double 类型。

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.

如果您想了解所有关于文字和可以在其中使用的后缀,您可以阅读 C++ 标准(1997 草案C++11 草案C++14 草案C++17 草案),或者,有看看一本像样的教科书,例如 Stroustrup 的The C++ Programming Language

As an aside, in your example (float)1/3the literals 1and 3are 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.)

(float)1/3顺便说一句,在您的示例中,文字13实际上是整数,但首先将 1 转换为浮点数,然后将 3 隐式转换为浮点数,因为它是浮点运算符的右侧操作数。(运算符是浮点数,因为它的左侧操作数是浮点数。)

回答by peoro

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

3.相当于3.0,它是一个双倍。

ffollowing a number literal makes it a float.

f跟随数字文字使其成为浮点数。

回答by Nawaz

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

默认情况下3.2被视为double; 所以要强制编译器把它当作float,你需要写f在最后。

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

a 不等于 3.2
b 等于 3.2f

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

在 ideone 上做实验:http: //www.ideone.com/WS1az

Try changing the type of the variable afrom floatto double, see the result again!

尝试将变量的类型a从更改floatdouble,再次查看结果!

回答by jcoder

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

3.f 是 3.0f 的缩写 - 数字 3.0 作为 float 类型的浮点文字。

回答by CashCow

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

小数点和 f 有不同的用途,所以它不是真正的 .f

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

你必须明白,在 C 和 C++ 中,一切都是类型化的,包括文字。

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

3 是一个文字整数。3. 是文字 double 3.f 是文字浮点数。

An IEEE float has less precision than a double. float uses only 32 bits, with a 22 bit mantissa and 10 bit exponent (including the sign bits of each).

IEEE 浮点数的精度低于双精度数。float 仅使用 32 位,带有 22 位尾数和 10 位指数(包括每个的符号位)。

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.

double 为您提供更高的准确性,但有时您不需要这样的准确性(例如,如果您正在对最初只是估计值的数字进行计算)并且由 float 给出的就足够了,并且如果您要存储大量它们(例如处理大量时间序列数据),这可能比准确性更重要。

Thus float is still a useful type.

因此 float 仍然是一个有用的类型。

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

您不应将此与 printf 和等效语句使用的符号混淆。