Math expression error using parenthesis

I have some expressions in my code to control motor response settings. For some odd reason I'm getting a weird result when I use parenthesis. I've tried using different variable types in case the problem is with roll-over. The problem only seem to go away when I rewrite the expression without the parenthesis. Any help with this issue would be greatly appreciated as there are quite a few expressions in my motor control code I'd have to reformat! The following is an example of one of the expressions I'm being troubled by.

void loop() {
static unsigned long tSerial = 0;
int ansInt = 0;
long ansLong = 0;
double ansDouble = 0;
int ans2Int = 0;
int a = 48;
int b = 99;
int c = 100;

ansInt = 60E6/(abc);
ansLong = 60
E6/(abc);
ansDouble = 60E6/(abc);
ans2Int = 60
E6/a/b/c;

if(millis() - tSerial > 2000){
tSerial = millis();
Serial.print("Expression 1 stored as int: ");
Serial.println(ansInt);
Serial.print("Expression 1 stored as long: ");
Serial.println(ansLong);
Serial.print("Expression 1 stored as double: ");
Serial.println(ansDouble);
Serial.print("Expression 2 stored as int: ");
Serial.println(ans2Int);
Serial.println("\n");
} //end if
} //end function

Capture.JPG

try using 'float' since division will yield you decimals.

I tired float and even 60. instead of 60 with the same result as before.

Note: E6 has been defined as 1000000 using #define in the header.

For some reason, I can't see the JPEG, nor can I see what the value of E6 is.

What is the value of

(a*b*c)

?

int a = 48;
int b = 99;
int c = 100;

abc is 475200, which won't fit into an "int" (as required by your use of parentheses).

The JPEG was a screenshot of the serial output:

Expression 1 stored as int: 3647
Expression 1 stored as long: 3647
Expression 1 stored as double: 3647.00
Expression 2 stored as int: 126

E6 has been defined as 1000000

jremington - your observation is correct. I changed the three variables a,b,c to long and got the correct solution. Any reason these variables are driving the variable type for the parenthesis and not ans?

Cast them to long if they're ints.

The problem is integer overflow. This (abc) is a product of "int" whose result is defined to be "int".
4899100 = 475,200. The result will be truncated to 16 bits (the size of an int on the 386-based Arduinos). 475,200 = 74040(hex). Truncate to 16-bits is 4040(hex) and convert back to decimal is 16448.
60000000/16448 = 3647.86 which is where the 3647 result comes from.

With /a/b/c, the divisions are done one at a time and involve a long integer being divided by an int. The division will produce the correct result because the intermediate results are long integers.

Pete

The reason I asked the question about the product of a, b and c in reply #3 was to prompt you to find out by printing it, then you would have seen the problem immediately.

@AWOL - sorry, I didn't see your question otherwise I wouldn't have spoiled it by providing the answer.

Pete

No worries - got there in the end.

Don't worry AWOL. As soon as jremington suggested the int issue I casted 'a' to long and re-ran. I get it now. Thanks for the help - I thought I was going crazy! lol