在C和Objective-C,我们应该用0.5F或0.5? [英] In C and Objective-C, should we use 0.5f or 0.5?

查看:197
本文介绍了在C和Objective-C,我们应该用0.5F或0.5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多,我看到一个作家的地方用途:

There are many places that I saw an author uses:

sprite.anchorPoint = CGPointMake(1, 0.5f);

这就是,为什么不使用 0.5 而不是 0.5F - 有优势使用 0.5F

that is, why not use 0.5 instead of 0.5f -- is there advantage of using 0.5f?

推荐答案

0.5 双击不变,而 0.5F 浮动不变。

是有优势使用0.5F的?

is there advantage of using 0.5f?

是的。你要么需要指定类型(读:precision)进行计算或避免隐性缩小(以及相关的编译器警告)

yes. you either want to specify the type (read: precision) for the calculation or avoid implicit narrowing (and associated compiler warnings).

您提供的例子并不引人注目,除了避免编译器警告,或只是被非常严格有关参数类型。

the example you provide isn't compelling, beyond avoiding compiler warnings or just being very exacting regarding the parameter type.

然而,低于明显的促销活动,以双precision可能是昂贵的。

however, less-than-obvious promotions to double precision can be costly.

要回答的我们应该用0.5F或0.5的:因为​​每一个合适的时候,你应该使用的两个的在你的程序。你应该要么是使用适合您需要的计算/ precision类型,为你传递参数,或适当的前pression其他值类型的类型。这就像整数 - 有适当的时间来考虑宽度,并有适当的时间后缀(如 U )。有些时候,该区别是重要的,和(自然)倍时,它并不重要。

To answer "Should we use 0.5f or 0.5": Because there are appropriate times for each, you should be using both in your programs. You should either be using the type appropriate for the calculation/precision you need, the type for the parameter you pass, or the type appropriate for other values in the expression. This is just like integers -- there are appropriate times to consider widths, and there appropriate times to suffix (e.g. U and L). There are times when the distinction is important, and (naturally) times when it is not important.

这篇关于在C和Objective-C,我们应该用0.5F或0.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆