Compiling error due to many formula??

Dear all as part of my code, I have a lot of conditions; entering them gives an error. how can i arrange this????

long double key_adcbit1(int gas_after)
{
  long double adcbit;
  //int adc_compare;
  
  if(gas_after >= 7585 && gas_after <= 8073 )
  {
    adcbit = (gas_after + 413.96)/(21.217);
  }
  
  else if(gas_after >= 8073 && gas_after <= 11073 )
  {
    adcbit = (gas_after + 437.64)/(21.277);
  }
  else if(gas_after >= 11073 && gas_after <= 13073 )
  {
    adcbit = (gas_after + 687.87)/(21.739);
  }
  else if(gas_after >= 13073 && gas_after <= 14073 )
  {
    adcbit = (gas_after + 395.09)/(21.277);
  }
  else if(gas_after >= 14073 && gas_after <= 14673 )
  {
    adcbit = (gas_after + 498.43)/(21.429);
  }
  
  else if(gas_after >= 14673 && gas_after <= 15673 )
  {
    adcbit = (gas_after + 232.26)/(21.053);
  }
  
  else if(gas_after >= 15073 && gas_after <= 15673 )
  {
    adcbit = (gas_after + 505.57)/(21.429);
  }
 // else
  {
//    key_adcbit2(gas_after);
  }
  
  return adcbit;
  
}

long double key_adcbit2(int gas_after)
{
  long double adcbit;
  
  if(gas_after >= 15673 && gas_after <= 16673 )
  {
    adcbit = (gas_after + 1104.8.26)/(22.222);
  }
  
  else if(gas_after >= 16673 && gas_after <= 17673 )
  {
    adcbit = (gas_after + 348.28)/(21.277);
  }
  else if(gas_after >= 17673 && gas_after <= 19673 )
  {
    adcbit = (gas_after + 740.04)/(21.739);
  }
  else if(gas_after >= 19673 && gas_after <= 20673 )
  {
    adcbit = (gas_after + 305.72)/(21.277);
  }
  return adcbit;
}

tnx

Error : too many decimal points in number.....

here? 1104.8.26

That was the error..

tnx wildbill i dont know how the hell you spoted that hhaha :slight_smile:

tnx

FYI, the compiler tells you what line it found the error. It is not always 100% accurate - the error may be nearby. In this case though, I'd expect that it nailed it.

It didnt...try it and you will see

Maybe it didn't, but there are only 60 lines of code.
Discounting braces and comments, fewer than 30.

Given the massive clue in the error message, it didn't take long to pinpoint the problem.

The IDE added these lines to the front of your code:

#include "WProgram.h"
long double key_adcbit1(int gas_after);
long double key_adcbit2(int gas_after);

That is the "standard" include of WProgram.h and then it added in function prototypes for your two functions. Hence the error line number was 3 out. Well, actually 5 by my reckoning but sometimes compilers take a line or two to notice something has gone wrong.

You may notice down the bottom LH corner of your editing window a number? That is the current line number the cursor is on. So scroll around until that matches the line number in the error message, and then look around.

It would be helpful if there was a "goto line" menu item in the IDE (hint hint).

I'd also like to point something else out - you likely couldn't find the mistake either because you didn't know what the error meant, or more likely, you had been working on it so long with so many numbers, and you went "cross-eyed" and just didn't see it.

My first question is - what are all of those numbers?

Those numbers are called, in programming, "magic numbers" - in that, you have put the values into your code, but they aren't defined anywhere as to what the values mean.

It would be better to declare them as constants or defines (with clearly worded names), in the body of the code, then use the names in the code so you could easily see what the logic is supposed to be.

For instance, suppose I have the following function:

double feet_to_inches(int feet) {
  double inches = feet / 12; // note 12 is a "magic number" - what does it mean?
  return inches;
}

It would be clearer in the code if this was used:

const double INCHES_IN_FOOT = 12;

double feet_to_inches(int feet) {
  double inches = feet / INCHES_IN_FOOT;
  return inches;
}

Also note that by putting things in the body of the code in a central spot (ie, at the top) - you can easily make one change that will cascade throughout your code - makes maintenance much easier.

:slight_smile:

/actually - note that the above code should be considered "pseudo" - in that I am not sure if it compiles or runs right; its meant to convey a concept, nothing more...

I agree with cr0sh here, and in particular with reference to the original code:

else if(gas_after >= 8073 && gas_after <= 11073 )
  {
    adcbit = (gas_after + 437.64)/(21.277);
  }
  else if(gas_after >= 11073 && gas_after <= 13073 )
  {
    adcbit = (gas_after + 687.87)/(21.739);
  }

I notice you have duplicates (in this example 11073). So if you ever decide to change 11073 to 11075 you have to remember to changes two places, and if you forget you get insidious "gaps" (eg. if you change only one place then the number 11074 might never be considered).

If also doesn't make sense to test:

gas_after >= 8073 && gas_after <= 11073

and then

gas_after >= 11073 && gas_after <= 13073

Both compare "equal" for 11073. So is it in the first group or the second group? It should be:

gas_after >= 8073 && gas_after <= 11073

and:

gas_after > 11073 && gas_after <= 13073

Note the change of >= 11073 in the second line to > 11073. Now it is clear that 11073 is in the first group. Still, better to move those numbers to the top of the sketch like cr0sh said.

cr0sh:
...
For instance, suppose I have the following function:

double feet_to_inches(int feet) {

double inches = feet / 12; // note 12 is a "magic number" - what does it mean?
  return inches;
}




It would be clearer in the code if this was used:



const double INCHES_IN_FOOT = 12;

double feet_to_inches(int feet) {
  double inches = feet / INCHES_IN_FOOT;
  return inches;
}




...

:)

/actually - note that the above code should be considered "pseudo" - in that I am not sure if it compiles or runs right; its meant to convey a concept, nothing more...

[pedantry]
OK, it's pseudo code. But I can't help myself -- to get inches from feet it's traditional to multiply rather than divide...
[/pedantry]

Adding my 2 cents,

such if then else if constructs can be written with half the tests as follows:

double key_adcbit1(int gas_after)
{
  double adcbit = -1; // allways initialize local vars in a function as this is not done automagically

  if (gas_after < 7585) return -1; // assumption; < 7585 not defined in your code; ; added for robustness sake
  
  if (gas_after <= 8073 ) 
  {
     adcbit  = (gas_after + 413.96)/21.217;
  }
  else if (gas_after <= 11073 ) 
  {
    adcbit = (gas_after + 437.64)/(21.277);
  }
  else if(gas_after <= 13073 )
  {
    adcbit = (gas_after + 687.87)/(21.739);
  }
  else if (gas_after <= 14073 )
  {
    adcbit = (gas_after + 395.09)/(21.277);
  }
  else if( gas_after <= 14673 )
  {
    adcbit = (gas_after + 498.43)/(21.429);
  }
  else if( gas_after <= 15673 )
  {
    adcbit = (gas_after + 232.26)/(21.053);
  }
  
  else if( gas_after <= 15673 )
  {
    adcbit = (gas_after + 505.57)/(21.429);
  }
  else
  {
    adcbit = -1; // assumption; > 15673 case not defined in your code; added for robustness sake
  }
  return adcbit;
}

And this code can be minimized to

double key_adcbit1(int gas_after)
{
  if (gas_after < 7585) return -1; // assumption 
  if (gas_after <= 08073 ) return (gas_after + 413.96) / 21.217;
  if (gas_after <= 11073 ) return (gas_after + 437.64) / 21.277;
  if (gas_after <= 13073 ) return (gas_after + 687.87) / 21.739;
  if (gas_after <= 14073 ) return (gas_after + 395.09) / 21.277;
  if (gas_after <= 14673 ) return (gas_after + 498.43) / 21.429;
  if (gas_after <= 15673 ) return (gas_after + 232.26) / 21.053;
  if (gas_after <= 15673 ) return (gas_after + 505.57) / 21.429;
  return -1;
}

in that code, the second line will NEVER be reached...

if (gas_after <= 15673 ) return (gas_after + 232.26) / 21.053;
if (gas_after <= 15673 ) return (gas_after + 505.57) / 21.429;

@Grag38
You're absolutely right, was just refactoring on auto pilot :slight_smile:

but the idea should be clear...

Rob