three integers compare - iphone

I have three integers
I would like to determine what is the highest and which is the lowest value using Objective-C
Thank you!

It is good to store that numbers in an array. Just plain C array is good enough and in Objective-C best for performance. To find a minimum you can use this function. Similar for maximum.
int find_min(int numbers[], int N){
int min = numbers[0];
for(int i=1;i<N;i++)
if(min>numbers[i])min=numbers[i];
return min;
}
If that is just three numbers you can do the comparisons manually for best performance. There is a MIN() and MAX() macro in Cocoa in Foundation/NSObjCRuntime.h. For the maximum, just do:
int m = MAX(myI1, MAX(myI2, myI3));
This may be scaled to more numbers and may be faster than the first approach using loop.

Unfortunately there is no short and elegant neither a generalized way for that in Cocoa.
Plain C Array + custom loop would be the best. With an NSArray you would have to wrap the Integers in NSNumbers without getting any benefit out of that.

Objective-C's built in MAX(a,b) and MIN(a,b) macros only work for two values.
I have two macros I've created for using 2 or more values called multi-max and multi-min (MMAX and MMIN)
Here is their definition, just copy paste into your .h
#define MMAX(...) ({\
long double __inputs[(sizeof((long double[]){__VA_ARGS__})/sizeof(long double))] = {__VA_ARGS__};\
long double __maxValue = __inputs[0];\
for (int __i = 0; __i < (sizeof((long double[]){__VA_ARGS__})/sizeof(long double)); ++__i) {\
long double __inputValue = __inputs[__i];\
__maxValue = __maxValue>__inputValue?__maxValue:__inputValue;\
}\
__maxValue;\
})
#define MMIN(...) ({\
long double __inputs[(sizeof((long double[]){__VA_ARGS__})/sizeof(long double))] = {__VA_ARGS__};\
long double __minValue = __inputs[0];\
for (int __i = 0; __i < (sizeof((long double[]){__VA_ARGS__})/sizeof(long double)); ++__i) {\
long double __inputValue = __inputs[__i];\
__minValue = __minValue<__inputValue?__minValue:__inputValue;\
}\
__minValue;\
})
Example use:
x = MMAX(2,3,9,5);
//sets x to 9.

Related

Float not returning the right decimal value

This is the code i have:
int resultInt = [ja.resultCount intValue];
float pages = resultInt / 10;
NSLog(#"%d",resultInt);
NSLog(#"%.2f",pages);
the resultInt comes back from php script with the value 3559 so the pages result should be 355.9 but i get the result as 355.00 which isn't right
Use
float pages = resultInt / 10.0f;
int/int is int
but int/float or float/int is float
Edited for more explanation
It is important to remember that the resultant value of a mathematical operation is subject to the rules of the receiving variable's data type. The result of a division operation may yield a floating point value. However, if assigned to an integer the fractional part will be lost. Equally important, and less obvious, is the effect of an operation performed on several integers and assigned to a non-integer. In this case, the result is calculated as an integer before being implicitly converted. This means that although the resultant value is assigned to a floating point variable, the fractional part is still truncated unless at least one of the values is explicitly converted first. The following examples illustrate this:
int a = 7;
int b = 3;
int integerResult;
float floatResult;
integerResult = a / b; // integerResult = 2 (truncated)
floatResult = a / b; // floatResult = 2.0 (truncated)
floatResult = (float)a / b; // floatResult = 2.33333325
This has to do with the fact that you're using integer and not float.
Tell the variables that you are using that they are floats and you are done.
int resultInt = [ja.resultCount intValue];
float pages = (float)resultInt / 10.f;

Math notation in Objective-C

I'm would like to create a formula I have, into Objective C code.
float loanP = ((interestRate/100/12) *loanAmount) /(1-(1+(interestRate/100/12))^- loanTens)
Is there some notation I should be using for the
^-loanTens
part?
Many Thanks,
-Code
objective C has all the libraries and classes which were in C/C++.
We have some math classes to perform math functions.
import math.h file
which has math functions useful to you.
here you need power operation i think so.
this will be something like this
int a = pow(10,4);
have a look at the available functions in the math.h and make use of it.
Use pow:
float loanP = ((interestRate/100/12) *loanAmount) / pow(1-(1+(interestRate/100/12)), -loanTens);
Use pow function or u can define value of (1+(interestRate/100/12))^- loanTens using your own code.
int value= 1 + (interestRate/100/12);
int total =1;
for(int i =1;i<=loanTens;i++){
total = total*value;
}
float loanP = ((interestRate/100/12) *loanAmount) /(1-(1/total));
or second method:
float loanP = ((interestRate/100/12) *loanAmount) / (1 - 1/(pow((1+(interestRate/100/12)), loanTens)));

Do I need to use decimal places when using floats? Is the "f" suffix necessary?

I've seen several examples in books and around the web where they sometimes use decimal places when declaring float values even if they are whole numbers, and sometimes using an "f" suffix. Is this necessary?
For example:
[UIColor colorWithRed:0.8 green:0.914 blue:0.9 alpha:1.00];
How is this different from:
[UIColor colorWithRed:0.8f green:0.914f blue:0.9f alpha:1.00f];
Does the trailing "f" mean anything special?
Getting rid of the trailing zeros for the alpha value works too, so it becomes:
[UIColor colorWithRed:0.8 green:0.914 blue:0.9 alpha:1];
So are the decimal zeros just there to remind myself and others that the value is a float?
Just one of those things that has puzzled me so any clarification is welcome :)
Decimal literals are treated as double by default. Using 1.0f tells the compiler to use a float (which is smaller than double) instead. In most cases it doesn't really matterĀ if a number is a double or a float, the compiler will make sure you get the right format for the job in the end. In high-performance code you may want to be explicit, but I'd suggest benchmarking it yourself.
As John said numbers with a decimal place default to double. TomTom is wrong.
I was curious to know if the compiler would just optimize the double to a const float (which I assumed would happen)... turns out it doesn't and the idea of the speed increase is actually legit... depending on how much you use it. In math-heavy application, you probably do want to use this trick.
It must be the case that it is taking the stored float variable, casting it to a double, performing the math against the double (the number without the f), then casting it back to a float to store it again. That would explain the diference in calculation even though we're storing in floats each time.
The code & raw results:
https://gist.github.com/1880400
Pulled out relevant benchmark on an iPad 1 in Debug profile (Release resulted in even more of a performance increase by using the f notation):
------------ 10000000 total loops
timeWithDoubles: 1.33593 sec
timeWithFloats: 0.80924 sec
Float speed up: 1.65x
Difference in calculation: -0.000038
Code:
int main (int argc, const char * argv[]) {
for (unsigned int magnitude = 100; magnitude < INT_MAX; magnitude *= 10) {
runTest(magnitude);
}
return 0;
}
void runTest(int numIterations) {
NSTimeInterval startTime = CFAbsoluteTimeGetCurrent();
float d = 1.2f;
for (int i = 0; i < numIterations; i++) {
d += 1.8368383;
d *= 0.976;
}
NSTimeInterval timeWithDoubles = CFAbsoluteTimeGetCurrent() - startTime;
startTime = CFAbsoluteTimeGetCurrent();
float f = 1.2f;
for (int i = 0; i < numIterations; i++) {
f += 1.8368383f;
f *= 0.976f;
}
NSTimeInterval timeWithFloats = CFAbsoluteTimeGetCurrent() - startTime;
printf("\n------------ %d total loops\n", numIterations);
printf("timeWithDoubles: %2.5f sec\n", timeWithDoubles);
printf("timeWithFloats: %2.5f sec\n", timeWithFloats);
printf("Float speed up: %2.2fx\n", timeWithDoubles / timeWithFloats);
printf("Difference in calculation: %f\n", d - f);
}
Trailing f: this is a float.
Trailing f + "." - redundant.
That simple.
8f is 8 as a float.
8.0 is 8 as a float.
8 is 8 as integer.
8.0f is 8 as a float.
Mostly the "f" can be style - to make sure it is a float, not a double.

Using a C function in Objective-C (for iPhone)

'lo all. I am a self-described admitted noob in iPhone programming (having a much longer perl & web background -- 30 years)...but took the plunge last week and bought a couple of good books. After cramming and reading well over 1000 pages -- and understanding it pretty well, I am well on my way to a good first Native iPhone app. My problem is this: I do not know how to do a simple Geographic (lat/long) point-in-polygon routine in Objective-C. I have 2 ways of doing this. One in C (the first code example) and one in JavaScript (the second code example):
// this is the poly.h file
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);
// this is the poly.c file
#include "poly.h"
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy){
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
or this (in Javascript):
function _isPointInPoly(poly, pt){
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
&& (c = !c);
return c;
}
(either one will work if i could get them converted)
So, to try this out...I put the .h file and .c file into xcode with my iPhone project. The only question now is how to call this from Objective-C and get the result.. :)
BTW: I searched the Great God Google all last night to get the answer to this but just TRY to search for "including C in an Objective-C iPhone app", etc.. you get so many entries and none have to do with this! :) Just letting you know I tried google before posting here.
Okay, my issues:
How do I call the pnpoly from Objective-C?
What types do i call it using? (int is fine, but the float
*vertx is obviously an array of floats..which NSArray does not have
-- that I can find)
(EDIT: HERE IS MORE INFO. I AM ASKING FOR HELP CONTRUCTING THE ARRAYS THAT WOULD BE PASSED AS WELL)
The question was not asked fully.
The routine (in objective-c) would be like this: (assuming this is coded right)
NSMutableArray *latitudeArray = [[NSMutableArray alloc] init];
NSMutableArray *longitudeArray = [[NSMutableArray alloc] init];
// coordinates surrounding 1 inifite loop.
[latitudeArray addObject:#"37.32812557141369"];
[longitudeArray addObject:#"-122.0320253896318"];
[latitudeArray addObject:#"37.32821852349916"];
[longitudeArray addObject:#"-122.0289014325174"];
[latitudeArray addObject:#"37.33021046381746"];
[longitudeArray addObject:#"-122.0289300638158"];
[latitudeArray addObject:#"37.33042111092124"];
[longitudeArray addObject:#"-122.0279574092159"];
[latitudeArray addObject:#"37.33395972491337"];
[longitudeArray addObject:#"-122.0279263955651"];
[latitudeArray addObject:#"37.33363270879559"];
[longitudeArray addObject:#"-122.0320527775551"];
[latitudeArray addObject:#"37.32812557141369"];
[longitudeArray addObject:#"-122.0320253896318"];
int nvert = [[latitudeArray count] intvalue];
// 37.33189399206268 x -122.0296274412866 should return true
float testx =37.33189399206268;
float testy =-122.0296274412866;
int y_or_n = pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);
I should've made it clear that I am learning Objective-c but FOUND that C routine--so was not sure how to construct either the C variables to call it with or the routine to call it with.
I know this is asking a lot...but it is really puzzling to me. Can anyone help me?
Thanks so much.
Jann
You can just call it. Objective-C is just a front-end to a C API and a way of re-writing methods as functions (to some approximation, anyway...) so you can call a C function just as you would in C code.
- (int)doWhatever {
// ...
int hitTest = pnPoly(/*blah*/);
return hitTest;
}
You can use C primitive types like int and float in Objective-C without issue, too. So call the function with floats :). If you need to store such values in Foundation collection classes like NSArray, then you can wrap them in a class called NSNumber.
NSNumber *someFloat = [NSNumber numberWithFloat: f];
float usefulValue = [someFloat floatValue];
Here's en example of how it would be used:
int nCoords = 4;
float vertexXCoords[n] = {0.0, 0.0, 20.0, 20.0};
float vertexYCoords[n] = {0.0, 20.0, 20.0, 0.0};
NSPoint testPoint = NSMakePoint(5, 10);
BOOL testPointIsInPoly = pnpoly(nCoords, xCoords, yCoords, testPoint.x, testPoint.y);
Note that there's nothing specific to Objective-C in here. This is C code (though it does use the Cocoa BOOL and NSPoint C types). Since Objective-C is a strict superset of C, any valid C code is also valid Objective-C. This is also a case in which Objective-C's unique features would not be particularly useful. (Numerical calculations in general are less complex and more readable in plain C.)
objective C is a superset of C, so you can call C routines. If you are calling that routine a lot, inline it or make it a macro.

Find the smallest value among variables?

I have from 4 up to 20 variables that differ in size.
They are all of type float and number values.
Is there an easy way to find the smallest value among them and assign it to a variable?
Thanks
Not sure about objective-c but the procedure's something like:
float min = arrayofvalues[0];
foreach( float value in arrayofvalues)
{
if(value < min)
min=value;
}
I agree with Davy8 - you could try rewriting his code into Objective C.
But, I have found some min()-like code - in Objective C!
Look at this:
- (int) smallestOf: (int) a andOf: (int) b andOf: (int) c
{
int min = a;
if ( b < min )
min = b;
if( c < min )
min = c;
return min;
}
This code assumes it'll always compare only three variables, but I guess that's something you can deal with ;)
The best solution, without foreach.
`- (float)minFromArray:(float *)array size:(int)arrSize
{
float min;
int i;
min = array[0]
for(i=1;i<arrSize;i++)
if(array[i] < min)
min = array[i];
return min;
}
`
If you want to be sure, add a check of the arrSize > 0.
Marco
Thanks for all your answers and comments.. I learn a lot from you guys :)
I ended up using something like Martin suggested.
if (segmentValueNumber == 11){
float min = 100000000;
if(game51 > 0, game51 < min){
min=game51;
}
if(game52 > 0, game52 < min){
min=game52;
}
}
...............................................
I could not figure out how to implement it all into one array since each result depends on a segment control, and I think the program is more optimised this way since it only checks relevant variables.
But thanks again, you are most helpful..