Why warning is issued in this case

Thread Starter

aamirali

Joined Feb 2, 2012
412
volatile long IntDegF;
volatile long IntDegC;

IntDegC = ((1000 + 1) * 666) / 4096;
IntDegF = ((1000 - 1) * 1199) / 4096 + 32;


Warning :i nteger operation result is out of range adc/source_files main.c line
 

MrChips

Joined Oct 2, 2009
30,808
Every compiler is different.

Sometimes you have to force long constant values by appending an "L" at the end of each constant, for example 4096L.

Also be aware of the order of operations in the arithmetic.

Since this is done in integer arithmetic, you have to be aware of overflows or loss of precision.

Mind you what you are doing seems to be correct, except that this expression results in a constant and the compiler could very well be preprocessing it to give a constant literal.
 

K7GUH

Joined Jan 28, 2011
190
The effect of the parentheses in the first expression is to multiply 1001 * 666, which gives you an intermediate product of 666,666. Your compiler is not smart enough to assign a long enough intermediate variable, hence it protests that the number is out of range. That's what you get for using C-like languages. In my business, C is merely average, neh?
 

THE_RB

Joined Feb 11, 2008
5,438
Yep. It was covered in a recent thread, some compilers will default to 16bit integer for resolving constant expressions! Which is pretty poor.

Instead of this;
IntDegC = ((1000 + 1) * 666) / 4096;

you could code this;
IntDegC = 666;
IntDegC = ((1000 + 1) * IntDegC) / 4096;

Which should compile and execute fine. Vertical code is your friend. :)
 
Top