 # Problems when casting some values to long, other cast just fine

Hi, I'm new to Arduino and I've been trying to get started, but I'm having trouble with some rather silly things. I have the Uno board, and I cannot get it to convert to long properly. I'm feeding it a frequency as input through the serial thingy, and it should calculate a time and print it back. With frequencies from 1 to 81 it works great, I get the same result as the calculator. With 81 I get 30 (which is okay) but with 82, It jumps to -30, and bigger frequencies go to even more negative numbers. Is it the conversion or the serial transmission that's wrong? I really have no clue as to how to debug this, any help is appreciated. Here's my code:

``````const int N  = 400;            //muestras en un período
int f0 = 1;
long Ti = long(pow(10,6)/(f0 * N)); //período de interrupción

void setup()
{

Serial.begin(9600);

}

void loop()
{

if(Serial.available()){
f0 = Serial.parseInt();
Ti = long(pow(10,6)/(f0 * N));//try any f0 between 1-81 and it works, 82 and bigger numbers break it
Serial.println(Ti);
}

}
``````

This is a mixed-types mess.

``````(pow(10,6)/(f0 * N))
``````

float / ( int * int )

where ( int * int ) resolves as an int....

82 * 400 = 32,800 which is more than 32767, the largest int possible.

If your integers will never have (or never should have) negative values, try using `unsigned int`. Doubles the max int from (2^15 - 1) to (2^16 - 1).

Not sure how large you want f0 to go, but watch that (f0 * 400) has a smaller value than the max possible value of the datatype you want to use over the expected range of f0. See the different datatypes on the Arduino Reference page.