Multi bitwise *XOR* and power operations in an expression get misleading results #1450
Comments
After inspecting the relevant specific source code (click here), I could find out that the operators that you are currently using are named the following ways:
Bitwise XOR (BXOR, with However, I would study further the following statements done, for ensuring that the precedence is correctly set in our code:
It could be seen that:
So, power has a higher precedence set than bitwise XOR. A higher precedence at an operator means that the operation would be done first. Then, power would be done first, and bitwise XOR would be done later. As an outcome of this study, the precedence settings for the given operators are correctly set in qsharp-compiler. Then, @weucode, Could you kindly clarify why are you currently stating that XOR is not working as expected? Additionally, I attach a screenshot of the precedence for the cited source code at the top of my message; See the highlighted first object, which has the lowest precedence as stated in its comment, which is set to 1. |
I‘m sorry to confuse bitwise NOT and bitwise XOR, and give you an incorrect description. |
@weucode The behavior that is described by your side does not consider parsing parenthesis, being these ignored. |
Describe the bug
The details of precedence can be found in page https://docs.microsoft.com/zh-cn/azure/quantum/user-guide/language/expressions/precedenceandassociativity.
It is shown that bitwise XOR has a higher priority than power operator,so we derived the following two formulas,but the result are confusing.
When calculate the expression
(~~~(~~~ 2)) ^ 3
, the order should be~~~ 2 = -3
,~~~ (-3) = 2
,2 ^ 3 = 8
.But the actual order is~~~ 2 = -3
,(-3) ^ 3 = -27
,~~~ (-27) = 26
.When calculate the expression
(~~~(~~~ 2 ^ 3))
,the order should be~~~ 2 = -3
,(-3) ^ 3 = -27
,~~~ (-27) = 26
.But the actual order is2 ^ 3 = 8
,~~~ 8 = -9
,~~~ (-9) = 8
.To Reproduce
Expected behavior
The result of first expression should be 8,and the second should be 26.
System information
operating system:
Windows 10dotnet version:
6.0.300QDK:
v0.24.210930The text was updated successfully, but these errors were encountered: