The Meal


#1

Continuing the discussion from Why is this wrong: meal = 44.50 tax = meal*0.0675 tip = (meal + tax)*0.15 total = meal + tax + tip print total:

Mine worked using one of the responses provided, I am not mathematical in sense though so I am trying to work out why the 6.75% was converted to the result which worked of 1.0675?
Thank you.


#2

We can work it out with arithmetic, or with algebra.

Eg.

a = 100
b = 6.75 / 100 # 0.0675
c = a * b      # 6.75
d = a + c      # 106.75
e = 15 / 100   # 0.15
f = d * e      # 16.0125
g = d + f      # 122.76

That's the long and drawn out approach. Let's work it backwards and see if we can simplify it with a little algebra.

g = d + f                     => but d = a + c
g = a + c + f                 => but f = d * e
g = a + c + e * (a + c)
g = (a + c) * (1 + e)         => but c = a * b
g = (a + a * b) * (1 + e)
g = a * (1 + b) * (1 + e)
g = 100 * (1 + 0.0675) * (1 + 0.15)
g = 100 * 1.0675 * 1.15
g = 106.75 * 1.15
g = 122.76 (rounded to two decimal places)

This looks convoluted, I know. Study it closely. It comes down to

 meal * 1.0675 * 1.15

Since we have two constant factors, we can multiply them out to arrive at a coefficient that will apply to all meals.

1.0675 * 1.15 == 1.227625

which we can round to 1.2276 so that,

meal_cost = meal * 1.2276

#3

Wow thank you mtf for such a detailed response, it is greatly appreciated :relaxed:


#4

So it boils down to this...

def tip_calculator(meal, tax, tip):
    return meal * (1 + tax/100) * (1 + tip/100)