Pages: 1 [2]   Go Down
Author Topic: small problem with float  (Read 1889 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26324
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you think the solution might be?
You don't really say what the problem is.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

louis

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


What target accuracy are you trying to achieve?


Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .
Logged

louis

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 28, 2012, 07:08:26 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Edison Member
*
Karma: 48
Posts: 1632
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

louis

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ...
Logged

louis

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2892
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ...
Logged

louis

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What region/country do you live?
Logged

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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:
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;
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: 1 [2]   Go Up
Jump to: