What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Advanced VB coders

Advanced VB coders
Author: Message:
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. Grin  Advanced VB coders
I'm making a program that will answer these types of equations and show all the steps (all the work) like this:

5-3(3x4) ^2
= 5-3x12^2
=5-3x144
=2x144
=288

(That’s probably wrong just did it fast for an example)


Now I can get it to just answer 5-3(3x4) ^2 and it would give me the straight answer but what I want is the answer and all the middle stuff like this:
= 5-3x12^2
=5-3x144
=2x144
=288


Now I know I have to fallow BEDMAS, which is the order of:

Brackets
Exponents
Division
Multiplication
Addition
Subtraction

So how would I go about searching through the equation for BEDMAS and answer each one in order then go to the next?

Any suggestions or help is greatly appreciated


Thanks!:D
[Image: top.gif]
09-23-2004 02:19 AM
Profile PM Web Find Quote Report
aptiva
Junior Member
**

Avatar
Chicken?

Posts: 39
35 / Male / –
Joined: Jul 2003
RE: Advanced VB coders
as of right now i cant think of any way to do this, except designating certain characters(such as ~ and/or some ASCII characters) which act as tags(much like html with <body> and </body>) to start and end the functions used to calculate the specified part of the equation.

or you might try making a list of all the character types and analize the list type by type(example: Numbers are their own type, and each mathematical symbol is its own types)

other than that, i dont know - as i've never processed equations like the ones you want to calculate...

normally i'd post some examples, but right now i'm bloody tired, i've been up for the last 3 days strait working...
if i have time when i wake up tomorrow morning, i'll try to get somethin' up here...but if you figured out how to calculate equations, i'm sure you can probably figure out how to use what i mentioned...

Sorry i couldnt be more of a help! (N)
Happy Coding :D(Y) (off to bed:P)
"You only get one shot at life. Why waste it on sleep?"
09-23-2004 02:37 AM
Profile E-Mail PM Web Find Quote Report
PiP
New Member
*


Posts: 3
Joined: Sep 2004
RE: Advanced VB coders
Using a stack (or two) and convertng the equation from infix to postfix (or prefix) notation, might make it a hell of alot easyer to evaluate :)
09-23-2004 07:08 AM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Advanced VB coders
quote:
Originally posted by PiP
Using a stack (or two) and convertng the equation from infix to postfix (or prefix) notation, might make it a hell of alot easyer to evaluate :)
true, but that wouldn't give the wanted result in steps (or you need to reverse everytime from postfix to infix....

DJeX, the easy way of doing it is to write a normal calculation program (by using infix only!) and instead of just outputting the result as you normaly would, also output the results in between, because they will be exactly what you want...

Also, if you are not familiar with recursive routines, you may want to learn/check that out first! Because you will need them....

Tip: and the very first check you need to make to avoid inproper input is to check if you have equaly the amount of "(" as the amount of ")"...
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-23-2004 08:56 AM
Profile PM Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: Advanced VB coders
Well you lost me Cookie lol, but I did understand the part about outputting the results in between. If I did that, there would be way to many varibles that the in between stuff could be. Which I don't want to try and figure out lol. Maybe if it could cut parts of the previous step in the question and put the remaineing of the step in the next step with the right anwser.

This post was edited on 09-23-2004 at 08:31 PM by DJeX.
[Image: top.gif]
09-23-2004 08:29 PM
Profile PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Advanced VB coders
I really can't explain it more in detail, sorry... well I could, but then I just wrote the program instead of you writing the program ;)...

This post was edited on 09-24-2004 at 06:41 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-24-2004 06:40 AM
Profile PM Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: Advanced VB coders
I need an example to work off. Can any one give me one?
[Image: top.gif]
09-24-2004 08:43 PM
Profile PM Web Find Quote Report
dotNorma
Veteran Member
*****

Avatar

Posts: 1745
Reputation: 17
32 / Male / –
Joined: May 2003
RE: Advanced VB coders
quote:
Originally posted by DJeX
Now I know I have to fallow BEDMAS, which is the order of:

Brackets
Exponents
Division
Multiplication
Addition
Subtraction

The order of operations here is PEMDAS

Parenthesis
Exponents
Multiplacation
Division
Addition
Subtraction :p
09-24-2004 10:54 PM
Profile PM Web Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: Advanced VB coders
they both work i guess
[Image: top.gif]
09-25-2004 12:22 AM
Profile PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
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 :D

so, DJeX, still up to it? :P

This post was edited on 09-25-2004 at 05:43 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-25-2004 03:10 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On