Go Down

Topic: small problem with float (Read 2 times) previous topic - next topic

AWOL

What do you think the solution might be?
You don't really say what the problem is.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

louish

all i want is to get number from this command : gps.f_get_position(&lat1, &lon1);
in this format :
lat1 = 32.11
lat2=34.78755
so i can use them with this command : gps.distance_between (lat1,lon1,lat2,lon2);
if i send lat1 and lon1 as they come out from :gps.f_get_position(&lat1, &lon1);
i get wrong answer but if i send them as shown above it works
louis

JohnSan

A variation of 0.01 equates to approx half a mile.
So,   +/-  this on two readings could be a mile out! :)


What target accuracy are you trying to achieve?


I'll be glad when I've had enough!

louish

yes i know i want the latitude to be 2 points after the decimal and longititude to be 4 points after the decimal for example :

lat = 32.12
lon=34.1234
im trying to dig in the headers and cpp files of tiny gps library to change the way: gps.f_get_position(&lat1, &lon1);
return numbers but with no luck untill now .
louis

GoForSmoke

IEEE floating point has that problem, like when you should have 1.0 but you get .999999.

If you can find the way, do your work using fixed-point with unsigned longs or long longs and the digits won't shift. Even signed longs will give you a solid 9 places.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

marco_c

#20
May 29, 2012, 01:35 am Last Edit: May 29, 2012, 02:08 am by marco_c Reason: 1
GoForSmoke is right. Floating point representation in computers is largely approximate because of the way that the numbers are represented. If you want accuracy you should long integers (for example 32.11 can be 3211). As long as all the number are scaled up the same way (eg, by 100 in this case) then you are all correct and at the end you just insert the decimal in the right place if you need to.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

el_supremo

Quote
i get wrong answer but if i send them as shown above it works

Give an example of the returned lat and long and the wrong answer. And then show why what you want to do with the numbers is an improvement.

Pete

louish

for example :
lat1=32.11   lon1=34.8780
lat2=32.11   lon2=34.8781

the distance is 9.4m
now i have lat1 and lon1 stored in the memory so i don't care about them . but to get lat2 and lon2 i need this function :
gps.f_get_position(&lat2, &lon2);
but this function keeps changing the coordinates all the time and any change in any digit is abig problem so i need to force this function to give the number as i want (2 digits . 4 digits) only ... this is how i done that with no sucess :
Code: [Select]
gps.f_get_position(&lat11, &lon11);
char buf2[30],buf3[5];
dtostrf(lon11, 2, 3, buf3);
float lon1= atof(buf3);
g=gps.distance_between (lat1,lon1,lat22,lon22);
louis

PeterH


to get lat2 and lon2 i need this function :
gps.f_get_position(&lat2, &lon2);
but this function keeps changing the coordinates all the time and any change in any digit is abig problem so i need to force this function to give the number as i want (2 digits . 4 digits) only


Is your GPS giving credible values for lat and long? To what resolution are they accurate? Since the float data type will be capable of holding numbers with more precision than the accuracy of your GPS, the least significant digits will be affectively noise. You could ignore these since they won't affect the accuracy of your calculation - the result you get will have more digits than are accurate and you will have to round or ignore the extra digits in any case, so there is no significant benefit to also doing that rounding/truncation to the input figures.

On the other hand, if the input figures from your GPS are varying wildly, maybe that is the root of your problem. If you're getting garbage in, everything else you calculate will also be garbage.
I only provide help via the forum - please do not contact me for private consultancy.

GoForSmoke

Peter, I think that the GPS data being stuffed into 32-bit floats is one source of error.

Code: [Select]

gps.f_get_position(&lat11, &lon11);
char buf2[30],buf3[5];
dtostrf(lon11, 2, 3, buf3);
float lon1= atof(buf3);
g=gps.distance_between (lat1,lon1,lat22,lon22);


Does dtostrf() as you use it do rounding, as in rounding up or down?

You could try converting the float to a string with more places than you need then placing a terminating zero after the last place you need, but with floats it may help repeatability while losing actual accuracy. In the very act of cutting off digits to get consistent results, the results will be consistently wrong.

Anyway here is how that would work. I have a C string buf[12] with a number in it.
text -- ASCII hex:
1.2345678 -- 31 2E 32 33 34 35 36 37 38 00 00 00
I cut off everything past the 4th decimal place:
1.2345 -- 31 2E 32 33 34 35 00 37 38 00 00 00

However when I convert that back into a float I don't expect it to equal 1.2345 but something close. I'm sure it works better with 64-bit floating point.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

louish

im using em406a and its giving me the right lat and long of my home i checked in google, the problem is the change in the last tow numbers , for example :
first call of the function : lat =32.1122
secound call :                 lat=32.1133
and this is a big problem for calculating distance .it works only with one change in the last number for example :

lat=32.1122
lat=32.1123
the distance with one number change and tow numbers change is completly defferent .
help me please ...
louis

Docedison

It sounds like there is an issue with the GPS unit. The numbers shouldn't jump around as you describe... 10th's of a second accuracy mean many meters difference in position. is it a 3V3 device? and if so do you have it interfaced properly? and is the device properly bypassed, like a 10 uF and a 100 nF capacitor right at the device?. Do you have a clean signal to the GPS Unit ?. In my direct experience even close to a window, operation of a GPS unit inside a building can be problematical. Mine will occasionally return bad data and be difficult to achieve lock.    IMO

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

louish

its 5 v device and its connected with gps arduino shield and the signal is ok im getting the time and speed and altitude with no problems ...
i think that the problem is with this function: gps.f_get_position(&lat2, &lon2);
but im not sure ...
louis

GoForSmoke

What region/country do you live?
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

majenko

If you are doing constant reading (always updating your location) then a rolling average would definitely be best.  That would smooth out those inconsistancies.

I have written a small "average" library: http://hacking.majenko.co.uk/libs/average which does rolling averages for you:

Code: [Select]


#include "Average.h"
#define SMOOTH 10
float h_lat[SMOOTH];
float h_lon[SMOOTH];

...

gps.f_get_position(&lat2, &lon2);
lat2 = rollingAverage(h_lat,SMOOTH,lat2);
lon2 = rollingAverage(h_lon,SMOOTH,lon2);


If you're not doing constant reading, then you could do say 10 reads, and add them to a total (starting at 0), then divide that result by 10, to give a more accurate reading:

Code: [Select]

float lat2,lon2;
float tot_lat,tot_lon;
unsigned char i;

tot_lat = tot_lon = 0;

for(i=0; i<10; i++)
{
  gps.f_get_position(&lat2, &lon2);
  tot_lat += lat2;
  tot_lon += lon2;
}
lat2 = tot_lat/10.0;
lon2 = tot_lon/10.0;
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Go Up