Math notation in Objective-C - iphone

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)));

Related

How to Cube Root in xcode?

I've been trying to make a specific calculation in xcode with user defined variables. Here's what I have:
-(IBAction)calculate:(id)sender{
NSString *oneField = self.one.text;
NSString *twoField = self.two.text;
double resultInNum;
double onedouble = [oneField doubleValue];
double twodouble = [twoField doubleValue];
And what I need to do, is cube root the outcome of
(twodouble/onedouble)
I can't quite find away. Any ideas?
Use the pow function from math.h
pow(x, 1.0/3.0)
Swift
Swift has cubic root functions built in. Here are the signatures:
public func cbrt(_: Double) -> Double
public func cbrtf(_: Float) -> Float
The definitions are located in Darwin module on OS X and Glibc module on Linux.
Include math.h and call pow():
pow(twodouble/onedouble,1.0/3);
Don't remember if math comes included but if it doesn't:
#import <math.h>
Then:
pow(twodouble/onedouble,1.0/3.0);
Taking something to 1/n is like taking the nth root. 1/3 is taking the cube root, 1/2 is taking the square root, etc.
Just raise the divided value to the power of 0.33 ?
NSLog(#"%.f", pow(result,0.33) );

How to Convert code to Obj-C?

How do I convert this to Objective-C?? Mainly this: float *newH= new float[newD];
What do I substitute new for in obj-c??
int newD = 100;
float *newH = new float[newD];
for(int i=0; i<newD; i++){
newH[i] = 0.0f;
}
Objective-C is a true superset of C. In C, one uses malloc to allocate memory.
So,
float *newH = malloc( newD * sizeof( float) );
Of course, depending on what you are really doing, you may also want to investigate NSArray and NSNumber.
Note, that Objective-C++ is available as well and you can continue to use 'new' to allocate your memory.
If you have a lot of C++ code to port, you might be better off writing your app in Objective-C++. Then you can use your C++ code as is.
I agree that Objective C++ is the way to go. However, if you absolutely want to stay within the spirit of Objective C (not straight C), here are the options:
use an NSArray of NSNumber objects. This is slow - float boxing penalty applies.
use a NSData as a allocation mechanism, cast [bytes] to float *. This is ugly, the way pointer typecasts are.

how can i swap value of two variables without third one in objective c

hey guys i want your suggestion that how can change value of two variables without 3rd one. in objective cc.
is there any way so please inform me,
it can be done in any language. x and y are 2 variables and we want to swap them
{
//lets say x , y are 1 ,2
x = x + y; // 1+2 =3
y = x - y; // 3 -2 = 1
x = x -y; // 3-1 = 2;
}
you can use these equation in any language to achieve this
Do you mean exchange the value of two variables, as in the XOR swap algorithm? Unless you're trying to answer a pointless interview question, programming in assembly language, or competing in the IOCCC, don't bother. A good optimizing compiler will probably handle the standard tmp = a; a = b; b = tmp; better than whatever trick you might come up with.
If you are doing one of those things (or are just curious), see the Wikipedia article for more info.
As far as number is concerned you can swap numbers in any language without using the third one whether it's java, objective-C OR C/C++,
For more info
Potential Problem in "Swapping values of two variables without using a third variable"
Since this is explicitly for iPhone, you can use the ARM instruction SWP, but it's almost inconceivable why you'd want to. The complier is much, much better at this kind of optimization. If you just want to avoid the temporary variable in code, write an inline function to handle it. The compiler will optimize it away if it can be done more efficiently.
NSString * first = #"bharath";
NSString * second = #"raj";
first = [NSString stringWithFormat:#"%#%#",first,second];
NSRange needleRange = NSMakeRange(0,
first.length - second.length);
second = [first substringWithRange:needleRange];
first = [first substringFromIndex:second.length];
NSLog(#"first---> %#, Second---> %#",first,second);

three integers compare

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.

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.