hn-classics/_stories/2008/9454473.md

136 lines
5.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
created_at: '2015-04-28T19:08:06.000Z'
title: How does the calculator percent key work? (2008)
url: http://blogs.msdn.com/b/oldnewthing/archive/2008/01/10/7047497.aspx
author: vmarsy
points: 128
story_text:
comment_text:
num_comments: 57
story_id:
story_title:
story_url:
parent_id:
created_at_i: 1430248086
_tags:
- story
- author_vmarsy
- story_9454473
objectID: '9454473'
year: 2008
---
The Windows calculator percent sign works the same way as those cheap
pocket calculators (which are often called four-function calculators
even though they have around six function nowadays). What you first have
to understand is that the percent key on those pocket calculators was
not designed for mathematicians and engineers. It was designed for your
everyday person doing some simple calculations. Therefore, the behavior
of the key to you, an engineer, seems bizarrely counter-intuitive and
even buggy. But to an everyday person, it makes perfect sense. Or at
least that's the theory.
Let's look at it from the point of view of that everyday person. Suppose
you want to compute how much a $72 sweater will cost after including 5%
tax.¹ Pull out your handy pocket calculator² (or fire up Calc if you
don't have a pocket calculator) and type
> 72 + 5% =
The result is 75.6, or $75.60, which is the correct answer, because 5%
of 72 is 3.6. Add that to 72 and you get 75.6.
Similarly, suppose that sweater was on sale at 20% off. What is the sale
price?
> 72 20% =
The result is 57.6 or $57.60. This is the correct answer, because 20% of
72 is 14.4. Subtract that from 72 and you get 57.6.
You can chain these percentage operations, too. For example, how much
will you have to pay for that 20%-off sweater after adding 5% tax?
> 72 20% + 5% =
The result is 60.48. A mathematician or engineer would have calculated
the same result via the equivalent computation:
> 72 × 0.80 × 1.05 =
Okay, now that we see how the calculator product designer intended the
percent key to be used, let's look at what the calculator engineer it
has to do in order to match the specification. When the user enters A +
B % =, the result should be A × (1 + B/100) or A + (A × B/100) after you
distribute the multiplication over the addition. Similarly, when the
user enters A B % =, the result should be A × (1 B/100) or A (A ×
B/100).
Aha, the calculator engineer says, we can achieve this result by
defining the percent key as follows:
> When the user enters a value, an operator, a second value, and then
> the percent key, the first two values are multiplied and the product
> divided by 100, and that result replaces the second value in the
> ongoing computation.
Let's walk through that algorithm with our first example.
You type Remarks 72 First value is 72 + Operation is addition 5 Second
value is 5 % 72 × 5 ÷ 100 = 3.6 3.6 becomes the new second value = 72 +
3.6 = 75.6, the final result
If you watch the display as you go through this exercise, you will even
see the number 3.6 appear in the display once you press the % key. The
percentage is calculated and replaces the original value in the ongoing
computation.
This algorithm also works for the chained percentages.
You type Remarks 72 First value is 72 Operation is subtraction 20
Second value is 20 % 72 × 20 ÷ 100 = 14.4 14.4 becomes the new second
value + 72 14.4 = 57.6, intermediate result 57.6 is the new first
value Operation is addition 5 Second value is 5 % 57.6 × 5 ÷ 100 = 2.88
2.88 becomes the new second value = 57.6 + 2.88 = 60.48, the final
result
This even works for multiplication and division, but there is much less
call for multiplying or dividing a number by a percentage of itself.
> 500 × 5 % =
The result of this is 12,500 because you are multiplying 500 by 5% of
500 (which is 25). The result of 500 × 25 is 12,500. You aren't
computing five percent *of* 500. You're multiplying 500 by 5% of 500.
(It appears that the authors of [this Knowledge Base
article](http://support.microsoft.com/kb/132984) didn't consult with the
calculator engineer before writing up their analysis. The percent key is
behaving as designed. The problem is that the percent key is not
designed for engineers.)
What if you want to compute 5% of 500? Just pick a dummy operation and
view the result when you press the percent key.
> 500 + 5 %
When you hit the percent key, the answer appears: 25. You could've used
the minus key, multiplication key, or division key instead of the
addition key. It doesn't matter since all you care about is the
percentage, not the combined operation. Once you hit the % key, you get
your answer, and then you can hit Clear to start a new calculation.
**Footnotes**
¹In the United States, quoted prices typically do not include applicable
taxes.
²In my limited experiments, it appears that no two manufacturers of
pocket calculators handle the percent key in exactly the same way. Casio
appears to handle it in a manner closest to the engineering way. TI is
closer to the layman algorithm. And when you get into cases like `1 ÷ 2
%`, calculators start wandering all over the map. Should the answer be
`50`, since 1/2 is equal to 50%? Or should it be `0.005` since that is
the numeric value of 0.5%? Should that answer appear immediately or
should it wait for you to hit the equals sign? I don't know what the
intuitive result should be either.