Mathematical Precedence


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Posts
    432

    Default Mathematical Precedence

    Hi All,

    I know that general convention is

    multiplication and division

    before

    addition and subtraction

    and the PBP manual states this as..
    PicBasic Pro Compiler performs all math operations in full hierarchal order. This means that there is precedence to the operators. Multiplies and divides are performed before adds and subtracts, for example. To ensure the operations are carried out in the order you would like, use parenthesis to group the operations

    But does this mean that....

    ALL Multiplication is done before ANY Division

    ALL Addition is done before ANY Subtraction

    eg

    30*4-6+18/2+6*6 would be evaluated as follows

    Multiplication
    30*4-6+18/2+6*6
    120-6+18/2+36

    Division
    120-6+18/2+36
    120-6+9+36

    Addition
    120-6+9+36
    120-51

    Subtraction
    120-51
    69

    ... or do Multipy and divide have equal precedence and likewise addition and subtraction in which case the above would be done as

    Multiply and Divide
    30*4-6+18/2+6*6
    120-6+9+36

    Addition and Subtraction
    120-6+9+36
    69

    I realise that in both cases the answer works out to be the same and that parts of a calculation can be forced to happen earlier by the use of brackets but I am wondering which if the above is the way it is done as it has a bearing on overflows. eg.

    ***// as opposed to */*/*
    or
    +++-- as opposed to +--++

    could result in a number overflow before you get to any division or subtraction.

    I need a definative answer as I am trying to create an Excel spreadsheet to find out at which point Melanies RTC code will overflow.
    Keith

    www.diyha.co.uk
    www.kat5.tv

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by keithdoxey
    ...
    Addition
    120-6+9+36
    120-51

    Subtraction
    120-51
    69

    ...

    I need a definative answer as I am trying to create an Excel spreadsheet to find out at which point Melanies RTC code will overflow.

    Hi keithdoxey,

    If you enter "120-6+9+36" into an excel cell without the parenthesis, you will get 159 instead of 69.

    Did you check?
    ----------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    Hi keithdoxey,

    If you enter "120-6+9+36" into an excel cell without the parenthesis, you will get 159 instead of 69.

    Did you check?
    ----------------
    My brain hurts !!!!

    Its fairly irrelevant now as I have just proved that my PIC code is not working right although I cant see how I broke it!

    I cant see how I am getting the wrong value for the Weekday as a test routing that just generates the values gives correct result. Maybe it is a problem when I write to the RTC chip. Will try that next.
    Keith

    www.diyha.co.uk
    www.kat5.tv

  4. #4
    Join Date
    Jul 2005
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    When you get back to the step, post the exact expression you are concerned with so we can play.

    The order of operation can be crucial when performing limited range integer only calculations. Concider a simple expression such a A*B/C.

    At first it looks like order doesn’t matter here. But it does. Let B=7, A=C=2, then we have: 2*7/2 Is this 6 or 7?

    How did I get 6? I worked it right to left. 7/2=3 (3?? Oh yeah, integer only!), and 2x3=6.

    Depending on the magnitude of the numbers you may want to multiply first (as to keep precision) or to divide first (to prevent an overflow).

  5. #5
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ErnieM
    When you get back to the step, post the exact expression you are concerned with so we can play.
    Hi Ernie,

    It doesnt matter now. I thought there was a problem with a calculation that Melanie put in her RTC code but it turns out that I broke it

    I had "modified" some code prior to the calculation that I thought was giving an error but it was a case of "Garbage In, Garbage Out".

    The changes I had made to the code resulted in it being given BCD values rather than Decimal ones which it why it was OK on early days in the month and for the first part of a year but was giving problems with numbers higher than 9.

    Thanks
    Keith

    www.diyha.co.uk
    www.kat5.tv

Similar Threads

  1. Mathematical calculation
    By amindzo in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 27th October 2013, 00:07
  2. Mathematical problem
    By iugmoh in forum General
    Replies: 2
    Last Post: - 25th January 2008, 18:24

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