什么是Objective-C中Math.abs()的等价物? [重复]

时间:2022-01-03 03:15:07

Possible Duplicate:
Convert to absolute value in Objective-C

可能重复:在Objective-C中转换为绝对值

As i am playing with 2d graphics, i'd like to calculate amount of points object has moved between 2 CGPoints. Given that object can move in both direction, i am only interested in sheer number of points representing the difference.

当我正在玩2D图形时,我想计算对象在2个CGPoints之间移动的点数。鉴于物体可以向两个方向移动,我只对表示差异的纯粹点数感兴趣。

In Java i'd Math.abs(startpoint.x - endpoint.x)

在Java中我是Math.abs(startpoint.x - endpoint.x)

How can i do the same in Objective-C?

我如何在Objective-C中做同样的事情?

1 个解决方案

#1


65  

There are C functions from <math.h> that will do what you want:

中的C函数可以执行您想要的操作:

abs(int val);
labs(long val);
llabs(long long val);
fabs(double val);
fabsf(float val);
fabsl(long double val):

Given that CGPoint structures are composed of CGFloats, you should use fabsf here.

鉴于CGPoint结构由CGFloats组成,您应该在这里使用fabsf。

Check out the Wikipedia page

查看Wikipedia页面

#1


65  

There are C functions from <math.h> that will do what you want:

中的C函数可以执行您想要的操作:

abs(int val);
labs(long val);
llabs(long long val);
fabs(double val);
fabsf(float val);
fabsl(long double val):

Given that CGPoint structures are composed of CGFloats, you should use fabsf here.

鉴于CGPoint结构由CGFloats组成,您应该在这里使用fabsf。

Check out the Wikipedia page

查看Wikipedia页面