Bug ? Error in calculating constants


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289

    Default Bug ? Error in calculating constants

    Yesterday i've broken down an interesting bug:

    Takt CON 40
    Prescaler CON 2
    UPM VAR WORD
    T8 VAR WORD

    UPM=((Takt/Prescaler)*25000/8)/T8

    is working wrong, because "(Takt/Prescaler)*25000" is bigger than 16 Bit - but there is no overflow-error !

    using
    UPM=((Takt/Prescaler)*(25000/8))/T8
    works !

    PBP 2.44

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Big Wompus,

    BW>>UPM=((Takt/Prescaler)*25000/8)/T8

    is working wrong, because "(Takt/Prescaler)*25000" is bigger than 16 Bit - but there is no overflow-error !

    using
    UPM=((Takt/Prescaler)*(25000/8))/T8
    works !
    <<

    The reason why your second series works, is because you are doing the Calculations *inside* the parenthises <sp> first.

    You must take in order the compiler does your calculations....
    parentheses are ALWAYS done first

    Value = 3 + 4 * 5; Value = 35
    Value = 3 + (4 * 5); Value = 27
    Value = (3 + 4) * 5; Value = 35

    Now working with integers...

    Value = 3 + 4 / 5 + 6 Value= 7 if compiler reads left to right
    Value = 3 + 4 / 5 + 6 Value= 10 if compiler does Multilplication ahead of addition
    Value = (3 + 4) / (5+6) Value = 0


    You must be careful on using parentheses and how a compiler computes values. I *always* use parantheses, so that I will force the compiler to calculate my values correctly... nomatter which order a compiler runs through complex calulations.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

Similar Threads

  1. 18F25K20 and EEPROM - Bug ???
    By BigWumpus in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 2nd March 2009, 21:14
  2. Did I find a bug or bug found me?
    By sayzer in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 13th September 2008, 09:58
  3. calculating arc tangent
    By mischl in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 14th July 2005, 07:29

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts