Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ABS(A) and abs(int)

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!

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

like image 974
Hexark Avatar asked Jul 05 '12 04:07

Hexark


2 Answers

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

like image 65
Habib Avatar answered Oct 04 '22 01:10

Habib


The ABS definition from NSObjCRuntime.h is :

#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

int abs (int number)

so it returns a value of type int.

like image 45
Abhisar Singhal Avatar answered Oct 03 '22 23:10

Abhisar Singhal