4 ms·
price + (10.percent * 2) looks like 12: If 10.percent = 1, 1*2 = 2. 10 + 2 = 12. Under the usual rules of operator precedence, that would be the expected answe
by ternaryoperator 2y ago
price + (10.percent * 2) looks like 12: If 10.percent = 1, 1*2 = 2. 10 + 2 = 12.
Under the usual rules of operator precedence, that would be the expected answer (multiplication having higher precedence than addition).
- bloated5048 2y agoHow does computer know it's 10% of 10? Unless specified, 10% is 0.1 Or am I an idiot and missing something here
- gus_massa 2y agoIf you oveload "+" to mean whatever-you-want, it's possible. Using bad python [1], it's something like: def sum(x,y): if is_number(x) and is_number(y): return x + y elif is_number(x) and is_percent(y): return x * (1 + y.value/100) else: raise fatal_error I don't claim it's a good idea, but it's possible if the language allows operator overload. [1] Sorry, my main current language is Racket, but I thought that python-like is easier to read for most people, in spite people that likes python can find like 10 error in 7 LOC.