简体   繁体   English

使用 floorf 与 floor

[英]Using floorf vs floor

I have the following situation in my code:我的代码中有以下情况:

//maxiumumHeight and rowHeight are CGFloats
CGFloat rows = floorf(maximumHeight / rowHeight);
CGFloat height = (rows * rowHeight);

It works just fine, but I notice that when I use floor , it works fine also.它工作得很好,但我注意到当我使用floor时,它也工作得很好。 I'm also confused about how to define rows , should it be an NSUInteger or CGFloat ?我也对如何定义rows感到困惑,它应该是NSUInteger还是CGFloat

It seems to work fine in all scenarios, but I have a little trouble understanding floating point math and was wondering what is the optimal way to do this code?它似乎在所有情况下都可以正常工作,但是我在理解浮点数学时遇到了一些麻烦,并且想知道执行此代码的最佳方法是什么? Or does it matter?还是有关系?

floor is for double (which is same as CGFloat ) and floorf is for float (meaning it takes float arguments). floor用于double (与CGFloat相同), floorf用于float (意味着它需要float参数)。 This comes from ol' C stuff.这来自 ol' C 的东西。 Since you use CGFloat stick with floor .由于您使用CGFloat棒与floor But, just in case, I would add a bit of tolerance, so change it to eg但是,以防万一,我会增加一点容忍度,所以将其更改为例如

CGFloat rows = floor ( ( maximumHeight + 0.5 ) / rowHeight );

assuming these values are always strictly positive.假设这些值始终严格为正。

Often you'd convert the number of rows to integer but since you calculate a UI height with it, which is also going to be used as CGFloat no need to use integers here.通常您会将行数转换为 integer 但由于您使用它计算 UI 高度,这也将用作CGFloat此处无需使用整数。 (Note if you are on watch CGFloat is float so there you'd want to use floorf but FWIW on many architectures floor and floorf are one and the same). (请注意,如果您正在观看CGFloatfloat的,那么您会想要使用floorf但在许多架构中的 FWIW floorfloorf是相同的)。

Not sure if this is what is troubling you, but floor rounds down to nearest int.不确定这是否是困扰您的问题,但 floor 会向下舍入到最接近的 int。 So if you have eg 100 pixels available and you want a row to have 20 pixels, you will have因此,如果您有例如 100 个像素可用并且您希望一行有 20 个像素,那么您将拥有

100 / 20 = 5

rows.行。 What if you want it to be 21 pixels?如果你希望它是 21 像素怎么办? Then然后

100 / 21 = 4.76

Now you need to decide.现在你需要做出决定。 If you are going to insert 4 rows here you use floor since如果要在此处插入 4 行,则使用floor ,因为

floor ( 4.7 ) = 4.0

and note that, since you use only 4 rows, the height of each row will be请注意,由于您仅使用 4 行,因此每行的高度将为

100 / 4 = 25

pixels, a bit more (since you use less rows) than the 21 you hoped for.像素,比你希望的 21 多一点(因为你使用的行更少)。

If you in stead want to use 5 rows then use ceil or ceiling since如果您想使用 5 行,请使用ceil或 ceiling ,因为

ceil ( 4.7 ) = 5.0

and here note that since you use more rows the height will be在这里请注意,由于您使用更多行,因此高度将为

100 / 5 = 20

pixels.像素。 So if the 21 you hoped for is a maximum you'd use ceil in this particular example.因此,如果您希望的 21 是最大值,那么您将在此特定示例中使用ceil If it is a minimum likewise you'd use floor .如果它是最小值,您同样会使用floor

Sometimes you may get funny and unexpected values, something like有时你可能会得到有趣和意想不到的值,比如

100 / 20 = 4.999999999

which is why I'd add a bit of tolerance.这就是为什么我会增加一点容忍度。

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

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