Pages: [1]   Go Down
Author Topic: Problems with floating point division  (Read 514 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
 }


Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

-Arne

Pages: [1]   Go Up
Jump to: