问题描述
我是否应该使用postfix f
编写CGFloat值?
Should I be writing CGFloat values with postfix f
or not?
CGFloat fValue = 1.2;
vs。
CGFloat fValue = 1.2f;
我知道这个后缀定义了一个浮点值。但它是否有必要,是否有意义,使用这两者之间是否有任何性能差异,或者这只是视觉呈现,因此您可以快速定义值类型(例如,在这种情况下浮动)?
I know that this postfix define a float value. But is it necessary, does it make sense, are there any performance differences between using those two or is this just visual presentation so you can quickly define value type (e.g. float in this case)?
推荐答案
1.2是 double
;即64位双精度浮点数。
1.2 is a double
; i.e. 64-bit double-precision floating point number.
1.2f是 float
;即32位单精度浮点数。
1.2f is a float
; i.e. 32-bit single-precision floating point number.
就性能而言,无关紧要,因为编译器将从 float转换文字
到 double
和 double
到 float
必要时。但是,从函数中分配浮点数时,您很可能需要进行强制转换以避免编译器警告。
In terms of performance, it doesn't matter as the compiler will convert literals from float
to double
and double
to float
as necessary. When assigning floating-point numbers from functions, however, you will most likely need to cast to avoid a compiler warning.
这篇关于在Objective-C中是否使用带有或不带有后缀.f的CGFloat进行性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!