CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: Advanced VB coders
The correct complete list is:
1) Parentheses " (...)"
2) Brackets " [...]"
3) Curly braces " {...}"
4) Named constants " pi", " e", etc...
5) Prefix functions " cos()", " sec()", " ln()", etc...
6) Postfix functions " !", etc...
7) Exponents / Roots " ^"
8) Negotiation / Multiplication / Division " -", " *", " /"
9) Integer Division " \"
10) Addition / Subtraction " +", " -"
Functions with the same precedence must be performed from left to right!
Expressions within nested bracktes, parentheses or curly braces are evaluated from inner to outer!
explainations:
- Parentheses/Brackets/Curly Braces:
The different types of grouping symbols are used only for convenience in theoretical mathematics. In practice, all are actually equal and it is not entirely incorrect to switch the order in which you use them.
But if you need to put an order in them, then parentheses come before brackets and brackets come before curly braces, despite what you sometimes see listed on the internet and even in math books!!!. This IS the correct mathematical order; this is the theoretical convention! eg: {[8 * (5+3)]^2} + 4, thus not ({8 * [5+3]}^2) + 4
But as said, this is only theory. In practice and in your program they should be treated equally. Thus these are all the same:
{[8 * (5+3)]^2}
([8 * {5+3}]^2)
[[8 * [5+3]]^2]
[{8 * [5+3]}^2]
etc...
Of course, they always come in pairs! And this combined with the above, gives your the absolutely first two things todo in your program:
1) replace all "{" with "(" and all "}" with ")",
replace all "[" with "(" and all "]" with ")"
2) count the number of "(" and count the number of ")"; both should be equal, otherwise return an error!
- Prefix Functions
These involve things like Logs, Trig functions, Expressions involving e and other similar functions... (log(), sin(), acos(), cosec(), exp(), etc.) eg: 3 * cos(pi) ^ 2 + e ^ 5
Your program could also accomodate these very easly.
- Postfix Functions
also called "unary operators". These involve things like factorials, square roots (for square roots also see exponents). These functions don't stand between two numbers, but only relate to one number, they are thought of as being attached directly to that number. eg: 3! ^ 5!
Your program could also accomodate these very easly.
- Exponents / Roots
Both are equally! These two operations need to be seen as a team rather than as separate entities because a square root is taking the power with the reversed number. eg: \/5 = 5 ^ (1/2)
Also note that roots aren't always with a factor 2 like in square roots! (high level maths). You can take an Xth root of a number also. For example the 3th root (cube root) from 5 is 5 ^ (1/3), the 5th root of 7 is 7 ^ (1/5), etc...
Since roots are actually also powers or exponents (whatever you wanna call it), you can even take roots from negative numbers, dispite what the math-teacher may say (even higher level maths)! But outputting the irrational number with your program and calculating further with that irrational number would be out of scope of your simple program I think. eg: square root from -5 = (-5) ^ (1/2)
Also note that square roots are actually a special case since they can also be at the "postfix functions" in the order of calculations (given that the square root is written as a square root and not as a exponent).
Also note that 5 ^ -1 = 1 / 5.
- Negotiation / Multiplication / Division
All are equally! These operations need to be seen as a team rather than separate entities because a division is a multiplication with the reversed number. eg: 5 / 3 = 5 * (1/3)
And a negotiation is a multiplication with -1. eg: -(4+3) = -1 * 7 = -7
Thus your program should be able to correctly deal with: 5 * -(4+3).
(Using "PEMDAS" to remember the words is fine, but you have to see the "MD" as a connected word.)
- Integer division, or difference between "/" and "\"
"/" (the normal division sign) means the normal dividing, thus results may contain fractional numbers. eg: 5 / 3 = 1.666666
"\" (the integer division sign) means that there is going to be a integer division. eg: 5 \ 3 = 1
The precedence between these two divisions can be debated. If no precedence is taken then that means that calculations must be performed left to right. And depending on the definition of "integer division" the result "could" be:
9 / 2 / 3 = 4.5 / 3 = 1.5
9 / 2 \ 3 = 4.5 \ 3 = 1
9 \ 2 / 3 = 4 / 3 = 1.3333333
However, in most programming languages normal divisions take precedence over integer division. eg: in most languages "9 \ 2 / 3" is treated as "9 \ (2 / 3)". This is because integer divisions take actually more steps to perform then a normal division (rounding/cropping/etc. see below). However dividing integers is much faster then dividing real numbers (but that's another tutorial).
Thus, the result of an integer division can vary much according to which programming language you use!!!!! And this is often the source for many calculation errors. You MUST know exactly how your programming languages performs the integer division before you're gonna use it!!
The definition of "integer division" in most languages is: divide two integers and result in another integer. And in some: divide two numbers and result in an integer. This isn't the biggest problem though. The biggest problem is how to convert the number to an integer (cropping vs. rounding) and in case roundings are used how are roundings performed (0.5 = 1 or 0?).
Believe it or not, but some possible results are:
1.75 \ 0.5 = "error: division by zero"
1.75 \ 0.5 = 2
1.75 \ 0.5 = 3
1.75 \ 0.5 = 4
And all have their own logic!
This is because there seems no fixed rule of how integer division should be performed among languages. It depens on how the language rounds numbers, and/or if it crops the numbers instead of rounding, etc. In detail:
- Some language crop the numbers first to the decimal point, so you get:
1.75 \ 0.5 = 1 / 0 = "error: division by zero"
- Some language round the numbers first to the decimal point, so you get:
1.75 \ 0.5 = 2 / 0 = "error: division by zero"
(in this case the language rounds 0.5 as 0)
- Some language round the numbers first to the decimal point, so you get:
1.75 \ 0.5 = 2 / 1 = 2
(in this case the language rounds 0.5 as 1)
- Some languages crop only the result of the division to the decimal point, so you get:
1.75 \ 0.5 = 1.75 / 0.5 = crop(3.5) = 3
- Some languages round only the result of the division to the decimal point, so you get:
1.75 \ 0.5 = 1.75 / 0.5 = round(3.5) = 3
(in this case the language rounds 0.5 as 0)
- Some languages round only the result of the division to the decimal point, so you get:
1.75 \ 0.5 = 1.75 / 0.5 = round(3.5) = 4
(in this case the language rounds 0.5 as 1)
Note also, that in some (very bad/old) programming languages as soon as you use the integer division, all divisions of that equaly precedence calculation are treated as integer divisions. This is however mathematicaly and logicaly totaly not correct!!! eg: in some languages "9 \ 2 / 3" and "9 / 2 \ 3" is mathematicly incorrectly treated the same as "9 \ 2 \ 3". It would be the same as if we would say that additions and multipications are the same, so this totaly incorrect...
If you really really really want to implement all this in your program, I suggest to build in some parameters which your program can use:
* a parameter how the integer division is done (convert first to integers then perform the division, or only convert the result to an integer)
* a parameter how the integer conversion is done (rounding, or cropping)
* a parameter to define how the rounding is done (0.5 = 1, or 0) <== you need this anyway if you include functions like int(), round(), etc.
- Addition / Subtraction
Both are equally! These two operations need to be seen as a team rather than separate entities because a substraction is an addition with the negative number. eg: 5 - 3 = 5 + -3
(Using "PEMDAS" to remember the words is fine, but you have to see the "AS" as a connected word.)
- Additional notes
- If you talk about order of calculations then you must be extremely carefull with signs! eg:
-2^2 = -4, it is not (-2)^2 = 4...!
-2^2 = -1 * (2^2) = -4
This is dispite the fact that negotiation is put below exponents in the order of calculations! -2 must be seen here as -1 * 2 ^ 2!!! Often however, in programming languages (like C++) or programs like Excel this is seen as (-2) ^ 2 and that equals 4, but pure mathematicly this is incorrect!!!!!! And your program is about pure maths, not about how a programming language interprets the order of operations, so make sure the example is calculated as -4...
- Absolute value of a difference:
This is not widely known but the tilde ("~") means absolute value of a difference. In programming language this is a function called abs(). Thus, you could accomodate the tilde also in your program. eg: ~(4-5) = abs(4-5) = 1 or ~-5 = 5
- Double signs:
Your programs should be able to handle double signs. Note that in some programming languages the notation of -- or ++ means something different or is used for different things. But in mathematics it means: eg. 4--5 = 4 - (-5) = 4 + 5 = 9.
Thus the order to calculate this: " 1+2-3!+4^5*6/7*-8/9" is " (((1+2)-(((((((3!)))))))+((((((4^5)*6)/7)*-8)/9))))". So first calculate 3!, then 4^5, then 1024*6, etc...
It seems all much to deal with, but this is the only correct way!!! Also, if you make your program in a logic and good way, all this can be handled very very easly. And you will see that your actual program is in fact very easy and not complicated at all.
This concludes my math-lesson for today
so, DJeX, still up to it?
This post was edited on 09-25-2004 at 05:43 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|