# Noob can't get arduino math to work

I’m fairly proficient in C++, and I’m sure that my problem is a simple one , but I just can’t seem to figure it out. The following line works perfectly and returns a 100 as expected:

``````int morseMessage = 0;
morseMessage = morseMessage + pow(10,2);
``````

However, the exponent in my particular program is a variable, and this causes problems:

``````while(exponent >=0)
{
if (array[counter] <=500)
{morseMessage = morseMessage + pow(10,exponent);}
``````

This is of course a snippet, there’s a lot more going on but I’ve wittled my program down to this to show the problem, so please ignore the lack of variable initialization and whatnot. If the variable ‘exponent’ is equal to 2, like the one above, it returns a 99 instead of a 100. All variables are 'int’s. Is there some weird rule like integer division which prevents me from getting the right answer? I was worried that other parts of my program were malfunctioning, but as it turns out it’s just a simple line of math that returns 99 instead of 100. Anyone know how to fix it?

I don't know why it's not working, but since pow() is a floating point routine, and you are doing integer math, I'd write my own routine:

``````int intpow10(int exponent)  //  returns 10 to the 'exponent' power
{
int val = 1;

while (exponent > 0)
{
val *= 10;
--exponent;
}
return val;
}
``````

Also, note that if you want to use exponents higher than 4, you'll need to use 'long' variables instead of 'int' variables.

Regards,

-Mike

Ah, thank you very much! That fixed it. I'm not 100% sure why though, it only affected the answer of 100, whereas the answer of 1, 10, and 1000 were unaffected...but the end result is that it's working consistently and accurately! Thanks again

On a related note, I now get the right value for a float, but I want to use the Switch-Case statement on this number, which only works on integers. I tried casting the value, but ironically I receive the same problem as before, my returned value is one off.

``````int otherMessage = (int)morseMessage;
``````

If morseMessage is 1222.00, then otherMessage is 1221. Again I am wondering if this is a small error in my usage of this function. morseMessage was declared as a float. If the (int) cast will consistently be 1 off then I guess I can just add one to the result, but I can't really test every number to double check that.

Sounds like a truncation problem. Floats on this platform are only good for about five or so digits of precision, if I recall correctly.

It may be that when you display the float, it displays as 1222.0, but it's really 1221.9. Converting it to an integer truncates, not rounds. You can make the conversion round like this:`int otherMessage = morseMessage + 0.5;`assuming that morseMessage is a float. You don't need the (int) cast, unless you want it to make your intent clear.

Regards,

-Mike

Well, I added the +0.5, it seems to work just as well as adding 1 to the end result of the truncation. Assuming it really is a rounding problem I guess this will work for all cases. Will declaring it a "long float" or something help with precision?

Another oddity: I'm absolutely sure that the value is 1220 and not 1221.9 because I specifically tested "morseMessage = 1220.00", so unless the Arduino platform itself is changing things on me, I still don't quite understand the error. But as long as I test this and find it to be accurate I won't explore the issue further. Thanks so much!

If you are testing with morseMessage = 1222.00, the result will always be true. The result of an assignment statement is the value assigned. Since that's not zero, the value is true.

A test of morseMessage == 1222.0 will most likely return false.

The actual value stored in morseMessage was probably 1221.99997, which prints as 1222.0, but isn't.

Ok, then my problem is fixed. Thanks!