variable printout error

I'm trying to print gps data to the serial monitor using this code

long Xcurrent = gps.location.lat(); // stores the current location
long Ycurrent = gps.location.lng();

Serial.print(Xcurrent, 4);
Serial.print(F(","));
Serial.print(Ycurrent, 4);

but when I see the coordinates on the serial monitor, It shows this:

231,3333333333332313

231 is the latitude and 3333333333332313 is the longitude

but when I upload this:

long Xcurrent = gps.location.lat(); // stores the current location
long Ycurrent = gps.location.lng();

Serial.print(Xcurrent, 2);
Serial.print(F(","));
Serial.print(Ycurrent, 2);

I get this:
101101,11111111111111111111111110110111

It looks like you're telling it to output binary format. If you define Xcurrent and Ycurrent as floats, does it give you the expected output?

The functions gps.location.lat() and gps.location.lng() return 'double' (a.k.a. 'float'), not 'long'. If you use floats you will only have 6 to 7 significant digits.

If you use gps.location.rawLat() and gps.location.rawLng() you will get values in billionths of a degree (9 digits after the decimal point).

ok that makes sense, but the Serial.print(Xcurrent, 2); outputs binary while Serial.print(Xcurrent, 4); outputs numbers 0-3 so how would I make the monitor printout a number with 4 decimal places?

Nathaniel_:
ok that makes sense, but the Serial.print(Xcurrent, 2); outputs binary while Serial.print(Xcurrent, 4); outputs numbers 0-3 so how would I make the monitor printout a number with 4 decimal places?

Did you try declaring Xcurrent and Ycurrent as floats?

It works! thanks

I have an issue in the code that I suspect is very similar

float RuleOfFunction = (734719-Y)/(453415 -X);
Serial.print(RuleOfFunction, 6); // Y2-Y1 / X2-X1

on the serial monitor it shows this : 1.000000

Are X and Y floats?
Just a hunch but does it work if you write it as:

float RuleOfFunction = (734719.0-Y)/(453415.0 -X);

Yes, it works, but I'm not sure why that makes it work

One of the old guys will be able to explain it more technically than I but in your version, the compiler assumes you're working with integers because they are written as whole numbers. So it does integer arithmetic on them (meaning all fractional parts of numbers are discarded). By adding the ".0" at the end, you tell it that you want it to deal with them as floating point numbers.

oh ok, thanks for explaining it!

A throwback from when? 1970 about?

-jim lee :confused:

Nathaniel_:
I'm not sure why that makes it work

The key-phrase is "arithmetic conversion". There are other key-phrases like "numeric promotion" and "implicit conversion" that overlap.

Basically, the C++ compiler uses the "narrowest" possible datatype going from left-to-right. If everything is int then the arithmetic is performed using int. If the first two arguments are int and a third is long then the arithmetic on the first two arguments is int which is "widened" to long for the final operation with the third argument. The suggestion to add a decimal to the first argument makes all the arithmetic performed using double with implicit conversions added as needed.

Note to @jimLee: you misspelled "Arduino".

Ha! I did! That took what? 2 years for anyone to notice?

Good job!

-jim lee