xcode ABS(A) 和 abs(int)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11337845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:47:24  来源:igfitidea点击:

ABS(A) and abs(int)

objective-ccxcodefunctionmacros

提问by Hexark

I am baffled in the difference of this two in xcode, ABS(A) and abs(int). Cant seem to find ay explanation online as well. Which should I use? I am actually working on an accelerometer. Using ABS(A) and abs(int) gives me two different values. Using abs(int) would result in an inf value at times, while ABS(A) would give me a different value but never inf. Thanks!

我对这两者在 xcode、ABS(A) 和 abs(int) 中的差异感到困惑。似乎也无法在网上找到任何解释。我应该使用哪个?我实际上正在研究一个加速度计。使用 ABS(A) 和 abs(int) 给了我两个不同的值。使用 abs(int) 有时会产生一个 inf 值,而 ABS(A) 会给我一个不同的值,但不会产生 inf。谢谢!

http://www.switchonthecode.com/tutorials/iphone-tutorial-reading-the-accelerometer

http://www.switchonthecode.com/tutorials/iphone-tutorial-reading-the-accelerometer

回答by Habib

abs()works on int, meaning abs(-10.123)would return 10, while ABS()is a macro from NSObjCRuntime.hwhich returns a value whose type is the type of the argument.

abs()在 int 上工作,意思abs(-10.123)是 return 10,whileABS()是一个宏,NSObjCRuntime.h从中返回一个类型是参数类型的值。

回答by Abhisar Singhal

The ABS definition from NSObjCRuntime.h is :

NSObjCRuntime.h 中的 ABS 定义是:

#define ABS(a) ({typeof(a) _a = (a); _a < 0 ? -_a : _a; })

so it returns the value with the type of the argument. abs on the other hand has the protoype

所以它返回带有参数类型的值。另一方面 abs 有原型

int abs (int number)

so it returns a value of type int.

所以它返回一个 int 类型的值。

回答by Richard J. Ross III

ABS()is most likely a macro, meaning it's a copy-pasta of some code into your application, whilst abs()is probably a function. In most situations, use the function, as it has no odd reprocussions should you include an assignment in there.

ABS()很可能是一个宏,这意味着它是一些代码的复制粘贴到您的应用程序中,而abs()可能是一个函数。在大多数情况下,请使用该函数,因为如果您在其中包含赋值,它不会产生奇怪的反作用。

回答by bliako

A word of warning about using macros with arguments which are functions, e.g.

关于使用带有函数参数的宏的警告,例如

#include <stdio.h>
#define BAD_ABS(A) ((A)<0 ? (-(A)):(A))
int     afunc(){
    printf("afunc was called\n");
    return 1;
}
int     main(void){
    int     a;
    a = BAD_ABS(afunc());
    /* this has the result of:
    a = (afunc()<0) ? (-(afunc())) : (afunc());
    */
}

The function 'afunc' will be called twice, first for the comparison <0 and second when it returns the result -A or A.

函数 'afunc' 将被调用两次,第一次用于比较 <0,第二次用于返回结果 -A 或 A。

The way the ABS macro in NSObjCRuntime.h is defined avoids this by introducing an extra variable, but some DIY macros might not, e.g. I have seen in several places

NSObjCRuntime.h 中 ABS 宏的定义方式通过引入一个额外的变量来避免这种情况,但一些 DIY 宏可能不会,例如我在几个地方看到过

 #define BAD_ABS(a) ((a)<0 ? (-(a)) : (a))

It gets worse if the function to be called is a random number generator, e.g.

如果要调用的函数是随机数生成器,则情况会变得更糟,例如

#include <stdio.h>
#include <stdlib.h>
int main(void){
   short seeds[] = {1,2,3};
   int i;
   for(i=0;i<10000;i++) printf("%ld\n", BAD_ABS(jrand48(seeds)));
}

this has a 50-50 chance of returning a negative number!!!

这有 50-50 的机会返回负数!!!

If on the other hand you want to use the stdlib abs() function then be warned that it will not work with long integers. In that case you should use labs().

另一方面,如果您想使用 stdlib abs() 函数,请注意它不适用于长整数。在这种情况下,您应该使用 labs()。

回答by Rama Krishna

We have two choices for

我们有两种选择

#define ABS(a) ({typeof(a) _a = (a); _a < 0 ? -_a : _a; })

in Swift 2 use

在 Swift 2 中使用

Int32 abs(Int32) which has a return type Int32
T abs(T) which has a return type T where T is a generic type

I hope it works, Thanks

我希望它有效,谢谢