The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Entry #100044 - The Overkill 4-Function Calculator

Last post 06-11-2007 12:09 PM by Alex Papadimoulis. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 06-11-2007 12:09 PM

    Entry #100044 - The Overkill 4-Function Calculator

    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 infix-to-postfix algorithm) to a program for a virtual machine, which supports the following instructions: * Halt (end of program) * Value (convert a single-precision 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 256-bit fixed-point format. 3. The value remaining atop the operand stack when the 'halt' instruction is run is converted back from its 256-bit fixed-point format to a single-precision 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 well-commented, and it uses functions and macros for clarity and simplicity. I was going for maximum cognitive dissonance: the program itself is fairly well-written, but the actual implementation is mind-numbing.
Page 1 of 1 (1 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems