Go Down

Topic: Problems with floating point division (Read 599 times) previous topic - next topic

Justin56

I'm having problems dividing a floating point number and getting a floating point answer.  It boils down to normally being around 50.3959/60 to convert minutes to degrees, but when I attempt to do that the number returned is a 0.

Code: [Select]

float convertLat(char lat[9])
{
 float latFloat;

 latFloat = (lat[0]-0x30)*10 + (lat[1]-0x30) + ((lat[2]-0x30)*10 + (lat[3]-0x30) + (lat[5]-0x30)/10 + (lat[6]-0x30)/100 + (lat[7]-0x30)/1000 + (lat[8]-0x30)/10000)/60;
 return latFloat;
}



crimony

The problem here is that the float conversion only happens after all the arithmetic evaluation takes place, ie. all the divisions in your code are integer.

You need to cast the chars to floats before you divide and use float literals (like 10.0) rather than integer literals (like 10):

Code: [Select]
latFloat = (lat[0]-0x30)*10 + (lat[1]-0x30) + ((lat[2]-0x30)*10 + (lat[3]-0x30) + (float)(lat[5]-0x30)/10.0 + (float)(lat[6]-0x30)/100.0 + (float)(lat[7]-0x30)/1000.0 + (float)(lat[8]-0x30)/10000.0)/60.0;

RIDDICK

i would recommend to use integers as long as possible and then use float/double-multiplication instead of float-division in order to save processing time (IIRC)...

example (i assume: 0x30 <= lat < 0x3a -- ascii: '0'..'9'):
(lat[0]-0x30)*10 +
(lat[1]-0x30) +
(
(lat[2])-0x30)*10 +
(lat[3])-0x30) +
(lat[5])-0x30)*1e-1 +
(lat[6])-0x30)*1e-2 +
(lat[7])-0x30)*1e-3 +
(lat[8])-0x30)*1e-4
)*(1/(float)60);

if u want shorter code u can use this (maybe the compiler unrolls the loop):
float result = (lat[0]-0x30)*10 + (lat[1]-0x30);
float fac=10/(float)60;
for (register uint8_t j=2; j<=8; j++, fac*=1e-1)
 result += (lat[j]-0x30) * fac;

-arne
-Arne

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy