0

How do languages calculates??

How does a language (Python or JavaScript) break down mathematical problem like this Assume a=7, b=5 ,c=4 . =>(a+c)-a*b%a

18th Jul 2023, 10:25 AM
Indiphile Menziwa
Indiphile Menziwa - avatar
4 Respuestas
+ 9
Indiphile Menziwa , the calculation (in python) is done according the so called * operator precedence*. this can be found here: https://docs.python.org/3/reference/expressions.html#operator-precedence so your expression is evaluated and performed like: a = 7 b = 5 c = 4 * and % have the same precedence, so they are used from left to right. print((a + c) - (a * b) % a) ( 11 ) - (7 * 5) % 7 ( 11 ) - ( 35 ) % 7 ( 11 ) - ( 0 ) => 11 so the result is 11.
18th Jul 2023, 6:17 PM
Lothar
Lothar - avatar
+ 5
Indiphile Menziwa it is a complicated process that is called "lexical analysis". Geeksforgeeks appears to have one of the better explanations of how it works. https://www.geeksforgeeks.org/introduction-of-lexical-analysis
18th Jul 2023, 6:03 PM
Brian
Brian - avatar
+ 3
Syntax error +% Usually the modern programming languages follow the same operator precedence as common math notation 1. parentheses 2. exponentiation 3. multiplication, division 4. addition, subtraction
18th Jul 2023, 11:00 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Thanks 🙏, that lexical explain explained more than just calculations. And appreciate the explanation Lothar that's exactly what I was looking for😸
18th Jul 2023, 8:57 PM
Indiphile Menziwa
Indiphile Menziwa - avatar