How to compare doubles in Objective-C [closed] - ios

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm developing an app that uses GPS position. For knowing position I get latitude and longitude.
I don't get position at regular intervals, but I want to know if user has moved. For that I store last position and current position (both latitude and longitude)
I was doing this:
if (latitudNew != latitudOld && longitudNew != longitudNew)
{
float R = 6378.137;
double dLat = ((latitudNew - latitudOld) * M_PI) / 180;
double dLong = ((longitudNew - longitudOld) * M_PI) / 180;
double a = sin(dLat/2) * sin(dLat/2) + cos((latitudOld*M_PI)/180) * cos((latitudNew*M_PI)/180) * sin(dLong/2) * sin(dLong/2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = R * c;
}
Where distance is stored in d.
My problem is that it never get past the if condition, so I know I'm not comparing it correctly.
I can't use this solution, as I can have negative numbers (one or both or none).
Any ideas??

You have the line:
if (latitudNew != latitudOld && longitudNew != longitudNew)
This is doing longitudNew != longitudNew. You have a typo there, you're testing to see if a variable is not equal to itself. It'll always be false. You meant to put longitudOld for one of them. Though you probably want to replace the && with ||, because you want to execute the code in the if block if either of these conditions is true.
Also, this is a bit of a nit... The proper spellings are longitude and latitude. You left the e off of both.

Comparing doubles isn't a good idea. What you'll need to do is to subtract the two values, then check if the difference between the two is greater than a threshold value you'll need to define, like 0.0001.

That looks correct to me, you'd compare doubles in Objective-C just as you would in C. The datasource you're getting the information from might be rounding the values so that they appear as equal or the data isn't updating often enough.
Also, you'd probably want to change the logic to have an || in the if instead of an &&. You'd still consider an update in location to be an update, even if it was just in one of the two.

Related

question on coding take profit point of mt4

I have question on take profit point of mt4:
I would like to set take profit point to stop loss * 2, so the code should be:
takeprofit = NormalizeDouble((Ask + ((Ask - stoploss)*Point()*2) + StopLevel*Point()),Digits);
However, it won't works, so anyone know how to fix it? I am new to mq4 coder. Thanks
Remember that you need to differentiate between BUY and SELL orders.
You also look like you are getting confused with stoploss and StopLevel. I'm assuming that your StopLevel is the distance of your stoploss in points.
If that is the case, your code should look something like this (you don't need to reference StopLevel if you already have the distance in Points):
if(ordertype==OP_BUY) takeprofit = NormalizeDouble(Ask+StopLevel*2*Point,Digits);
else takeprofit = NormalizeDouble(Bid-StopLevel*2*Point,Digits);

Decimal value is not recognized by F# console [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm practicing F# for the first time and I thought I'd try to make a program that would calculate the areas of different kinds of shapes. However, the portion that finds the area of a circle is giving me a lot of trouble.enter code here
elif stringInput = "2" then
let PI = 3.14156
printfn "What is the circle's radius: "
let radiusString = System.Console.ReadLine()
let radiusInt = radiusString |> float
let cirlceArea = (radiusInt * radiusInt) * PI
printfn "The area of the circle is : %d" cirlceArea
I'm sure it has something to do with the radiusString |> float part of the code, but nothing I've tried works and I've had no luck in finding any examples that can help. What can I do?
Ok I just found out the problem was that I was using %d instead of %f

Generate random number & after adding/subtract them comparing the result in objective c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
As I'm from other scripting language background, I'm not so much expert in objective-c & iPhone programming.
I've tried some code of objective-c myself, but they aren't working as I expected.
I have elaborated my requirements on this image:
So I'm seeking a help from the experts about my program-
I want to make a program for iPhone in objective-C that will generate random number & generate signs (like +, -, *, /).
Then it will subtract/add/multiply/divide with random values in 2 different sections.
Now users have to compare between two sections.
Is the result of 2 sections are = or < or > . There will be three individual buttons for the signs.
After getting the press on the button from the user a new label will show the answer, if it is right/wrong.
If the answer is wrong, it will show the correct answer also.
Any help on this program on objective-c language required.
Thanks in advance.
To generate a random number in a range use:
int randomInteger = arc4random_uniform(range);
To generate a random operator character:
NSArray *operators = #[#"+", #"-", #"*", #"/"];
int randomOperatorIndex = arc4random_uniform(4);
NSString *randomOperator = operators[randomOperatorIndex];
The rest of the code you need to figure out and write yourself. If you have problems post another question with the problem code for help with it.
Notes:
arc4random() is a high quality 32-bit pseudo-random number generator that does not need to be seeded, it seeds itself on a regular basis from the kernel strong random number subsystem.
arc4random_uniform(range) returns an integer in the range of 0 to (range-1). It has the added benefit that it avoids "modulo bias" that the mod (%) operator does.
Use arc4random() to generate random no.
Use following to generate random operator
-(NSString*) getRamdomOperator
{
int randNum = arc4random_uniform(4);
NSString *operator;
switch(randNum)
{
case 0:
operator = #"+";
break;
case 1:
operator = #"-";
break;
case 2:
operator = #"/";
break;
case 3:
operator = #"*";
break;
Default:
break;
}
return operator;
}

Looping through NSArray while subtracting values from each object? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have NSNumber objects stored in an NSMutableArray. I am attempting to perform a calculation on each object in the array.
What I want to do is:
1) Take a random higher number variable and keep subtracting a smaller number variable in increments until the value of the variable is equal to the value in the array.
For example:
NSMutableArray object is equal to 2.50.
I have an outside variable of 25 that is not in the array.
I want to subtract 0.25 multiple times from the variable until I reach less than or equal to 2.50. I also need a parameter so if the number does not divide evenly and goes below the array value, it resorts to the original array value of 2.50.
Lastly, for each iteration, I want to print the values as they are counting down as a string.
I was going to provide code, but I don't want to make this more confusing than it has to be.
So my output would be:
VALUE IS: 24.75
VALUE IS: 24.50
VALUE IS: 24.25
…
VALUE IS: 2.50
END
I'm on my iPad so this won't be as nice as I would like
float randomValue = 25; //or however you get the value
float subtractionValue = 0.25 // or whatever value or use a define
for (NSNumber *n in myArray)
{
while (randomValue > n.floatvalue)
{
randomValue -= subtractionValue;
NSLog(#"Value is: %f", randomValue);
if (randomValue < 2.5)
{
randomValue = n.floatvalue;
}
}
}
Well run the while loop and check like that below:-
Lets assume your variable here whose value is 25
float a=25;
while (a>=2.50)
{
a=a-0.25;
NSlog(#"count is %f",a);
}
Hope it helps, it is just the logic you can try with your code and if required please modify accordingly.

Round floating value to .1 (tens place) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I round a float value to 2 post decimal positions?
Lets say I have a double number of 3.46.
How do I round it to 3.50?
I tried
NSLog(#"res: %.f", round(3.46));
but it return 3.
Do some calculations....
float f=3.46;
float num=f+0.05;//3.51
int intNum=num*10;//35
float floatNum=intNum/10.0;//3.5
NSLog(#"res: %.2f", floatNum); //3.50
Following code may help
i = roundf(10 * i) / 10.0;
where i is your float variable
If you're willing to live with the rounding rules from printf, then the following should suffice when rounding for presentation:
NSLog(#"res: %.1f0", 3.46);
Note that the 0 is just a normal character that is added after the value is formatted to the appropriate number (1) of decimal places. This approach should be usable with [NSString stringWithFormat:] as well.
The original code results in "3" because round always returns an integral value.
YMMV; I don't even use iOS.

Resources