16
QB64 Discussion / Re: Double Check on order of operations
« on: January 30, 2022, 03:28:25 pm »
@luke
In all of math, Integer Division is not treated any different than division or multiplication.
The same goes for MOD. (See: https://mathworld.wolfram.com/Division.html
Integer Division returns the Quotient only from a division, where MOD returns only the remainder. All are considered division and is supposed to be performed left to right.
All three (four including MOD) should be treated on the same level. Hence, with a mixture of \ and /, all are computed left to right.
For example: 5 / 10.5 \ 5 * 6
The correct answer is 15, yet QB64 returns 0. 0 is incorrect.
Check the following results from Wolfram Alpha:
https://www.wolframalpha.com/input/?i=5+%2F+Quotient%2810.5%2C+5%29++*+6
What is the reason why Integer Division comes after division and multiplication?
In all of math, Integer Division is not treated any different than division or multiplication.
The same goes for MOD. (See: https://mathworld.wolfram.com/Division.html
Integer Division returns the Quotient only from a division, where MOD returns only the remainder. All are considered division and is supposed to be performed left to right.
All three (four including MOD) should be treated on the same level. Hence, with a mixture of \ and /, all are computed left to right.
For example: 5 / 10.5 \ 5 * 6
The correct answer is 15, yet QB64 returns 0. 0 is incorrect.
Check the following results from Wolfram Alpha:
https://www.wolframalpha.com/input/?i=5+%2F+Quotient%2810.5%2C+5%29++*+6
What is the reason why Integer Division comes after division and multiplication?
The full list, from highest to lowest precedence, is:Operators on the same line are evaluated left to right, or inside out in the case of the unary operators NOT and negation. In practice, this means:
- Exponentiation (^)
- Negation (-)
- Division (/), Multiplication (*)
- Integer Division (\)
- MOD
- Subtraction(-), Addition(+)
- Equality (=), Ordering (<, <=, >, >=)
- NOT
- AND
- OR
- XOR
- EQV
- IMP
- NOT 2 + 3 is NOT (2 + 3) because + has higher precedence and so is evaluated first
- - 2 ^ 3 = -(2 ^ 3) because ^ has higher precedence and so is evaluated first
Apparently this full list isn't in the wiki anywhere; it probably should be.