C语言 Objective-C/C 中数字/浮点数后的“f”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2391818/
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-09-02 04:44:50  来源:igfitidea点击:

"f" after number/float in Objective-C/C

objective-ccfloating-point

提问by typeoneerror

I cannot find this in the Apple docs so: what does the "f" after the numbers here indicate? Is this from C or Objective-C? Is there any difference in not adding this to a constant number?

我在 Apple 文档中找不到这个:这里数字后面的“f”表示什么?这是来自 C 还是 Objective-C?不把它加到一个常数上有什么区别吗?

CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 50.0f);

Can you explain why I wouldn't just write:

你能解释一下为什么我不写:

CGRect frame = CGRectMake(0, 0, 320, 50);

采纳答案by Frank Shearar

CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 50.0f);

uses float constants. (The constant 0.0 usually declares a double in Objective-C; putting an f on the end - 0.0f - declares the constant as a (32-bit) float.)

使用浮点常量。(常量 0.0 通常在 Objective-C 中声明双精度;在末尾放置一个 f - 0.0f - 将常量声明为(32 位)浮点数。)

CGRect frame = CGRectMake(0, 0, 320, 50);

uses ints which will be automatically converted to floats.

使用会自动转换为浮点数的整数。

In this case, there's no (practical) difference between the two.

在这种情况下,两者之间没有(实际)区别。

回答by epatel

When in doubt check the assembler output. For instance write a small, minimal snippet ie like this

如有疑问,请检查汇编器输出。例如写一个小的,最小的片段,即像这样

#import <Cocoa/Cocoa.h>

void test() {
  CGRect r = CGRectMake(0.0f, 0.0f, 320.0f, 50.0f);
  NSLog(@"%f", r.size.width);
}

Then compile it to assembler with the -Soption.

然后使用-S选项将其编译为汇编程序。

gcc -S test.m

Save the assembler output in the test.sfile and remove .0ffrom the constants and repeat the compile command. Then do a diffof the new test.sand previous one. Think that should show if there are any real differences. I think too many have a visionof what they think the compiler does, but at the end of the day one should know how to verify any theories.

将汇编器输出保存在test.s文件中并.0f从常量中删除并重复编译命令。然后做一个diff新的test.s和以前的。认为这应该表明是否存在任何真正的差异。我想太多有一个愿景,他们怎么想的编译器,但在一天结束的时候应该知道如何验证任何理论。

回答by Potatoswatter

Sometimes there is a difference.

有时是有区别的。

float f = 0.3; /* OK, throw away bits to convert 0.3 from double to float */
assert ( f == 0.3 ); /* not OK, f is converted from float to double
   and the value of 0.3 depends on how many bits you use to represent it. */
assert ( f == 0.3f ); /* OK, comparing two floats, although == is finicky. */

回答by NickLH

It tells the computer that this is a floating point number (I assume you are talking about c/c++ here). If there is no f after the number, it is considered a double or an integer (depending on if there is a decimal or not).

它告诉计算机这是一个浮点数(我假设你在这里谈论的是 c/c++)。如果数字后没有 f,则认为是双精度型或整数型(取决于是否有小数)。

3.0f -> float
3.0 -> double
3 -> integer

回答by Hans Passant

A floating point literal in your source code is parsed as a double. Assigning it to a variable that is of type float will lose precision. A lot of precision, you're throwing away 7 significant digits. The "f" postfix let's you tell the compiler: "I know what I'm doing, this is intentional. Don't bug me about it".

源代码中的浮点文字被解析为双精度值。将其分配给 float 类型的变量将失去精度。非常精确,您丢弃了 7 位有效数字。“f”后缀让你告诉编译器:“我知道我在做什么,这是故意的。不要为此烦恼”。

The odds of producing a bug isn't that small btw. Many a program has keeled over on an ill-conceived floating point comparison or assuming that 0.1 is exactly representable.

顺便说一句,产生错误的几率并不小。许多程序都在考虑不周到的浮点比较或假设 0.1 是完全可表示的。

回答by Yuri

The fthat you are talking about is probably meant to tell the compiler that it's working with a float. When you omit the f, it is usually translated to a double.

f你所谈论的是大概意思告诉编译器,它的工作有浮动。当您省略 时f,它通常被转换为双精度。

Both are floating point numbers, but a floatuses less bits (thus smaller and less precise) than a double.

两者都是浮点数,但 afloat使用的位数比 a 少(因此更小且精度更低)double

回答by Paul R

It's a C thing - floating point literals are double precision (double) by default. Adding an f suffix makes them single precision (float).

这是 C 语言 - 默认情况下浮点文字是双精度(double)。添加 f 后缀使它们成为单精度(浮点数)。

You can use ints to specify the values here and in this case it will make no difference, but using the correct type is a good habit to get into - consistency is a good thing in general, and if you need to change these values later you'll know at first glance what type they are.

您可以使用 ints 来指定此处的值,在这种情况下它没有区别,但使用正确的类型是一个好习惯 - 一致性通常是一件好事,如果您以后需要更改这些值一看就知道它们是什么类型。

回答by Wildcat

From C. It means float literal constant. You can omit both "f" and ".0" and use ints in your example because of implicit conversion of ints to floats.

来自 C。它的意思是浮点字面常量。由于整数到浮点数的隐式转换,您可以省略“f”和“.0”并在示例中使用整数。

回答by tyranid

It is almost certainly from C and reflects the desire to use a 'float' rather than a 'double' type. It is similar to suffixes such as L on numbers to indicate they are long integers. You can just use integers and the compiler will auto convert as appropriate (for this specific scenario).

它几乎可以肯定来自 C 并且反映了使用“float”而不是“double”类型的愿望。它类似于数字上的后缀,如 L,表示它们是长整数。您可以只使用整数,编译器将根据需要自动转换(针对此特定场景)。

回答by Polynomial

It usually tells the compiler that the value is a float, i.e. a floating point integer. This means that it can store integers, decimal values and exponentials, e.g. 1, 0.4or 1.2e+22.

它通常告诉编译器该值是 a float,即浮点整数。这意味着它可以存储整数、十进制值和指数,例如10.41.2e+22