0

Hey guys so i have a gps and i take the longitude and latitude through a library. I want to have 4 decimal digits so i want to convert the almost 20 decimal digits long numbers into 4 decimals so i send the coordinates into a function as shown below. I was doing a debugging in every step and i see inconsistencies with the type casting. Can you tell why i get that output? I mean i divide a number with decimal digits all zeros and i get something out of the ordinary as you can see in final variable thats ridiculous.

float conv_4digits(float coordinate){

  Serial.print("coordinate ");Serial.println(coordinate,30);

  float final;
  float var1;
  var1 = (int)coordinate;
  Serial.print("var1 "); Serial.println(var1,30);
  float var2 = coordinate - var1;
  Serial.print("var2 "); Serial.println(var2,30);
  var2 = var2 * 10000;
  Serial.print("var2 "); Serial.println(var2,30);
  float var3 = (int)var2;
  Serial.print("var3 "); Serial.println(var3,30);
  float var4 = var3 / 10000;
  Serial.print("var4 "); Serial.println(var4,30);
  final = var1 + var4;
  Serial.println(final, 10);

  return final;
}

OUTPUT

coordinate 34.684024810791015625000000000000
var1 34.000000000000000000000000000000
var2 0.684024810791015625000000000000
var2 6840.248046875000000000000000000000
var3 6840.000000000000000000000000000000
var4 0.684000015258789062500000000000
34.6839981079

Any clues? i tried to type cast integers where as applicable i got hex numbers.

Is there a better way to output the longitude and latitude with 4 decimal digits and store it in a variable? Because i want to make if statements and compare them against some fixed longitudes and latitudes with 4 decimal digits.

Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82

1 Answers1

0

You cannot just round floats to some arbitrary number of decimal digits and then hope that an equality comparison between the floats will work. Float equality comparison does not work, this is a very well known problem, for example see What's wrong with using == to compare floats in Java?

What it boils down to is that you cannot check whether X is equal to Y, (if X and Y are floats,) you can only check whether X is roughly equal to Y.

This means that instead of doing X == Y you will need to do if Y + e >= X && X >= Y - e, where e is some very small, but positive value.

So, since you are going to be doing range comparisons you do not need to be worried about rounding. Forget rounding.

Community
  • 1
  • 1
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • thank you the problem is that i didnt think of the variable with the 4 decimal digits as a float number with zeros behind it to correspond to the decimal digits range of the real time value. – Petros Kyriakou Feb 07 '15 at 22:07