Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of Math.abs() in Objective-C? [duplicate]

Tags:

objective-c

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?

like image 915
James Raitsev Avatar asked Nov 02 '11 01:11

James Raitsev


People also ask

Is there any abs function in 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.

What is the value of math abs?

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.

Does math abs return double?

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.

What C library is abs in?

The abs () function is a predefined function in the stdlib. h header file to return the absolute value of the given integers.


1 Answers

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 CGFloats, you should use fabsf here.

Check out the Wikipedia page

like image 55
Jacob Relkin Avatar answered Sep 21 '22 05:09

Jacob Relkin