I'm building a calculator web app as a learning exercise, and when I got to the percent function I noticed something strange.
While entering this input on an iPhone calculator - I didn't have a handheld calculator available to verify this - I got this output.
4, *, %, -> (0.04), = (0.16)
but
4, +, %, -> (0.16), = (4.16)
What is confusing here is that the first operation seems to be applying the percent function to the number (4) resulting in the intermediate value of $(0.04)$ and then multiplying that by the number (4) and resulting in $(0.16)$ $4 * (0.04) = 0.16$
However, the addition does not seem to be doing the same number of operations. It seems to be taking the percent of the number (0.04) and be multiplying it by the number (4) and then adding the number (4) and resulting in $(4.16)$ $4 + 4 * 0.04 = 4.16$
Can some one explain this?
Is this correct?
What math rule is this following?
Or have I perhaps stumbled upon a bug?
Also if this is not the correct forum for this question please let me know which is the correct forum.
This is not a bug, it is a carry-over from how many physical digital calculators behave, although they do not always display the intermediate calculations.
If you want to calculate $p$% of $X$ (e.g. to work out the tip on a bill), you would press
X * p % =which relates to the fact that to do that calculation you can calculate $X \times \frac{p}{100}$ (and is why the iPhone calculator shows the conversion from $p$ to $p$%).
On the other hand, if you want to find the result of increasing $X$ by $p$% (e.g. to add sales tax), you press
X + p % =and so there is an implicit equation here of $X(1 + \frac{p}{100})$, i.e. you're taking that $p$% of $X$ and adding it onto itself.
If you want to reduce $X$ by $p$% (e.g. to calculate a price after a discount), you press
X - p % =for similar reasons.
Finally, if you press
X ÷ p % =then this will calculate $X \div \frac{p}{100}$ (or equivalently $X \times \frac{100}{p}$), which lets you find the value that $X$ is $p$% of.
Notice that the addition and subtraction have a kind of symmetry between them, as do the multiplication and division, but those two groups do have different behaviour between them which is not immediately intuitive.