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

This question already has answers here:
Closed 11 years ago.
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?

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

Related

Why is there NSInteger and not NSLong or even NSLongLong? [duplicate]

This question already has answers here:
When to use NSInteger vs. int
(8 answers)
Closed 6 years ago.
NSInteger is integer object. So surely there should be long object?
NSInteger and NSUInteger are platform specific, so on 32 bit system is declared as an int and on 64 bit system it is declared as a long.
NSInteger is a foundation type, as you can see here.
There are also other useful primitive types, like NSRect, NSSize, etc.
Apple simply chose not to have a NSLong or NSLongLong type defined in foundation.
More information as to when to use NSInteger (and what the considerations are behind them) can be found in this related question.

Why would we use a pointer in an NSInteger but not in an NSNumber? [duplicate]

This question already has answers here:
What's the difference between NSNumber and NSInteger?
(5 answers)
Closed 6 years ago.
I have an NSInteger that doesn't use a pointer and an NSNumber that does use a pointer. Can someone explain to me why this is the case? All my teacher said was the NSInteger is being used as a type alias but I'm not familiar with that either yet.
This question asked a direct question asking for the reason there was no pointer in NSInteger; not asking for all the differences.
Bear in mind that Objective-C is C. Thus:
An NSInteger is a scalar, a built-in C data type (an integer). [The actual size of this integer depends on the architecture, 32-bit vs. 64-bit. But it is still some form of C integer.]
An NSNumber is an object; Objective-C object references are represented as C pointers.

Objective C Struct Syntax [duplicate]

This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 7 years ago.
I want to understand the syntax of the struct which i have seen some where. Can some please explain the meaning of unsigned int xyz:1;. Is it just assigning default value to a variable xyz? BTW this code is in Objective C.
struct
{
unsigned int xyz:1;
} testStruct;
It's a bit field. You are telling the structure that you will only be using one bit of xyz.
This allows the compiler to make packing optimisations.

Evaluate math expression in string? (NSString) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a fast C or Objective-C math parser?
I have a NSString which represents a calculation eg. #"(10+10)*2" and I want to evaluate the string as if it was actually something like this;
double result = (10+10)*2;
What is the most straightforward approach to take in iOS?
Take a look at Dave DeLong's DDMathParser framework.
You can use GCMathParser.

Objective-C 2D arrays [duplicate]

This question already has answers here:
Multi-dimensional NSArray object
(3 answers)
Closed 8 years ago.
In objective C how can I add specific numeric values to an array at given points (for example row3 column6) And how can I then also retrieve that value to compare it to another array at that same given point? When I try using examples I found online I get memory leaks so how do you all initiate them? What's the probably allocating / deallocating methods, etc.
Objective c is a super set of c so you can follow all the same rules of c programming for declaring and using arrays. ARC (if using ios5) will not manage the memory for these objects because they are not objective c objects so you'll have to manually allocate and destroy the objects yourself. This tutorial should get you started:
http://www.cprogramming.com/tutorial/lesson8.html