If the price is 10, I would expect total to be 10,2.
(price + 10.percent) * 2 would be 22.
I don't see any way to get 12.
If the price is 10, I would expect total to be 10,2.
(price + 10.percent) * 2 would be 22.
I don't see any way to get 12.
(price + 10.percent) * 2 = (10 + 1) * 2 = 11 * 2 = 22
and price + (10.percent * 2) = 10 + 20.percent = 10 + 2 = 12
Personally I would assume the latter, given standard operator precedence, where multiplication precedes addition. x + y = 100
90% x + 80% y = 84
you translate it to x + y = 100
90/100 x + 80/100 y = 84
and then you use the Gauss method (or whatever) to solve it.So in a math class, when you see 20% you translate it to .2, and 10+20% means 10.2. But in a calculator and in the OP project the % button is magic and and 10+20% means 12.
[1] You have to buy two item that in total cost $100, but today there is a 10% discount in one and a 20% discount in the other so you pay only $84. ¿How much is each item?
Under the usual rules of operator precedence, that would be the expected answer (multiplication having higher precedence than addition).
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.