Go Down

Topic: small problem with float (Read 2290 times) previous topic - next topic

louish

hii all ... if i'm gettinig afloat values lets say 34.789854 and i want only 34.78 how it could be done ?

louis

el_supremo

I presume you mean when you print it, in which case this little sketch will print 34.13 on the serial monitor.
Code: [Select]
void setup(void)
{
  float a = 34.125456;
  Serial.begin(9600);
  Serial.println(a,2);
}

void loop(void)
{}


Pete

louish

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

James C4S


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?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

majenko

Code: [Select]

float source = 34.125456;
int temp;
float destination;

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


or

Code: [Select]

float source = 34.125456;
float destination;

destination = floor(source*100.0)/100.0;


louish

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

louis

dkl65

Or better yet, make a function macro to round any number to any decimal point. Put this at the top of your sketch:
Code: [Select]
#define roundTo(a,b) round(a*(1/b))/(1/b)
Here are some examples:
Code: [Select]
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.
Visit http://www.glenof.webs.com/!

louish

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

majenko

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

louish

louis

Fletcher Chr

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

dkl65

Look at the Smoothing example, and use floats or doubles instead of ints.
Visit http://www.glenof.webs.com/!

AWOL

I wouldn't bother with doubles; they're overrated on the Arduino  ;)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

majenko

Double == float on arduino.

louish

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 /]
louis

Go Up