CLLocationmanger - decimal points for lat and long - ios

To find my phone's lat and long (current location) - i am using CLLocationmanager.
Whenever I try to fetch the location the decimal points are up to 6 values, like 10.007841 and 76.147856.
How can I make it is 8 decimal points.
I have given -
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;

I think what you mean is that when you print them out, you see 6 values. This is a property of "printf" formatting - the default is 6. The actual location information is stored as a double. So if you print out the number as:
NSLog(#"location %.8lf", location.latitude);
you will get 8 digits.

Related

Why multiply two double in dart result in very strange number

Can anyone explain why the result is 252.99999999999997 and not 253? What should be used instead to get 253?
double x = 2.11;
double y = 0.42;
print(((x + y) * 100)); // print 252.99999999999997
I am basically trying to convert a currency value with 2 decimal (ie £2.11) into pence/cent (ie 211p)
Thanks
In short: Because many fractional double values are not precise, and adding imprecise values can give even more imprecise results. That's an inherent property of IEEE-754 floating point numbers, which is what Dart (and most other languages and the CPUs running them) are working with.
Neither of the rational numbers 2.11 and 0.42 are precisely representable as a double value. When you write 2.11 as source code, the meaning of that is the actual double values that is closest to the mathematical number 2.11.
The value of 2.11 is precisely 2.109999999999999875655021241982467472553253173828125.
The value of 0.42 is precisely 0.419999999999999984456877655247808434069156646728515625.
As you can see, both are slightly smaller than the value you intended.
Then you add those two values, which gives the precise double result 2.529999999999999804600747665972448885440826416015625. This loses a few of the last digits of the 0.42 to rounding, and since both were already smaller than 2.11 and 0.42, the result is now even more smaller than 2.53.
Finally you multiply that by 100, which gives the precise result 252.999999999999971578290569595992565155029296875.
This is different from the double value 253.0.
The double.toString method doesn't return a string of the exact value, but it does return different strings for different values, and since the value is different from 253.0, it must return a different string. It then returns a string of the shortest number which is still closer to the result than to the next adjacent double value, and that is the string you see.

How to Convert Decimal coordinates to structured Coordinates in Delphi?

Hey there guys im working on a project for school and in the project it requires the user the enter decimal coordinates and convert it into structured format, ie:
17.428333° --> 17° 25' 42"
Does anyone possibly know how to do this? Also the program should be able to do it the opposite way; enter structured coordinates 17° 25' 42" and covert to decimal coordinates 17.4283333.
I have got the basic maths used for the converting. To convert from structured coordinates to decimal coordinates you can use the following :
17+25/60+42/3600
and to covert from decimal to structured takes first value 17 as the degrees then to work out the minutes
17.4285-17=0.4285*60=25.71
gets rid of the decimal or rounds the decimal to get the minutes, then to work out the seconds
25.71-25=0.71*60=42.6
either rounds the decimal or gets rid of the decimal for seconds.
Let's assume you start with a coord in a floating point variable x. Calculate degrees, minutes and seconds like this:
Degrees := Trunc(x);
x := (x - Degrees)*60;
Minutes := Trunc(x);
x := (x - Minutes)*60
Seconds := Round(x);
The opposite direction is simpler. The expression you need is:
Degrees + Minutes/60 + Seconds/3600

iOS: NSDecimalNumber calculations and accuracy with currencies

I'm calculating back and forth some values and that's where I get the problem. Let's say you have a yearly amount of 100 Dollar and save them as as monthly value to CoreData using decimalnumbers:
[self.sumPerMonth decimalNumberByAdding:[amount decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:#"12"]]];
Ok so my 100 Dollars are now 8.33333333333333.... Now in some places in my app I want to get the value back to yearly. So if I calculate now 12*8.33333333 I don't get 100 but something very close: 99.999999999999999999999999999999999996.
For a progress bar I'm doing this and this results in 100% which is what I actually want (moneySpent being 100 and budget being 99.999999999999999999999999999999999996):
[[moneySpent decimalNumberByDividingBy:budget] doubleValue]
But if I'm doing this (budgetLeft being budget-moneySpent):
[budgetLeft compare:[NSDecimalNumber zero]] == NSOrderedAscending
This results in TRUE, which is wrong. With currencies this should be 0 because 99.999999...6 should be 100.
So how would you handle this? I'm a bit confused...should I only compare using doubleValues? Or should I always round the NSDecimalNumbers or what?
You can use the decimalNumberByRoundingAccordingToBehaviour: method of NSDecimalNumber whenever you need to round back to 'currency'. The NSDecimalNumberBehaviours protocol allows you to specify the rounding mode and the scale (decimal places) - so you can round to two decimal places whenever you need an actual currency amount - typically at the end of any calculations.

Invalid latitude and longitude in android

I testing the below code snippet. I suddenly get a weird location (0,0) as latitude and longitude which disturbs my distance calculation.
if (loc != null) {
double currentLatitude = loc.getLatitude();
double currentLongitude = loc.getLongitude();
if (first_time) {
loc.reset();
first_time = false;
}
synchronized (testObject) {
… do some work
previousLat = currentLatitude;
previousLon = currentLongitude;
}
}
I would like to know in what situations does the latitude and longitude become 0. I also would like to know where my code bugs out due to which I am getting 0 latitude and longitude. Is location.reset() results in 0 latitude and longitude?
Of course it is 0 when you reset it. Why do you do that? which value you would expect when calling reset()?
A location delivered by android location services has an acuracy set (getAccuracy()). If you create your own location object or reset() it, it will be 0.
Further info I have written before, shows why i always check the validity before calculating:
lat,lon 0,0 is theoretically a valid location. but in my years of GPS experience, i give you the tipp: ignore all locations with coord (0,0).
This has no impact on real world behavipur, bevause a device cannot be located exactly at 0,0 (with centimeter accuracy), and even if it could, then not more than one second.
If you want you can init your lat,lon with a special value out of the range of the coord range. but even then you are not save from errors of other people.
Generally check if the location is valid, if you have a valid flag,which should exist on android. look at the horicontal accuracy value. read the docu how to distinguish between invalid locations.

Latitude and Longitude conversion

I need to convert a latitude in ddmm.mmmmm (minutes in 4 decimal places) to ddmm.mmmmmm (minutes in 5 decimal places) format. Is there any good formula to convert this ?
I got the answer
We need to follow these steps for this conversion
1. Convert value in ddmm.mmmm format to dd.ddddddd by using the following formula
dd.ddddddd = dd + ( mm.mmmm / 60 )
convert back
ddmm.mmmmm = concat(dd, (.dddddd * 60))
Example:
To convert 3323.8733 from ddmm.mmmm format
convert to degrees (dd.dddd) format
33 + (23.8733 / 60 ) = 33.397888333333334
convert back to ddmm.mmmmm format
multiply decimal part by 60 i.e 0.397888333333334 * 60 => 23.87330000000004
append with degree
3323.87330000000004
As we need ddmm.mmmmm we can round of 5 decimal places i.e 3323.87330
Sans other information I would recommend following mkk's advice.
If you want to convert "ddmm.mmmmm" (4 decimal places) to "ddmm.mmmmm" (5 decimal places), you should probably just add a zero to the end.
Other methods may appear to give a more satisfactory result by placing a non-zero value in the fifth decimal place. But they cannot add more information than was present in the original number. There is, however, the potential to lose information through loss of significance in mathematical calculations.

Resources