PDA

View Full Version : Mathematical Precedence



keithdoxey
- 6th October 2006, 10:21
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.

sayzer
- 6th October 2006, 12:33
...
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?
----------------

keithdoxey
- 6th October 2006, 13:23
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.

ErnieM
- 6th October 2006, 21:15
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).

keithdoxey
- 6th October 2006, 21:44
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