small problem with float

I presume you mean when you print it, in which case this little sketch will print 34.13 on the serial monitor.

void setup(void)
{
  float a = 34.125456;
  Serial.begin(9600);
  Serial.println(a,2);
}

void loop(void)
{}

Pete

no ... i want to save it in adeffrent variable ... so i can use it in math. equation after that .

louish:
no ... i want to save it in adeffrent variable ... so i can use it in math. equation after that .

Why does it matter for the calculation?

float source = 34.125456;
int temp;
float destination;

temp = source * 100;
destination = (float)source / 100.0;

or

float source = 34.125456;
float destination;

destination = floor(source*100.0)/100.0;

okk here is the problem : my project mesures distance between 2 points with gps and xbee , so far im able to collect the latitude and longtitude for the points in 4 variable , but the the coordinates that im getting from the gps changes rapidly after 3 points in both long and lat and that effects the result so badly. so what i need is to get numbers in this format for example :

lat1 : 32.11 , lon1 : 34.8717
lat2 : 32.11 , lon2 : 34.8716

Or better yet, make a function macro to round any number to any decimal point. Put this at the top of your sketch:

#define roundTo(a,b) round(a*(1/b))/(1/b)

Here are some examples:

float num1 = roundTo(34.789854, 0.01); //returns 34.79
byte num2 = roundTo(34.789854, 10); //returns 30
float num3 = roundTo(34.789854, 0.1); //returns 34.8

If you want to drop the extra numbers instead of rounding them, change round to floor.

noooo i can't round at all every number is extreemly important for distance calculations .

If you want to get a more "stable" value, you can get a whole bunch of readings, and average them.

how it can be done ?

Hi louish

I think you are doing it wrong - you should keep the high resolution numbers as long as possible.

Recalculate the distance even if the responce from the gps is noisy and then you should decide if the new distance has changed enough -or you could make a running average.

-Fletcher

Look at the Smoothing example, and use floats or doubles instead of ints.

I wouldn't bother with doubles; they're overrated on the Arduino :wink:

Double == float on arduino.

i don't think smoothing is the solution for my problem because the gps transmitting NEMA protocol via serial communication in pin 2,3 . from the protocol im only interested in position and i've no way to get the position exept using this command : gps.f_get_position(&lat11, &lon11);
and this comand doesn't give me any choice to control the numbers i get so im storing the data in deferent variables to get the numbers as i want and its not workinig so far /]

What do you think the solution might be?
You don't really say what the problem is.

all i want is to get number from this command : gps.f_get_position(&lat1, &lon1);
in this format :
lat1 = 32.11
lat2=34.78755
so i can use them with this command : gps.distance_between (lat1,lon1,lat2,lon2);
if i send lat1 and lon1 as they come out from :gps.f_get_position(&lat1, &lon1);
i get wrong answer but if i send them as shown above it works

A variation of 0.01 equates to approx half a mile.
So, +/- this on two readings could be a mile out! :slight_smile:

What target accuracy are you trying to achieve?

yes i know i want the latitude to be 2 points after the decimal and longititude to be 4 points after the decimal for example :

lat = 32.12
lon=34.1234
im trying to dig in the headers and cpp files of tiny gps library to change the way: gps.f_get_position(&lat1, &lon1);
return numbers but with no luck untill now .

IEEE floating point has that problem, like when you should have 1.0 but you get .999999.

If you can find the way, do your work using fixed-point with unsigned longs or long longs and the digits won't shift. Even signed longs will give you a solid 9 places.

GoForSmoke is right. Floating point representation in computers is largely approximate because of the way that the numbers are represented. If you want accuracy you should long integers (for example 32.11 can be 3211). As long as all the number are scaled up the same way (eg, by 100 in this case) then you are all correct and at the end you just insert the decimal in the right place if you need to.