Required code multiplies two numbers and receives unexpected result.
Does anybody know why 82 * 400 would not equal 32800?
Here is a simple test:
void setup() {
Serial.begin(9600);
//TEST.
long int test1 = 81 * 400; //expect 32400
long int test2 = 82 * 400; //expect 32800
Serial.print(test1);
Serial.print(" , ");
Serial.print(test2);
//SerialMonitor OUTPUT: 32400 , -32736
}
I have my suspicions as to what the problem is but I have not been able to fix this by changing datatypes or serial.print specifications.
Anybody know the solution?
Thanks, that is what I suspected. But it doesn't help me.
Any ideas on a data type that will have the range?
32800 is rather small anyway.
If signed long int is out of maximum limit, (32800),
what wouldn't be?
Code from limits.h:
/* Minimum and maximum values a `signed long int' can hold.
(Same as `int'). */
#undef LONG_MIN
#define LONG_MIN (-LONG_MAX - 1L)
#undef LONG_MAX
#define LONG_MAX __LONG_MAX__
The problem was indeed in the use of a magic token of 400.
apparently when multiplying 400 by a long it results in the non
long result(makes sense). Not sure what datatype that result is currently, but one with a MAX around 32,500!
So, I see the error of my ways.
Adjusting the magic token to 400L solved the problem,
as did improving it to a long int membervariable=400;
Calculations done by the compiler are normally done on (int) variables. So your 400*82 gave you 32800, an arithmatic overflow to -32736, which it is then legal to assign to a long.
Using any long value in the expression would "upgrade" the expression to being done with longs, and your problem would be solved. This is one of C's more annoying habits; having a hard and fast rule instead of doing the right thing...