You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openoffice.apache.org by Mathias Röllig <mr...@gmx.net> on 2015/10/12 21:40:13 UTC

[API] Priority Problem with AND and OR

Hello!

I stumbled into a priority problem with the boolean operators AND and 
OR. I cannot find any documentation for this.

First mathematical examples.

With
MsgBox( 3 * 2 ^ 2 )
you will see, that ^ has a higher priority than *.
(2 ^ 2) = 4
4 * 3 = 12
With
MsgBox( (3 * 2) ^ 2 )
you will get the right result 6 ^ 2 = 36.

With
MsgBox( 3 + 4 * 3 )
you will see, that * has a higher priority than +.
(4 * 3) = 12
3 * 12 = 36
With
MsgBox( (3 + 4) * 3 )
you will get the right result 7 * 3 = 21.

Now looking at the same logic with logical operators.

Dim bResult As Boolean
bResult = TRUE Or FALSE And TRUE
MsgBox( bResult )
bResult = TRUE Or TRUE And FALSE
MsgBox( bResult )

What do you expect?
For logical operations AND is equivalent to * and OR is equivalent to +.
AND should have a higher priority than OR, so I would expect in both 
cases (because TRUE Or (<anything>) = TRUE):
bResult = TRUE
But you will get
TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE

Is there any explanation that AND and OR have (and should have) the same 
priority?

Regards, Mathias

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Andrew Pitonyak <an...@pitonyak.org>.
I was tempted to open a bug against this some years back, but, this is 
the type of change that I can't help but wonder if it is more dangerous 
to affect existing code, or to use rules by new users who are unaware 
that ^ does not follow expected rules, and neither does AND and OR. Same 
is true for bitwise operations.


On 12.10.2015 15:58, Rory O'Farrell wrote:
> On Mon, 12 Oct 2015 15:45:16 -0400
> Andrew Pitonyak <an...@pitonyak.org> wrote:
>
>> Yes, I note this travesty in OOME. There is also a difference in the
>> way that it handles exponentiation. Standard rules indicate that 
>> 2^3^4
>> is evaluated as 2^(3^4) rather than (2^3)^4, wihch is what OOo does.
>
> This problem came up recently several times on the en-Forum; we
> advised that it is best to use the brackets to define order of
> calculation (particularly as so many Calc users are mathematically
> inexperienced).  It would nevertheless be good to have the matter
> correct in some revision.
>
> Rory O'Farrell
>> On 12.10.2015 15:40, Mathias Röllig wrote:
>> > Hello!
>> >
>> > I stumbled into a priority problem with the boolean operators AND 
>> and
>> > OR. I cannot find any documentation for this.
>> >
>> > First mathematical examples.
>> >
>> > With
>> > MsgBox( 3 * 2 ^ 2 )
>> > you will see, that ^ has a higher priority than *.
>> > (2 ^ 2) = 4
>> > 4 * 3 = 12
>> > With
>> > MsgBox( (3 * 2) ^ 2 )
>> > you will get the right result 6 ^ 2 = 36.
>> >
>> > With
>> > MsgBox( 3 + 4 * 3 )
>> > you will see, that * has a higher priority than +.
>> > (4 * 3) = 12
>> > 3 * 12 = 36
>> > With
>> > MsgBox( (3 + 4) * 3 )
>> > you will get the right result 7 * 3 = 21.
>> >
>> > Now looking at the same logic with logical operators.
>> >
>> > Dim bResult As Boolean
>> > bResult = TRUE Or FALSE And TRUE
>> > MsgBox( bResult )
>> > bResult = TRUE Or TRUE And FALSE
>> > MsgBox( bResult )
>> >
>> > What do you expect?
>> > For logical operations AND is equivalent to * and OR is equivalent 
>> to
>> > +.
>> > AND should have a higher priority than OR, so I would expect in 
>> both
>> > cases (because TRUE Or (<anything>) = TRUE):
>> > bResult = TRUE
>> > But you will get
>> > TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
>> > TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE
>> >
>> > Is there any explanation that AND and OR have (and should have) 
>> the
>> > same priority?
>> >
>> > Regards, Mathias
>> >
>> > 
>> ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
>> > For additional commands, e-mail: dev-help@openoffice.apache.org
>>
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
>> For additional commands, e-mail: dev-help@openoffice.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Rory O'Farrell <of...@iol.ie>.
On Mon, 12 Oct 2015 15:45:16 -0400
Andrew Pitonyak <an...@pitonyak.org> wrote:

> Yes, I note this travesty in OOME. There is also a difference in the 
> way that it handles exponentiation. Standard rules indicate that 2^3^4 
> is evaluated as 2^(3^4) rather than (2^3)^4, wihch is what OOo does.

This problem came up recently several times on the en-Forum; we advised that it is best to use the brackets to define order of calculation (particularly as so many Calc users are mathematically inexperienced).  It would nevertheless be good to have the matter correct in some revision.

Rory O'Farrell 
> On 12.10.2015 15:40, Mathias Röllig wrote:
> > Hello!
> >
> > I stumbled into a priority problem with the boolean operators AND and
> > OR. I cannot find any documentation for this.
> >
> > First mathematical examples.
> >
> > With
> > MsgBox( 3 * 2 ^ 2 )
> > you will see, that ^ has a higher priority than *.
> > (2 ^ 2) = 4
> > 4 * 3 = 12
> > With
> > MsgBox( (3 * 2) ^ 2 )
> > you will get the right result 6 ^ 2 = 36.
> >
> > With
> > MsgBox( 3 + 4 * 3 )
> > you will see, that * has a higher priority than +.
> > (4 * 3) = 12
> > 3 * 12 = 36
> > With
> > MsgBox( (3 + 4) * 3 )
> > you will get the right result 7 * 3 = 21.
> >
> > Now looking at the same logic with logical operators.
> >
> > Dim bResult As Boolean
> > bResult = TRUE Or FALSE And TRUE
> > MsgBox( bResult )
> > bResult = TRUE Or TRUE And FALSE
> > MsgBox( bResult )
> >
> > What do you expect?
> > For logical operations AND is equivalent to * and OR is equivalent to 
> > +.
> > AND should have a higher priority than OR, so I would expect in both
> > cases (because TRUE Or (<anything>) = TRUE):
> > bResult = TRUE
> > But you will get
> > TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
> > TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE
> >
> > Is there any explanation that AND and OR have (and should have) the
> > same priority?
> >
> > Regards, Mathias
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
> > For additional commands, e-mail: dev-help@openoffice.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: dev-help@openoffice.apache.org
> 
> 


-- 
Rory O'Farrell <of...@iol.ie>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Andrew Pitonyak <an...@pitonyak.org>.
Yes, I note this travesty in OOME. There is also a difference in the 
way that it handles exponentiation. Standard rules indicate that 2^3^4 
is evaluated as 2^(3^4) rather than (2^3)^4, wihch is what OOo does.


On 12.10.2015 15:40, Mathias Röllig wrote:
> Hello!
>
> I stumbled into a priority problem with the boolean operators AND and
> OR. I cannot find any documentation for this.
>
> First mathematical examples.
>
> With
> MsgBox( 3 * 2 ^ 2 )
> you will see, that ^ has a higher priority than *.
> (2 ^ 2) = 4
> 4 * 3 = 12
> With
> MsgBox( (3 * 2) ^ 2 )
> you will get the right result 6 ^ 2 = 36.
>
> With
> MsgBox( 3 + 4 * 3 )
> you will see, that * has a higher priority than +.
> (4 * 3) = 12
> 3 * 12 = 36
> With
> MsgBox( (3 + 4) * 3 )
> you will get the right result 7 * 3 = 21.
>
> Now looking at the same logic with logical operators.
>
> Dim bResult As Boolean
> bResult = TRUE Or FALSE And TRUE
> MsgBox( bResult )
> bResult = TRUE Or TRUE And FALSE
> MsgBox( bResult )
>
> What do you expect?
> For logical operations AND is equivalent to * and OR is equivalent to 
> +.
> AND should have a higher priority than OR, so I would expect in both
> cases (because TRUE Or (<anything>) = TRUE):
> bResult = TRUE
> But you will get
> TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
> TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE
>
> Is there any explanation that AND and OR have (and should have) the
> same priority?
>
> Regards, Mathias
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: dev-help@openoffice.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by "Howard Morris (aka Col Boogie)" <Ho...@hotmail.com>.
Not true or misstated, see https://technet.microsoft.com/en-us/library/ms186992%28v=sql.105%29.aspx for correct priority of operators.
Without parentheses AND is evaluated before OR

Attached is a little browser program I use to test Boolean arithmetic.
It is written in html && stands for AND, || stands for OR.
Use == to test if two things are equal, like (3 == 2+1)

Howard

Hi,

Am 12.10.2015 um 21:40 schrieb Mathias Röllig:
> ...
> What do you expect?
> For logical operations AND is equivalent to * and OR is equivalent to +.

no, that's not true. AND and OR have the same priority, just as * and / 
(division). (* and / have a higher priority than + and -.)

> AND should have a higher priority than OR, so I would expect in both

Expressions have to be evaluated from left to right with respect to 
priority and parenthesis.

"and" and "or" are binary operators. Therfore your examples can be 
written as "A or B" where "B = (C and D)". If you write your example as
"A or (C and D)" then it is the same and everyone sees immediatly, how 
it is evaluated.

In both cases, the result is TRUE, because
"TRUE or anything" is TRUE, just as "TRUE or (anything)".

"(A or B) and C" is totally different.

On http://www.p-roocks.de/truthtable2.php you can create "tables of 
truth" (Wahrheitstabellen) for logical expressions like "A or B and C" 
and more complex ones.

> cases (because TRUE Or (<anything>) = TRUE):
> bResult = TRUE
> But you will get
> TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
> TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE
>
> Is there any explanation that AND and OR have (and should have) the same
> priority?

It is as it is!

-- 
Grüße

Günter Marxen


---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Andrew Douglas Pitonyak <an...@pitonyak.org>.

On 10/13/2015 06:25 AM, Regina Henschel wrote:
>
> Other example: Basic has no shortcut evaluation of boolean expressions.
>
A major annoyance to me..... It causes a bunch or nested if statements.

-- 
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
Info:  http://www.pitonyak.org/oo.php


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Regina Henschel <rb...@t-online.de>.
Hi,

Andrew Douglas Pitonyak schrieb:
[..]
>
> The real question becomes.... should it be changed to follow expected
> mathematical norms (breaking all existing correct code and fixing all
> broken code where people assumed it was done in the generally accepted
> way). I am glad I need not make that call.

No, it should not be changed. You cannot break existing macros after so 
many years, and the behavior is not really "wrong", because it follows 
the mathematical definition.

The problem is not, that Basic makes it different than some other 
languages. The problem is, that it is so badly documented.

Other example: Basic has no shortcut evaluation of boolean expressions.

Kind regards
Regina

>
> On 10/12/2015 08:15 PM, Guenter Marxen wrote:
>> Hi,
>>
>> Am 12.10.2015 um 21:40 schrieb Mathias Röllig:
>>> ...
>>> What do you expect?
>>> For logical operations AND is equivalent to * and OR is equivalent to +.
>>
>> no, that's not true. AND and OR have the same priority, just as * and
>> / (division). (* and / have a higher priority than + and -.)
>>
>>> AND should have a higher priority than OR, so I would expect in both
>>
>> Expressions have to be evaluated from left to right with respect to
>> priority and parenthesis.
>
> Only for those specific operators. And the complaint was that your
> statement is unexpectedly true.
>
>>
>> "and" and "or" are binary operators. Therfore your examples can be
>> written as "A or B" where "B = (C and D)". If you write your example as
>> "A or (C and D)" then it is the same and everyone sees immediatly, how
>> it is evaluated.
>
> I wrote an implementation for a proposed international standard in
> Fortran 90 many years back.... It was suggested that I assume that
> people would not know precedence rules so that I should use parenthesis
> for all statements.... and that I should avoid things like ternary
> operators since beginning programmers may not know what they were.
>
>>
>> In both cases, the result is TRUE, because
>> "TRUE or anything" is TRUE, just as "TRUE or (anything)".
>>
>> "(A or B) and C" is totally different.
>>
>> On http://www.p-roocks.de/truthtable2.php you can create "tables of
>> truth" (Wahrheitstabellen) for logical expressions like "A or B and C"
>> and more complex ones.
>>
>>> cases (because TRUE Or (<anything>) = TRUE):
>>> bResult = TRUE
>>> But you will get
>>> TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
>>> TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE
>>>
>>> Is there any explanation that AND and OR have (and should have) the same
>>> priority?
>>
>> It is as it is!
>>
> You are correct in that!
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Andrea Pescetti <pe...@apache.org>.
On 13/10/2015 Andrew Douglas Pitonyak wrote:
> The real question becomes.... should it be changed to follow expected
> mathematical norms (breaking all existing correct code and fixing all
> broken code where people assumed it was done in the generally accepted
> way).

No, but we should advise people to use parentheses to ensure 
disambiguation when the operator precedence it is not totally obvious.

Regards,
   Andrea.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Andrew Douglas Pitonyak <an...@pitonyak.org>.
Guenter, although you are correct with respect to Basic as implemented 
in AOO (and you stated it very well... I enjoyed your post), I am not 
aware of any other computer language where this is true. For example:

C and C++
http://en.cppreference.com/w/c/language/operator_precedence

Java is the same
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Even VBA gives AND higher precedence than OR
https://msdn.microsoft.com/en-us/library/fw84t893.aspx

General comments on what is expected is listed here:

https://en.wikipedia.org/wiki/Order_of_operations

It is noted that APL, Smalltalk, and Occam do not have operator 
precedence. Can't say I have used APL in years (like 25 to 30 years). I 
have not used the other two.

The real question becomes.... should it be changed to follow expected 
mathematical norms (breaking all existing correct code and fixing all 
broken code where people assumed it was done in the generally accepted 
way). I am glad I need not make that call.

On 10/12/2015 08:15 PM, Guenter Marxen wrote:
> Hi,
>
> Am 12.10.2015 um 21:40 schrieb Mathias Röllig:
>> ...
>> What do you expect?
>> For logical operations AND is equivalent to * and OR is equivalent to +.
>
> no, that's not true. AND and OR have the same priority, just as * and 
> / (division). (* and / have a higher priority than + and -.)
>
>> AND should have a higher priority than OR, so I would expect in both
>
> Expressions have to be evaluated from left to right with respect to 
> priority and parenthesis.

Only for those specific operators. And the complaint was that your 
statement is unexpectedly true.

>
> "and" and "or" are binary operators. Therfore your examples can be 
> written as "A or B" where "B = (C and D)". If you write your example as
> "A or (C and D)" then it is the same and everyone sees immediatly, how 
> it is evaluated.

I wrote an implementation for a proposed international standard in 
Fortran 90 many years back.... It was suggested that I assume that 
people would not know precedence rules so that I should use parenthesis 
for all statements.... and that I should avoid things like ternary 
operators since beginning programmers may not know what they were.

>
> In both cases, the result is TRUE, because
> "TRUE or anything" is TRUE, just as "TRUE or (anything)".
>
> "(A or B) and C" is totally different.
>
> On http://www.p-roocks.de/truthtable2.php you can create "tables of 
> truth" (Wahrheitstabellen) for logical expressions like "A or B and C" 
> and more complex ones.
>
>> cases (because TRUE Or (<anything>) = TRUE):
>> bResult = TRUE
>> But you will get
>> TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
>> TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE
>>
>> Is there any explanation that AND and OR have (and should have) the same
>> priority?
>
> It is as it is!
>
You are correct in that!

-- 
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
Info:  http://www.pitonyak.org/oo.php


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org


Re: [API] Priority Problem with AND and OR

Posted by Guenter Marxen <gu...@gmail.com>.
Hi,

Am 12.10.2015 um 21:40 schrieb Mathias Röllig:
> ...
> What do you expect?
> For logical operations AND is equivalent to * and OR is equivalent to +.

no, that's not true. AND and OR have the same priority, just as * and / 
(division). (* and / have a higher priority than + and -.)

> AND should have a higher priority than OR, so I would expect in both

Expressions have to be evaluated from left to right with respect to 
priority and parenthesis.

"and" and "or" are binary operators. Therfore your examples can be 
written as "A or B" where "B = (C and D)". If you write your example as
"A or (C and D)" then it is the same and everyone sees immediatly, how 
it is evaluated.

In both cases, the result is TRUE, because
"TRUE or anything" is TRUE, just as "TRUE or (anything)".

"(A or B) and C" is totally different.

On http://www.p-roocks.de/truthtable2.php you can create "tables of 
truth" (Wahrheitstabellen) for logical expressions like "A or B and C" 
and more complex ones.

> cases (because TRUE Or (<anything>) = TRUE):
> bResult = TRUE
> But you will get
> TRUE Or FALSE And TRUE = (TRUE Or FALSE) And TRUE = TRUE
> TRUE Or TRUE And FALSE = (TRUE Or TRUE) And FALSE = FALSE
>
> Is there any explanation that AND and OR have (and should have) the same
> priority?

It is as it is!

-- 
Grüße

Günter Marxen


---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org