Pages: [1] 2   Go Down
Author Topic: small problem with float  (Read 2063 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

louis

Offline Offline
Edison Member
*
Karma: 50
Posts: 1701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

void loop(void)
{}

Pete
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

louis

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
float source = 34.125456;
int temp;
float destination;

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

or

Code:
float source = 34.125456;
float destination;

destination = floor(source*100.0)/100.0;
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

louis

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

louis

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how it can be done ?
Logged

louis

0
Offline Offline
Full Member
***
Karma: 2
Posts: 156
It was all digital
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wouldn't bother with doubles; they're overrated on the Arduino  smiley-wink
Logged

"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.

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Double == float on arduino.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

louis

Pages: [1] 2   Go Up
Jump to: