2

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.

  • 1
    A float has roughly 7-digit resolution. Printing it with 10 decimal places is ludicrous. – Edgar Bonet Mar 23 '17 at 9:55
  • 1
    Please add the library (source) for "client.add()". Impossible to guess what you are using. Also you should start a new issue if the original question has been answered even though the issue has evolved. How could others find information if the question changes? – Mikael Patel Mar 23 '17 at 14:33
  • does using double instead of float help? – dandavis Mar 23 '17 at 18:09
  • @dandavis: It might help on an ARM-based Arduino, like the Due. On AVR (most Arduinos), double is the same as float. – Edgar Bonet Mar 24 '17 at 9:10
4

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 completely – user32149 Mar 23 '17 at 14:07
1

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 Mar 23 '17 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 Mar 23 '17 at 9:58
  • 2
    Every 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 Mar 23 '17 at 10:05
  • @EdgarBonet Yes you are completely right about the integers ... I will update the answer. Thanks. – Michel Keijzers Mar 23 '17 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.