gpt4 book ai didi

ios - 替换 `fabs` 、 `fmax` 等,用于 64 位 iOS 设备上的 CGFloat

转载 作者:技术小花猫 更新时间:2023-10-29 10:51:45 28 4
gpt4 key购买 nike

问题

考虑这样的布局代码:

CGFloat descriptionHeight = // height of a paragraph of text with zero or more words;
CGFloat imageHeight = // height of an image;
CGFloat yCoordinateOfSomeOtherView = fmax(descriptionHeight, imageHeight) + 5.0f;

第三行应该如何重写以支持 64 位 iOS 设备?

(当前代码没有考虑yCoordinateOfSomeOtherViewfloat 还是double。)

选项

几个选项(我不确定哪个最好):

1。定义我自己的宏

#if defined(__LP64__) && __LP64__
#define cgfmax(x,y) fmaxf(x,y)
#else
#define cgfmax(x,y) fmax(x,y)
#endif

然后我可以将 fmax 替换为 cgfmax

2。使用 tgmath.h

This Stack Overflow answer从 2011 年开始,建议将 math.h 替换为 tgmath.h。这将 fmax 的实现替换为在每个参数上调用 __typeof__ 并返回 fmaxfmaxf 的实现,具体取决于参数类型。

3。忽略这个问题

由于 CGFloats 与布局相关,将 float 存储到 double 中可能导致的数据丢失通常是微不足道的。也就是说,它们将代表像素的极小部分。

总结

我正在寻找之前从 32 位到 64 位转换的人的任何其他选项或有用的建议。

最佳答案

float 转换为 double 时,不会发生任何“数据丢失”。该转换始终是准确的。

您的解决方案 1 和 2 在语义上完全相同,尽管 (2) 在风格上更正确。

你的解决方案 3 在形式上是不等价的;它可能会在 float 和 double 之间产生额外的转换,这可能会使其效率稍低(但实际结果是相同的)。

基本上没关系,但是用tgmath

关于ios - 替换 `fabs` 、 `fmax` 等,用于 64 位 iOS 设备上的 CGFloat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19506238/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com