简体   繁体   English

在为CGFloat赋值时使用'f'有什么意义?

[英]What is the point of using 'f' when assigning a value to a CGFloat?

I see this all the time: 我一直都看到这个:

CGFloat someCGFloat = 1.2f;

Why is the 'f' used? 为什么使用'f'? If the CGFloat is defined as float , the value will be converted to a float , and if the CGFloat is defined as a double , the value will be converted to a double . 如果CGFloat定义为float ,则该值将转换为float ,如果CGFloat定义为double ,则该值将转换为double

Is it just to make sure a conversion from double to float doesn't occur? 是否只是为了确保不会发生从doublefloat的转换? What's the point of doing that? 这样做有什么意义? Also, wouldn't the compiler take care of this? 此外,编译器不会处理这个吗?

EDIT: Hmmm…which answer to accept…both are very good! 编辑:嗯......接受的答案......两者都非常好!

1.0 by default is double, if the right value is 1.2 there is an implicit cast, and the value gets casted from double to float (the cast isn't a runtime operation). 1.0默认为double,如果正确值为1.2则存在隐式转换,并且值从double转换为float(转换不是运行时操作)。 In this case it's not important to call it 1.2f. 在这种情况下,称它为1.2f并不重要。 Programmers mostly abuse it, but there are cases where it's really important. 程序员大多滥用它,但有些情况下它非常重要。

For example: 例如:

float var= 1.0e-45;
NSLog(@"%d",var==1.0e-45);

This prints zero, because 1.0e-45 is too small to be stored into a single precision floating point variable, so it becomes equal to zero. 这打印为零,因为1.0e-45太小而无法存储到单个精度浮点变量中,因此它等于零。 Writing var==1.0e-45f changes the result. 写var == 1.0e-45f会改变结果。

Using format specifiers is important mostly when writing expressions, and since the left value is a float you expect that also the expression gets treated as a float, but that's not what happens. 在编写表达式时,使用格式说明符很重要,因为左值是浮点数,你期望表达式也被视为浮点数,但事实并非如此。

A more striking case is when using the l format specifier on a number that gets shifted so much to become zero, and get surprised about the result: 一个更引人注目的情况是,在一个数字上使用l格式说明符时,这个数字会变得很大而变为零,并对结果感到惊讶:

long var= 1<<32;  // I assume that an int takes 4 bytes and a long 8 bytes

The result is zero, and writing 1l<<32 completely changes the result. 结果为零,并且写入1l << 32完全改变结果。

In your snippet, just an assignment, you don't need the 'f' suffix, and in fact you shouldn't use it. 在你的代码片段中,只是一个赋值,你不需要'f'后缀,实际上你不应该使用它。 If CGFloat is single precision (like in iOS) then your value will be stored single precision with or without the 'f' and if CGFloat is double precision (like on Mac OS) then you'll be unnecessarily creating a single precision value to be stored double precision. 如果CGFloat是单精度(如在iOS中)那么你的值将存储单精度,有或没有'f',如果CGFloat是双精度(如在Mac OS上),那么你将不必要地创建一个单一的精度值存储双精度。

On the other hand, if you're doing arithmetic you should be careful to use 'f' or not use it as appropriate. 另一方面,如果你在做算术,你应该小心使用'f'或不使用它。 If you're working with single precision and include a literal like '1.2' without the 'f' then the compiler will promote the other operands to double precision. 如果你正在使用单精度并且包含一个没有'f'的文字'1.2'那么编译器会将其他操作数提升为双精度。 And if you're working with double precision and you include the 'f' then (like the assignment on Mac OS) you'll be creating a single precision value only to have it immediately converted to double. 如果你正在使用双精度并且然后包含'f'(就像Mac OS上的赋值一样),你将创建一个单一的精度值,只是让它立即转换为double。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM