About The Contest

The first ever Olympiad of Misguided Geeks contest at Worse Than Failure (or OMGWTF for short) is a new kind of programming contest. Readers are invited to be creative with devising a calculator with the craziest code they can write. One lucky and potentially insane winner will get either a brand new MacBook Pro or comparable Sony VAIO laptop.

Navigation

Advertisements

Entry #100044: The Overkill 4-Function Calculator

by 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 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.

Download0 Comments