
Entry #100044: The Overkill 4Function Calculatorby Stephen Oberholtzer
This calculator evaluates quite differently from most: The only button that does any work is the "Equals" button. All the others just build up a string in the edit box.
Upon hitting Equals, however, an insane set of operations are performed: 1. The expression is parsed and converted (using the infixtopostfix algorithm) to a program for a virtual machine, which supports the following instructions: * Halt (end of program) * Value (convert a singleprecision constant to the internal format and push it onto the operand stack) * Unary Minus (multiply the value on top of the operand stack by 1) * Add (add the two values on top of the stack) * Multiply (multiply the two values on top of the stack) * Divide (divide the value on top of the stack by the value just below it. Or the other way around, I forget.) * There is no subtraction operator. Subtraction is achieved by performing Unary Minus followed by the Add. 2. (Did you forget I was listing a sequence of steps?) The program created in step 1 is then run through the aforementioned virtual machine. Incidentally, the virtual machine performs all mathematical operations in a 256bit fixedpoint format. 3. The value remaining atop the operand stack when the 'halt' instruction is run is converted back from its 256bit fixedpoint format to a singleprecision float, then formatted as a string on the display. Unless an error occurred (which will most likely only happen when dividing by zero), in which case it displays "Err". You may note that this code is, on the surface, relatively straightforward. It's wellcommented, and it uses functions and macros for clarity and simplicity. I was going for maximum cognitive dissonance: the program itself is fairly wellwritten, but the actual implementation is mindnumbing. Download • 0 Comments 