# Which is generally preferred, a complex equation or a look-up table?

I have the following equation I need to enter into my code:

y=2.77x^2-68.29x+573.27

When I solve for x, I get the following:

x = 1/554*(6829-5*sqrt(4432*y-675323)) and x = 1/554*(5*sqrt(4432*y-675323)+6829) (The selection depends on the value of y.)

I could code these as equations into my code, but I also considered using a simple lookup table to get approximate values for about 20 entries. Given the math constraints of the Arduino, should I skip the equations and just use a look-up table? Or are the equations doable within the Arduino? I'm flexible in that I could use either method for my application.

Depends. Lookup is faster, Doing the calc gives more granularity. Do either of these things matter in your application? Is it likely that the lookup table would need to grow a lot? That would be an issue with so little RAM. So, either will work, which makes more sense for your particular app is impossible to say.

Yes, my look-up table would grow over time. That's a good point.

Okay, I tried entering in the first part of the equation now, but the Arduino is not giving me the expected equation output. Any idea why this equation would not work correct?

(This comes at the end of an if statement:

``````else {return ((1/554)*(6829-(5*(sqrt((4432*sensorValue)-675323)))))}
``````

Any idea why this equation would not work correct?

What do you expect 1/554 to be? It IS 0.

1.0/554.0 would, of course, produce different results.

Good catch, but I keep getting -2147483648 as the answer! I know the sensor value is going to be between 150-400, so I know this should result in a real, positive number.

Solved: When I change all numbers to xxx.0, then it works. Funny Arduino math!

Funny Arduino math!

It works like any other compiler. The simplest type is used, unless you tell it otherwise.

Another thing that can bite you. 675323 is a literal. Literals are interpreted as ints, unless you specify otherwise. 675323 is not valid int, so unexpected behavior will occur when the value overflows the int register that it is stored in.

Alligator: Solved: When I change all numbers to xxx.0, then it works. Funny Arduino math!

You can also write 123d, where d tells the compiler that it's of type double.

Alligator: Given the math constraints of the Arduino, should I skip the equations and just use a look-up table? Or are the equations doable within the Arduino?

On the original question... I always like to solve for the most constrained resource. Typically, that resource is the programmer's time, so keeping it flexible is paramount. So I would stick with the functions because it's clear and should be easier to debug. At some point, if processing time becomes the paramount constraint (like the sketch is just running too darn slow), then it's time to reconsider and go for the lookup table.