替换“fabs”,`fmax`等等,用于64位iOS设备上的CGFloat [英] Replacement for `fabs`, `fmax`, etc. for use with CGFloat on 64-bit iOS devices

查看:168
本文介绍了替换“fabs”,`fmax`等等,用于64位iOS设备上的CGFloat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑如下布局代码:

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设备?

How should the third line be rewritten with support for 64-bit iOS devices?

(目前的代码没有考虑 yCoordinateOfSomeOtherView 是否为 float double 。)

(The current code doesn't take into account whether yCoordinateOfSomeOtherView is a float or a double.)

几个选项(我不知道哪个是最好的):

A few options (I'm not sure which is best):

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

我可以用<$ c替换 fmax $ c> cgfmax 。

I could then replace fmax with cgfmax.

2011年的Stack Overflow answer 建议用 tgmath.h 替换 math.h 。这将替换每个参数上调用 __ typeof __ fmax 的实现,并返回 fmax fmaxf (取决于参数类型)。

This Stack Overflow answer from 2011 suggests replacing math.h with tgmath.h. This replaces the implementation of fmax with one that calls __typeof__ on each argument and returns either fmax or fmaxf depending on the argument types.

由于CGFloats与布局相关,数据丢失可能会将 float 存储到 double 通常不重要。也就是说,它们将代表像素的小部分。

Since CGFloats relate to layout, the data loss potentially incurred storing a float into a double will usually be insignificant. That is, they'll represent tiny fractions of pixels.

我正在寻找任何其他选项或有用的建议, 。

I'm looking for any other options or helpful advice from someone who's done a 32-bit to 64-bit transition before.

推荐答案

转换时,不会发生任何数据丢失 float 转换为 double 。这种转换总是准确的。

There is never any "data loss" incurred when converting a float into a double. That conversion is always exact.

您的解决方案1& 2在语义上完全等价,但(2)更符合风格。

Your solutions 1 & 2 are entirely equivalent semantically, though (2) is more stylistically correct.

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

Your solution 3 is formally not equivalent; it may incur extra conversions between float and double, which may make it slightly less efficient (but the actual result is identical).

基本上,这没关系,使用 tgmath

Basically, it doesn't matter, but use tgmath.

这篇关于替换“fabs”,`fmax`等等,用于64位iOS设备上的CGFloat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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