Double precision options (software or hardware)

Hello all :slight_smile:

I am working on a project which will need to compute the distance between two GPS coordinates.
I have read that most arduinos treat "double" precision the same as "long" which would make GPS coordinate math (which require more significant digits after the decimal) wildly inaccurate.

So my question is, which Arduino (chipset specifically) is the minimum (in terms of complexity, power usage, etc) that can do true "double" precision floating point operations...

OR

Is there a clever workaround in software to alleviate the need for double precision.
i.e. some way to temporarily ignore the integer part of the number (left of the decimal place) in order to free up two additional digits for the "long" precision to work on the remaining number, then adding it back in (this is just a wild guess... sorry if it is terrible stupid).

Any kind of software workaround that would allow fairly precise GPS coordinate distance calculation on the minimal Arduino chipset would be fine with me.

Thanks in advance for any assistance!

--Sean

ATmega-based Arduinos treat "double" the same as "float" (not "long").

Several GPS libraries (e.g. TinyGPS) are available that store latitude and longitude as long integers, in degrees10^6 or degrees10^7, so there is no significant loss in precision.

However none that I know of do course calculations in integer units. If your navigational needs are for short distances (up to a few km), it is easy to use the equirectangular approximation for both waypoint projection and for bearing/distance calculations in integer units, with no significant loss in precision or accuracy. I can post a couple of routines for that, if you like.

To my knowledge, no one has implemented great circle calculations in integer units on the Arduino.

...fairly precise GPS coordinate distance calculation

As JRemington notes, the Haversine distance calculation with single-precision float is fairly accurate... about 1 meter or so. This is more accurate than the GPS device is reporting, so it's probably good enough. Are you using longer time periods or expensive GPS devices to get sub-meter accuracy in your coordinates?

I also second his suggestion about treating the lat/lon as planar coordinate (i.e., equirectangular), not spherical. It is ok for small distances, and it changes the Haversine calculation (expensive trig functions) to plain, old pythagorean distance calculations (one square root). Degrees of latitude are (fairly) constant, but degrees of longitude are smaller at the poles, requiring one trig function call to scale them, based on your latitude.

Of course, I have to pitch my NeoGPS library as possible solution. It handles the GPS coordinates as integer degrees x 107. They will retain about 10 significant digits, while single-precision float is only about 6. So the instant you switch to float, you will lose about 4 significant digits. Again, that will get you down to 1 meter or so. Is that good enough?

One advantage of NeoGPS is its more-robust examples. As you try to extend the examples from other libraries, you will eventually run into problems. If you stick with TinyGPS, I would suggest looking at the NeoGPS examples as a pattern for your own sketch.

Another advantage to NeoGPS is the amount of RAM and CPU time you could save. If you find yourself needing a 100 bytes to 1000 bytes of RAM, or a little more CPU time, you might take a look. NeoGPS is 40% to 70% faster, and uses 70% to 95% less RAM than other libraries. Even the program space can be smaller, depending on how many sentences and fields you really need.

jremington:
To my knowledge, no one has implemented great circle calculations in integer units on the Arduino.

Bahahaha! I'm actually working on that now. :smiley: It just seems a shame to pull in the floating-point libraries (add, subtract, multiply, sine, cosine, arctangent) just to get the distance. So yes, there is a "clever workaround." Printing lat/lon is another reason people convert the integers to floats, and I'm working on methods for that, too. Real Soon Now.

Cheers,
/dev

Might be useful - Angle library - Libraries - Arduino Forum