It is currently Thu Mar 30, 2017 11:32 am



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Operators precedence
Unread postPosted: Sat Aug 27, 2016 4:52 am 
Offline
User avatar

Joined: Sun Nov 23, 2014 8:15 am
Posts: 2184
Location: romania
HI
What is the precedence relationship between boolean and arithmetic operators?
eg.
how is this evaluated?:
x = b or c * d
Can this be added to manual:
A table with all operators precedence (bools and math in the same precedence table)?
thanks
gik


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Sat Aug 27, 2016 6:45 am 
Offline
User avatar

Joined: Thu Jan 08, 2015 11:28 am
Posts: 1087
Location: .NET
i'd imagine it goes like this (in order of evaluation):
Code:
      ( )      | parentheses
       ^       | pow
     *   /     | mul, div
     +   -     | add, sub
       !       | not
> < = <= => <> | gr, ls, eq, geq, leq, neq
     &   |     | and, or

_________________
https://github.com/evolbug
http://toobasic.jimdo.com


Last edited by evolbug on Sat Aug 27, 2016 1:03 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Sat Aug 27, 2016 12:38 pm 
Offline
User avatar

Joined: Sun Nov 23, 2014 8:15 am
Posts: 2184
Location: romania
you imagine but you are not sure,right?. (in Pascal 'not' has highest precedence; AND is higher than OR, for example)
(btw, you forgot <>)


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Sat Aug 27, 2016 1:02 pm 
Offline
User avatar

Joined: Thu Jan 08, 2015 11:28 am
Posts: 1087
Location: .NET
i am not sure because the manual doesn't have the list of operators and their precedence ;)

_________________
https://github.com/evolbug
http://toobasic.jimdo.com


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Sat Aug 27, 2016 1:03 pm 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5459
Location: Paris, France
evolbug wrote:
i am not sure because the manual doesn't have the list of operators and their precedence ;)

It has, but incomplete.
(Look for "precedence")

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Sat Aug 27, 2016 1:06 pm 
Offline
User avatar

Joined: Thu Jan 08, 2015 11:28 am
Posts: 1087
Location: .NET
well it doesn't have the "full" list :P
imo, the operator table should be the first thing under Expressions

_________________
https://github.com/evolbug
http://toobasic.jimdo.com


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Sat Aug 27, 2016 5:12 pm 
Offline

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2785
Location: Colorado, U.S.
I agree that we have to fix the problem gika wrote about.

I don't know yet what the solution will look like. I can't put a full precedence table as the first thing under Expressions, because you have not yet had any explanation of what an expression is or what the operators do. Also because most of the operators do not apply to String Expressions.

For the record:
  • The manual has a table (well, okay, a list) of arithmetic operator precedence. The table is correct.
    • Unary + and -
    • ^
    • * and /
    • + and -
  • The manual has another table for all logical operators (comparison and boolean). The table is NOT correct (will fix in v01.91).
    • <, >, =, <>, <=, >= (comparisons)
    • Unary !
    • &
    • |
  • Everything in the first table is higher precedence than everything in the second table.

This code proves that the manual is incorrect:
Code:
a=1 : b=2
PRINT (!a)=b        % what the manual says, prints 0.0
PRINT !(a=b)        % what the parsing table says, prints 1.0
PRINT !a=b          % drum roll please: prints 1.0


You can read the parsing table in the source (Run.java, search for "GoesOnPrecedence"), but it's a little mind-bending. Easier to write a few code snippets.

- Marc


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Mon Aug 29, 2016 11:58 am 
Offline
User avatar

Joined: Sun Nov 23, 2014 8:15 am
Posts: 2184
Location: romania
jMarc wrote:
I agree that we have to fix the problem gika wrote about.

I don't know yet what the solution will look like. I can't put a full precedence table as the first thing under Expressions, because you have not yet had any explanation of what an expression is or what the operators do. Also because most of the operators do not apply to String Expressions.

For the record:
  • The manual has a table (well, okay, a list) of arithmetic operator precedence. The table is correct.
    • Unary + and -
    • ^
    • * and /
    • + and -
  • The manual has another table for all logical operators (comparison and boolean). The table is NOT correct (will fix in v01.91).
    • <, >, =, <>, <=, >= (comparisons)
    • Unary !
    • &
    • |
  • Everything in the first table is higher precedence than everything in the second table.

This code proves that the manual is incorrect:
Code:
a=1 : b=2
PRINT (!a)=b        % what the manual says, prints 0.0
PRINT !(a=b)        % what the parsing table says, prints 1.0
PRINT !a=b          % drum roll please: prints 1.0


You can read the parsing table in the source (Run.java, search for "GoesOnPrecedence"), but it's a little mind-bending. Easier to write a few code snippets.

- Marc

badly, Pascal precedence is completely different, so I have to heavily trick my BEXGen compiler.
BTW
something is going terribly wrong:
i don't know what school did you,guys, but I learned that -2^4 equals -16, not 16.
So I don;t understand why -2^4 should be different from 0-2^4, do you?
eg:
BASIC! true:
-2^4=16
add 3 to both:
3-2^4=3+16
result:
-13=19
COOL!
Nevermind.. just watch out


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Tue Aug 30, 2016 12:20 am 
Offline

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2785
Location: Colorado, U.S.
Look at the precedence table. Unary + and Unary - have very high precedence, higher than anything except parentheses and assignment.

That's intentional: you're supposed to be able to think of "-2" as the number "negative two", not an operator applied to the number positive two. (It isn't really, but you're supposed to be able to think of it that way.)

-2^4 is parsed as (-2)^4 , not -(2^4)

gikam wrote:
So I don;t understand why -2^4 should be different from 0-2^4, do you?
Yes.
In "-2^4", the minus sign is a unary operator.
In "0-2^4", the minus sign is a binary operator.

It makes sense if you think of the first as "raising negative two to the fourth power" and the second as "zero minus the result of raising (positive) two to the fourth power".

Here's where it does not make sense:
a=2 : PRINT -a^4
"negative a" is not a number in the same way that "negative two" is, but BASIC! treats them both the same way.

For the record: none of this was my idea. BASIC! uses an expression parser and precedence table like nothing I've ever seen before. Within that context, what I'm telling you is true. I don't have any opinion about whether it is right. If that English idiom is too subtle, it means I am stating fact without making a value judgement.

- Marc (negative Marc?)


Top
 Profile  
 
 Post subject: Re: Operators precedence
Unread postPosted: Tue Aug 30, 2016 6:21 am 
Offline
User avatar

Joined: Wed Jul 10, 2013 8:11 am
Posts: 287
In BASIC! you can use in opposite to ^ the pow() function.

print -2^4 returns 16 : print -2^3 returns -8
print 3 -2^4 returns -13 : print 3 -2^3 returns -5 : print 3 - pow (-2,3) returns 11


print -2 * -2 * -2 * -2 returns 16 : print -2 * -2 * -2 returns -8
print -(2 * 2 * 2 * 2) returns -16 : print -(2 * 2 * 2) returns -8
print 3 -2 * -2 * -2 * -2 returns 19 : print 3 -2 * -2 * -2 returns -5
print 3 -(2 * -2 * -2 * -2) returns 19 : : print 3 -(2 * -2 * -2) returns -5
print 3 - (-2 * -2 * -2 * -2) returns -13 : print 3 - (-2 * -2 * -2) returns 11

print 3 - (2 * 2 * 2 * 2) returns -13 : print 3 - (2 * 2 * 2) returns -5

In other BASIC dialects:
print -2^4 returns -16 : print -2^3 returns -8
print 3 -2^4 returns -13 : print 3 -2^3 returns -5
print (-2)^4 returns 16

You got it? No?

If you not sure, use in BASIC dialects instead of a^3 a*a*a or -a^3 -(a*a*a) in most implementations this is also faster. For exponets like 1.33 or -0.32 in your BASIC! code it is recomended to use the POW() function or enclose your term with brackets like -(a)^3.14.

Happy coding

Gregor

Edit the red line!


Last edited by aFox on Tue Aug 30, 2016 3:41 pm, edited 2 times in total.

Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred