I'm quite a novice in arduino programming. I am doing a small home project and I am using sim808 to obtain GPS data.
My question is quite simple but i can't find any solution online.
Serial.print("latitude :");
Serial.println(sim808.GPSdata.lat, 10);
Serial.print("longitude :");
Serial.println(sim808.GPSdata.lon, 10;
This code display Longitude and Latitude data on serial monitor to 10 decimal place perfectly.
But when after setting the Longitude and Latitude to float.When I use this code, I don't get a valid gps data.
Longitude = (sim808.GPSdata.lon, 10 );
Latitude = (sim808.GPSdata.lat, 10 );
Please help, Thanks.
(edited) I understand my error in coding for the above.
In fact, I am using a DF robot shield sim 808 for vehicle tracking. I am uploading my GPS data, longitude, latitude and speed to Ubidots. The only difficulty is in saving accurate data.
I am using this codes
Longitude = sim808.GPSdata.lon;
Latitude = sim808.GPSdata.lat;
Speedperkm = sim808.GPSdata.speed_kph;
client.add("Longitude", Longitude);
client.add("Latitude", Latitude);
client.add("Speed per Km", Speedperkm);
The code is working fine but only GPS data of 2 decimal place is being uploaded. If data of up to 6 decimal place is uploaded then the system will more accurate.
That my problem that I am trying to solve.
2 Answers 2
But when after setting the Longitude and Latitude to float.When I use this code, I don't get a valid gps data.
Some statements in C/C++ are not what they might seem. The value of the statements:
Longitude = (sim808.GPSdata.lon, 10 );
Latitude = (sim808.GPSdata.lat, 10 );
Are actually the same as:
sim808.GPSdata.lon;
Longitude = 10;
sim808.GPSdata.lat;
Latitude = 10;
Which is then optimized to:
Longitude = 10.0;
Latitude = 10.0;
That is, when using list notation, the value of the expression is the same as the value of last expression in the list.
The other answers have covered the fact that float and double on Arduino (AVR-GCC) are only 32-bit, and that gives 6 to 9 significant decimal digits precision.
Cheers!
-
i get your point completelyuser32149– user3214903/23/2017 14:07:16Commented Mar 23, 2017 at 14:07
You always should calculate with the maximum resolution (not rounding to 10 digits), so use:
Longitude = sim808.GPSdata.lon;
Latitude = sim808.GPSdata.lat;
For printing you can use the code you have.
Serial.print("latitude :");
Serial.println(sim808.GPSdata.lat, 10);
Serial.print("longitude :");
Serial.println(sim808.GPSdata.lon, 10);
-
Floats are always exact numbers, and 10.0 will always be 10, never 9.9999999999. You get rounding errors only when trying to represent as a float a number that cannot be exactly representable as a float.Edgar Bonet– Edgar Bonet03/23/2017 09:52:49Commented Mar 23, 2017 at 9:52
-
@EdgarBonet not according technet.microsoft.com/en-us/library/ms187912(v=sql.105).aspx ... I mean a float as type (probably you mean a 'mathematical float' ?Michel Keijzers– Michel Keijzers03/23/2017 09:58:03Commented Mar 23, 2017 at 9:58
-
2Every float represents a number, in the mathematical sense of the word "number", i.e. defined with infinite precision. Most numbers are not representable as floats, and can thus only be approximated by floats. Every single integer in the range ±16777216 (and this include the number 10) is an exact float.Edgar Bonet– Edgar Bonet03/23/2017 10:05:03Commented Mar 23, 2017 at 10:05
-
@EdgarBonet Yes you are completely right about the integers ... I will update the answer. Thanks.Michel Keijzers– Michel Keijzers03/23/2017 10:34:03Commented Mar 23, 2017 at 10:34
float
has roughly 7-digit resolution. Printing it with 10 decimal places is ludicrous.double
instead offloat
help?double
is the same asfloat
.