Why warning is issued in this case

Discussion in 'General Electronics Chat' started by aamirali, Jul 5, 2012.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    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
     
  2. Wendy

    Moderator

    Mar 24, 2008
    20,766
    2,536
    More information is needed. Programming a PIC?
     
  3. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    msp430g2553, ccsv4
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    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.
     
  5. K7GUH

    Member

    Jan 28, 2011
    191
    23
    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?
     
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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. :)
     
Loading...