Results 1 to 4 of 4

Thread: Raising a negative number to a power

  1. #1
    Junior Member
    Join Date
    Aug 2015
    Posts
    17
    Rep Power
    11

    Raising a negative number to a power

    It appears that the use of parentheses in the following code gives an invalid delimiter error

    Uses "Console"
    printl (-2)^2
    WaitKey
    
    while the following alternative runs well

    Uses "Console"
    printl -2^2
    WaitKey
    
    On the other hand, the following version gives the wrong answer!

    Uses "Console"
    printl -(2)^2
    WaitKey
    
    So, something is not right with parsing such numerical expressions.
    Last edited by paravantis; 27-08-2024 at 13:05.

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10
    Problem is in PRINTL
    It assumes that the expression to print is a string so parsing consider it as a string and not a number

    Try this where I've included your numeric expressions into STR$(...) that assumes to parse a number and convert into a string

    Uses "Console"
    
    printl "(-2)^2 =", str$((-2)^2)
    printl "-2^2   =", str$(-2^2)
    printl "-(2)^2 =", str$(-(2)^2)
    
    WaitKey
    
    thinBasic is a real-time interpreter without any intermediate code.
    String parsing try to look ahead to check if next expression can be a numeric expression and not a string, switch into numeric parsing and at the end covert into a string.
    But seems there is something to improve here.

    Will have a look
    Thanks
    Last edited by ErosOlmi; 27-08-2024 at 20:51.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10
    Interesting

    Excel gives 4
    Calc gives 4
    Google gives -4 but because it transform -(2)^2 into -(2^2) and that gives -4 also in all other programs thinBasic included
    Attached Images Attached Images
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    Junior Member
    Join Date
    Aug 2015
    Posts
    17
    Rep Power
    11
    Like you have suggested here and in older posts, I am going to be using

    Tstr$(numeric expression)
    
    and plenty of parentheses everywhere, to be on the safe side.

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2017, 13:41
  2. The Power of Nightmares
    By Charles Pegge in forum Shout Box Area
    Replies: 5
    Last Post: 23-05-2012, 07:45
  3. The power of ...
    By ErosOlmi in forum Shout Box Area
    Replies: 3
    Last Post: 05-01-2012, 08:36
  4. Replies: 3
    Last Post: 09-10-2007, 21:56

Members who have read this thread: 4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •