Is there a performance diff using CGFloat with or without postfix .f in Objective-C

核能气质少年 提交于 2019-12-06 03:17:55

问题


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:


1.2 is a double; i.e. 64-bit double-precision floating point number.

1.2f is a float; i.e. 32-bit single-precision floating point number.

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.




回答2:


The basic difference is as :

1.0 or 1. is a double constant

1.0f is a float constant

Without a suffix, a literal with a decimal in it (123.0) will be treated as a double-precision floating-point number.

If you assign or pass that to a single-precision variable or parameter, the compiler will (should) issue a warning. Appending f tells the compiler you want the literal to be treated as a single-precision floating-point number.



来源:https://stackoverflow.com/questions/15405129/is-there-a-performance-diff-using-cgfloat-with-or-without-postfix-f-in-objectiv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!