Possible Duplicate:
Convert to absolute value in 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.
In Java
i'd Math.abs(startpoint.x - endpoint.x)
How can i do the same in Objective-C
?
In C programming language the use of the function abs in C is to return the absolute value of an integer. By integer, it means that the abs() is an arithmetics function. The stdlib. h library contains predefined function abs() for computing the absolute value of integers.
The Math. abs() function returns the absolute value of a number. That is, it returns x if x is positive or zero, and the negation of x if x is negative.
abs() method returns the absolute (Positive) value of a int value. This method gives the absolute value of the argument. The argument can be int, double, long and float.
The abs () function is a predefined function in the stdlib. h header file to return the absolute value of the given integers.
There are C functions from <math.h>
that will do what you want:
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 CGFloat
s, you should use fabsf
here.
Check out the Wikipedia page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With