Scientific Math Text Input Evaluator

Discussion in 'Code Samples & Tips' started by Rioven, Nov 21, 2007.

  1. Rioven

    Rioven Active Member Licensed User

    Hi

    Attached is code to evaluate math same as Basic4ppc code operations.

    This supports ^, MOD, /,*,-,+, INT,ABS,SIN,COS,TAN,ASIN,ACOS, ATAN,LN,LOG,EX(for e),E[E+,E-](for eng'g),PI,SQRT,Factorial(!)integer up to 170(max).

    calculations in radians.

    I've tried to compare results to Basic4PPC operations and it's ok, but still need to test for other bugs.(sometimes I got last (13th)decimal place descripancy in comparison).

    EDIT: file updated by Rioven 26JAN09-fixed bugs on ^ function, added input error filters on parenthesis.
    EDIT: file updated by Rioven 24JAN09 -fixed bug
    EDIT: file updated by Klaus 21JAN09 -added LOG & SQRT functions

    Regards,
     

    Attached Files:

    Last edited: Jan 26, 2009
  2. Rioven

    Rioven Active Member Licensed User

    Factorial function added

    Now also supports factorial 170! maximum. (integer number only)

    Fixed, Entering constant(PI or EX) alone now returns value.

    TIP: New Variables/constants can be added at line 380.
     

    Attached Files:

    Last edited: Dec 6, 2007
  3. HarleyM

    HarleyM Member Licensed User

    Just the ticket!!

    Nice work !!! I have a partially written project to graph a range of numbers based on a formula in a text box. Bingo !! you have the missing link I was looking for. :sign0188:
     
  4. Rioven

    Rioven Active Member Licensed User

    Hi HarleyM,

    This from my old code just converted to Basic4ppc, It can be optimized further. I created this function for my project that user can alter the default calculation formula or input their own formula.

    Hope it works well for you.
    Please test it more and let me know if there are still problems.

    Regards,
    Rioven
     
  5. HarleyM

    HarleyM Member Licensed User

    Hi Rioven

    I connected your code into a program that I'd written around Erel's Eval routine. Your code breathes new life into it. Allowing the user to select a range of x values & uses your routines to populate a table based on the formula entered into a text box. The user can then display the table in one of 3 graph formats. Thanks again for the work you did in creating the expression calculator. I only needed to change one thing & that was to reset the 'C' variable after a calculation so that it would accept modified variables thereafter.

    Cheers!!
    :sign0142:
     
  6. Rioven

    Rioven Active Member Licensed User

    Hi HarleyM,
    Thanks for pointing out the C variable to reset.
    I attached the revised code and this is still :sign0025:
    I've fixed some input variable/constant filters and bug on dividing negative value.
    The code requires more input filters or errorhandling and tests.

    Regards,
     

    Attached Files:

    Last edited: Nov 24, 2007
  7. Filippo

    Filippo Expert Licensed User

    Hallo Rioven,

    your Function "COMPUTER(FORMULAs)" is very good!:sign0188:
    I use it in my B4P-Calculator. What I still miss is the SQRT-function.


    Ciao,
    Filippo
     
  8. klaus

    klaus Expert Licensed User

    Hallo Filippo,

    Here you are, added SQRT and LOG.
    There are some strange behaviours, also in version 2.1, I haven't yet looked in detail on what happens.

    Best regards.

    EDIT: Removed the program, it has been updated by Rioven in the original post.
     
    Last edited: Jan 22, 2009
  9. Filippo

    Filippo Expert Licensed User

    Hallo Klaus,

    thank you very much. :)
     
  10. Rioven

    Rioven Active Member Licensed User

    Hello Klaus,
    Thanks for the added functions.
    I hope you could have some times to have a look on these strange behaviours.

    Hello Filippo,
    If there are other functions that still need to add, It can be simply added under line 272 'special operators', just follow the pattern.

    Any additional Constant can also be simply added under line 390 & 403 and also define under 'Sub Globals'.

    Note: the latest file 'Mathcode02_2.sbp' updated by Klaus also been uploaded on the first post of this thread.

    Thanks & regards,
    Rioven
     
    Last edited: Jan 22, 2009
  11. klaus

    klaus Expert Licensed User

    Hi Rioven,

    Nice to hear from you.

    Two examples of what I called 'strange behaviours'.
    If you enter ABS(-2) and execute it, you get twice the error message 'Undefined Variable', but the program does the calculation.

    if you enter LN(Sin(1.4))+Cos(1.3) , you get 4 times the same error message, the calculation is OK.
    If you change any value in the equation the program calculates well without any error message.

    I will have a look at it and keep you informed.

    Best regards.
     
  12. Filippo

    Filippo Expert Licensed User

    Hallo Rioven,

    I have your program expanded and the error "Undefined variable" fixed.

    EDIT: Removed the program, it has been updated by Rioven in the original post.

    Ciao,
    Filippo
     
    Last edited: Jan 24, 2009
  13. klaus

    klaus Expert Licensed User

    Hi Filippo,

    Good job, you were faster than me !

    Best regards.
     
  14. Rioven

    Rioven Active Member Licensed User

    Hi Filippo,

    Thanks for the revisions but it is not quite fixed yet!
    Your updated file calculation of the test default Text doesn't matched the Basic4PPC calculation anymore. It returns value of 6.96157773231185 instead of 6.96158017853703 as the previous file MathCode2_2.sbp. Please check.

    The strange behaviour still there...
    If you enter 'abs(yy)+(uu)' still gives 4 times the 'undefined variables' messages. Message should appear only twice or just once.

    Thanks & regards,
     
  15. Rioven

    Rioven Active Member Licensed User

    New file uploaded

    Hello Filippo,
    The updated file MathCode2_2_1.sbp was uploaded on the first post.
    undefined variables fixed and also identified on the error message.
    you can update again your version.

    Hello Klaus
    Hope you can test it more.

    Thanks for the help guys:)
     
  16. klaus

    klaus Expert Licensed User

    Hi Rioven,

    I have found another problem.
    (-2)^2+(-2)^2

    The result should be 8, but the evaluator gives 0.

    The first (-2)^2 = 4 is OK
    but the 2nd (-2)^2 gives -4, which is wrong.

    It seems that the program takes the minus sign of -2 as the operator and not the + sign between 2+(.

    The problem happens with all even powers, 2, 4, 6 . . . .

    I have not yet looked where it does come from, but I think you know the routione better than me.

    Best regards.
     
    Last edited: Jan 24, 2009
  17. Rioven

    Rioven Active Member Licensed User

    bug fixed

    Thanks Klaus,
    The New file were uploaded on the first post.
    I don't know why if you code -2^2 it returns -4 on Basic4PPC? is this a bug?

    Regards,
     
  18. klaus

    klaus Expert Licensed User

    Hi Rioven,

    Thank's for updating the program.

    -2^2 = -4 that's normal and must be so !

    Some comments on that. Let's take two examples with the equation and it's literal meaning:
    1) -2^2 means: minus the square of number two
    2) (-2)^2 means: the square of the number minus two

    The difference is due to the order of operations Order of operations - Wikipedia, the free encyclopedia (you know this link from another thread, I put it here if other readers who might be interseted in).
    1) The operator with the highest level is ^, on it's right is the exponent and it's the number 2, on it's left it's a also a number 2. So the result is the square of the number two and minus the result, so -4.
    2) The operator with the highest level is ^, on it's right is the exponent and it's the number 2, on it's left it's a bracket so the ^operator applies to the content between the two brackets which is number minus two and the result is the square of the number minus two = 4

    In the first case minus is an operator and in the second one it belongs to the number.

    I hope this is not too pedantic.

    I am shure that Filippo will be happy with the update, he is using your evaluator in his http://www.basic4ppc.com/forum/german-forum/3692-b4p-calculator.html#post21633 calculator. A pretty nice looking calculator.

    Best regards.
     
  19. Rioven

    Rioven Active Member Licensed User

    Thanks again Klaus for the information.
    Filippo, I will fix this bug again, please standby for the update.

    Regards,
     
  20. Rioven

    Rioven Active Member Licensed User

    MathCode02_2_3.sbp

    Hello Klaus/Filippo,
    The updated file have been uploaded to the first post.
    Please continue testing the code.

    Thanks again,
     
Loading...