News for hackers. Stuff that matters.
Re:How does the calculator percent key work? (2008) by userbinator on Wednesday April 29, 2015 @12:10AM (#9456444)
> I never understood how these cheap calculators worked.

They are usually based on the two-register model, or at least the very very simplest ones: one accumulator, R, holds the "result"; another accumulator, I, holds the input. The button CE clears I to 0, and C clears R to 0. The display always shows the contents of the most recently modified accumulator. An "operation" register, O, contains the most recently pressed operation button and is initially the null operation. Inputting digits/decimal points modifies I.

Whenever +, -, , / are entered, it performs R = R O I and then sets O to the new most recent operation. When '=' is pressed, R = R O I without changing the most recent operation. This is why sequences like 1+2+54= gives 32, and then '=' again will multiply 32 by 4, etc.

This type of calculator is easy to identify because it has no notion of operator precedence and always evaluates in the sequence it's given, and furthermore doesn't actually require an '=' key - observe that if e.g. you input 2+2-, 2+2+, 2+2/, or 2+2*, the display already shows 4 because entering the last op has caused the previous op + to be evaluated giving R += I, and the display shows the new value of R.

Those with a "memory" function add another accumulator, M. Then the operation MC is M=0, M+ performs M+=R or M+=I (depending on the currently displayed register), M- is M-=R or M-=I, and MR is I=M.

Re:How does the calculator percent key work? (2008) by userbinator on Wednesday April 29, 2015 @07:05AM (#9457439)
Also, in terms of this register machine the percent key performs the operation I = R * I /100, and modifying I causes the display to show the new value of I just like the article mentions:

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.

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.

Re:How does the calculator percent key work? (2008) by cbhl on Wednesday April 29, 2015 @12:42PM (#9459577)
Hacker News uses asterisks for italics. Consider replacing some of them with 'x'es.