 # math operations

Hi!

I got some problems with math operations in my sketch:

Polynom and divisions does not work.

I’ve got this polynom 6th grade which does not work:

OelTemp = 2*pow(10, -18)*pow(Oelu2, 6)

• 2*pow(10, -14)*pow(Oelu2, 5)
• 1*pow(10, -10)*pow(Oelu2, 4)
• 3*pow(10, -7) *pow(Oelu2, 3)
• 0.0003 *pow(Oelu2, 2)
• 0.2793* Oelu2
• 160.81;

even this does not work val = val/3600
but this works val = val * 0.00027778 (which is exactly the same)

Is there something I do wrong or are there some specifications of arduino I should know?

Thank you!

Martin

even this does not work val = val/3600 but this works val = val * 0.00027778 (which is exactly the same)

It's not exactly the same. 3600 is an integer. How is val defined?

How are OelTemp and Oelu2 defined?

Martin419: even this does not work val = val/3600 but this works val = val * 0.00027778 (which is exactly the same)

The only thing that is exactly the same as:

``````val = val/3600
``````

is:

``````val = val/3600
``````

Try:

``````val = val/3600.0
``````

they're all defined as integer.

Ok...it's not exactly the same but for my needs it's accurat enough.

Where is the difference between val/3600 and val/3600.0 ??

So your position is that dividing by something is the same as multiplying by the inverse? OK, try this sketch:

``````void setup ()
{
int a = 10;
int b = 5;
int c = a / b;   // divide
Serial.begin (115200);
Serial.println ();
Serial.println (c);
c = a * (1/b);  // multiply by the inverse
Serial.println (c);
}

void loop () {}
``````

Let me know what you get.

It’s not about accuracy, it is about understanding how maths works in C/C++ (and many other programming languages).

3600 is an integer, val is an integer. If val < 3600, val/3600 = 0. If val is between 3600 and 7199, val/3600 = 1. 7200 to 10799, val/3600 = 2 etc.

@ Nick

first value of c is 2, as expectet. The second value of c is 0. Is it because c is defined as integer?

Whenever you do floating point math, make sure all variables are declared as float, and all constants should have a decimal point in (so you write 1.0 for 1, 2.0 for 2, etc) If anything is an int things can go wrong in unexpected ways.

Instead of 2*pow(10, -18) you can write 2.0e-18 (and similar for the other coefficients)

Here’s a useful trick for polynomials: Using algebra, the polynomial

y=ax6+bx5+cx4+dx3+ex2+fx+g

can be rearranged as

y=(((((ax+b)x+c)x+d)x+e)x+f)x+g

which can be coded as:

``````y=a*x+b;
y=y*x+c;
y=y*x+d;
y=y*x+e;
y=y*x+f;
y=y*x+g;
``````

Instead of 2*pow(10, -18) you can write 2.0e-18 (and similar for the other coefficients)

Can and must, as the pow function takes float arguments, and quickly introduces errors when the exponent value increases. Even with an exponent of 4, the result is not 10000.0.

Be aware that Arduino IDE float and double are both 4 byte floating point so you gain no precision using doubles.

You can store values how you will and cast them to float when processing.

int a = 5; int b = 12; float x = (float) b / (float) a;

result is x == 2.4

Thanks a lot!! really good to know that stuff :)