Float or Double? GPS

I'm using float in my project to get the GPS data, float store 32bits, 4bytes of data and up to 7digits as
the arduino website describe but they also say that double hold the same value as float but it can hold up to 64bits 8bytes, can a atmega328 handle double ?

I'm using GPS and as I read to get an exact logation you need 7digits, **.7 at least to get an exact location but the Float gives me only 5 after coma.

Can atemga328 and I2C handle double?

On ATMega boards, a double is the same size as a float - 32-bits.
From the reference:-

double
Description

Double precision floating point number. On the Uno and other ATMEGA based boards, this occupies 4 bytes. That is, the double implementation is exactly the same as the float, with no gain in precision.

On the Arduino Due, doubles have 8-byte (64 bit) precision.

Whenever you have a question like this, it's faster to check the Arduino reference pages than it is to start a thread with the question.

On ATMega boards, a double is the same size as a float - 32-bits.

So that means I can't get bigger numbers than 7 digits?

If you need more precision, have a look at fixed point notations :wink:

Domino60:
So that means I can't get bigger numbers than 7 digits?

You can get more than 7 digits. Indeed, you can get up to 38 digits. However, the precision of a 32-bit floating point number on the Arduino is 6-7 digits (best to rely on 6). Everything after the 7th digit is the computer's best guess as to its value.

I mentioned that after 7 digits my other digits are kinda random :smiley: and i was like what the heck is going on :smiley: so yea i think im gonna stay with 7 digits,

12.34567 I think it's enough for gps cordinates

Domino60:
I'm using float in my project to get the GPS data, float store 32bits, 4bytes of data and up to 7digits as
the arduino website describe but they also say that double hold the same value as float but it can hold up to 64bits 8bytes, can a atmega328 handle double ?

I'm using GPS and as I read to get an exact logation you need 7digits, **.7 at least to get an exact location but the Float gives me only 5 after coma.

Can atemga328 and I2C handle double?

As OldSteve said, float and double are the same (32 bits or 4 bytes). This is also true for LONG DOUBLE. All floating point types (in AVR-GCC) are 32 bit only. :frowning:

As OldSteve said, float and double are the same (32 bits or 4 bytes). This is also true for LONG DOUBLE. All floating point types (in AVR-GCC) are 32 bit only.

Really sad, what other micro chips can handle 64bits? Can a atmega32u4?

It's not sad at all. An ATmega or ATmega32u2 are both 8-bit micro controllers. Handling 32-bit already takes some cycles. And then we're not even talking about floats! Floats are heavy, the 8-bit micros don't have float hardware so all is done in software. That's why it's in 99% of the cases (at least here on the forum) better to go with a fixed point notation. If you want to save 12,34567 (sorry, I'm European so a comma as decimal) why not simply save 1234567? Aka, comma-less. It's all just a matter of interpretation. If you know where the decimal is then it's all fine.

Think about it like with money. If you want to save an amount of money like €1,45 you can use a float and handle all the heavy float stuff (and maybe even some across nice float rounding errors :wink: ) or you can just save it in cents and save 145, easy!

This might work for you with a worse case resolution of about 11 inches and still use only four bytes. Just parse the NMEA sentence into the appropriate elements of the variable.

struct positionGPS
{
    uint8_t degrees;        // Degrees from 0 to 180
    int8_t  minutes;        // Minutes from 0 to 59 with + or - meaning east/west or north/south
    uint8_t seconds;        // Seconds from 0 to 59
    uint8_t hundredths;     // Hundredths of seconds from 0 to 99
};

positionGPS longitude;
positionGPS latitude;

Think about it like with money. If you want to save an amount of money like €1,45 you can use a float and handle all the heavy float stuff (and maybe even some across nice float rounding errors :wink: ) or you can just save it in cents and save 145, easy!

That's a really nice idea to end up with 145 euro instead of 1.45cEuro :smiley:

I understand the logic and it's really nice, didn't thought about it. But let me give you an example where this may fail using GPS

Let's say we are in a part of the world were we get this lat 12.3456789 (89 for more precision) and we write the code in the way that we understand were the coma is, but we sell/gift the circuit to a person that lives in the other side of the world with the GPS lat 1.2345678 now the code we wrote inside the chip will have
logic errors why?

Because we had this : 123456789 and we knew that the coma is after 2 digits
But in the other side of the world we get : 12345678 but the code will write
12.345678 instead of 1.2345678 now you may say we gonna add a zero in front of the digits but
it's too late because the circuit is on the other side of the world and it got code bugs.

:smiley:

I'm not saying that we can't fix that adding extra functions to determine were the coma is but i just gave an example of what could happen. We can write a function to determine 1st from the GPS after how many digits the coma is and then convert the double/float intro a float without commas. But that's extra work, extra space of code.

It's actually a good idea but even using a exact determination of the GPS the cheap GPS module on the market got a 5~10 even 30 meters of error so 5 digits after coma it's enough to use to see approximatly
were to go where you are or what ever the device u build is doing :smiley:

No. Why would you count digits from the left? The fixed point representation counts from the right.

Let's start from 123456789 as the internal representation for 12.3456789. What number is represented by 12345678? I think it should be 1.2345678. What number is represented by 1000? It should be shown on the screen as 0.0001 or 0.0001000 or maybe 00.0001000 depending on what makes sense for your display.

If you write bugs in your code then you know who to blame for the bug.

If you write bugs in your code then you know who to blame for the bug.

:smiley:

counting digits from the right? what about infinit numbers ?

For a computer they simply don't exist. Even for us humans they don't "exist". Ever written down a infinite number? Our minds can't comprehend it so we tend to use some rounded figure with some number of digits instead.

You can cheat with a fraction but you could do the same on a computer. But the moment you have to calculate or draw it you're going to round it if you have to use it as a human (not just write it down).

septillion:
Ever written down a infinite number?

Just a minute...I'm still writing it...

if you need 64 bit double:
why no use a ARM cpu (Arduino Zero or Due ) ?

Ever written down a infinite number?

I put my printer to print infinit numbers but got out of ink after 10min

Domino60:
:smiley:

counting digits from the right? what about infinit numbers ?

In what universe are there more than 180 degrees in a half circle?

In what universe are there more than 180 degrees in a half circle?

In the universe that a circle can be a sphere or infinit lines that intersect each other which
can be a line, a circle or a sphere or multiple spheres at the same time if count the 4th dimension
if we add as well the 5th dimension we can observe lines, circles, spheres inside each other in a different axes space and time.

Domino60:
In the universe that a circle can be a sphere or infinit lines that intersect each other which
can be a line, a circle or a sphere or multiple spheres at the same time if count the 4th dimension
if we add as well the 5th dimension we can observe lines, circles, spheres inside each other in a different axes space and time.

Well in that universe, your GPS won't work. So you should either upgrade your receiver, or stay in this universe. :slight_smile: